Skip to content

Commit a60b0c2

Browse files
bnoordhuisMyles Borins
authored and
Myles Borins
committed
tls: fix assert in context._external accessor
* Restrict the receiver to instances of the FunctionTemplate. * Use `args.This()` instead of `args.Holder()`. Fixes: #3682 PR-URL: #5521 Reviewed-By: Fedor Indutny <[email protected]>
1 parent cf02135 commit a60b0c2

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

src/node_crypto.cc

+24-21
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static const int X509_NAME_FLAGS = ASN1_STRFLGS_ESC_CTRL
6363
namespace node {
6464
namespace crypto {
6565

66+
using v8::AccessorSignature;
6667
using v8::Array;
6768
using v8::Boolean;
6869
using v8::Context;
@@ -315,7 +316,8 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
315316
nullptr,
316317
env->as_external(),
317318
DEFAULT,
318-
static_cast<PropertyAttribute>(ReadOnly | DontDelete));
319+
static_cast<PropertyAttribute>(ReadOnly | DontDelete),
320+
AccessorSignature::New(env->isolate(), t));
319321

320322
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"),
321323
t->GetFunction());
@@ -1146,9 +1148,7 @@ int SecureContext::TicketKeyCallback(SSL* ssl,
11461148

11471149
void SecureContext::CtxGetter(Local<String> property,
11481150
const PropertyCallbackInfo<Value>& info) {
1149-
HandleScope scope(info.GetIsolate());
1150-
1151-
SSL_CTX* ctx = Unwrap<SecureContext>(info.Holder())->ctx_;
1151+
SSL_CTX* ctx = Unwrap<SecureContext>(info.This())->ctx_;
11521152
Local<External> ext = External::New(info.GetIsolate(), ctx);
11531153
info.GetReturnValue().Set(ext);
11541154
}
@@ -1216,7 +1216,8 @@ void SSLWrap<Base>::AddMethods(Environment* env, Local<FunctionTemplate> t) {
12161216
nullptr,
12171217
env->as_external(),
12181218
DEFAULT,
1219-
static_cast<PropertyAttribute>(ReadOnly | DontDelete));
1219+
static_cast<PropertyAttribute>(ReadOnly | DontDelete),
1220+
AccessorSignature::New(env->isolate(), t));
12201221
}
12211222

12221223

@@ -2203,10 +2204,8 @@ void SSLWrap<Base>::CertCbDone(const FunctionCallbackInfo<Value>& args) {
22032204

22042205
template <class Base>
22052206
void SSLWrap<Base>::SSLGetter(Local<String> property,
2206-
const PropertyCallbackInfo<Value>& info) {
2207-
HandleScope scope(info.GetIsolate());
2208-
2209-
SSL* ssl = Unwrap<Base>(info.Holder())->ssl_;
2207+
const PropertyCallbackInfo<Value>& info) {
2208+
SSL* ssl = Unwrap<Base>(info.This())->ssl_;
22102209
Local<External> ext = External::New(info.GetIsolate(), ssl);
22112210
info.GetReturnValue().Set(ext);
22122211
}
@@ -4144,12 +4143,14 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
41444143
env->SetProtoMethod(t, "setPublicKey", SetPublicKey);
41454144
env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey);
41464145

4147-
t->InstanceTemplate()->SetAccessor(env->verify_error_string(),
4148-
DiffieHellman::VerifyErrorGetter,
4149-
nullptr,
4150-
env->as_external(),
4151-
DEFAULT,
4152-
attributes);
4146+
t->InstanceTemplate()->SetAccessor(
4147+
env->verify_error_string(),
4148+
DiffieHellman::VerifyErrorGetter,
4149+
nullptr,
4150+
env->as_external(),
4151+
DEFAULT,
4152+
attributes,
4153+
AccessorSignature::New(env->isolate(), t));
41534154

41544155
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellman"),
41554156
t->GetFunction());
@@ -4164,12 +4165,14 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
41644165
env->SetProtoMethod(t2, "getPublicKey", GetPublicKey);
41654166
env->SetProtoMethod(t2, "getPrivateKey", GetPrivateKey);
41664167

4167-
t2->InstanceTemplate()->SetAccessor(env->verify_error_string(),
4168-
DiffieHellman::VerifyErrorGetter,
4169-
nullptr,
4170-
env->as_external(),
4171-
DEFAULT,
4172-
attributes);
4168+
t2->InstanceTemplate()->SetAccessor(
4169+
env->verify_error_string(),
4170+
DiffieHellman::VerifyErrorGetter,
4171+
nullptr,
4172+
env->as_external(),
4173+
DEFAULT,
4174+
attributes,
4175+
AccessorSignature::New(env->isolate(), t2));
41734176

41744177
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellmanGroup"),
41754178
t2->GetFunction());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
6+
if (!common.hasCrypto) {
7+
console.log('1..0 # Skipped: missing crypto');
8+
return;
9+
}
10+
11+
// Ensure accessing ._external doesn't hit an assert in the accessor method.
12+
const tls = require('tls');
13+
{
14+
const pctx = tls.createSecureContext().context;
15+
const cctx = Object.create(pctx);
16+
assert.throws(() => cctx._external, /incompatible receiver/);
17+
pctx._external;
18+
}
19+
{
20+
const pctx = tls.createSecurePair().credentials.context;
21+
const cctx = Object.create(pctx);
22+
assert.throws(() => cctx._external, /incompatible receiver/);
23+
pctx._external;
24+
}

0 commit comments

Comments
 (0)