Skip to content

Commit 0f56b91

Browse files
committed
Add strict checking to detect public file request
1 parent ea0ab0c commit 0f56b91

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

.changeset/warm-nails-jam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
Add strict checking to detect public file request

packages/open-next/src/adapters/server-adapter.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import NextServer from "next/dist/server/next-server.js";
1414
import { loadConfig, setNodeEnv } from "./util.js";
1515
import { isBinaryContentType } from "./binary.js";
1616
import { debug } from "./logger.js";
17-
import type { PublicAssets } from "../build.js";
17+
import type { PublicFiles } from "../build.js";
1818

1919
setNodeEnv();
2020
setNextjsServerWorkingDirectory();
@@ -138,7 +138,7 @@ export async function handler(
138138
: eventParser.apiv2(event as APIGatewayProxyEventV2);
139139

140140
// WORKAROUND: public/ static files served by the server function (AWS specific) — https://github.com/serverless-stack/open-next#workaround-public-static-files-served-by-the-server-function-aws-specific
141-
if (isPublicAsset(parser.rawPath)) {
141+
if (publicAssets.files.includes(parser.rawPath)) {
142142
return isCloudFrontEvent
143143
? formatCloudFrontFailoverResponse(event as CloudFrontRequestEvent)
144144
: formatApiv2FailoverResponse();
@@ -200,18 +200,7 @@ function loadHtmlPages() {
200200
function loadPublicAssets() {
201201
const filePath = path.join(openNextDir, "public-files.json");
202202
const json = fs.readFileSync(filePath, "utf-8");
203-
return JSON.parse(json) as PublicAssets;
204-
}
205-
206-
function isPublicAsset(rawPath: string) {
207-
console.log(publicAssets);
208-
console.log({ rawPath });
209-
// rawPath = "/favicon.ico"
210-
// rawPath = "/images/logo.png"
211-
return (
212-
publicAssets[rawPath] === "file" ||
213-
publicAssets[rawPath.split("/").slice(0, 2).join("/")] === "dir"
214-
);
203+
return JSON.parse(json) as PublicFiles;
215204
}
216205

217206
async function processRequest(req: IncomingMessage, res: ServerResponse) {

packages/open-next/src/build.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ const appPath = process.cwd();
99
const outputDir = ".open-next";
1010
const tempDir = path.join(outputDir, ".build");
1111

12-
export type PublicAssets = Record<string, "file" | "dir">;
12+
export type PublicFiles = {
13+
files: string[];
14+
};
1315

1416
export async function build() {
1517
// Pre-build validation
@@ -112,17 +114,25 @@ function initOutputDir() {
112114
fs.mkdirSync(tempDir, { recursive: true });
113115
}
114116

115-
function readTopLevelPublicFilesAndDirs() {
117+
function listPublicFiles() {
116118
const publicPath = path.join(appPath, "public");
117119

118-
const items: PublicAssets = {};
120+
const result: PublicFiles = { files: [] };
119121

120-
fs.readdirSync(publicPath).map((file) => {
121-
items[`/${file}`] = fs.statSync(path.join(publicPath, file)).isDirectory()
122-
? "dir"
123-
: "file";
124-
});
125-
return items;
122+
function processDirectory(pathInPublic: string) {
123+
const files = fs.readdirSync(path.join(publicPath, pathInPublic), {
124+
withFileTypes: true,
125+
});
126+
127+
for (const file of files) {
128+
file.isDirectory()
129+
? processDirectory(path.join(pathInPublic, file.name))
130+
: result.files.push(path.join(pathInPublic, file.name));
131+
}
132+
}
133+
134+
processDirectory("/");
135+
return result;
126136
}
127137

128138
function createServerBundle(monorepoRoot: string) {
@@ -186,7 +196,7 @@ function createServerBundle(monorepoRoot: string) {
186196
fs.mkdirSync(outputOpenNextPath, { recursive: true });
187197
fs.writeFileSync(
188198
path.join(outputOpenNextPath, "public-files.json"),
189-
JSON.stringify(readTopLevelPublicFilesAndDirs())
199+
JSON.stringify(listPublicFiles())
190200
);
191201
}
192202

0 commit comments

Comments
 (0)