Skip to content

Make encrypt_after_connect support work - #40709

Merged
DanielRyanSmith merged 2 commits into
web-platform-tests:masterfrom
cbiesinger:proxysupport
Oct 9, 2026
Merged

DanielRyanSmith merged 2 commits into
web-platform-tests:masterfrom
cbiesinger:proxysupport

Conversation

@cbiesinger

Copy link
Copy Markdown
Contributor

And also enable it for wptrunner.

There were a few bugs here:

  • close_connection was not set to False in handle_connect. As a result, the actual request coming over the now-encrypted connection was not handled.
  • When the server is used as a proxy, for HTTP requests the client will send a full URL in the request line. This is always http because https will use the CONNECT method. So use http instead of using the server's scheme.
  • For some reason, even with close_connection=True, the socket does not seem to actually get closed. Because many handlers do not send a content length header, this leads to the request timing out. Directly call shutdown() to ensure the socket gets closed.

This PR also sets encrypt_after_connect to True when SSL is not turned on.

Comment thread tools/wptserve/wptserve/server.py Outdated

_host, self.port = self.httpd.socket.getsockname()
except Exception:
except Exception as e:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

unit test failed due to this line:

./wptserve/wptserve/server.py:846:9: F841 local variable 'e' is assigned to but never used

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed now

Comment thread tools/serve/serve.py Outdated
use_ssl=False,
key_file=None,
certificate=None,
use_ssl=True,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems when use_ssl is true, the scheme will be https. Is this what we want? (A bit of background might help.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, hm, I think you're right that it shouldn't. Fixed.

Comment thread tools/wptserve/wptserve/request.py Outdated
# We can hardcore http here instead of using scheme because this only
# happens when the server is used as a proxy server, and that never
# sends an HTTPS URL (because those use CONNECT)
if self.request_path.startswith("http://"):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So now line 294 is only for connecting to a proxy server's case. This does not seem to be the case previously.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Why do you say that? In which non-proxy cases would a user agent send a URL with scheme in the request?

That said, given the other change this is no longer needed so I reverted it.

Comment thread tools/wptserve/wptserve/server.py Resolved

@cbiesinger cbiesinger left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the comments! Please take another look, @WeizhongX

Comment thread tools/wptserve/wptserve/server.py Resolved
Comment thread tools/wptserve/wptserve/request.py Outdated
# We can hardcore http here instead of using scheme because this only
# happens when the server is used as a proxy server, and that never
# sends an HTTPS URL (because those use CONNECT)
if self.request_path.startswith("http://"):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Why do you say that? In which non-proxy cases would a user agent send a URL with scheme in the request?

That said, given the other change this is no longer needed so I reverted it.

Comment thread tools/serve/serve.py Outdated
use_ssl=False,
key_file=None,
certificate=None,
use_ssl=True,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, hm, I think you're right that it shouldn't. Fixed.

@WeizhongX

Copy link
Copy Markdown
Contributor

The test failed with below message. Is this expected?

/infrastructure/server/test-pac.html
FAIL test that PAC metadata is respected - promise_test: Unhandled rejection with value: object "TypeError: NetworkError when attempting to fetch resource."

@cbiesinger

Copy link
Copy Markdown
Contributor Author

FYI this is not ready for merge, it breaks wpt serve and I don't know yet why

@cbiesinger

Copy link
Copy Markdown
Contributor Author

OK, I think this is now ready to land. Sorry for the very long delay.

This will be useful for FedCM and EVP tests.

@cbiesinger

Copy link
Copy Markdown
Contributor Author

The MacOS failure looks unrelated (unexpected passes in unrelated files)

@jonathan-j-lee jonathan-j-lee left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Given how old this PR is, I re-reviewed it and it still LGTM.

Comment thread infrastructure/server/test-pac.sub.html Resolved
cbiesinger and others added 2 commits October 8, 2026 17:21
Enable encrypt_after_connect on the plain HTTP server when SSL is
available, so that a PAC file can route https:// requests through it.
Always compute an SSL config when certificates are available, even if
the SSL environment is not otherwise enabled.

Also pass the CA certificate path for the pregenerated certificates so
that an SSL config is produced by default, and fall back to plain HTTP
in start_http_server when there is no SSL config.

Extend infrastructure/server/test-pac to cover an https:// request
through the proxy.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Close TLS connections set up after a CONNECT request in finish(),
sending a close_notify first. socketserver only closes the socket it
accepted, which wrap_socket() detaches, so the TLS socket was left
open. This replaces shutting down every connection that is marked to
be closed, which ended TLS connections without a close_notify.

Track the scheme per connection, so that requests sent through a
CONNECT tunnel have an https URL instead of the HTTP server's scheme.

Explain why the connection is kept open after a CONNECT request.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@cbiesinger
cbiesinger enabled auto-merge (rebase) October 9, 2026 14:33
@DanielRyanSmith
DanielRyanSmith merged commit 3b8aed1 into web-platform-tests:master Oct 9, 2026
51 of 56 checks passed
@cbiesinger
cbiesinger deleted the proxysupport branch October 9, 2026 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants