{"record":{"id":"5e0a2ac1b63c27b9","repo":"grafana/k6","slug":"typedarray-parameter-is-required","errorCode":null,"errorMessage":"typedArray parameter is required","messagePattern":"typedArray parameter is required","errorType":"panic","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/webcrypto/crypto.go","lineNumber":45,"sourceCode":"//\n// The array given as the parameter is filled with random numbers (random in\n// its cryptographic sense, not in its statistical sense).\n//\n// To guarantee enough performance, this implementation is not using a truly\n// random number generator, but is using a pseudo-random number generator\n// seeded with a value with enough entropy. We are using the golang\n// crypto/rand package, which uses the operating system's random number\n// generator.\n//\n// [specification]: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues\nfunc (c *Crypto) GetRandomValues(typedArray sobek.Value) sobek.Value {\n\t// The typedArray parameter is not optional in the specification's WebIDL\n\t// definition, so reject a missing or nullish one before IsInstanceOf gets\n\t// to dereference it. This is an ECMAScript TypeError rather than one of the\n\t// WebCrypto error names, which is what browsers and Node throw here, and\n\t// what a nullish argument already produced before this check existed.\n\tif common.IsNullish(typedArray) {\n\t\tpanic(c.vu.Runtime().NewTypeError(\"typedArray parameter is required\"))\n\t}\n\n\tacceptedTypes := []JSType{\n\t\tInt8ArrayConstructor,\n\t\tUint8ArrayConstructor,\n\t\tUint8ClampedArrayConstructor,\n\t\tInt16ArrayConstructor,\n\t\tUint16ArrayConstructor,\n\t\tInt32ArrayConstructor,\n\t\tUint32ArrayConstructor,\n\t}\n\n\t// 1.\n\tif !IsInstanceOf(c.vu.Runtime(), typedArray, acceptedTypes...) {\n\t\tcommon.Throw(c.vu.Runtime(), NewError(TypeMismatchError, \"typedArray parameter isn't a TypedArray instance\"))\n\t}\n\n\t// 2.","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/webcrypto/crypto.go#L27-L63","documentation":"crypto.getRandomValues() requires a TypedArray argument per the WebCrypto spec. k6 rejects a missing/nullish argument with an ECMAScript TypeError before dereferencing it, matching browser and Node behavior.","triggerScenarios":"Calling crypto.getRandomValues() with no arguments, with undefined, or with null.","commonSituations":"Forgetting the argument entirely; passing the result of a lookup that returned undefined (e.g. new Uint8Array(N) typo'd); porting code where the array creation was removed.","solutions":["Always pass a TypedArray: crypto.getRandomValues(new Uint8Array(16)).","Create the buffer first and verify it exists before the call.","If the argument comes from elsewhere, default it to a fresh Uint8Array when nullish."],"exampleFix":"// before\nconst bytes = crypto.getRandomValues();\n// after\nconst bytes = crypto.getRandomValues(new Uint8Array(16));","handlingStrategy":"validation","validationCode":"function secureRandom(n) {\n  if (!Number.isInteger(n) || n <= 0) throw new TypeError('need a positive length');\n  return crypto.getRandomValues(new Uint8Array(n));\n}","typeGuard":"function isTypedArray(v) {\n  return ArrayBuffer.isView(v) && !(v instanceof DataView);\n}","tryCatchPattern":"try {\n  crypto.getRandomValues(buf);\n} catch (e) {\n  buf = crypto.getRandomValues(new Uint8Array(16)); // fallback buffer\n}","preventionTips":["Always construct the TypedArray inline at the call site.","Never call getRandomValues() with a possibly-nullish variable without checking first.","Wrap crypto calls in a helper that guarantees a valid buffer."],"tags":["webcrypto","typedarray","javascript","k6"],"backgroundTag":"null-argument","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-09-20T22:01:45.163Z","contentChangedAt":"2026-09-20T22:01:45.163Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}