{"record":{"id":"eb96f3cda234835e","repo":"grafana/k6","slug":"invalid-size","errorCode":null,"errorMessage":"invalid size","messagePattern":"invalid size","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/crypto/crypto.go","lineNumber":79,"sourceCode":"\t\t\t\"md4\":         c.md4,\n\t\t\t\"md5\":         c.md5,\n\t\t\t\"randomBytes\": c.randomBytes,\n\t\t\t\"ripemd160\":   c.ripemd160,\n\t\t\t\"sha1\":        c.sha1,\n\t\t\t\"sha256\":      c.sha256,\n\t\t\t\"sha384\":      c.sha384,\n\t\t\t\"sha512\":      c.sha512,\n\t\t\t\"sha512_224\":  c.sha512_224,\n\t\t\t\"sha512_256\":  c.sha512_256,\n\t\t\t\"hexEncode\":   c.hexEncode,\n\t\t},\n\t}\n}\n\n// randomBytes returns random data of the given size.\nfunc (c *Crypto) randomBytes(size int) (*sobek.ArrayBuffer, error) {\n\tif size < 1 {\n\t\treturn nil, errors.New(\"invalid size\")\n\t}\n\tbytes := make([]byte, size)\n\t_, err := c.randReader(bytes)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tab := c.vu.Runtime().NewArrayBuffer(bytes)\n\treturn &ab, nil\n}\n\n// md4 returns the MD4 hash of input in the given encoding.\nfunc (c *Crypto) md4(input any, outputEncoding string) (any, error) {\n\treturn c.buildInputsDigest(\"md4\", input, outputEncoding)\n}\n\n// md5 returns the MD5 hash of input in the given encoding.\nfunc (c *Crypto) md5(input any, outputEncoding string) (any, error) {\n\treturn c.buildInputsDigest(\"md5\", input, outputEncoding)","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/crypto/crypto.go#L61-L97","documentation":"crypto.randomBytes (crypto.go:77-82, exported to scripts via the k6/crypto module exports at crypto.go:63) returns `size` random bytes as an ArrayBuffer. Sizes below 1 are rejected with 'invalid size' because a zero or negative buffer can never be useful — unlike Node's crypto.randomBytes, k6 does not return an empty buffer for 0.","triggerScenarios":"crypto.randomBytes(0), crypto.randomBytes(-5), or a size computed from input that can legitimately be zero (e.g. a length parameter from a data file, a modulo/ subtraction landing on 0).","commonSituations":"Lengths derived from empty datasets or empty string lengths; off-by-one loop bounds; Node.js code ported to k6 where randomBytes(0) silently returned an empty buffer.","solutions":["Pass size >= 1 — clamp or default the computed size before calling","Guard the call: if (n > 0) buf = crypto.randomBytes(n) and handle the n==0 case explicitly","Validate lengths at test/data-load time so zero sizes fail with a clearer upstream message","If an empty result is legitimate for your logic, skip the call and use an empty buffer you construct yourself"],"exampleFix":"// before\nconst salt = crypto.randomBytes(params.saltLength); // 0 for some params\n\n// after\nconst saltLength = Math.max(1, params.saltLength | 0);\nconst salt = crypto.randomBytes(saltLength);","handlingStrategy":"validation","validationCode":"function randomBytesSafe(n) {\n  const size = Number(n);\n  if (!Number.isInteger(size) || size < 1) {\n    throw new Error(`randomBytes requires an integer >= 1, got ${n}`);\n  }\n  return crypto.randomBytes(size);\n}","typeGuard":"const isValidSize = n => Number.isInteger(n) && n >= 1;","tryCatchPattern":"try {\n  buf = crypto.randomBytes(len);\n} catch (e) {\n  if (/invalid size/.test(e.message)) {\n    buf = crypto.randomBytes(16); // sensible default\n  } else throw e;\n}","preventionTips":["Clamp computed sizes: Math.max(1, n)|0 before calling","Remember k6 is stricter than Node: randomBytes(0) throws here","Validate length inputs from data files at load time"],"tags":["crypto","randombytes","validation","argument"],"backgroundTag":"randombytes-invalid-size","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"}