Skip to content

Commit d1bc6f0

Browse files
addaleaxmcollina
authored andcommitted
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 bad4167 commit d1bc6f0

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

lib/internal/http2/core.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -2499,8 +2499,17 @@ function connectionListener(socket) {
24992499
return httpConnectionListener.call(this, socket);
25002500
}
25012501
// Let event handler deal with the socket
2502-
if (!this.emit('unknownProtocol', socket))
2503-
socket.destroy();
2502+
debug(`Unknown protocol from ${socket.remoteAddress}:${socket.remotePort}`);
2503+
if (!this.emit('unknownProtocol', socket)) {
2504+
// We don't know what to do, so let's just tell the other side what's
2505+
// going on in a format that they *might* understand.
2506+
socket.end('HTTP/1.0 403 Forbidden\r\n' +
2507+
'Content-Type: text/plain\r\n\r\n' +
2508+
'Unknown ALPN Protocol, expected `h2` to be available.\n' +
2509+
'If this is a HTTP request: The server was not ' +
2510+
'configured with the `allowHTTP1` option or a ' +
2511+
'listener for the `unknownProtocol` event.\n');
2512+
}
25042513
return;
25052514
}
25062515

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

+10-3
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)