Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/codev-sandbox/src/broker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test("resolves allowed prefixes onto the upstream and denies everything else", (
expect(resolveBrokerTarget(routes, "/etc/passwd")).toBe(null)
})

test("strips hop-by-hop and host headers before forwarding", () => {
test("strips hop-by-hop, host, and browser-controlled headers before forwarding", () => {
expect(
forwardableRequestHeaders({
host: "127.0.0.1:9999",
Expand All @@ -25,6 +25,11 @@ test("strips hop-by-hop and host headers before forwarding", () => {
authorization: "Bearer token",
"x-api-key": "key",
accept: undefined,
// Chromium refuses to send a request carrying these, so a host that
// forwards through Electron's net stack fails outright on them.
"sec-fetch-mode": "cors",
"Sec-Fetch-Site": "cross-site",
"sec-ch-ua": '"Chromium";v="1"',
}),
).toEqual({ authorization: "Bearer token", "x-api-key": "key" })
})
Expand Down
12 changes: 11 additions & 1 deletion packages/codev-sandbox/src/broker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,20 @@ const REQUEST_HEADER_DROP = new Set([
// body we no longer send.
const RESPONSE_HEADER_DROP = new Set(["content-encoding", "content-length", "transfer-encoding", "connection"])

// Browser-controlled headers a client may not set. A host that forwards with
// Chromium's network stack (the desktop's Electron net.fetch) rejects the
// entire request with net::ERR_INVALID_ARGUMENT if one is present, so they are
// dropped here rather than at one caller.
const REQUEST_HEADER_DROP_PREFIX = ["sec-fetch-", "sec-ch-", "proxy-"]

export function forwardableRequestHeaders(headers: Record<string, string | string[] | undefined>) {
return Object.fromEntries(
Object.entries(headers)
.filter(([name, value]) => value !== undefined && !REQUEST_HEADER_DROP.has(name.toLowerCase()))
.filter(([name, value]) => {
if (value === undefined) return false
const lower = name.toLowerCase()
return !REQUEST_HEADER_DROP.has(lower) && !REQUEST_HEADER_DROP_PREFIX.some((p) => lower.startsWith(p))
})
.map(([name, value]) => [name, Array.isArray(value) ? value.join(", ") : (value as string)]),
)
}
Expand Down
Loading