grafana/k6 · error

invalid size

Error message

invalid size

What it means

crypto.randomBytes(size) returns `size` cryptographically random bytes as an ArrayBuffer. Any size below 1 — zero or negative — is rejected with 'invalid size' (crypto.go:76-83) because an empty or negative-length buffer is meaningless.

Source

Thrown at internal/js/modules/k6/crypto/crypto.go:79

			"md4":         c.md4,
			"md5":         c.md5,
			"randomBytes": c.randomBytes,
			"ripemd160":   c.ripemd160,
			"sha1":        c.sha1,
			"sha256":      c.sha256,
			"sha384":      c.sha384,
			"sha512":      c.sha512,
			"sha512_224":  c.sha512_224,
			"sha512_256":  c.sha512_256,
			"hexEncode":   c.hexEncode,
		},
	}
}

// randomBytes returns random data of the given size.
func (c *Crypto) randomBytes(size int) (*sobek.ArrayBuffer, error) {
	if size < 1 {
		return nil, errors.New("invalid size")
	}
	bytes := make([]byte, size)
	_, err := c.randReader(bytes)
	if err != nil {
		return nil, err
	}
	ab := c.vu.Runtime().NewArrayBuffer(bytes)
	return &ab, nil
}

// md4 returns the MD4 hash of input in the given encoding.
func (c *Crypto) md4(input any, outputEncoding string) (any, error) {
	return c.buildInputsDigest("md4", input, outputEncoding)
}

// md5 returns the MD5 hash of input in the given encoding.
func (c *Crypto) md5(input any, outputEncoding string) (any, error) {
	return c.buildInputsDigest("md5", input, outputEncoding)

View on GitHub (pinned to 93accf6570)

Solutions

  1. Pass a positive integer size, e.g. crypto.randomBytes(16)
  2. Clamp and validate before the call: const n = Math.max(1, parseInt(size))
  3. Check the data source feeding the size (env var, config file) for empty or missing values

Example fix

// before
const bytes = crypto.randomBytes(input.length - 1); // -1 when input is empty

// after
const n = Math.max(1, input.length - 1);
const bytes = crypto.randomBytes(n);
Defensive patterns

Strategy: validation

Validate before calling

const size = Math.max(1, Math.floor(Number(rawSize)));
if (!Number.isFinite(size)) throw new Error('randomBytes size must be a positive integer');
const buf = crypto.randomBytes(size);

Type guard

const isValidByteSize = (n) => Number.isInteger(n) && n >= 1;

Prevention

When it happens

Trigger: crypto.randomBytes(0) or crypto.randomBytes(-3); a size computed from data or configuration that evaluates to 0 or negative (empty array length, misparsed env var, off-by-one math).

Common situations: Parameterized token/salt lengths driven by env vars that default to 0; generating nonces sized from dataset counts; arithmetic like length - 1 on an empty input.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/eb96f3cda234835e. Report an issue: GitHub.