Skip to content

Commit 25c01cd

Browse files
bnoordhuisFishrock123
authored andcommitted
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 <fedor@indutny.com>
1 parent 12ae6ab commit 25c01cd

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;
@@ -324,7 +325,8 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
324325
nullptr,
325326
env->as_external(),
326327
DEFAULT,
327-
static_cast<PropertyAttribute>(ReadOnly | DontDelete));
328+
static_cast<PropertyAttribute>(ReadOnly | DontDelete),
329+
AccessorSignature::New(env->isolate(), t));
328330

329331
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"),
330332
t->GetFunction());
@@ -1138,9 +1140,7 @@ int SecureContext::TicketKeyCallback(SSL* ssl,
11381140

11391141
void SecureContext::CtxGetter(Local<String> property,
11401142
const PropertyCallbackInfo<Value>& info) {
1141-
HandleScope scope(info.GetIsolate());
1142-
1143-
SSL_CTX* ctx = Unwrap<SecureContext>(info.Holder())->ctx_;
1143+
SSL_CTX* ctx = Unwrap<SecureContext>(info.This())->ctx_;
11441144
Local<External> ext = External::New(info.GetIsolate(), ctx);
11451145
info.GetReturnValue().Set(ext);
11461146
}
@@ -1213,7 +1213,8 @@ void SSLWrap<Base>::AddMethods(Environment* env, Local<FunctionTemplate> t) {
12131213
nullptr,
12141214
env->as_external(),
12151215
DEFAULT,
1216-
static_cast<PropertyAttribute>(ReadOnly | DontDelete));
1216+
static_cast<PropertyAttribute>(ReadOnly | DontDelete),
1217+
AccessorSignature::New(env->isolate(), t));
12171218
}
12181219

12191220

@@ -2357,10 +2358,8 @@ void SSLWrap<Base>::CertCbDone(const FunctionCallbackInfo<Value>& args) {
23572358

23582359
template <class Base>
23592360
void SSLWrap<Base>::SSLGetter(Local<String> property,
2360-
const PropertyCallbackInfo<Value>& info) {
2361-
HandleScope scope(info.GetIsolate());
2362-
2363-
SSL* ssl = Unwrap<Base>(info.Holder())->ssl_;
2361+
const PropertyCallbackInfo<Value>& info) {
2362+
SSL* ssl = Unwrap<Base>(info.This())->ssl_;
23642363
Local<External> ext = External::New(info.GetIsolate(), ssl);
23652364
info.GetReturnValue().Set(ext);
23662365
}
@@ -4298,12 +4297,14 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
42984297
env->SetProtoMethod(t, "setPublicKey", SetPublicKey);
42994298
env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey);
43004299

4301-
t->InstanceTemplate()->SetAccessor(env->verify_error_string(),
4302-
DiffieHellman::VerifyErrorGetter,
4303-
nullptr,
4304-
env->as_external(),
4305-
DEFAULT,
4306-
attributes);
4300+
t->InstanceTemplate()->SetAccessor(
4301+
env->verify_error_string(),
4302+
DiffieHellman::VerifyErrorGetter,
4303+
nullptr,
4304+
env->as_external(),
4305+
DEFAULT,
4306+
attributes,
4307+
AccessorSignature::New(env->isolate(), t));
43074308

43084309
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellman"),
43094310
t->GetFunction());
@@ -4318,12 +4319,14 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
43184319
env->SetProtoMethod(t2, "getPublicKey", GetPublicKey);
43194320
env->SetProtoMethod(t2, "getPrivateKey", GetPrivateKey);
43204321

4321-
t2->InstanceTemplate()->SetAccessor(env->verify_error_string(),
4322-
DiffieHellman::VerifyErrorGetter,
4323-
nullptr,
4324-
env->as_external(),
4325-
DEFAULT,
4326-
attributes);
4322+
t2->InstanceTemplate()->SetAccessor(
4323+
env->verify_error_string(),
4324+
DiffieHellman::VerifyErrorGetter,
4325+
nullptr,
4326+
env->as_external(),
4327+
DEFAULT,
4328+
attributes,
4329+
AccessorSignature::New(env->isolate(), t2));
43274330

43284331
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellmanGroup"),
43294332
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)