Skip to content

Commit 91afc7b

Browse files
committed
fix: pre-read static fallback file to prevent un-rate-limited fs access
🤖 Generated with [OpenCode](https://opencode.ai) (Smart-router)
1 parent 987f143 commit 91afc7b

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,18 @@ const createServer = (domain = process.env["HOST"] || "localhost"): HttpsLocalho
5757
const p404 = staticPath + "/404.html"
5858
const index = staticPath + "/index.html"
5959
const fallback = fs.existsSync(p404)
60-
? { status: 404, file: path.resolve(p404) }
60+
? { status: 404, content: fs.readFileSync(path.resolve(p404)) }
6161
: fs.existsSync(index)
62-
? { status: 200, file: path.resolve(index) }
62+
? { status: 200, content: fs.readFileSync(path.resolve(index)) }
6363
: undefined
6464

6565
app.use(express.static(staticPath))
66-
// codeql[js/missing-rate-limiting]
6766
app.use((_req: Request, res: Response) => {
68-
if (fallback) res.status(fallback.status).sendFile(fallback.file)
69-
else res.status(404).send("Not found.")
67+
if (fallback) {
68+
res.status(fallback.status).type("html").send(fallback.content)
69+
} else {
70+
res.status(404).send("Not found.")
71+
}
7072
})
7173
console.info("Serving static path: " + staticPath)
7274
void app.listen(port)

0 commit comments

Comments
 (0)