Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4471369

Browse filesBrowse files
committed
http2: send error text in case of ALPN mismatch
Send a human-readable HTTP/1 response in case of an unexpected ALPN protocol. This helps with debugging this condition, since previously the only result of it would be a closed socket. PR-URL: #18986 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 12856b0 commit 4471369
Copy full SHA for 4471369

File tree

Expand file treeCollapse file tree

2 files changed

+21
-5
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+21
-5
lines changed
Open diff view settings
Collapse file

‎lib/internal/http2/core.js‎

Copy file name to clipboardExpand all lines: lib/internal/http2/core.js
+11-2Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2488,8 +2488,17 @@ function connectionListener(socket) {
24882488
return httpConnectionListener.call(this, socket);
24892489
}
24902490
// Let event handler deal with the socket
2491-
if (!this.emit('unknownProtocol', socket))
2492-
socket.destroy();
2491+
debug(`Unknown protocol from ${socket.remoteAddress}:${socket.remotePort}`);
2492+
if (!this.emit('unknownProtocol', socket)) {
2493+
// We don't know what to do, so let's just tell the other side what's
2494+
// going on in a format that they *might* understand.
2495+
socket.end('HTTP/1.0 403 Forbidden\r\n' +
2496+
'Content-Type: text/plain\r\n\r\n' +
2497+
'Unknown ALPN Protocol, expected `h2` to be available.\n' +
2498+
'If this is a HTTP request: The server was not ' +
2499+
'configured with the `allowHTTP1` option or a ' +
2500+
'listener for the `unknownProtocol` event.\n');
2501+
}
24932502
return;
24942503
}
24952504

Collapse file

‎test/parallel/test-http2-https-fallback.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-http2-https-fallback.js
+10-3Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const fixtures = require('../common/fixtures');
66
if (!common.hasCrypto)
77
common.skip('missing crypto');
88

9-
const { strictEqual } = require('assert');
9+
const { strictEqual, ok } = require('assert');
1010
const { createSecureContext } = require('tls');
1111
const { createSecureServer, connect } = require('http2');
1212
const { get } = require('https');
@@ -131,10 +131,17 @@ function onSession(session) {
131131

132132
// HTTP/1.1 client
133133
get(Object.assign(parse(origin), clientOptions), common.mustNotCall())
134-
.on('error', common.mustCall(cleanup));
134+
.on('error', common.mustCall(cleanup))
135+
.end();
135136

136137
// Incompatible ALPN TLS client
138+
let text = '';
137139
tls(Object.assign({ port, ALPNProtocols: ['fake'] }, clientOptions))
138-
.on('error', common.mustCall(cleanup));
140+
.setEncoding('utf8')
141+
.on('data', (chunk) => text += chunk)
142+
.on('end', common.mustCall(() => {
143+
ok(/Unknown ALPN Protocol, expected `h2` to be available/.test(text));
144+
cleanup();
145+
}));
139146
}));
140147
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.