Skip to content

Commit 6e689ec

Browse files
chrisdickinsontjfontaine
authored andcommitted
crypto: use domains for any callback-taking method
This adds domains coverage for pdbkdf2, pseudoRandomBytes, and randomBytes. All others should be covered by event emitters. Fixes #5801. Reviewed-By: Timothy J Fontaine <tjfontaine@gmail.com>
1 parent 0664ddc commit 6e689ec

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

src/node_crypto.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3984,6 +3984,8 @@ Handle<Value> PBKDF2(const Arguments& args) {
39843984
if (args[4]->IsFunction()) {
39853985
req->obj = Persistent<Object>::New(Object::New());
39863986
req->obj->Set(String::New("ondone"), args[4]);
3987+
SetActiveDomain(req->obj);
3988+
39873989
uv_queue_work(uv_default_loop(),
39883990
&req->work_req,
39893991
EIO_PBKDF2,
@@ -4111,6 +4113,7 @@ Handle<Value> RandomBytes(const Arguments& args) {
41114113
if (args[1]->IsFunction()) {
41124114
req->obj_ = Persistent<Object>::New(Object::New());
41134115
req->obj_->Set(String::New("ondone"), args[1]);
4116+
SetActiveDomain(req->obj_);
41144117

41154118
uv_queue_work(uv_default_loop(),
41164119
&req->work_req_,

src/util.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@
2626
#include "string_bytes.h"
2727

2828
namespace node {
29+
// defined in node.cc
30+
extern v8::Persistent<v8::String> process_symbol;
31+
extern v8::Persistent<v8::String> domain_symbol;
32+
33+
inline void SetActiveDomain(v8::Persistent<v8::Object> obj) {
34+
assert(!process_symbol.IsEmpty());
35+
assert(!domain_symbol.IsEmpty());
36+
v8::Local<v8::Value> domain = v8::Context::GetCurrent()
37+
->Global()
38+
->Get(process_symbol)
39+
->ToObject()
40+
->Get(domain_symbol);
41+
obj->Set(domain_symbol, domain);
42+
}
2943

3044
class Utf8Value {
3145
public:

test/simple/test-crypto-domains.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var crypto = require('crypto');
23+
var domain = require('domain');
24+
var assert = require('assert');
25+
var d = domain.create();
26+
var expect = ['pbkdf2', 'randomBytes', 'pseudoRandomBytes']
27+
28+
d.on('error', function (e) {
29+
assert.equal(e.message, expect.shift());
30+
});
31+
32+
d.run(function () {
33+
crypto.pbkdf2('a', 'b', 1, 8, function () {
34+
throw new Error('pbkdf2');
35+
});
36+
37+
crypto.randomBytes(4, function () {
38+
throw new Error('randomBytes');
39+
});
40+
41+
crypto.pseudoRandomBytes(4, function () {
42+
throw new Error('pseudoRandomBytes');
43+
});
44+
});

0 commit comments

Comments
 (0)