Repository navigation
node:http: enforce blockList on https servers and on server.blockList assignments #38261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // Hardcoded module "node:_http_server" | ||
| const EventEmitter: typeof import("node:events").EventEmitter = require("node:events"); | ||
| const { Stream } = require("node:stream"); | ||
| const { isIP } = require("internal/net/isIP"); | ||
| const { | ||
| _checkInvalidHeaderChar: checkInvalidHeaderChar, | ||
| chunkExpression, | ||
|
|
@@ -118,6 +119,12 @@ const DateNow = Date.now; | |
|
|
||
| let cluster; | ||
|
|
||
| let BlockList; | ||
| function isBlockList(value) { | ||
| BlockList ??= $rust("node_net_binding.rs", "BlockList"); | ||
| return BlockList.isBlockList(value); | ||
| } | ||
|
|
||
| function emitCloseServer(self: Server) { | ||
| // A native close promise belongs to one listener; Node's Server 'close' spans overlapping listeners and handed-off sockets (net.js _emitCloseIfDrained). | ||
| if (!self[kPendingDrainClose]) return; | ||
|
|
@@ -409,6 +416,15 @@ function Server(options, callback): void { | |
| requestCert: options.requestCert, | ||
| rejectUnauthorized: options.rejectUnauthorized, | ||
| }); | ||
|
|
||
| // TLS-only like Node, where tls.Server passes its options through to net.Server and http.Server does not. | ||
| const blockList = options.blockList; | ||
| if (blockList) { | ||
| if (!isBlockList(blockList)) { | ||
| throw $ERR_INVALID_ARG_TYPE("options.blockList", "net.BlockList", blockList); | ||
| } | ||
| this.blockList = blockList; | ||
| } | ||
| } else { | ||
| this[tlsSymbol] = null; | ||
| } | ||
|
|
@@ -1263,6 +1279,18 @@ function onServerConnection(this: Server, listenerGeneration, socketHandle) { | |
| return; | ||
| } | ||
|
|
||
| // Node's onconnection checks server.blockList next and closes a blocked peer silently: no 'drop', no 'connection'. | ||
| const blockList = this.blockList; | ||
| if (blockList) { | ||
| const remoteAddress = socket.remoteAddress; | ||
| const addressType = isIP(remoteAddress); | ||
| if (addressType && blockList.check(remoteAddress, `ipv${addressType}`)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 (optional) Operators whose assigned Why this was flaggedTrigger: Verification: Triggered when the operator assigns a
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reproduced. With The fix is to destroy the socket before the error propagates: if (addressType) {
// The native listener serves this socket unless it is destroyed here, so a check() that throws must not admit it.
let blocked = true;
try {
blocked = !!blockList.check(remoteAddress, `ipv${addressType}`);
} finally {
if (blocked) {
tracked?.delete(socket);
socket.destroy();
}
}
if (blocked) return;
}The test runs an http server and an https server in a child process that handles An accessor that validates on assignment would reject objects that Node accepts, so the guard stays at the call. The change is written and passes locally. It is not pushed, because the head is the diff that was verified for merge. It goes in with the accept-time check from the other thread, or into this PR first if a maintainer prefers. The description lists this case under Downsides until then. |
||
| tracked?.delete(socket); | ||
| socket.destroy(); | ||
| return; | ||
| } | ||
| } | ||
|
Comment on lines
+1282
to
+1292
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 (optional) Operators who put abusive IPs on an https server's blockList still pay a full server-side TLS handshake per blocked connection; Node closes at TCP accept. The check at src/js/node/_http_server.ts:1283-1291 runs from the uws open filter ( Why this was flaggedAn https server created with { key, cert, blockList } receives a connection from a listed IP. HttpContext.h:224-227 fires every filter with 2 at TCP accept; the Rust thunk registered by server_set_on_connection at src/runtime/server/server_body.rs:3655 returns unless opened == 1, and for SSL the 1 notification is sent from onHandshake at HttpContext.h:173-177 after the handshake finished. So onServerConnection at src/js/node/_http_server.ts:1283-1291 only sees the peer once the server has done the key exchange and signed the handshake. The blocked peer is then destroyed. On Node the same peer is closed in onconnection before any TLS state is created, so a blocked IP costs no asymmetric crypto; after this merge a blocked IP can force the server into one full handshake per connection attempt. The PR text says a pre-handshake hook would need to be added to the uws/Rust accept path; src/runtime/server/mod.rs:498-519 shows on_connection_filter already receives and handles the 2 event, so the hook exists and only the node:http forwarding ignores it. Verification: The Rust filter thunk at src/runtime/server/server_body.rs:3650-3657 does
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed on current main. The accept notification is the better place for this check. Node closes there, the peer never starts a TLS handshake, and a blocked peer can also skip the JS socket object. The accept loop already checks for a socket that closed during open, and This PR does not contain that change. The head is the diff that was verified for merge, rebased with no change to the added lines. I will send the accept-time check as a follow-up, or add it here first if a maintainer prefers to hold the merge. |
||
|
|
||
| // Node's connectionListener attaches the HTTPParser (socket.parser) before | ||
| // emitting 'connection'; expose the shim here so listeners see it populated. | ||
| socket.parser = createServerParserShim(socket); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { tempDir, tls } from "harness"; | ||
| import { once } from "node:events"; | ||
| import http from "node:http"; | ||
| import https from "node:https"; | ||
| import type { AddressInfo } from "node:net"; | ||
| import { BlockList } from "node:net"; | ||
| import { join } from "node:path"; | ||
|
|
||
| // Node stores the list as a plain property on the server (net.Server constructor). | ||
| type ServerWithBlockList = http.Server & { blockList?: BlockList }; | ||
|
|
||
| type Outcome = { statusCode: number | undefined; body: string } | { error: string | undefined }; | ||
|
|
||
| // Every client in this file connects from 127.0.0.1. | ||
| function blockListCoveringLocalhost() { | ||
| const blockList = new BlockList(); | ||
| blockList.addAddress("127.0.0.1"); | ||
| return blockList; | ||
| } | ||
|
|
||
| function blockListNotCoveringLocalhost() { | ||
| const blockList = new BlockList(); | ||
| blockList.addSubnet("10.0.0.0", 8); | ||
| return blockList; | ||
| } | ||
|
|
||
| async function listen(server: http.Server) { | ||
| server.listen(0, "127.0.0.1"); | ||
| await once(server, "listening"); | ||
| return (server.address() as AddressInfo).port; | ||
| } | ||
|
|
||
| function request(mod: typeof http | typeof https, port: number): Promise<Outcome> { | ||
| return new Promise(resolve => { | ||
| mod | ||
| .get({ host: "127.0.0.1", port, agent: false, rejectUnauthorized: false }, res => { | ||
| let body = ""; | ||
| res.setEncoding("utf8"); | ||
| res.on("data", chunk => (body += chunk)); | ||
| res.on("end", () => resolve({ statusCode: res.statusCode, body })); | ||
| }) | ||
| .on("error", (error: NodeJS.ErrnoException) => resolve({ error: error.code })); | ||
| }); | ||
| } | ||
|
|
||
| describe("https server blockList option", () => { | ||
| const constructors: [string, (options: https.ServerOptions, listener: http.RequestListener) => https.Server][] = [ | ||
| ["https.createServer()", (options, listener) => https.createServer(options, listener)], | ||
| ["new https.Server()", (options, listener) => new https.Server(options, listener)], | ||
| ]; | ||
|
|
||
| test.each(constructors)( | ||
| "%s: a blocked peer is closed before 'connection' and never reaches the request handler", | ||
| async (_name, construct) => { | ||
| const blockList = blockListCoveringLocalhost(); | ||
| const events: string[] = []; | ||
| const server = construct({ ...tls, blockList }, (_req, res) => { | ||
| events.push("request"); | ||
| res.end("served"); | ||
| }) as ServerWithBlockList; | ||
| server.on("connection", () => events.push("connection")); | ||
| server.on("secureConnection", () => events.push("secureConnection")); | ||
| // Node only emits 'drop' for maxConnections; a blocked peer is closed silently. | ||
| server.on("drop", () => events.push("drop")); | ||
| try { | ||
| const port = await listen(server); | ||
| expect(await request(https, port)).toEqual({ error: "ECONNRESET" }); | ||
| expect(events).toEqual([]); | ||
| expect(server.blockList).toBe(blockList); | ||
| } finally { | ||
| server.close(); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| test("a peer outside the list is served", async () => { | ||
| const server = https.createServer({ ...tls, blockList: blockListNotCoveringLocalhost() }, (_req, res) => { | ||
| res.end("served"); | ||
| }); | ||
| try { | ||
| const port = await listen(server); | ||
| expect(await request(https, port)).toEqual({ statusCode: 200, body: "served" }); | ||
| } finally { | ||
| server.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("a value that is not a net.BlockList is rejected", () => { | ||
| const notABlockList = { check: () => true } as any; | ||
| expect(() => https.createServer({ ...tls, blockList: notABlockList })).toThrowWithCode( | ||
| TypeError, | ||
| "ERR_INVALID_ARG_TYPE", | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("http.Server blockList", () => { | ||
| test("server.blockList assigned after listen() applies to the next connection", async () => { | ||
| let requests = 0; | ||
| const server = http.createServer((_req, res) => { | ||
| requests++; | ||
| res.end("served"); | ||
| }) as ServerWithBlockList; | ||
| try { | ||
| const port = await listen(server); | ||
| expect(await request(http, port)).toEqual({ statusCode: 200, body: "served" }); | ||
|
|
||
| server.blockList = blockListCoveringLocalhost(); | ||
| expect(await request(http, port)).toEqual({ error: "ECONNRESET" }); | ||
| expect(requests).toBe(1); | ||
|
|
||
| server.blockList = undefined; | ||
| expect(await request(http, port)).toEqual({ statusCode: 200, body: "served" }); | ||
| expect(requests).toBe(2); | ||
| } finally { | ||
| server.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("a unix socket peer has no IP address to check and is served", async () => { | ||
| using dir = tempDir("http-blocklist", {}); | ||
| const socketPath = join(String(dir), "server.sock"); | ||
| const server = http.createServer((_req, res) => { | ||
| res.end("served"); | ||
| }) as ServerWithBlockList; | ||
| server.blockList = blockListCoveringLocalhost(); | ||
| try { | ||
| server.listen(socketPath); | ||
| await once(server, "listening"); | ||
| const response = await fetch("http://localhost/", { unix: socketPath, headers: { connection: "close" } }); | ||
| expect({ statusCode: response.status, body: await response.text() }).toEqual({ statusCode: 200, body: "served" }); | ||
| } finally { | ||
| server.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("the constructor option is ignored on a plain http server, like Node's http.Server", async () => { | ||
| const options = { blockList: blockListCoveringLocalhost() } as any; | ||
| const server = http.createServer(options, (_req, res) => { | ||
| res.end("served"); | ||
| }) as ServerWithBlockList; | ||
| try { | ||
| expect(server.blockList).toBeUndefined(); | ||
| const port = await listen(server); | ||
| expect(await request(http, port)).toEqual({ statusCode: 200, body: "served" }); | ||
| } finally { | ||
| server.close(); | ||
| } | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.