Is it possible to make an HTTP CONNECT request with JavaScript in a browser?

1.5k views Asked by At

I know that if I change the proxy setting of my browser to use an HTTP forward proxy, it will issue HTTP CONNECT requests to the proxy server. But this kind of behavior is transparent to the frontend JavaScript code.

Out of pure curiosity, I'm wondering if one can do it on the client side JavaScript level. Is that even possible?

Chrome console:

fetch("https://example.com", {method: 'CONNECT'})

Error message:

PromiseĀ {: TypeError: Failed to execute 'fetch' on 'Window': 'CONNECT' HTTP method is unsupported.

I don't expect this request to return any meaningful response of couse. What surprises me is that the browser doesn't even allow the request to be sent.

2

There are 2 answers

0
Quentin On

No. The fetch specification lists CONNECT as a forbidden method as it can be used as part of a security exploit.

0
btwiuse On

Answering my own question in 2022.

Two years have passed and the situation has changed a bit with the advent of the WebTransport API, which uses the HTTP/3 protocol as a bidirectional transport.

According to it specs, it will send a request using an extended version of CONNECT method with the :protocol field set to webtransport on session establishment

So, if your browser supports WebTransport, it will initiate the connection using the CONNECT method under the hood.

At the time writing, only Chrome, Edge and Opera support WebTransport API. You can check the status of WebTransport API support at https://caniuse.com/webtransport

That being said, sending a normal CONNECT proxy request outside of the scope of WebTransport in the browser is currently not possible yet.