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

Disallow destructuring internalbinding c #54023

Closed
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
24 changes: 12 additions & 12 deletions lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const {
} = primordials;
const { setImmediate } = require('timers');

const { methods, allMethods, HTTPParser } = internalBinding('http_parser');
const httpParserBinding = internalBinding('http_parser');
const { getOptionValue } = require('internal/options');
const insecureHTTPParser = getOptionValue('--insecure-http-parser');

Expand All @@ -40,13 +40,13 @@ const {
} = incoming;

const kIncomingMessage = Symbol('IncomingMessage');
const kOnMessageBegin = HTTPParser.kOnMessageBegin | 0;
const kOnHeaders = HTTPParser.kOnHeaders | 0;
const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
const kOnBody = HTTPParser.kOnBody | 0;
const kOnMessageComplete = HTTPParser.kOnMessageComplete | 0;
const kOnExecute = HTTPParser.kOnExecute | 0;
const kOnTimeout = HTTPParser.kOnTimeout | 0;
const kOnMessageBegin = httpParserBinding.HTTPParser.kOnMessageBegin | 0;
const kOnHeaders = httpParserBinding.HTTPParser.kOnHeaders | 0;
const kOnHeadersComplete = httpParserBinding.HTTPParser.kOnHeadersComplete | 0;
const kOnBody = httpParserBinding.HTTPParser.kOnBody | 0;
const kOnMessageComplete = httpParserBinding.HTTPParser.kOnMessageComplete | 0;
const kOnExecute = httpParserBinding.HTTPParser.kOnExecute | 0;
const kOnTimeout = httpParserBinding.HTTPParser.kOnTimeout | 0;

const MAX_HEADER_PAIRS = 2000;

Expand Down Expand Up @@ -108,7 +108,7 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,

