Commit f966e9f0156 for nodejs

commit f966e9f015622232fa27fb377f1df8a955206396
Author: Tim Perry <pimterry@gmail.com>
Date:   Wed Nov 25 14:47:00 2026 +0100

    crypto: don't allow null in generated cert subjects, matching SANs

    Signed-off-by: Tim Perry <pimterry@gmail.com>
    PR-URL: https://github.com/nodejs/node/pull/67781
    Reviewed-By: Filip Skokan <panva.ip@gmail.com>
    Reviewed-By: James M Snell <jasnell@gmail.com>

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<Array> spec) {
       if (!oid) return {};

       const std::string text = ReadString(env, content_value);
-      if (text.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
+      if (text.size() > static_cast<size_t>(std::numeric_limits<int>::max()) ||
+          text.find('\0') != std::string::npos) {
         return {};
       }
       const auto* bytes = reinterpret_cast<const unsigned char*>(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',