{"record":{"id":"149a7f44987e5bd3","repo":"grafana/k6","slug":"public-exponent-too-large","errorCode":null,"errorMessage":"public exponent too large","messagePattern":"public exponent too large","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/webcrypto/rsa.go","lineNumber":154,"sourceCode":"\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\n\n\t// validate the key pair, since we are setting the public exponent manually\n\tif err := privateKey.Validate(); err != nil {","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/webcrypto/rsa.go#L136-L172","documentation":"Thrown by crypto.subtle.generateKey for RSA algorithms when publicExponent exceeds 2^31-1. validatePublicExponent (internal/js/modules/k6/webcrypto/rsa.go:154) caps the exponent at the maximum signed 32-bit int, matching Go's rsa package limits; larger values are rejected before key generation.","triggerScenarios":"Passing publicExponent as a big-endian byte array longer than 4 bytes with the high bits set, e.g. new Uint8Array([0x7F, 0xFF, 0xFF, 0xFF, 0xFF]) or new Uint8Array(8) filled with 0xFF, in the generateKey algorithm identifier.","commonSituations":"Concatenating the exponent onto the wrong end of the modulus buffer; a config value stored as a 64-bit integer; porting a test vector that encodes the exponent in a fixed-width 8-byte field without trimming leading zero bytes — note [0x00, 0x01, 0x00, 0x01] trims fine but [0xFF x8] does not.","solutions":["Use the standard exponent 65537: new Uint8Array([0x01, 0x00, 0x01])","Trim leading zero bytes from fixed-width exponent buffers before passing them","Validate the decoded exponent value fits 2 <= e <= 2^31-1 before generateKey"],"exampleFix":"// before\npublicExponent: new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00]) // decoded value overflows int32\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 ** 31 - 1) {\n  throw new Error(`publicExponent ${e} exceeds the 2^31-1 limit — check for untrimmed or misaligned bytes`);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Trim leading zero bytes from fixed-width exponent fields before passing them","Keep the exponent in its own variable — never concatenate it with modulus buffers","Assert the decoded value fits 2 <= e <= 2^31-1 before generateKey"],"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"}