if (typeof method === 'number') {
// server only
incoming.method = allMethods[method];
incoming.method = httpParserBinding.allMethods[method];
} else {
// client only
incoming.statusCode = statusCode;
Expand Down Expand Up @@ -157,7 +157,7 @@ function parserOnMessageComplete() {


const parsers = new FreeList('parsers', 1000, function parsersCb() {
const parser = new HTTPParser();
const parser = new httpParserBinding.HTTPParser();

cleanParser(parser);

Expand Down Expand Up @@ -261,10 +261,10 @@ module.exports = {
continueExpression: /(?:^|\W)100-continue(?:$|\W)/i,
CRLF: '\r\n', // TODO: Deprecate this.
freeParser,
methods,
methods: httpParserBinding.methods,
parsers,
kIncomingMessage,
HTTPParser,
HTTPParser: httpParserBinding.HTTPParser,
isLenient,
prepareError,
};
4 changes: 2 additions & 2 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const {
_checkInvalidHeaderChar: checkInvalidHeaderChar,
prepareError,
} = require('_http_common');
const { ConnectionsList } = internalBinding('http_parser');
const httpParserBinding = internalBinding('http_parser');
const {
kUniqueHeaders,
parseUniqueHeadersOption,
Expand Down Expand Up @@ -509,7 +509,7 @@ function storeHTTPOptions(options) {
function setupConnectionsTracking() {
// Start connection handling
if (!this[kConnections]) {
this[kConnections] = new ConnectionsList();
this[kConnections] = new httpParserBinding.ConnectionsList();
}

if (this[kConnectionsCheckingInterval]) {
Expand Down
40 changes: 13 additions & 27 deletions lib/_tls_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,10 @@ const {

const tls = require('tls');

const {
codes: {
ERR_TLS_INVALID_PROTOCOL_VERSION,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Destructing symbols and constants should be safe and disallowing this could be way too verbose.

ERR_TLS_PROTOCOL_VERSION_CONFLICT,
},
} = require('internal/errors');
// eslint-disable-next-line node-core/alphabetize-errors
const internalErrorsBinding = require('internal/errors');

const {
crypto: {
SSL_OP_CIPHER_SERVER_PREFERENCE,
TLS1_VERSION,
TLS1_1_VERSION,
TLS1_2_VERSION,
TLS1_3_VERSION,
},
} = internalBinding('constants');
const constantsBinding = internalBinding('constants');

const {
kEmptyObject,
Expand All @@ -58,16 +46,14 @@ const {

function toV(which, v, def) {
if (v == null) v = def;
if (v === 'TLSv1') return TLS1_VERSION;
if (v === 'TLSv1.1') return TLS1_1_VERSION;
if (v === 'TLSv1.2') return TLS1_2_VERSION;
if (v === 'TLSv1.3') return TLS1_3_VERSION;
throw new ERR_TLS_INVALID_PROTOCOL_VERSION(v, which);
if (v === 'TLSv1') return constantsBinding.crypto.TLS1_VERSION;
if (v === 'TLSv1.1') return constantsBinding.crypto.TLS1_1_VERSION;
if (v === 'TLSv1.2') return constantsBinding.crypto.TLS1_2_VERSION;
if (v === 'TLSv1.3') return constantsBinding.crypto.TLS1_3_VERSION;
throw new internalErrorsBinding.codes.ERR_TLS_INVALID_PROTOCOL_VERSION(v, which);
}

const {
SecureContext: NativeSecureContext,
} = internalBinding('crypto');
const cryptoBinding = internalBinding('crypto');

function SecureContext(secureProtocol, secureOptions, minVersion, maxVersion) {
if (!(this instanceof SecureContext)) {
Expand All @@ -77,12 +63,12 @@ function SecureContext(secureProtocol, secureOptions, minVersion, maxVersion) {

if (secureProtocol) {
if (minVersion != null)
throw new ERR_TLS_PROTOCOL_VERSION_CONFLICT(minVersion, secureProtocol);
throw new internalErrorsBinding.codes.ERR_TLS_PROTOCOL_VERSION_CONFLICT(minVersion, secureProtocol);
if (maxVersion != null)
throw new ERR_TLS_PROTOCOL_VERSION_CONFLICT(maxVersion, secureProtocol);
throw new internalErrorsBinding.codes.ERR_TLS_PROTOCOL_VERSION_CONFLICT(maxVersion, secureProtocol);
}

this.context = new NativeSecureContext();
this.context = new cryptoBinding.SecureContext();
this.context.init(secureProtocol,
toV('minimum', minVersion, tls.DEFAULT_MIN_VERSION),
toV('maximum', maxVersion, tls.DEFAULT_MAX_VERSION));
Expand All @@ -106,7 +92,7 @@ function createSecureContext(options) {
let { secureOptions } = options;

if (honorCipherOrder)
secureOptions |= SSL_OP_CIPHER_SERVER_PREFERENCE;
secureOptions |= constantsBinding.crypto.SSL_OP_CIPHER_SERVER_PREFERENCE;

const c = new SecureContext(secureProtocol, secureOptions,
minVersion, maxVersion);
Expand Down
54 changes: 27 additions & 27 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ const { Buffer } = require('buffer');
let debug = require('internal/util/debuglog').debuglog('tls', (fn) => {
debug = fn;
});
const { TCP, constants: TCPConstants } = internalBinding('tcp_wrap');
const tcpWrapBinding = internalBinding('tcp_wrap');
const tls_wrap = internalBinding('tls_wrap');
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');
const { owner_symbol } = require('internal/async_hooks').symbols;
const { isArrayBufferView } = require('internal/util/types');
const { SecureContext: NativeSecureContext } = internalBinding('crypto');
const pipeWrapBinding = internalBinding('pipe_wrap');
const asyncHooksBinding = require('internal/async_hooks').symbols;
const utilBinding = require('internal/util/types');
const cryptoBinding = internalBinding('crypto');
const {
ConnResetException,
codes: {
Expand All @@ -77,7 +77,7 @@ const {
ERR_TLS_SNI_FROM_SERVER,
},
} = require('internal/errors');
const { onpskexchange: kOnPskExchange } = internalBinding('symbols');
const symbolsBinding = internalBinding('symbols');
const {
getOptionValue,
getAllowUnauthorized,
Expand Down Expand Up @@ -136,7 +136,7 @@ function onhandshakestart(now) {
else
this.handshakes++;

const owner = this[owner_symbol];
const owner = this[asyncHooksBinding.owner_symbol];

assert(owner._tlsOptions.isServer);

Expand All @@ -152,7 +152,7 @@ function onhandshakestart(now) {
function onhandshakedone() {
debug('server onhandshakedone');

const owner = this[owner_symbol];
const owner = this[asyncHooksBinding.owner_symbol];
assert(owner._tlsOptions.isServer);

// `newSession` callback wasn't called yet
Expand All @@ -170,7 +170,7 @@ function loadSession(hello) {
'sessionid.len', hello.sessionId.length,
'ticket?', hello.tlsTicket,
);
const owner = this[owner_symbol];
const owner = this[asyncHooksBinding.owner_symbol];

let once = false;
function onSession(err, session) {
Expand Down Expand Up @@ -205,7 +205,7 @@ function loadSession(hello) {


function loadSNI(info) {
const owner = this[owner_symbol];
const owner = this[asyncHooksBinding.owner_symbol];
const servername = info.servername;
if (!servername || !owner._SNICallback)
return requestOCSP(owner, info);
Expand Down Expand Up @@ -233,7 +233,7 @@ function loadSNI(info) {

function callALPNCallback(protocolsBuffer) {
const handle = this;
const socket = handle[owner_symbol];
const socket = handle[asyncHooksBinding.owner_symbol];

const servername = handle.getServername();

Expand Down Expand Up @@ -331,7 +331,7 @@ function requestOCSPDone(socket) {

function onnewsessionclient(sessionId, session) {
debug('client emit session');
const owner = this[owner_symbol];
const owner = this[asyncHooksBinding.owner_symbol];
if (owner[kIsVerified]) {
owner.emit('session', session);
} else {
Expand All @@ -341,7 +341,7 @@ function onnewsessionclient(sessionId, session) {

function onnewsession(sessionId, session) {
debug('onnewsession');
const owner = this[owner_symbol];
const owner = this[asyncHooksBinding.owner_symbol];

// TODO(@sam-github) no server to emit the event on, but handshake won't
// continue unless newSessionDone() is called, should it be, or is that
Expand Down Expand Up @@ -373,13 +373,13 @@ function onnewsession(sessionId, session) {
}

function onPskServerCallback(identity, maxPskLen) {
const owner = this[owner_symbol];
const owner = this[asyncHooksBinding.owner_symbol];
const ret = owner[kPskCallback](owner, identity);
if (ret == null)
return undefined;

let psk;
if (isArrayBufferView(ret)) {
if (utilBinding.isArrayBufferView(ret)) {
psk = ret;
} else {
if (typeof ret !== 'object') {
Expand All @@ -405,7 +405,7 @@ function onPskServerCallback(identity, maxPskLen) {
}

function onPskClientCallback(hint, maxPskLen, maxIdentityLen) {
const owner = this[owner_symbol];
const owner = this[asyncHooksBinding.owner_symbol];
const ret = owner[kPskCallback](hint);
if (ret == null)
return undefined;
Expand Down Expand Up @@ -435,16 +435,16 @@ function onPskClientCallback(hint, maxPskLen, maxIdentityLen) {

function onkeylog(line) {
debug('onkeylog');
this[owner_symbol].emit('keylog', line);
this[asyncHooksBinding.owner_symbol].emit('keylog', line);
}

function onocspresponse(resp) {
debug('client onocspresponse');
this[owner_symbol].emit('OCSPResponse', resp);
this[asyncHooksBinding.owner_symbol].emit('OCSPResponse', resp);
}

function onerror(err) {
const owner = this[owner_symbol];
const owner = this[asyncHooksBinding.owner_symbol];
debug('%s onerror %s had? %j',
(typeof owner._tlsOptions === 'object' && owner._tlsOptions !== null) ?
owner._tlsOptions.isServer ? 'server' : 'client' :
Expand Down Expand Up @@ -631,9 +631,9 @@ for (const proxiedMethod of proxiedMethods) {

tls_wrap.TLSWrap.prototype.close = function close(cb) {
let ssl;
if (this[owner_symbol]) {
ssl = this[owner_symbol].ssl;
this[owner_symbol].ssl = null;
if (this[asyncHooksBinding.owner_symbol]) {
ssl = this[asyncHooksBinding.owner_symbol].ssl;
this[asyncHooksBinding.owner_symbol].ssl = null;
}

// Invoke `destroySSL` on close to clean up possibly pending write requests
Expand Down Expand Up @@ -682,17 +682,17 @@ TLSSocket.prototype._wrapHandle = function(wrap, handle, wrapHasActiveWriteFromP
const options = this._tlsOptions;
if (!handle) {
handle = options.pipe ?
new Pipe(PipeConstants.SOCKET) :
new TCP(TCPConstants.SOCKET);
handle[owner_symbol] = this;
new pipeWrapBinding.Pipe(pipeWrapBinding.constants.SOCKET) :
new tcpWrapBinding.TCP(tcpWrapBinding.constants.SOCKET);
handle[asyncHooksBinding.owner_symbol] = this;
}

// Wrap socket's handle
const context = options.secureContext ||
options.credentials ||
tls.createSecureContext(options);
assert(handle.isStreamBase, 'handle must be a StreamBase');
if (!(context.context instanceof NativeSecureContext)) {
if (!(context.context instanceof cryptoBinding.SecureContext)) {
throw new ERR_TLS_INVALID_CONTEXT('context');
}

Expand Down Expand Up @@ -913,7 +913,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
if (options.pskCallback && ssl.enablePskCallback) {
validateFunction(options.pskCallback, 'pskCallback');

ssl[kOnPskExchange] = options.isServer ?
ssl[symbolsBinding.onpskexchange] = options.isServer ?
onPskServerCallback : onPskClientCallback;

this[kPskCallback] = options.pskCallback;
Expand Down
Loading
Loading