Skip to content

Commit 86cad8f

Browse files
author
Frank
committed
Sites: fix .well-known files return 403
Closes #5869
1 parent 637d8f6 commit 86cad8f

File tree

2 files changed

+60
-37
lines changed

2 files changed

+60
-37
lines changed

platform/src/components/aws/ssr-site.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,38 +1392,43 @@ async function handler(event) {
13921392
]).apply(([servers, imageOptimizerUrl]) => {
13931393
const kvEntries: Record<string, string> = {};
13941394
const dirs: string[] = [];
1395+
// Router append .html and index.html suffixes to requests to s3 routes:
1396+
// - `.well-known` contain files without suffix, hence will be appended .html
1397+
// - in the future, it might make sense for each dir to have props that controls
1398+
// the suffixes ie. "handleTrailingSlashse"
1399+
const expandDirs = [".well-known"];
13951400

13961401
plan.assets.forEach((copy) => {
1397-
fs.readdirSync(path.join(outputPath, copy.from), {
1398-
withFileTypes: true,
1399-
}).forEach((item) => {
1400-
if (item.isFile()) {
1401-
kvEntries[toPosix(path.join("/", item.name))] = "s3";
1402-
return;
1403-
}
1404-
1405-
// Handle deep routes
1406-
// In Next.js, asset requests are prefixed with is /_next/static, and
1407-
// image optimization requests are prefixed with /_next/image. We cannot
1408-
// route by 1 level of subdirs (ie. /_next/`), so we need to route by 2
1409-
// levels of subdirs.
1410-
if (item.name !== copy.deepRoute) {
1411-
dirs.push(toPosix(path.join("/", item.name)));
1412-
return;
1413-
}
1414-
1415-
fs.readdirSync(path.join(outputPath, copy.from, item.name), {
1416-
withFileTypes: true,
1417-
}).forEach((subItem) => {
1418-
if (subItem.isFile()) {
1419-
kvEntries[
1420-
toPosix(path.join("/", item.name, subItem.name))
1421-
] = "s3";
1422-
return;
1423-
}
1424-
dirs.push(toPosix(path.join("/", item.name, subItem.name)));
1425-
});
1426-
});
1402+
const processDir = (childPath = "", level = 0) => {
1403+
const currentPath = path.join(outputPath, copy.from, childPath);
1404+
fs.readdirSync(currentPath, { withFileTypes: true }).forEach(
1405+
(item) => {
1406+
// File: add to kvEntries
1407+
if (item.isFile()) {
1408+
kvEntries[toPosix(path.join("/", childPath, item.name))] =
1409+
"s3";
1410+
return;
1411+
}
1412+
// Directory + deep routes: recursively process it
1413+
// In Next.js, asset requests are prefixed with is /_next/static,
1414+
// and image optimization requests are prefixed with /_next/image.
1415+
// We cannot route by 1 level of subdirs (ie. /_next/`), so we need
1416+
// to route by 2 levels of subdirs.
1417+
// Directory + expand: recursively process it
1418+
if (
1419+
level === 0 &&
1420+
(expandDirs.includes(item.name) ||
1421+
item.name === copy.deepRoute)
1422+
) {
1423+
processDir(path.join(childPath, item.name), level + 1);
1424+
return;
1425+
}
1426+
// Directory + NOT expand: add to route
1427+
dirs.push(toPosix(path.join("/", childPath, item.name)));
1428+
},
1429+
);
1430+
};
1431+
processDir();
14271432
});
14281433

14291434
kvEntries["metadata"] = JSON.stringify({

platform/src/components/aws/static-site.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,14 +1012,32 @@ export class StaticSite extends Component implements Link.Linkable {
10121012
]).apply(async ([outputPath, assets, bucketDomain, errorPage, route]) => {
10131013
const kvEntries: Record<string, string> = {};
10141014
const dirs: string[] = [];
1015+
// Router append .html and index.html suffixes to requests to s3 routes:
1016+
// - `.well-known` contain files without suffix, hence will be appended .html
1017+
// - in the future, it might make sense for each dir to have props that controls
1018+
// the suffixes ie. "handleTrailingSlashse"
1019+
const expandDirs = [".well-known"];
10151020

1016-
fs.readdirSync(outputPath, { withFileTypes: true }).forEach((item) => {
1017-
if (item.isDirectory()) {
1018-
dirs.push(toPosix(path.join("/", item.name)));
1019-
return;
1020-
}
1021-
kvEntries[toPosix(path.join("/", item.name))] = "s3";
1022-
});
1021+
const processDir = (childPath = "", level = 0) => {
1022+
const currentPath = path.join(outputPath, childPath);
1023+
fs.readdirSync(currentPath, { withFileTypes: true }).forEach(
1024+
(item) => {
1025+
// File: add to kvEntries
1026+
if (item.isFile()) {
1027+
kvEntries[toPosix(path.join("/", childPath, item.name))] = "s3";
1028+
return;
1029+
}
1030+
// Directory + expand: recursively process it
1031+
if (level === 0 && expandDirs.includes(item.name)) {
1032+
processDir(path.join(childPath, item.name), level + 1);
1033+
return;
1034+
}
1035+
// Directory + NOT expand: add to route
1036+
dirs.push(toPosix(path.join("/", childPath, item.name)));
1037+
},
1038+
);
1039+
};
1040+
processDir();
10231041

10241042
kvEntries["metadata"] = JSON.stringify({
10251043
base: route?.pathPrefix === "/" ? undefined : route?.pathPrefix,

0 commit comments

Comments
 (0)