Skip to content

Commit

Permalink
http2: throw better error when accessing unbound socket proxy
Browse files Browse the repository at this point in the history
Fixes: #22268

Backport-PR-URL: #22850
PR-URL: #22486
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
jasnell authored and BethGriggs committed Oct 16, 2018
1 parent d0be932 commit 6a396ff
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
6 changes: 6 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,12 @@ The `Http2Session` settings canceled.
An attempt was made to connect a `Http2Session` object to a `net.Socket` or
`tls.TLSSocket` that had already been bound to another `Http2Session` object.

<a id="ERR_HTTP2_SOCKET_UNBOUND"></a>
### ERR_HTTP2_SOCKET_UNBOUND

An attempt was made to use the `socket` property of an `Http2Session` that
has already been closed.

<a id="ERR_HTTP2_STATUS_101"></a>
### ERR_HTTP2_STATUS_101

Expand Down
2 changes: 2 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ E('ERR_HTTP2_SESSION_ERROR', 'Session closed with error code %s');
E('ERR_HTTP2_SETTINGS_CANCEL', 'HTTP2 session settings canceled');
E('ERR_HTTP2_SOCKET_BOUND',
'The socket is already bound to an Http2Session');
E('ERR_HTTP2_SOCKET_UNBOUND',
'The socket has been disconnected from the Http2Session');
E('ERR_HTTP2_STATUS_101',
'HTTP status code 101 (Switching Protocols) is forbidden in HTTP/2');
E('ERR_HTTP2_STATUS_INVALID', 'Invalid status code: %s');
Expand Down
12 changes: 10 additions & 2 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,17 @@ const proxySocketHandler = {
throw new errors.Error('ERR_HTTP2_NO_SOCKET_MANIPULATION');
default:
const socket = session[kSocket];
if (socket === undefined)
throw new errors.Error('ERR_HTTP2_SOCKET_UNBOUND');
const value = socket[prop];
return typeof value === 'function' ? value.bind(socket) : value;
}
},
getPrototypeOf(session) {
return Reflect.getPrototypeOf(session[kSocket]);
const socket = session[kSocket];
if (socket === undefined)
throw new errors.Error('ERR_HTTP2_SOCKET_UNBOUND');
return Reflect.getPrototypeOf(socket);
},
set(session, prop, value) {
switch (prop) {
Expand All @@ -662,7 +667,10 @@ const proxySocketHandler = {
case 'write':
throw new errors.Error('ERR_HTTP2_NO_SOCKET_MANIPULATION');
default:
session[kSocket][prop] = value;
const socket = session[kSocket];
if (socket === undefined)
throw new errors.Error('ERR_HTTP2_SOCKET_UNBOUND');
socket[prop] = value;
return true;
}
}
Expand Down
69 changes: 69 additions & 0 deletions test/parallel/test-http2-unbound-socket-proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const http2 = require('http2');
const net = require('net');

const server = http2.createServer();
server.on('stream', common.mustCall((stream) => {
stream.respond();
stream.end('ok');
}));

server.listen(0, common.mustCall(() => {
const client = http2.connect(`http://localhost:${server.address().port}`);
const socket = client.socket;
const req = client.request();
req.resume();
req.on('close', common.mustCall(() => {
client.close();
server.close();

// Tests to make sure accessing the socket proxy fails with an
// informative error.
setImmediate(common.mustCall(() => {
common.expectsError(() => {
socket.example;
}, {
code: 'ERR_HTTP2_SOCKET_UNBOUND'
});
common.expectsError(() => {
socket.example = 1;
}, {
code: 'ERR_HTTP2_SOCKET_UNBOUND'
});
common.expectsError(() => {
socket instanceof net.Socket;
}, {
code: 'ERR_HTTP2_SOCKET_UNBOUND'
});
common.expectsError(() => {
socket.ref();
}, {
code: 'ERR_HTTP2_SOCKET_UNBOUND'
});
common.expectsError(() => {
socket.unref();
}, {
code: 'ERR_HTTP2_SOCKET_UNBOUND'
});
common.expectsError(() => {
socket.setEncoding();
}, {
code: 'ERR_HTTP2_SOCKET_UNBOUND'
});
common.expectsError(() => {
socket.setKeepAlive();
}, {
code: 'ERR_HTTP2_SOCKET_UNBOUND'
});
common.expectsError(() => {
socket.setNoDelay();
}, {
code: 'ERR_HTTP2_SOCKET_UNBOUND'
});
}));
}));
}));

0 comments on commit 6a396ff

Please sign in to comment.