fix(tests): serve.mjs resolves --host before listening and refuses any wildcard result (every spelling: 0, 0::, ::ffff:0.0.0.0, scoped ::), listens on the resolved address and checks the bound one; report-compare opens once and checks size and type on the handle
This commit is contained in:
1 parent
80cc261c24
commit
9965d7f0a0
2 files changed
+48
-17
No files matched your search
@@ -118,7 +118,9 @@ impl Files for Disk {
|
||||
}
|
||||
|
||||
fn read(&self, path: &str) -> Result<Vec<u8>, FileFailure> {
|
||||
let meta = fs::metadata(path).map_err(|e| failure_of(&e))?;
|
||||
// One open, then the checks on that handle: the path is resolved once.
|
||||
let file = fs::File::open(path).map_err(|e| failure_of(&e))?;
|
||||
let meta = file.metadata().map_err(|e| failure_of(&e))?;
|
||||
if !meta.is_file() {
|
||||
return Err(FileFailure::NotRegular);
|
||||
}
|
||||
@@ -127,8 +129,8 @@ impl Files for Disk {
|
||||
}
|
||||
// Bounded read: a file that grew after the size check is still refused.
|
||||
let mut bytes = Vec::new();
|
||||
fs::File::open(path)
|
||||
.and_then(|f| f.take(MAX_PDF_BYTES as u64 + 1).read_to_end(&mut bytes))
|
||||
file.take(MAX_PDF_BYTES as u64 + 1)
|
||||
.read_to_end(&mut bytes)
|
||||
.map_err(|e| failure_of(&e))?;
|
||||
if bytes.len() > MAX_PDF_BYTES {
|
||||
return Err(FileFailure::TooLarge);
|
||||
|
||||
+43
-14
@@ -2,7 +2,8 @@
|
||||
// apps/app/dist with the SPA fallback of docker/nginx.conf, `.wasm` as
|
||||
// application/wasm, the production CSP of DEPLOY.md on every response, and
|
||||
// the env.js docker/entrypoint.sh writes when no APP_* variable is set (the
|
||||
// local tool, no Supabase). Binds 127.0.0.1 only.
|
||||
// local tool, no Supabase). Binds 127.0.0.1 by default, or the one address
|
||||
// `--host` resolves to (the DEV origin); never a wildcard address.
|
||||
//
|
||||
// node scripts/browser/serve.mjs [--port 4173] [--csp with|without] [--r4]
|
||||
//
|
||||
@@ -20,14 +21,37 @@
|
||||
// there; not a CLI option.
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { readFile, stat } from 'node:fs/promises'
|
||||
import { lookup } from 'node:dns/promises'
|
||||
import { createServer } from 'node:http'
|
||||
import { BlockList } from 'node:net'
|
||||
import { extname, join, resolve, sep } from 'node:path'
|
||||
import { pathToFileURL } from 'node:url'
|
||||
import { parseArgs } from 'node:util'
|
||||
import { DIST_DIR, HARNESS_DIR, REPO_ROOT } from './lib/paths.mjs'
|
||||
|
||||
const HOST = '127.0.0.1'
|
||||
const UNSPECIFIED_HOSTS = new Set(['', '0.0.0.0', '::', '::0', '[::]'])
|
||||
|
||||
/** The wildcard addresses, in every spelling the resolver and the kernel accept. */
|
||||
const WILDCARDS = new BlockList()
|
||||
WILDCARDS.addAddress('0.0.0.0', 'ipv4')
|
||||
WILDCARDS.addAddress('::', 'ipv6')
|
||||
|
||||
const isWildcard = ({ address, family }) =>
|
||||
WILDCARDS.check(address.replace(/%.*$/, ''), family === 6 ? 'ipv6' : 'ipv4')
|
||||
|
||||
/**
|
||||
* The one address `host` names, resolved before any socket exists: an empty
|
||||
* host, a wildcard in any spelling (`0`, `0::`, `::ffff:0.0.0.0`…) or a name
|
||||
* resolving to one is refused, so the server can never bind every interface.
|
||||
*/
|
||||
async function bindAddress(host) {
|
||||
if (!host.trim()) throw new Error('--host must name one address')
|
||||
const results = await lookup(host, { all: true, verbatim: true })
|
||||
if (results.length === 0 || results.some(isWildcard)) {
|
||||
throw new Error(`--host "${host}" resolves to a wildcard address`)
|
||||
}
|
||||
return results[0]
|
||||
}
|
||||
const WASM_KEYWORD = "'wasm-unsafe-eval'"
|
||||
const REPORT_PATH = '/__csp-report'
|
||||
const MAX_REPORT_BYTES = 64 * 1024
|
||||
@@ -198,13 +222,23 @@ export function startServer({
|
||||
const server = createServer((req, res) =>
|
||||
respond(req, res, { root, csp: policy, mounts, reports }),
|
||||
)
|
||||
return new Promise((done, fail) => {
|
||||
server.once('error', fail)
|
||||
server.listen(port, host, () => {
|
||||
const url = `http://${host}:${server.address().port}`
|
||||
done({ url, csp: policy, reports, close: () => server.close() })
|
||||
})
|
||||
})
|
||||
return bindAddress(host).then(
|
||||
({ address }) =>
|
||||
new Promise((done, fail) => {
|
||||
server.once('error', fail)
|
||||
server.listen(port, address, () => {
|
||||
// Belt and braces: the socket itself is never on a wildcard.
|
||||
if (isWildcard(server.address())) {
|
||||
server.close()
|
||||
fail(new Error('the server bound a wildcard address'))
|
||||
return
|
||||
}
|
||||
const shown = address.includes(':') ? `[${address}]` : address
|
||||
const url = `http://${shown}:${server.address().port}`
|
||||
done({ url, csp: policy, reports, close: () => server.close() })
|
||||
})
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
async function main() {
|
||||
@@ -221,11 +255,6 @@ async function main() {
|
||||
if (!['with', 'without'].includes(values.csp)) {
|
||||
throw new Error('--csp is "with" or "without"')
|
||||
}
|
||||
// An empty or unspecified address would bind every interface (an unset
|
||||
// FAUNOSTEO_DEV_HOST expands to ''): refused, the server never starts.
|
||||
if (UNSPECIFIED_HOSTS.has(values.host.trim())) {
|
||||
throw new Error(`--host must name one address, not "${values.host}"`)
|
||||
}
|
||||
const server = await startServer({
|
||||
port: Number(values.port),
|
||||
csp: values.csp,
|
||||
|
||||
Reference in new issue
Block a user