Skip to content

Commit bc52d49

Browse files
committed
Add a test for the bug of unwanted openSSL errors
* bad_sign() would inject an error into OpenSSL's error stack * Second time of calling requestThunk would make the error be thrown
1 parent 539acb5 commit bc52d49

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

test/fixtures/bad_rsa_privkey.pem

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIIBSwIBADCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2EbdS
3+
PO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdrmVCl
4+
pJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXzrith
5+
1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+GghdabPd7L
6+
vKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6EwoFhO3
7+
zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhRkImo
8+
g9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoEFgIUV6pvYD6fM1L4KxjlIUIeN+yvq/U=
9+
-----END RSA PRIVATE KEY-----

test/openssl.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**!
2+
* urllib - openssl.test.js
3+
*
4+
* Copyright(c) node-modules and other contributors.
5+
* MIT Licensed
6+
*
7+
* Authors:
8+
* P.S.V.R <pmq2001@gmail.com> (http://www.ofpsvr.com)
9+
*/
10+
11+
'use strict';
12+
13+
/**
14+
* This is a test for the bug of unwanted openSSL errors.
15+
*/
16+
17+
var should = require('should');
18+
var urllib = require('../');
19+
var crypto = require('crypto');
20+
var fs = require('fs');
21+
var path = require('path');
22+
var Agent = require('agentkeepalive');
23+
var HttpsAgent = require('agentkeepalive').HttpsAgent;
24+
25+
var bad_sign = function () {
26+
// This triggers an implicit error
27+
// that is recorded onto OpenSSL's error stack
28+
var privateKey = fs.readFileSync(path.join(__dirname, './fixtures/bad_rsa_privkey.pem'), 'utf8');
29+
var s = crypto.createSign('sha1');
30+
var sign = s.sign(privateKey);
31+
};
32+
33+
describe('httpclient.test.js', function () {
34+
it('should requestThunk()', function (done) {
35+
var conf = {
36+
"keepAlive": true,
37+
"keepAliveTimeout": 300000,
38+
"timeout": 300000,
39+
"maxSockets": null,
40+
"maxFreeSockets": 10,
41+
"enableStatusLog": false
42+
};
43+
44+
var httpAgent = new Agent(conf);
45+
var httpsAgent = new HttpsAgent(conf);
46+
47+
var client = urllib.create({
48+
agent: httpAgent,
49+
httpsAgent: httpsAgent
50+
});
51+
52+
bad_sign();
53+
// 1st time when we establish a https connection,
54+
// ~ClearErrorOnReturn is called so the errors of bad_sign() won't be thrown
55+
client.requestThunk('https://www.alipay.com')(function (err, result) {
56+
should.not.exist(err);
57+
result.data.should.be.a.Buffer;
58+
result.status.should.equal(200);
59+
60+
setImmediate(function () {
61+
// 2nd time when we reuse an existing connection,
62+
// ~ClearErrorOnReturn will not be called,
63+
// making the errors of bad_sign() be thrown
64+
// A fix of this bug should prevent this from happening
65+
bad_sign();
66+
client.requestThunk('https://www.alipay.com')(function (err, result) {
67+
should.not.exist(err);
68+
result.data.should.be.a.Buffer;
69+
result.status.should.equal(200);
70+
done();
71+
});
72+
});
73+
});
74+
});
75+
});

0 commit comments

Comments
 (0)