Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/http_jsc/websocket_client/WebSocketUpgradeClient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,18 +832,6 @@ where
fn handle_proxy_response(this: ThisPtr<Self>, socket: Socket<SSL>, data: &[u8]) {
log!("handleProxyResponse");

const HTTP_200: &[u8] = b"HTTP/1.1 200 ";
const HTTP_200_ALT: &[u8] = b"HTTP/1.0 200 ";
if this.body.get().is_empty()
&& data.len() > HTTP_200.len()
&& !data.starts_with(HTTP_200)
&& !data.starts_with(HTTP_200_ALT)
{
// Proxy connection failed
Self::terminate(this, ErrorCode::ProxyConnectFailed);
return;
}

let (full, status_code, head_len) = match this.buffer_and_parse_head(data) {
HeadParse::Done {
full,
Comment thread
robobun marked this conversation as resolved.
Resolved
Expand Down
2 changes: 1 addition & 1 deletion test/js/first_party/ws/ws-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe("ws package through HTTP CONNECT proxy", () => {
});
// The proxy answered 407 to a CONNECT without credentials.
expect({ events: await wsFailingSession(ws), requests: recorded.requests }).toEqual({
events: failed(url, "Proxy connection failed", 1006),
events: failed(url, "Proxy authentication required", 1006),
requests: [connectRequest(wsPort)],
});
gc();
Expand Down
120 changes: 116 additions & 4 deletions test/js/web/websocket/websocket-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { tls as tlsCerts } from "harness";
import type { HttpsProxyAgent as HttpsProxyAgentType } from "https-proxy-agent";
import { createHash } from "node:crypto";
import { once } from "node:events";
import type { AddressInfo } from "node:net";
import net, { type AddressInfo } from "node:net";
import tls from "node:tls";
import {
type ClientEvent,
Expand Down Expand Up @@ -249,7 +249,7 @@ describe("WebSocket through HTTP CONNECT proxy", () => {
});
// The proxy answered 407 to a CONNECT without credentials.
expect({ events: await failingSession(ws), requests: recorded.requests }).toEqual({
events: failed(url, "Proxy connection failed", 1006),
events: failed(url, "Proxy authentication required", 1006),
requests: [connectRequest(wsPort)],
});
gc();
Expand All @@ -270,6 +270,118 @@ describe("WebSocket through HTTP CONNECT proxy", () => {
});
});

describe("the CONNECT reply gives the same outcome wherever the first read ends", () => {
const message = "hello after the CONNECT reply";

// The event loop runs setImmediate callbacks, then polls sockets. The second
// callback therefore runs after a poll, and in that poll the client read
// what the proxy wrote before this call.
async function socketPoll() {
await new Promise<void>(resolve => setImmediate(resolve));
await new Promise<void>(resolve => setImmediate(resolve));
}

/**
* A proxy that answers every CONNECT with `reply`. With `splitAt`, the client
* reads that many bytes of the reply before the proxy writes the rest. With
* `tunnel`, the proxy then relays to the echo server, otherwise it hangs up.
*/
async function startFixedReplyProxy(reply: string, { splitAt, tunnel }: { splitAt?: number; tunnel: boolean }) {
let connections = 0;
const proxy = net.createServer(client => {
connections++;
let request = "";
let target: net.Socket | undefined;
// The client hangs up as soon as it rejects a reply, so a later write can fail.
client.on("error", () => {});
client.on("data", async chunk => {
if (target) {
target.write(chunk);
return;
}
request += chunk.toString("latin1");
if (!request.endsWith("\r\n\r\n")) return;

if (splitAt === undefined) {
client.write(reply);
} else {
client.write(reply.slice(0, splitAt));
await socketPoll();
client.write(reply.slice(splitAt));
}
if (!tunnel) {
client.end();
return;
}
target = net.connect(wsPort, "127.0.0.1");
target.on("data", bytes => client.write(bytes));
target.on("error", () => client.destroy());
target.on("close", () => client.destroy());
client.on("close", () => target!.destroy());
});
});
const port = await startProxy(proxy);
return {
port,
get connections() {
return connections;
},
[Symbol.dispose]() {
proxy.close();
},
};
}

describe.each([
{
name: "407",
reply:
"HTTP/1.1 407 Proxy Authentication Required\r\n" +
'Proxy-Authenticate: Basic realm="proxy"\r\n' +
"Content-Length: 0\r\n\r\n",
tunnel: false,
events: (url: string) => failed(url, "Proxy authentication required", 1006),
},
{
name: "502",
reply: "HTTP/1.1 502 Bad Gateway\r\nContent-Length: 0\r\n\r\n",
tunnel: false,
events: (url: string) => failed(url, "Proxy connection failed", 1006),
},
{
name: "200 without a reason phrase",
reply: "HTTP/1.1 200\r\n\r\n",
tunnel: true,
events: () => echoed(message),
},
{
name: "200 over HTTP/1.0",
reply: "HTTP/1.0 200 Connection established\r\n\r\n",
tunnel: true,
events: () => echoed(message),
},
{
name: "a reply that is not HTTP",
reply: "SSH-2.0-OpenSSH_9.6\r\n",
tunnel: false,
events: (url: string) => failed(url, "Invalid response", 1002),
},
])("$name", ({ reply, tunnel, events }) => {
test.each([
["in one read", undefined],
["in two reads, the first one 10 bytes long", 10],
] as const)("%s", async (_, splitAt) => {
using proxy = await startFixedReplyProxy(reply, { splitAt, tunnel });
const url = `ws://127.0.0.1:${wsPort}`;
const ws = new WebSocket(url, { proxy: `http://127.0.0.1:${proxy.port}` });
expect({ events: await echoSession(ws, message), proxyConnections: proxy.connections }).toEqual({
events: events(url),
proxyConnections: 1,
});
});
});
});

describe("WebSocket wss:// through HTTP proxy (TLS tunnel)", () => {
// This tests the TLS tunnel: wss:// target through HTTP proxy
// The outer connection is plain TCP to the HTTP proxy, then TLS is
Expand Down Expand Up @@ -904,8 +1016,8 @@ describe.concurrent("WebSocket NO_PROXY bypass", () => {
? { stdout: `message: connected\nclose: 1000 ""\n`, stderr: "", exitCode: 0, proxyConnections: 0 }
: {
stdout:
`error: WebSocket connection to 'ws://127.0.0.1:${wsPort}/' failed: Proxy connection failed\n` +
`close: 1006 "Proxy connection failed"\n`,
`error: WebSocket connection to 'ws://127.0.0.1:${wsPort}/' failed: Proxy authentication required\n` +
`close: 1006 "Proxy authentication required"\n`,
stderr: "",
exitCode: 0,
proxyConnections: 1,
Expand Down
Loading