Skip to content

Commit cdb6da9

Browse files
dyatlovMylesBorins
authored andcommitted
src: revert removal of SecureContext _external getter
This `_external` getter is essential for some libs to work: uWebSockets as an example. PR-URL: #21711 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 1afd207 commit cdb6da9

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

src/node_crypto.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,19 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
359359
t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kTicketKeyIVIndex"),
360360
Integer::NewFromUnsigned(env->isolate(), kTicketKeyIVIndex));
361361

362+
Local<FunctionTemplate> ctx_getter_templ =
363+
FunctionTemplate::New(env->isolate(),
364+
CtxGetter,
365+
env->as_external(),
366+
Signature::New(env->isolate(), t));
367+
368+
369+
t->PrototypeTemplate()->SetAccessorProperty(
370+
FIXED_ONE_BYTE_STRING(env->isolate(), "_external"),
371+
ctx_getter_templ,
372+
Local<FunctionTemplate>(),
373+
static_cast<PropertyAttribute>(ReadOnly | DontDelete));
374+
362375
target->Set(secureContextString,
363376
t->GetFunction(env->context()).ToLocalChecked());
364377
env->set_secure_context_constructor_template(t);
@@ -1327,6 +1340,14 @@ int SecureContext::TicketCompatibilityCallback(SSL* ssl,
13271340
}
13281341

13291342

1343+
void SecureContext::CtxGetter(const FunctionCallbackInfo<Value>& info) {
1344+
SecureContext* sc;
1345+
ASSIGN_OR_RETURN_UNWRAP(&sc, info.This());
1346+
Local<External> ext = External::New(info.GetIsolate(), sc->ctx_.get());
1347+
info.GetReturnValue().Set(ext);
1348+
}
1349+
1350+
13301351
template <bool primary>
13311352
void SecureContext::GetCertificate(const FunctionCallbackInfo<Value>& args) {
13321353
SecureContext* wrap;

src/node_crypto.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ class SecureContext : public BaseObject {
165165
const v8::FunctionCallbackInfo<v8::Value>& args);
166166
static void EnableTicketKeyCallback(
167167
const v8::FunctionCallbackInfo<v8::Value>& args);
168+
static void CtxGetter(const v8::FunctionCallbackInfo<v8::Value>& info);
168169

169170
template <bool primary>
170171
static void GetCertificate(const v8::FunctionCallbackInfo<v8::Value>& args);

test/parallel/test-accessor-properties.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44

55
// This tests that the accessor properties do not raise assertions
66
// when called with incompatible receivers.
@@ -52,4 +52,19 @@ const UDP = process.binding('udp_wrap').UDP;
5252
typeof Object.getOwnPropertyDescriptor(StreamWrapProto, 'fd'),
5353
'object'
5454
);
55+
56+
if (common.hasCrypto) { // eslint-disable-line node-core/crypto-check
57+
// There are accessor properties in crypto too
58+
const crypto = process.binding('crypto');
59+
60+
assert.throws(() => {
61+
crypto.SecureContext.prototype._external;
62+
}, TypeError);
63+
64+
assert.strictEqual(
65+
typeof Object.getOwnPropertyDescriptor(
66+
crypto.SecureContext.prototype, '_external'),
67+
'object'
68+
);
69+
}
5570
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const assert = require('assert');
8+
const tls = require('tls');
9+
10+
// Ensure accessing ._external doesn't hit an assert in the accessor method.
11+
{
12+
const pctx = tls.createSecureContext().context;
13+
const cctx = Object.create(pctx);
14+
assert.throws(() => cctx._external, TypeError);
15+
pctx._external;
16+
}
17+
{
18+
const pctx = tls.createSecurePair().credentials.context;
19+
const cctx = Object.create(pctx);
20+
assert.throws(() => cctx._external, TypeError);
21+
pctx._external;
22+
}

0 commit comments

Comments
 (0)