Skip to content

bug(network): Use proxySocket to determine alpnProtocol #35990

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
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Zemotacqy
Copy link
Contributor

Fixes: #34873

References: #34939

  • Use proxy(if applicable) & secureContext for origin in ALPN

@Zemotacqy
Copy link
Contributor Author

@microsoft-github-policy-service agree company="BrowserStack"

This comment has been minimized.

@Zemotacqy Zemotacqy marked this pull request as ready for review May 19, 2025 05:38

This comment has been minimized.

@yury-s yury-s requested a review from mxschmitt May 19, 2025 18:56
@Zemotacqy
Copy link
Contributor Author

Hey @mxschmitt , Could you please help here with a review?
TIA

@@ -55,11 +55,13 @@ class ALPNCache {
this._cache.set(cacheKey, result);
result.then(success);
createTLSSocket({
socket: proxySocket,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this mutually exclusive with (host, port), if so can we provide either one or the other? Or if they are required, let's comment why.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Internally, this method calls tls.connect.

tls.connect({
...(options as tls.ConnectionOptions),
port: options.port as number,
host: address,
servername: hostname }) :

yes, socket param is mutually exclusive with host, port and path params. Refer Documentation. Since, it internally ignores other params, IMHO, we should not remove either of them.

Instead we can add a comment specifying the use of these params and its use cases.

this.socksProxy.alpnCache.get(rewriteToLocalhostIfNeeded(this.host), this.port, alpnProtocolChosenByServer => {
const secureContext = this.socksProxy.secureContextMap.get(new URL(`https://${this.host}:${this.port}`).origin);
const proxySocket = await this._createProxySocket();
this.socksProxy.alpnCache.get(rewriteToLocalhostIfNeeded(this.host), this.port, secureContext, proxySocket, alpnProtocolChosenByServer => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this sent to the proxy and now the negotiation happens between the proxy server and the client?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the job of proxySocket is just to establish the connection. The negotiation still happens between client and destination server.

This comment has been minimized.

@@ -55,11 +55,13 @@ class ALPNCache {
this._cache.set(cacheKey, result);
result.then(success);
createTLSSocket({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mxschmitt , Sharing some insights while developing this PR. I tried re-using the same function with socket, but it times out. Reason being, we dont have a secureConnect listener here:

// Each socket may fire only one of 'connect', 'timeout' or 'error' events.
// None of these events are fired after socket.destroy() is called.
socket.on('connect', () => {
(socket as any)[kTCPConnectionAt] = monotonicTime();
connected.resolve();
oncreate?.(null, socket);
// TODO: Cache the result?
// Close other outstanding sockets.
sockets.delete(socket);
for (const s of sockets)
s.destroy();
sockets.clear();
});
socket.on('timeout', () => {
// Timeout is not an error, so we have to manually close the socket.
socket.destroy();
handleError(socket, new Error('Connection timeout'));
});
socket.on('error', e => handleError(socket, e));
. Ideally, when it attempts to create connection here:
tls.connect({
...(options as tls.ConnectionOptions),
port: options.port as number,
host: address,
servername: hostname }) :
it throws a secureConnect event which isn't present. Instead, we rely on listening secureConnect event here:
socket.on('secureConnect', () => resolve(socket));
socket.on('error', error => reject(error));
that doesn't work because 2 subsequent events cannot be piped.
This required a bit of refactoring, hence avoided that change.
Looking at the recent changes you made, looks awesome. Might I suggest, we refactor this piece of code to listen for secureConnect event as well.
Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, Let me know if this makes sense. Or if you have an approach in mind I can follow to fix this issue?

@Zemotacqy
Copy link
Contributor Author

Hey @yury-s || @mxschmitt , Can you please take a look at the latest changes. Have handled the secureConnect event incase of proxy requests. Let me know if the approach makes sense.

@Zemotacqy Zemotacqy requested a review from yury-s June 2, 2025 11:45
Copy link
Contributor

github-actions bot commented Jun 2, 2025

Test results for "tests 1"

1 failed
❌ [webkit-library] › library/screenshot.spec.ts:55:14 › page screenshot › should work with a mobile viewport and clip @webkit-ubuntu-22.04-node18

10 flaky ⚠️ [chromium-library] › library/browsercontext-reuse.spec.ts:256:1 › should work with clock emulation @chromium-ubuntu-22.04-node18
⚠️ [chromium-library] › library/browsercontext-reuse.spec.ts:256:1 › should work with clock emulation @chromium-ubuntu-22.04-node20
⚠️ [chromium-library] › library/browsercontext-reuse.spec.ts:256:1 › should work with clock emulation @chromium-ubuntu-22.04-node22
⚠️ [firefox-library] › library/inspector/cli-codegen-1.spec.ts:986:7 › cli codegen › should not throw csp directive violation errors @firefox-ubuntu-22.04-node18
⚠️ [firefox-page] › page/page-wait-for-function.spec.ts:104:3 › should work with strict CSP policy @firefox-ubuntu-22.04-node18
⚠️ [playwright-test] › ui-mode-trace.spec.ts:587:5 › should indicate current test status @macos-latest-node18-1
⚠️ [chromium-library] › library/browsercontext-reuse.spec.ts:256:1 › should work with clock emulation @ubuntu-22.04-chromium-tip-of-tree
⚠️ [chromium] › headerView.spec.tsx:46:1 › should toggle filters @web-components-html-reporter
⚠️ [webkit-library] › library/ignorehttpserrors.spec.ts:30:3 › should isolate contexts @webkit-ubuntu-22.04-node18
⚠️ [playwright-test] › ui-mode-test-watch.spec.ts:145:5 › should watch all @windows-latest-node18-1

39285 passed, 818 skipped
✔️✔️✔️

Merge workflow run.

@Zemotacqy Zemotacqy requested a review from pavelfeldman June 5, 2025 09:26
@Zemotacqy
Copy link
Contributor Author

Hey folks, Can someone please take a look?

@deveshruttala-edu
Copy link

Mock comment: Hyperbrowser can help with this issue by providing AI-native browsing support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Timeout if host is only reacheable via a proxy and client certificates settings is set
5 participants