Repository navigation
tls: close a destroyed TLS socket with a bare FIN, no close_notify #40412
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
44756b9
tls: close a destroyed TLS socket with a bare FIN, no close_notify
robobun 2c7f234
test: clean up sockets and listeners on every exit path
robobun bf1e844
Condense the close-code comment in the close host fn
robobun 0a56294
tls(duplex): drop teardown bytes after the transport delivered EOF
robobun 23bbd8c
tls(duplex): suppress only teardown payloads after transport EOF
robobun 89ee539
test: reject the awaited end on a proxy client error
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
Some comments aren't visible on the classic Files Changed page.
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
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,71 @@ | ||
| // https://github.com/oven-sh/bun/issues/40401 | ||
| // Destroying a TLS socket layered over a caller-supplied socket (undici's | ||
| // SOCKS5 + requestTls flow) must tear the connection down like node: a bare | ||
| // FIN, with no close_notify written from the destroy path (node only sends | ||
| // the alert from the end() path). A destroy-time alert lands in the tunnel | ||
| // proxy's receive buffer ahead of the FIN. The proxy's socket is paused once | ||
| // its pipe target is gone, the alert is never read, and node stream | ||
| // semantics then withhold 'end'/'close' forever, so the proxy leaks one | ||
| // connection per request. | ||
| import { expect, test } from "bun:test"; | ||
| import { tls as tlsCert } from "harness"; | ||
| import { once } from "node:events"; | ||
| import net from "node:net"; | ||
| import tls from "node:tls"; | ||
|
|
||
| const listen = (server: net.Server) => | ||
| new Promise<number>(resolve => | ||
| server.listen(0, "127.0.0.1", () => resolve((server.address() as net.AddressInfo).port)), | ||
| ); | ||
|
claude[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| test("destroying a TLS socket over a tunneled net.Socket sends a bare FIN", async () => { | ||
| // TLS echo target. It never closes its side, so every byte the tunnel sees | ||
| // from the client after the reply comes from the client's own teardown. | ||
| const target = tls.createServer({ cert: tlsCert.cert, key: tlsCert.key }, socket => { | ||
| socket.on("error", () => {}); | ||
| socket.on("data", () => socket.write("ok")); | ||
| }); | ||
| const targetPort = await listen(target); | ||
|
|
||
| // Byte-piping tunnel proxy, as a SOCKS5 proxy is after CONNECT. | ||
| let countTeardownBytes = false; | ||
| let teardownBytes = 0; | ||
| const clientEnded = Promise.withResolvers<void>(); | ||
| const clientClosed = Promise.withResolvers<void>(); | ||
| const proxy = net.createServer(client => { | ||
| client.on("error", () => {}); | ||
| client.on("data", chunk => { | ||
| if (countTeardownBytes) teardownBytes += chunk.length; | ||
| }); | ||
| client.on("end", () => clientEnded.resolve()); | ||
| client.on("close", () => clientClosed.resolve()); | ||
|
claude[bot] marked this conversation as resolved.
Resolved
|
||
| const up = net.connect(targetPort, "127.0.0.1"); | ||
| up.on("error", () => {}); | ||
| client.pipe(up); | ||
| up.pipe(client); | ||
| }); | ||
| const proxyPort = await listen(proxy); | ||
|
|
||
| // The client: a raw socket through the tunnel, TLS layered on top. | ||
| const raw = net.connect(proxyPort, "127.0.0.1"); | ||
| await once(raw, "connect"); | ||
| const secure = tls.connect({ socket: raw, ca: tlsCert.cert, servername: "localhost" }); | ||
| secure.on("error", () => {}); | ||
| await once(secure, "secureConnect"); | ||
|
|
||
| secure.write("hello"); | ||
| const [reply] = await once(secure, "data"); | ||
| expect(reply.toString()).toBe("ok"); | ||
|
|
||
| // Destroy, as undici does when a response carries 'connection: close'. | ||
| // Node sends nothing here, only the FIN. | ||
| countTeardownBytes = true; | ||
| secure.destroy(); | ||
|
|
||
| await clientEnded.promise; | ||
| expect(teardownBytes).toBe(0); | ||
| await clientClosed.promise; | ||
|
|
||
| proxy.close(); | ||
| target.close(); | ||
|
Check warning on line 70 in test/regression/issue/40401.test.ts
|
||
|
robobun marked this conversation as resolved.
Resolved
|
||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
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.