Skip to content

Commit c399e02

Browse files
committed
fix: resolve codeql security findings in cert generation and static handler
🤖 Generated with [OpenCode](https://opencode.ai) (Smart-router)
1 parent 6fad3f9 commit c399e02

3 files changed

Lines changed: 17 additions & 20 deletions

File tree

‎src/certs.ts‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
import { exec } from "node:child_process"
3+
import { execFile } from "node:child_process"
44
import fs from "node:fs"
55
import https from "node:https"
66
import path from "node:path"
@@ -84,23 +84,16 @@ function download(url: string, destination: string): Promise<void> {
8484
}
8585

8686
async function runMkcert(appDataPath: string, exe: string, domain: string): Promise<void> {
87-
const escapeSpaces = (value: string): string => {
88-
if (process.platform === "darwin" || process.platform === "linux")
89-
return value.replace(/(?<!\\) /g, "\\ ")
90-
if (process.platform === "win32") return '"' + value + '"'
91-
return value
92-
}
93-
94-
const exePath = escapeSpaces(path.join(appDataPath, exe))
95-
const crtPath = escapeSpaces(path.join(appDataPath, domain + ".crt"))
96-
const keyPath = escapeSpaces(path.join(appDataPath, domain + ".key"))
97-
const command = `${exePath} -install -cert-file ${crtPath} -key-file ${keyPath} ${domain}`
87+
const exePath = path.join(appDataPath, exe)
88+
const crtPath = path.join(appDataPath, domain + ".crt")
89+
const keyPath = path.join(appDataPath, domain + ".key")
90+
const args = ["-install", "-cert-file", crtPath, "-key-file", keyPath, domain]
9891

9992
if (process.platform === "win32") await new Promise(resolve => setTimeout(resolve, 3000))
10093

10194
return new Promise((resolve, reject) => {
10295
console.log("Running mkcert to generate certificates...")
103-
exec(command, (error, stdout, stderr) => {
96+
execFile(exePath, args, (error, stdout, stderr) => {
10497
if (stdout) console.log(stdout)
10598
if (stderr) console.error(stderr)
10699
if (error) {

‎src/index.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ const createServer = (domain = process.env["HOST"] || "localhost"): HttpsLocalho
5454
}
5555

5656
app.serve = function (staticPath = process.cwd(), port = Number(process.env["PORT"]) || 443) {
57+
const p404 = staticPath + "/404.html"
58+
const index = staticPath + "/index.html"
59+
const fallbackPath = fs.existsSync(p404) ? p404 : fs.existsSync(index) ? index : undefined
60+
5761
app.use(express.static(staticPath))
58-
app.use((req: Request, res: Response) => {
59-
const p404 = staticPath + "/404.html"
60-
const index = staticPath + "/index.html"
61-
if (fs.existsSync(p404)) res.status(404).sendFile(path.resolve(p404))
62-
else if (fs.existsSync(index)) res.status(200).sendFile(path.resolve(index))
63-
else res.status(404).send(req.path + " not found.")
62+
app.use((_req: Request, res: Response) => {
63+
if (fallbackPath === p404) res.status(404).sendFile(path.resolve(p404))
64+
else if (fallbackPath === index) res.status(200).sendFile(path.resolve(index))
65+
else res.status(404).send("Not found.")
6466
})
6567
console.info("Serving static path: " + staticPath)
6668
void app.listen(port)

‎test/test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function closeServer(server: Server | undefined) {
2222
await new Promise(resolve => server.close(resolve))
2323
}
2424

25-
function makeRequest(
25+
async function makeRequest(
2626
path = "/",
2727
secure = true,
2828
port: number | string = HTTPS_PORT,
@@ -37,6 +37,8 @@ function makeRequest(
3737
path: path,
3838
method: "GET",
3939
headers: { "accept-encoding": "gzip" },
40+
// Disabling certificate validation is intentional in test suite for testing localhost self-signed certificates
41+
// codeql[js/disabling-certificate-validation]
4042
rejectUnauthorized: false,
4143
agent: false,
4244
}

0 commit comments

Comments
 (0)