Skip to content

Commit 19873ab

Browse files
tniessenkumarak
authored andcommitted
crypto,tls: implement safe x509 GeneralName format
This change introduces JSON-compatible escaping rules for strings that include X.509 GeneralName components (see RFC 5280). This non-standard format avoids ambiguities and prevents injection attacks that could previously lead to X.509 certificates being accepted even though they were not valid for the target hostname. These changes affect the format of subject alternative names and the format of authority information access. The checkServerIdentity function has been modified to safely handle the new format, eliminating the possibility of injecting subject alternative names into the verification logic. Because each subject alternative name is only encoded as a JSON string literal if necessary for security purposes, this change will only be visible in rare cases. This addresses CVE-2021-44532. Co-authored-by: Akshay K <iit.akshay@gmail.com> CVE-ID: CVE-2021-44532 Backport-PR-URL: nodejs-private/node-private#306 PR-URL: nodejs-private/node-private#300 Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent d4e5d1b commit 19873ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2429
-42
lines changed

doc/api/errors.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,14 @@ An unspecified or non-specific system error has occurred within the Node.js
18691869
process. The error object will have an `err.info` object property with
18701870
additional details.
18711871

1872+
<a id="ERR_TLS_CERT_ALTNAME_FORMAT"></a>
1873+
### `ERR_TLS_CERT_ALTNAME_FORMAT`
1874+
1875+
This error is thrown by `checkServerIdentity` if a user-supplied
1876+
`subjectaltname` property violates encoding rules. Certificate objects produced
1877+
by Node.js itself always comply with encoding rules and will never cause
1878+
this error.
1879+
18721880
<a id="ERR_TLS_CERT_ALTNAME_INVALID"></a>
18731881
### `ERR_TLS_CERT_ALTNAME_INVALID`
18741882

lib/_tls_common.js

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

2424
const {
2525
ArrayIsArray,
26+
JSONParse,
2627
ObjectCreate,
2728
} = primordials;
2829

@@ -323,6 +324,14 @@ exports.translatePeerCertificate = function translatePeerCertificate(c) {
323324

324325
// XXX: More key validation?
325326
info.replace(/([^\n:]*):([^\n]*)(?:\n|$)/g, (all, key, val) => {
327+
if (val.charCodeAt(0) === 0x22) {
328+
// The translatePeerCertificate function is only
329+
// used on internally created legacy certificate
330+
// objects, and any value that contains a quote
331+
// will always be a valid JSON string literal,
332+
// so this should never throw.
333+
val = JSONParse(val);
334+
}
326335
if (key in c.infoAccess)
327336
c.infoAccess[key].push(val);
328337
else

lib/internal/errors.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,8 @@ E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode', Error);
13451345
E('ERR_STREAM_WRITE_AFTER_END', 'write after end', Error);
13461346
E('ERR_SYNTHETIC', 'JavaScript Callstack', Error);
13471347
E('ERR_SYSTEM_ERROR', 'A system error occurred', SystemError);
1348+
E('ERR_TLS_CERT_ALTNAME_FORMAT', 'Invalid subject alternative name string',
1349+
SyntaxError);
13481350
E('ERR_TLS_CERT_ALTNAME_INVALID', function(reason, host, cert) {
13491351
this.reason = reason;
13501352
this.host = host;

lib/tls.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@
2424
const {
2525
Array,
2626
ArrayIsArray,
27+
ArrayPrototypePush,
28+
JSONParse,
2729
ObjectDefineProperty,
2830
ObjectFreeze,
31+
RegExpPrototypeExec,
32+
StringPrototypeIncludes,
33+
StringPrototypeIndexOf,
34+
StringPrototypeSplit,
35+
StringPrototypeSubstring,
2936
} = primordials;
3037

3138
const {
39+
ERR_TLS_CERT_ALTNAME_FORMAT,
3240
ERR_TLS_CERT_ALTNAME_INVALID,
3341
ERR_OUT_OF_RANGE
3442
} = require('internal/errors').codes;
@@ -207,6 +215,45 @@ function check(hostParts, pattern, wildcards) {
207215
return true;
208216
}
209217

218+
// This pattern is used to determine the length of escaped sequences within
219+
// the subject alt names string. It allows any valid JSON string literal.
220+
// This MUST match the JSON specification (ECMA-404 / RFC8259) exactly.
221+
const jsonStringPattern =
222+
// eslint-disable-next-line no-control-regex
223+
/^"(?:[^"\\\u0000-\u001f]|\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4}))*"/;
224+
225+
function splitEscapedAltNames(altNames) {
226+
const result = [];
227+
let currentToken = '';
228+
let offset = 0;
229+
while (offset !== altNames.length) {
230+
const nextSep = StringPrototypeIndexOf(altNames, ', ', offset);
231+
const nextQuote = StringPrototypeIndexOf(altNames, '"', offset);
232+
if (nextQuote !== -1 && (nextSep === -1 || nextQuote < nextSep)) {
233+
// There is a quote character and there is no separator before the quote.
234+
currentToken += StringPrototypeSubstring(altNames, offset, nextQuote);
235+
const match = RegExpPrototypeExec(
236+
jsonStringPattern, StringPrototypeSubstring(altNames, nextQuote));
237+
if (!match) {
238+
throw new ERR_TLS_CERT_ALTNAME_FORMAT();
239+
}
240+
currentToken += JSONParse(match[0]);
241+
offset = nextQuote + match[0].length;
242+
} else if (nextSep !== -1) {
243+
// There is a separator and no quote before it.
244+
currentToken += StringPrototypeSubstring(altNames, offset, nextSep);
245+
ArrayPrototypePush(result, currentToken);
246+
currentToken = '';
247+
offset = nextSep + 2;
248+
} else {
249+
currentToken += StringPrototypeSubstring(altNames, offset);
250+
offset = altNames.length;
251+
}
252+
}
253+
ArrayPrototypePush(result, currentToken);
254+
return result;
255+
}
256+
210257
let urlWarningEmitted = false;
211258
exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
212259
const subject = cert.subject;
@@ -218,7 +265,10 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
218265
hostname = '' + hostname;
219266

220267
if (altNames) {
221-
for (const name of altNames.split(', ')) {
268+
const splitAltNames = StringPrototypeIncludes(altNames, '"') ?
269+
splitEscapedAltNames(altNames) :
270+
StringPrototypeSplit(altNames, ', ');
271+
for (const name of splitAltNames) {
222272
if (name.startsWith('DNS:')) {
223273
dnsNames.push(name.slice(4));
224274
} else if (name.startsWith('URI:')) {

0 commit comments

Comments
 (0)