-
Notifications
You must be signed in to change notification settings - Fork 4.7k
ws: forward top-level TLS options (rejectUnauthorized, ca, ...) to the connection #31397
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
Open
robobun
wants to merge
11
commits into
main
Choose a base branch
from
farm/3cdf7917/ws-top-level-tls-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+281
−35
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
00a0cdc
ws: forward top-level TLS options (rejectUnauthorized, ca, ...) to th…
robobun 1bf65a5
test: move ws TLS regression to isolated file
robobun b4c121f
ws: merge agent TLS options with top-level TLS options
robobun a467761
test: place ws TLS tests in the module dir, not regression/issue
robobun 8c84f66
ci: re-run gate (release build was transient infra failure)
robobun d4e9939
ws: don't forward string[] ALPNProtocols into SSLConfig.fromJS
robobun 5f6b6b3
ws: keep an explicit tls object authoritative over agent options
robobun 4bb0159
ws: preserve explicit falsy scalar TLS options, keep empty file keys …
robobun 6eb061a
[autofix.ci] apply automated fixes
autofix-ci[bot] b53adc1
ws: don't forward the object-array key/cert form into SSLConfig.fromJS
robobun 2111ced
ws: also skip the bare-object key/cert form, not just the array-wrapp…
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| import { describe, expect, it } from "bun:test"; | ||
| import { tls as tlsCert } from "harness"; | ||
| import WebSocket from "ws"; | ||
|
|
||
| // https://github.com/oven-sh/bun/issues/31396 | ||
| // | ||
| // The npm `ws` package accepts TLS options as top-level options on the | ||
| // WebSocket constructor and forwards them to https.request/tls.connect: | ||
| // | ||
| // new WebSocket("wss://host", { rejectUnauthorized: false }); | ||
| // | ||
| // Bun's `ws` shim only read TLS options from `options.tls`, so top-level keys | ||
| // like `rejectUnauthorized: false` were dropped and connecting to a self-signed | ||
| // `wss://` server failed with "TLS handshake failed". | ||
| describe("ws top-level TLS options", () => { | ||
|
claude[bot] marked this conversation as resolved.
|
||
| function serveTls() { | ||
| return Bun.serve({ | ||
| port: 0, | ||
| tls: { key: tlsCert.key, cert: tlsCert.cert }, | ||
| fetch(req, server) { | ||
| if (server.upgrade(req)) return; | ||
| return new Response("expected websocket", { status: 400 }); | ||
| }, | ||
| websocket: { | ||
| open(ws) { | ||
| ws.close(); | ||
| }, | ||
| message() {}, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| it("rejectUnauthorized: false connects to a self-signed server", async () => { | ||
| await using server = serveTls(); | ||
| const { resolve, reject, promise } = Promise.withResolvers<void>(); | ||
|
|
||
| const ws = new WebSocket(`wss://localhost:${server.port}`, { rejectUnauthorized: false }); | ||
| ws.on("open", () => { | ||
| ws.close(); | ||
| resolve(); | ||
| }); | ||
| ws.on("error", reject); | ||
|
|
||
| await promise; | ||
| }); | ||
|
|
||
| it("a self-signed server is still rejected without rejectUnauthorized: false", async () => { | ||
| await using server = serveTls(); | ||
| const { resolve, reject, promise } = Promise.withResolvers<{ message: string }>(); | ||
|
|
||
| const ws = new WebSocket(`wss://localhost:${server.port}`); | ||
| ws.on("open", () => reject(new Error("unexpectedly connected to a self-signed server"))); | ||
| ws.on("error", resolve); | ||
|
|
||
| const err = await promise; | ||
| expect(err.message).toContain("TLS handshake failed"); | ||
| }); | ||
|
|
||
| // A top-level TLS key must not shadow TLS material an agent carries: `ws` | ||
| // forwards both to the connection. Here the agent supplies `ca` (the | ||
| // self-signed cert is its own CA, so validation passes with the default | ||
| // `rejectUnauthorized: true`) while the top level supplies `servername`. | ||
| // Both must reach the handshake — a naive replace would drop the agent's `ca`. | ||
| it("merges agent TLS options with top-level TLS options", async () => { | ||
| await using server = serveTls(); | ||
| const { resolve, reject, promise } = Promise.withResolvers<void>(); | ||
|
|
||
| const agent = { connectOpts: { ca: tlsCert.cert } }; | ||
| const ws = new WebSocket(`wss://localhost:${server.port}`, { agent, servername: "localhost" }); | ||
| ws.on("open", () => { | ||
| ws.close(); | ||
| resolve(); | ||
| }); | ||
| ws.on("error", reject); | ||
|
|
||
| await promise; | ||
| }); | ||
|
|
||
| // Node/`ws` accept `ALPNProtocols` as a string[], but Bun's native TLS parser | ||
| // only takes string/ArrayBuffer/null. Forwarding the array form used to throw | ||
| // a TypeError from the constructor; it must stay a no-op (WebSocket negotiates | ||
| // subprotocols over Sec-WebSocket-Protocol, not TLS ALPN) so the rest of the | ||
| // options still apply and the connection proceeds. | ||
| it("ignores a string[] ALPNProtocols instead of throwing", async () => { | ||
| await using server = serveTls(); | ||
| const { resolve, reject, promise } = Promise.withResolvers<void>(); | ||
|
|
||
| const ws = new WebSocket(`wss://localhost:${server.port}`, { | ||
| rejectUnauthorized: false, | ||
| ALPNProtocols: ["http/1.1"], | ||
| }); | ||
| ws.on("open", () => { | ||
| ws.close(); | ||
| resolve(); | ||
| }); | ||
| ws.on("error", reject); | ||
|
|
||
| await promise; | ||
| }); | ||
|
|
||
| // Node/`ws` accept `key`/`cert` as an array of `{ pem, passphrase }` objects | ||
| // (per-key passphrases), but Bun's native parser only understands | ||
| // string/ArrayBuffer/Blob (or arrays of those). Forwarding the object-array | ||
| // form used to throw a TypeError from the constructor; it must stay a no-op | ||
| // (as it was before top-level TLS forwarding) so construction doesn't throw. | ||
| it("ignores an object-array key instead of throwing", async () => { | ||
| await using server = serveTls(); | ||
| const { resolve, reject, promise } = Promise.withResolvers<void>(); | ||
|
|
||
| // The server doesn't request a client cert, so dropping the unparseable key | ||
| // is harmless and the connection still opens with rejectUnauthorized: false. | ||
| const ws = new WebSocket(`wss://localhost:${server.port}`, { | ||
| rejectUnauthorized: false, | ||
| key: [{ pem: tlsCert.key, passphrase: "" }], | ||
| cert: tlsCert.cert, | ||
| }); | ||
| ws.on("open", () => { | ||
| ws.close(); | ||
| resolve(); | ||
| }); | ||
| ws.on("error", reject); | ||
|
|
||
| await promise; | ||
| }); | ||
|
|
||
| // The bare (non-array) `{ pem, passphrase }` object form must behave the same | ||
| // as the array-wrapped form above: the native parser has no arm for a plain | ||
| // object, so it's skipped rather than forwarded into a constructor throw. | ||
| it("ignores a bare object key instead of throwing", async () => { | ||
| await using server = serveTls(); | ||
| const { resolve, reject, promise } = Promise.withResolvers<void>(); | ||
|
|
||
| const ws = new WebSocket(`wss://localhost:${server.port}`, { | ||
| rejectUnauthorized: false, | ||
| key: { pem: tlsCert.key, passphrase: "" }, | ||
| cert: tlsCert.cert, | ||
| }); | ||
| ws.on("open", () => { | ||
| ws.close(); | ||
| resolve(); | ||
| }); | ||
| ws.on("error", reject); | ||
|
|
||
| await promise; | ||
| }); | ||
|
|
||
| // An explicit Bun `tls` object is a hard override: an agent's connect options | ||
| // (which target the proxy hop) must not leak into it. Here the explicit `tls` | ||
| // leaves `rejectUnauthorized` at its default (true) while the agent carries | ||
| // `rejectUnauthorized: false`. The agent's value must not disable target | ||
| // verification, so the self-signed server is still rejected. | ||
| it("keeps an explicit tls object authoritative over agent options", async () => { | ||
| await using server = serveTls(); | ||
| const { resolve, reject, promise } = Promise.withResolvers<{ message: string }>(); | ||
|
|
||
| const agent = { connectOpts: { rejectUnauthorized: false } }; | ||
| const ws = new WebSocket(`wss://localhost:${server.port}`, { tls: {}, agent }); | ||
| ws.on("open", () => reject(new Error("agent rejectUnauthorized:false leaked into explicit tls"))); | ||
| ws.on("error", resolve); | ||
|
|
||
| const err = await promise; | ||
| expect(err.message).toContain("TLS handshake failed"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.