Skip to content

Commit 3483d92

Browse files
committed
Correctly handle subdomains for linkry redirects
1 parent 40c3e78 commit 3483d92

File tree

3 files changed

+221
-12
lines changed

3 files changed

+221
-12
lines changed

src/linkryEdgeFunction/index.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Organizations } from "@acm-uiuc/js-shared";
12
import {
23
DynamoDBClient,
34
QueryCommand,
@@ -14,6 +15,38 @@ const DYNAMODB_TABLE = "infra-core-api-linkry";
1415
const FALLBACK_URL = process.env.FALLBACK_URL || "https://acm.illinois.edu/404";
1516
const DEFAULT_URL = process.env.DEFAULT_URL || "https://www.acm.illinois.edu";
1617
const CACHE_TTL = "30"; // seconds to hold response in PoP
18+
const BASE_DOMAINS = [".acm.illinois.edu", ".aws.qa.acmuiuc.org", ".acm.gg"];
19+
20+
const entries = Object.entries(Organizations);
21+
const shortToOrgCodeMapper: Record<string, string> = {};
22+
for (const item of entries) {
23+
shortToOrgCodeMapper[item[1].shortcode] = item[0];
24+
}
25+
26+
function getSlugToQuery(path: string, host: string): string {
27+
let cleanedHost = host.toLowerCase();
28+
29+
for (const domain of BASE_DOMAINS) {
30+
if (cleanedHost.endsWith(domain)) {
31+
cleanedHost = cleanedHost.substring(
32+
0,
33+
cleanedHost.length - domain.length,
34+
);
35+
break;
36+
}
37+
}
38+
39+
const hostParts = cleanedHost.split(".");
40+
41+
if (hostParts.length > 1 && host !== "acm") {
42+
const short = hostParts[0];
43+
if (shortToOrgCodeMapper[short]) {
44+
return `${shortToOrgCodeMapper[short]}#${path}`;
45+
}
46+
}
47+
48+
return path;
49+
}
1750

1851
/**
1952
* Determine which DynamoDB replica to use based on Lambda execution region
@@ -50,8 +83,9 @@ export const handler = async (
5083
): Promise<CloudFrontRequestResult> => {
5184
const request = event.Records[0].cf.request;
5285
const path = request.uri.replace(/^\/+/, "");
53-
54-
console.log(`Processing path: ${path}`);
86+
const host = request.headers.host?.[0]?.value || "";
87+
const slugToQuery = getSlugToQuery(path, host);
88+
console.log(`Host: ${host}, Path: ${path}, Querying Slug: ${slugToQuery}`);
5589

5690
if (!path) {
5791
return {
@@ -73,7 +107,7 @@ export const handler = async (
73107
KeyConditionExpression:
74108
"slug = :slug AND begins_with(access, :owner_prefix)",
75109
ExpressionAttributeValues: {
76-
":slug": { S: path },
110+
":slug": { S: slugToQuery },
77111
":owner_prefix": { S: "OWNER#" },
78112
},
79113
ProjectionExpression: "redirect",

src/linkryEdgeFunction/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
"devDependencies": {
1616
"@types/aws-lambda": "^8.10.138",
1717
"@types/node": "^24.3.0",
18-
"typescript": "^5.9.2",
19-
"esbuild": "^0.25.12"
18+
"esbuild": "^0.25.12",
19+
"typescript": "^5.9.2"
2020
},
2121
"dependencies": {
22+
"@acm-uiuc/js-shared": "^3.2.1",
2223
"@aws-sdk/client-dynamodb": "^3.922.0"
2324
}
24-
}
25+
}

0 commit comments

Comments
 (0)