{"record":{"id":"54376863a20663f8","repo":"grafana/k6","slug":"modulus-n-is-required","errorCode":null,"errorMessage":"modulus (n) is required","messagePattern":"modulus \\(n\\) is required","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/webcrypto/jwk.go","lineNumber":314,"sourceCode":"type rsaJWK struct {\n\tKty string `json:\"kty\"`          // Key Type\n\tN   string `json:\"n\"`            // Modulus\n\tE   string `json:\"e\"`            // Exponent\n\tD   string `json:\"d,omitempty\"`  // Private exponent\n\tP   string `json:\"p,omitempty\"`  // First prime factor\n\tQ   string `json:\"q,omitempty\"`  // Second prime factor\n\tDp  string `json:\"dp,omitempty\"` // Exponent1\n\tDq  string `json:\"dq,omitempty\"` // Exponent2\n\tQi  string `json:\"qi,omitempty\"` // Coefficient\n}\n\nfunc (jwk *rsaJWK) validate() error {\n\tif jwk.Kty != \"RSA\" {\n\t\treturn fmt.Errorf(\"invalid key type: %s\", jwk.Kty)\n\t}\n\n\tif jwk.N == \"\" {\n\t\treturn errors.New(\"modulus (n) is required\")\n\t}\n\n\tif jwk.E == \"\" {\n\t\treturn errors.New(\"exponent (e) is required\")\n\t}\n\n\t// TODO: consider validating the other fields in future\n\treturn nil\n}\n\nfunc importRSAJWK(jsonKeyData []byte) (any, CryptoKeyType, int, error) {\n\tvar jwk rsaJWK\n\tif err := json.Unmarshal(jsonKeyData, &jwk); err != nil {\n\t\treturn nil, UnknownCryptoKeyType, 0, fmt.Errorf(\"failed to parse input as RSA JWK key: %w\", err)\n\t}\n\n\tif err := jwk.validate(); err != nil {\n\t\treturn nil, UnknownCryptoKeyType, 0, fmt.Errorf(\"invalid RSA JWK key: %w\", err)","sourceCodeStart":296,"sourceCodeEnd":332,"githubUrl":"https://github.com/grafana/k6/blob/8d06114777191d21ef885d05c5f8aa9e1ed838ae/internal/js/modules/k6/webcrypto/jwk.go#L296-L332","documentation":"Thrown when crypto.subtle.importKey receives a JWK with kty 'RSA' that is missing the 'n' (modulus) member. rsaJWK.validate() (internal/js/modules/k6/webcrypto/jwk.go:313) requires kty, n, and e before the key is parsed; the failure rejects the importKey promise. A private JWK still needs n and e — only primes and CRT fields are optional.","triggerScenarios":"Calling crypto.subtle.importKey('jwk', jwk, { name: 'RSA-OAEP', hash: 'SHA-256' }, true, ['encrypt']) with a JWK like { kty: 'RSA', e: 'AQAB', d: '...' } — private primes are present but the modulus is not.","commonSituations":"Copying only the private parts (d, p, q, dp, dq, qi) out of a PEM breakdown; a JWK truncated by a config limit or copy-paste; converting a PEM with a tool configured to omit public fields.","solutions":["Add the 'n' member: the modulus as an unpadded base64url string (256 bytes for RSA-2048, 384 for RSA-3072, 512 for RSA-4096)","Re-export the complete JWK with crypto.subtle.exportKey('jwk', key) or convert the full PEM with a tool like pem-jose/pem-jwk rather than hand-assembling","Validate that kty, n, and e exist and are non-empty before calling importKey"],"exampleFix":"// before\nconst jwk = { kty: 'RSA', e: 'AQAB', d: 'Kk9...' };\nconst key = await crypto.subtle.importKey('jwk', jwk, { name: 'RSA-OAEP', hash: 'SHA-256' }, true, ['decrypt']);\n\n// after\nconst jwk = { kty: 'RSA', n: 'vHkg...', e: 'AQAB', d: 'Kk9...' };\nconst key = await crypto.subtle.importKey('jwk', jwk, { name: 'RSA-OAEP', hash: 'SHA-256' }, true, ['decrypt']);","handlingStrategy":"validation","validationCode":"function assertRsaJwkComplete(jwk) {\n  const missing = ['n', 'e'].filter((f) => !jwk || typeof jwk[f] !== 'string' || jwk[f] === '');\n  if (jwk && jwk.kty !== 'RSA') throw new Error(`expected kty RSA, got ${jwk.kty}`);\n  if (missing.length > 0) throw new Error(`RSA JWK is missing required field(s): ${missing.join(', ')}`);\n}\n\nassertRsaJwkComplete(jwk);\nawait crypto.subtle.importKey('jwk', jwk, { name: 'RSA-OAEP', hash: 'SHA-256' }, true, ['encrypt']);","typeGuard":"function isCompleteRsaJwk(v) {\n  return !!v && v.kty === 'RSA'\n    && typeof v.n === 'string' && v.n !== ''\n    && typeof v.e === 'string' && v.e !== '';\n}","tryCatchPattern":"try {\n  const key = await crypto.subtle.importKey('jwk', jwk, alg, true, usages);\n} catch (err) {\n  if (/modulus \\(n\\) is required/.test(String(err))) {\n    throw new Error('RSA JWK missing modulus — re-export the full key with exportKey(\\'jwk\\')');\n  }\n  throw err;\n}","preventionTips":["Convert PEMs to JWK with a complete, tested tool instead of manual field extraction","Private RSA JWKs still require n and e — never copy only d/p/q","Validate required members before importKey so the error names your own context"],"tags":["webcrypto","jwk","rsa","importkey","key-validation"],"backgroundTag":"jwk-validation-failed","analyzedSha":"8d06114777191d21ef885d05c5f8aa9e1ed838ae","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}