Skip to content

Commit

Permalink
tls: move parseCertString to internal
Browse files Browse the repository at this point in the history
`tls.parseCertString()` exposed by accident. Now move this function to
`internal/tls` and mark the original one as deprecated.

Refs: nodejs#14193
Refs: nodejs@af80e7b#diff-cc32376ce1eaf679ec2298cd483f15c7R188
  • Loading branch information
XadillaX committed Jul 13, 2017
1 parent 5ffb5b6 commit 1a031f2
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 28 deletions.
7 changes: 7 additions & 0 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,13 @@ Type: Runtime

*Note*: change was made while `async_hooks` was an experimental API.

<a id="DEP0073"></a>
### DEP0073: tls.parseCertString()

Type: Runtime

`tls.parseCertString()` was move to `internal/tls.js`.

[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
[`Buffer.from(buffer)`]: buffer.html#buffer_class_method_buffer_from_buffer
Expand Down
2 changes: 1 addition & 1 deletion lib/_tls_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

'use strict';

const tls = require('tls');
const tls = require('internal/tls');

const SSL_OP_CIPHER_SERVER_PREFERENCE =
process.binding('constants').crypto.SSL_OP_CIPHER_SERVER_PREFERENCE;
Expand Down
33 changes: 33 additions & 0 deletions lib/internal/tls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const DEFAULT_CIPHERS = process.binding('constants').crypto.defaultCipherList;
const DEFAULT_ECDH_CURVE = 'prime256v1';

// Example:
// C=US\nST=CA\nL=SF\nO=Joyent\nOU=Node.js\nCN=ca1\nemailAddress=ry@clouds.org
function parseCertString(s) {
var out = {};
var parts = s.split('\n');
for (var i = 0, len = parts.length; i < len; i++) {
var sepIndex = parts[i].indexOf('=');
if (sepIndex > 0) {
var key = parts[i].slice(0, sepIndex);
var value = parts[i].slice(sepIndex + 1);
if (key in out) {
if (!Array.isArray(out[key])) {
out[key] = [out[key]];
}
out[key].push(value);
} else {
out[key] = value;
}
}
}
return out;
}

module.exports = {
parseCertString,
DEFAULT_CIPHERS,
DEFAULT_ECDH_CURVE
};
37 changes: 11 additions & 26 deletions lib/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
const internalUtil = require('internal/util');
internalUtil.assertCrypto();

const internalTLS = require('internal/tls');
const net = require('net');
const url = require('url');
const binding = process.binding('crypto');
Expand All @@ -39,10 +40,12 @@ exports.CLIENT_RENEG_WINDOW = 600;

exports.SLAB_BUFFER_SIZE = 10 * 1024 * 1024;

exports.DEFAULT_CIPHERS =
process.binding('constants').crypto.defaultCipherList;

exports.DEFAULT_ECDH_CURVE = 'prime256v1';
[ 'DEFAULT_CIPHERS', 'DEFAULT_ECDH_CURVE' ].forEach((key) => {
Object.defineProperty(exports, key, {
get: () => { return internalTLS[key]; },
set: (c) => { internalTLS[key] = c; }
});
});

exports.getCiphers = internalUtil.cachedResult(
() => internalUtil.filterDuplicateStrings(binding.getSSLCiphers(), true)
Expand Down Expand Up @@ -228,28 +231,10 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) {
}
};

// Example:
// C=US\nST=CA\nL=SF\nO=Joyent\nOU=Node.js\nCN=ca1\nemailAddress=ry@clouds.org
exports.parseCertString = function parseCertString(s) {
var out = {};
var parts = s.split('\n');
for (var i = 0, len = parts.length; i < len; i++) {
var sepIndex = parts[i].indexOf('=');
if (sepIndex > 0) {
var key = parts[i].slice(0, sepIndex);
var value = parts[i].slice(sepIndex + 1);
if (key in out) {
if (!Array.isArray(out[key])) {
out[key] = [out[key]];
}
out[key].push(value);
} else {
out[key] = value;
}
}
}
return out;
};
exports.parseCertString = internalUtil.deprecate(
internalTLS.parseCertString,
'tls.parseCertString is deprecated',
'DEP0073');

// Public API
exports.createSecureContext = require('_tls_common').createSecureContext;
Expand Down
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
'lib/internal/repl.js',
'lib/internal/socket_list.js',
'lib/internal/test/unicode.js',
'lib/internal/tls.js',
'lib/internal/url.js',
'lib/internal/util.js',
'lib/internal/v8_prof_polyfill.js',
Expand Down
18 changes: 17 additions & 1 deletion test/parallel/test-tls-parse-cert-string.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';

// Flags: --expose_internals
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const tls = require('tls');
const tls = require('internal/tls');

{
const singles = 'C=US\nST=CA\nL=SF\nO=Node.js Foundation\nOU=Node.js\n' +
Expand Down Expand Up @@ -36,3 +38,17 @@ const tls = require('tls');
const invalidOut = tls.parseCertString(invalid);
assert.deepStrictEqual(invalidOut, {});
}

{
const regexp = new RegExp('^\\(node:\\d+\\) [DEP0073] DeprecationWarning: ' +
'tls\\.parseCertString is deprecated$');

// test for deprecate message
common.hijackStderr(common.mustCall(function(data) {
assert.ok(regexp.test(data));
common.restoreStderr();
}));

const ret = require('tls').parseCertString('foo=bar');
assert.deepStrictEqual(ret, { foo: 'bar' });
}

0 comments on commit 1a031f2

Please sign in to comment.