{"record":{"id":"8f38ef6dc1821ba6","repo":"grafana/k6","slug":"public-exponent-too-small","errorCode":null,"errorMessage":"public exponent too small","messagePattern":"public exponent too small","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/webcrypto/rsa.go","lineNumber":151,"sourceCode":"\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn &CryptoKeyPair{\n\t\tPrivateKey: privateKey,\n\t\tPublicKey:  publicKey,\n\t}, nil\n}\n\n// validatePublicExponent validates the public exponent.\n// it's done same way how golang's rsa package does it + additional check for evenness.\nfunc validatePublicExponent(e int) error {\n\tif e%2 == 0 {\n\t\treturn errors.New(\"public exponent is even\")\n\t}\n\n\tif e < 2 {\n\t\treturn errors.New(\"public exponent too small\")\n\t}\n\tif e > 1<<31-1 {\n\t\treturn errors.New(\"public exponent too large\")\n\t}\n\n\treturn nil\n}\n\nfunc generateRSAKeyPair(\n\tmodulusLength int,\n\tpublicExponent int,\n) (any, any, error) {\n\tprivateKey, err := rsa.GenerateKey(rand.Reader, modulusLength)\n\tif err != nil {\n\t\treturn nil, nil, NewError(OperationError, \"could not generate RSA key pair\")\n\t}\n\n\tprivateKey.E = publicExponent","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/webcrypto/rsa.go#L133-L169","documentation":"Thrown by crypto.subtle.generateKey for RSA algorithms when publicExponent is odd but smaller than 2. validatePublicExponent (internal/js/modules/k6/webcrypto/rsa.go:151) checks evenness first, then rejects e < 2 — an exponent of 1 (or a negative odd value) makes RSA signatures trivially forgeable and is refused before key generation.","triggerScenarios":"Passing publicExponent: new Uint8Array([0x01]) or new Uint8Array([0x00, 0x01]) in the generateKey algorithm identifier. Even values like 0 hit the 'public exponent is even' sibling error instead.","commonSituations":"A default exponent constant initialized to 1 ('set later, never set'); trimming the exponent array to its last non-zero byte; test scripts that shrink the exponent 'to be fast'.","solutions":["Use the standard exponent 65537: new Uint8Array([0x01, 0x00, 0x01])","Otherwise pick any odd value >= 3 (3 and 17 are the usual alternatives)","Validate configured exponents against the same rule (odd, 2 <= e <= 2^31-1) before generateKey"],"exampleFix":"// before\npublicExponent: new Uint8Array([0x01]) // 1 — too small\n\n// after\npublicExponent: new Uint8Array([0x01, 0x00, 0x01]) // 65537","handlingStrategy":"validation","validationCode":"const e = publicExponent.reduce((acc, b) => acc * 256 + b, 0);\nif (e % 2 !== 0 && e < 2) {\n  throw new Error(`publicExponent ${e} is too small — use [0x01, 0x00, 0x01] (65537)`);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never ship an exponent default of 1 or 0 — initialize to [0x01, 0x00, 0x01]","Validate decoded exponent bounds before generateKey","Even and too-small exponents have distinct messages — read which one fired to locate the bad byte"],"tags":["webcrypto","rsa","generatekey","public-exponent","key-generation"],"backgroundTag":"invalid-rsa-key-parameters","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}