Skip to content

Commit 6a8b5b2

Browse files
committed
tls: deprecate parseCertString & move to internal
`tls.parseCertString()` exposed by accident. Now move this function to `internal/tls` and mark the original one as deprecated. Refs: #14193 Refs: af80e7b#diff-cc32376ce1eaf679ec2298cd483f15c7R188
1 parent 4e8bc71 commit 6a8b5b2

File tree

6 files changed

+82
-27
lines changed

6 files changed

+82
-27
lines changed

doc/api/deprecations.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,28 @@ Type: Runtime
660660

661661
`REPLServer.parseREPLKeyword()` was removed from userland visibility.
662662

663+
<a id="DEP00XX"></a>
664+
### DEP00XX: tls.parseCertString()
665+
666+
Type: Runtime
667+
668+
`tls.parseCertString()` is a trivial parsing helper that was made public by
669+
mistake. This function can usually be replaced with
670+
671+
```js
672+
const querystring = require('querystring');
673+
querystring.parse(str, '\n', '=');
674+
```
675+
676+
*Note*: This function is not completely equivalent to `querystring.parse()`, one
677+
notable difference is that `querystring.parse()` does URLDecoding, e.g.:
678+
679+
```sh
680+
> querystring.parse("%E5%A5%BD=1", "\n", "=");
681+
{ '': '1' }
682+
> tls.parseCertString("%E5%A5%BD=1");
683+
{ '%E5%A5%BD': '1' }
684+
```
663685
664686
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
665687
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array

lib/_tls_common.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
'use strict';
2323

24+
const internalTLS = require('internal/tls');
2425
const tls = require('tls');
2526

2627
const SSL_OP_CIPHER_SERVER_PREFERENCE =
@@ -169,11 +170,11 @@ exports.translatePeerCertificate = function translatePeerCertificate(c) {
169170
if (!c)
170171
return null;
171172

172-
if (c.issuer != null) c.issuer = tls.parseCertString(c.issuer);
173+
if (c.issuer != null) c.issuer = internalTLS.parseCertString(c.issuer);
173174
if (c.issuerCertificate != null && c.issuerCertificate !== c) {
174175
c.issuerCertificate = translatePeerCertificate(c.issuerCertificate);
175176
}
176-
if (c.subject != null) c.subject = tls.parseCertString(c.subject);
177+
if (c.subject != null) c.subject = internalTLS.parseCertString(c.subject);
177178
if (c.infoAccess != null) {
178179
var info = c.infoAccess;
179180
c.infoAccess = {};

lib/internal/tls.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
// Example:
4+
// C=US\nST=CA\nL=SF\nO=Joyent\nOU=Node.js\nCN=ca1\[email protected]
5+
function parseCertString(s) {
6+
var out = {};
7+
var parts = s.split('\n');
8+
for (var i = 0, len = parts.length; i < len; i++) {
9+
var sepIndex = parts[i].indexOf('=');
10+
if (sepIndex > 0) {
11+
var key = parts[i].slice(0, sepIndex);
12+
var value = parts[i].slice(sepIndex + 1);
13+
if (key in out) {
14+
if (!Array.isArray(out[key])) {
15+
out[key] = [out[key]];
16+
}
17+
out[key].push(value);
18+
} else {
19+
out[key] = value;
20+
}
21+
}
22+
}
23+
return out;
24+
}
25+
26+
module.exports = {
27+
parseCertString
28+
};

lib/tls.js

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
const errors = require('internal/errors');
2525
const internalUtil = require('internal/util');
26+
const internalTLS = require('internal/tls');
2627
internalUtil.assertCrypto();
2728

2829
const net = require('net');
@@ -228,28 +229,11 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) {
228229
}
229230
};
230231

231-
// Example:
232-
// C=US\nST=CA\nL=SF\nO=Joyent\nOU=Node.js\nCN=ca1\[email protected]
233-
exports.parseCertString = function parseCertString(s) {
234-
var out = {};
235-
var parts = s.split('\n');
236-
for (var i = 0, len = parts.length; i < len; i++) {
237-
var sepIndex = parts[i].indexOf('=');
238-
if (sepIndex > 0) {
239-
var key = parts[i].slice(0, sepIndex);
240-
var value = parts[i].slice(sepIndex + 1);
241-
if (key in out) {
242-
if (!Array.isArray(out[key])) {
243-
out[key] = [out[key]];
244-
}
245-
out[key].push(value);
246-
} else {
247-
out[key] = value;
248-
}
249-
}
250-
}
251-
return out;
252-
};
232+
exports.parseCertString = internalUtil.deprecate(
233+
internalTLS.parseCertString,
234+
'tls.parseCertString() is deprecated. ' +
235+
'Please use querystring.parse() instead.',
236+
'DEP00XX');
253237

254238
// Public API
255239
exports.createSecureContext = require('_tls_common').createSecureContext;

node.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
'lib/internal/repl.js',
102102
'lib/internal/socket_list.js',
103103
'lib/internal/test/unicode.js',
104+
'lib/internal/tls.js',
104105
'lib/internal/url.js',
105106
'lib/internal/util.js',
106107
'lib/internal/v8_prof_polyfill.js',

test/parallel/test-tls-parse-cert-string.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
'use strict';
2+
3+
// Flags: --expose_internals
24
const common = require('../common');
35
if (!common.hasCrypto)
46
common.skip('missing crypto');
57

68
const assert = require('assert');
9+
const internalTLS = require('internal/tls');
710
const tls = require('tls');
811

12+
const noOutput = common.mustNotCall();
13+
common.hijackStderr(function() {
14+
process.nextTick(noOutput);
15+
});
16+
917
{
1018
const singles = 'C=US\nST=CA\nL=SF\nO=Node.js Foundation\nOU=Node.js\n' +
1119
12-
const singlesOut = tls.parseCertString(singles);
20+
const singlesOut = internalTLS.parseCertString(singles);
1321
assert.deepStrictEqual(singlesOut, {
1422
C: 'US',
1523
ST: 'CA',
@@ -24,7 +32,7 @@ const tls = require('tls');
2432
{
2533
const doubles = 'OU=Domain Control Validated\nOU=PositiveSSL Wildcard\n' +
2634
'CN=*.nodejs.org';
27-
const doublesOut = tls.parseCertString(doubles);
35+
const doublesOut = internalTLS.parseCertString(doubles);
2836
assert.deepStrictEqual(doublesOut, {
2937
OU: [ 'Domain Control Validated', 'PositiveSSL Wildcard' ],
3038
CN: '*.nodejs.org'
@@ -33,6 +41,17 @@ const tls = require('tls');
3341

3442
{
3543
const invalid = 'fhqwhgads';
36-
const invalidOut = tls.parseCertString(invalid);
44+
const invalidOut = internalTLS.parseCertString(invalid);
3745
assert.deepStrictEqual(invalidOut, {});
3846
}
47+
48+
common.restoreStderr();
49+
50+
{
51+
common.expectWarning('DeprecationWarning',
52+
'tls.parseCertString() is deprecated. ' +
53+
'Please use querystring.parse() instead.');
54+
55+
const ret = tls.parseCertString('foo=bar');
56+
assert.deepStrictEqual(ret, { foo: 'bar' });
57+
}

0 commit comments

Comments
 (0)