-
Notifications
You must be signed in to change notification settings - Fork 5k
node:https: make req.socket a tls.TLSSocket with getPeerCertificate() #37255
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
4
commits into
main
Choose a base branch
from
farm/f61464fe/https-req-socket-tlssocket
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.
+295
−7
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4f97cf8
node:https: make req.socket a tls.TLSSocket with getPeerCertificate()
robobun c929261
test: move the https req.socket TLSSocket coverage to its own file
robobun e9ec5d1
refactor: fold the native-socket check into a predicate, trim comments
robobun 501d893
address review: use or_pending_exception, set isServer as own property
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
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,120 @@ | ||
| import { expect, it } from "bun:test"; | ||
| import { readFileSync } from "fs"; | ||
| import https from "https"; | ||
| import net, { AddressInfo } from "net"; | ||
| import { once } from "node:events"; | ||
| import { join } from "path"; | ||
| import tls, { TLSSocket } from "tls"; | ||
|
|
||
| // https://github.com/oven-sh/bun/issues/37251: the socket node:https hands | ||
| // the request handler must be a tls.TLSSocket so mTLS servers can read the | ||
| // client identity via req.socket.getPeerCertificate(). | ||
| it("https req.socket is a TLSSocket that exposes the client certificate", async () => { | ||
| const fixtures = join(import.meta.dir, "../tls/fixtures"); | ||
| const serverOptions = { | ||
| key: readFileSync(join(fixtures, "agent10-key.pem"), "utf8"), | ||
| cert: readFileSync(join(fixtures, "agent10-cert.pem"), "utf8"), | ||
| ca: readFileSync(join(fixtures, "ca5-cert.pem"), "utf8"), | ||
| requestCert: true, | ||
| rejectUnauthorized: false, | ||
| }; | ||
| const { promise, resolve, reject } = Promise.withResolvers<Record<string, unknown>>(); | ||
| const server = https.createServer(serverOptions, (req, res) => { | ||
| try { | ||
| const s = req.socket as TLSSocket; | ||
| resolve({ | ||
| ctor: s.constructor.name, | ||
| isTLSSocket: s instanceof TLSSocket, | ||
| isNetSocket: s instanceof net.Socket, | ||
| authorized: s.authorized, | ||
| peerCN: s.getPeerCertificate()?.subject?.CN, | ||
| detailedCN: s.getPeerCertificate(true)?.subject?.CN, | ||
| x509Subject: s.getPeerX509Certificate()?.subject, | ||
| cipherName: s.getCipher()?.name, | ||
| protocol: s.getProtocol(), | ||
| }); | ||
| } catch (err) { | ||
| reject(err); | ||
| } | ||
| res.end("ok"); | ||
| }); | ||
| server.on("error", reject); | ||
| server.listen(0, "127.0.0.1"); | ||
| await once(server, "listening"); | ||
| const { port } = server.address() as AddressInfo; | ||
| const clientRequest = https.request({ | ||
| port, | ||
| host: "127.0.0.1", | ||
| rejectUnauthorized: false, | ||
| key: readFileSync(join(fixtures, "ec10-key.pem"), "utf8"), | ||
| cert: readFileSync(join(fixtures, "ec10-cert.pem"), "utf8"), | ||
| agent: false, | ||
| }); | ||
| clientRequest.on("error", reject); | ||
| clientRequest.end(); | ||
| try { | ||
| const got = await promise; | ||
| expect(got).toEqual({ | ||
| ctor: "TLSSocket", | ||
| isTLSSocket: true, | ||
| isNetSocket: true, | ||
| authorized: true, | ||
| peerCN: "agent10.example.com", | ||
| detailedCN: "agent10.example.com", | ||
| x509Subject: expect.stringContaining("CN=agent10.example.com"), | ||
| cipherName: expect.any(String), | ||
| protocol: expect.stringMatching(/^TLSv/), | ||
| }); | ||
| } finally { | ||
| clientRequest.destroy(); | ||
| server.close(); | ||
| } | ||
| }); | ||
|
|
||
| // tls.createServer was already fine (the issue is specific to node:https); | ||
| // keep it covered so the two server paths stay in sync. | ||
| it("tls.createServer connection sockets expose the client certificate the same way", async () => { | ||
| const fixtures = join(import.meta.dir, "../tls/fixtures"); | ||
| const serverOptions = { | ||
| key: readFileSync(join(fixtures, "agent10-key.pem"), "utf8"), | ||
| cert: readFileSync(join(fixtures, "agent10-cert.pem"), "utf8"), | ||
| ca: readFileSync(join(fixtures, "ca5-cert.pem"), "utf8"), | ||
| requestCert: true, | ||
| rejectUnauthorized: false, | ||
| }; | ||
| const { promise, resolve, reject } = Promise.withResolvers<Record<string, unknown>>(); | ||
| const server = tls.createServer(serverOptions, socket => { | ||
| try { | ||
| resolve({ | ||
| isTLSSocket: socket instanceof TLSSocket, | ||
| authorized: socket.authorized, | ||
| peerCN: socket.getPeerCertificate()?.subject?.CN, | ||
| }); | ||
| } catch (err) { | ||
| reject(err); | ||
| } | ||
| socket.end(); | ||
| }); | ||
| server.on("error", reject); | ||
| server.listen(0, "127.0.0.1"); | ||
| await once(server, "listening"); | ||
| const { port } = server.address() as AddressInfo; | ||
| const client = tls.connect({ | ||
| port, | ||
| host: "127.0.0.1", | ||
| rejectUnauthorized: false, | ||
| key: readFileSync(join(fixtures, "ec10-key.pem"), "utf8"), | ||
| cert: readFileSync(join(fixtures, "ec10-cert.pem"), "utf8"), | ||
| }); | ||
| client.on("error", reject); | ||
| try { | ||
| expect(await promise).toEqual({ | ||
| isTLSSocket: true, | ||
| authorized: true, | ||
| peerCN: "agent10.example.com", | ||
| }); | ||
| } finally { | ||
| client.end(); | ||
| server.close(); | ||
| } | ||
| }); |
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.