From f966e9f015622232fa27fb377f1df8a955206396 Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Wed, 25 Nov 2026 14:47:00 +0100 Subject: [PATCH] crypto: don't allow null in generated cert subjects, matching SANs Signed-off-by: Tim Perry PR-URL: https://github.com/nodejs/node/pull/67781 Reviewed-By: Filip Skokan Reviewed-By: James M Snell --- doc/api/crypto.md | 3 ++- lib/internal/crypto/x509_generation.js | 7 ++++--- src/crypto/crypto_x509_gen.cc | 3 ++- test/parallel/test-crypto-x509-generate.js | 9 +++++++++ 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 152dddde2c1..c10313b7241 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -5346,7 +5346,8 @@ OID or one of `commonName`/`CN`, `countryName`/`C`, `localityName`/`L`, `stateOrProvinceName`/`ST`, `organizationName`/`O`, `organizationalUnitName`/`OU`, `serialNumber`, `streetAddress`, `postalCode`, `domainComponent`/`DC`, `userId`/`UID`, -`emailAddress`/`E`, `title`, `surname`/`SN`, or `givenName`/`GN`. +`emailAddress`/`E`, `title`, `surname`/`SN`, or `givenName`/`GN`. `value` must +not contain NUL characters. ```js const subject = [ diff --git a/lib/internal/crypto/x509_generation.js b/lib/internal/crypto/x509_generation.js index 5591981d68f..3399fb8bf21 100644 --- a/lib/internal/crypto/x509_generation.js +++ b/lib/internal/crypto/x509_generation.js @@ -58,6 +58,7 @@ const { validateInteger, validateObject, validateString, + validateStringWithoutNullBytes, } = require('internal/validators'); const { @@ -170,8 +171,8 @@ function snapshotCertificateRequest(value) { ]); } -function validateUnicode(value, name) { - validateString(value, name); +function validateUnicodeWithoutNul(value, name) { + validateStringWithoutNullBytes(value, name); if (!StringPrototypeIsWellFormed(value)) { throw new ERR_INVALID_ARG_VALUE( name, value, 'contains an unpaired surrogate'); @@ -206,7 +207,7 @@ function normalizeNameAttribute(attribute, name) { if (ObjectPrototypeHasOwnProperty(kNameAttributes, attributeName)) { oid = kNameAttributes[attributeName]; } - validateUnicode(value, `${name}.value`); + validateUnicodeWithoutNul(value, `${name}.value`); return [oid, value]; } diff --git a/src/crypto/crypto_x509_gen.cc b/src/crypto/crypto_x509_gen.cc index 0b206a2b5de..ca13382819e 100644 --- a/src/crypto/crypto_x509_gen.cc +++ b/src/crypto/crypto_x509_gen.cc @@ -298,7 +298,8 @@ X509NamePointer BuildName(Environment* env, Local spec) { if (!oid) return {}; const std::string text = ReadString(env, content_value); - if (text.size() > static_cast(std::numeric_limits::max())) { + if (text.size() > static_cast(std::numeric_limits::max()) || + text.find('\0') != std::string::npos) { return {}; } const auto* bytes = reinterpret_cast(text.data()); diff --git a/test/parallel/test-crypto-x509-generate.js b/test/parallel/test-crypto-x509-generate.js index 7c7193c4749..15218776b3c 100644 --- a/test/parallel/test-crypto-x509-generate.js +++ b/test/parallel/test-crypto-x509-generate.js @@ -615,6 +615,15 @@ const invalidOptions = [ names: [{ type: 'dns', value: 'nul\0.example' }], }], })], + ['ERR_INVALID_ARG_VALUE', baseOptions(leafKeys, { + subject: [{ name: 'CN', value: 'example.com\0.example' }], + })], + ['ERR_INVALID_ARG_VALUE', baseOptions(leafKeys, { + subject: [ + { name: 'CN', value: 'example.com' }, + [{ name: 'O', value: 'Node.js' }, { name: '2.5.4.11', value: '\0' }], + ], + })], ['ERR_INVALID_ARG_VALUE', baseOptions(leafKeys, { extensions: [{ type: 'subjectAltName',