Skip to content
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

http_client: prevent domain fronting (Host mismatch) when using proxy #36805

Closed
wants to merge 5 commits into from
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
16 changes: 14 additions & 2 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const {
StringPrototypeCharCodeAt,
StringPrototypeIncludes,
StringPrototypeIndexOf,
StringPrototypeStartsWith,
StringPrototypeToUpperCase,
Symbol,
TypedArrayPrototypeSlice,
Expand Down Expand Up @@ -270,6 +271,17 @@ function ClientRequest(input, options, cb) {

if (host && !this.getHeader('host') && setHost) {
let hostHeader = host;
let hostHeaderPort = port;

// Prevent potential domain fronting misjudgement when using proxy
// Overwrite the default Host request header with the host of path
if (!StringPrototypeStartsWith(this.path, '/')) {
const proxyURL = new URL(RegExpPrototypeTest(
/^[^:]+:\/\//, this.path) ? this.path : `http://${this.path}`);
hostHeader = proxyURL.hostname;
hostHeaderPort = proxyURL.port || (
proxyURL.protocol === 'https:' ? '443' : '80');
}

// For the Host header, ensure that IPv6 addresses are enclosed
// in square brackets, as defined by URI formatting
Expand All @@ -281,8 +293,8 @@ function ClientRequest(input, options, cb) {
hostHeader = `[${hostHeader}]`;
}

if (port && +port !== defaultPort) {
hostHeader += ':' + port;
if (hostHeaderPort && +hostHeaderPort !== defaultPort) {
hostHeader += ':' + hostHeaderPort;
}
this.setHeader('Host', hostHeader);
}
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-tls-over-http-tunnel.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ const proxy = net.createServer((clientSocket) => {
`CONNECT localhost:${server.address().port} ` +
'HTTP/1.1\r\n' +
'Proxy-Connections: keep-alive\r\n' +
`Host: localhost:${proxy.address().port}\r\n` +
// Match header Host with destination Host
`Host: localhost:${server.address().port}\r\n` +
'Connection: close\r\n\r\n');

console.log('PROXY: got CONNECT request');
Expand Down