grafana/k6 · error

invalid size

Error message

invalid size

What it means

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.

Solutions

  1. Pass size >= 1 — clamp or default the computed size before calling
  2. Guard the call: if (n > 0) buf = crypto.randomBytes(n) and handle the n==0 case explicitly
  3. Validate lengths at test/data-load time so zero sizes fail with a clearer upstream message
  4. If an empty result is legitimate for your logic, skip the call and use an empty buffer you construct yourself

Example fix

// before
const salt = crypto.randomBytes(params.saltLength); // 0 for some params

// after
const saltLength = Math.max(1, params.saltLength | 0);
const salt = crypto.randomBytes(saltLength);
Defensive patterns

Strategy: validation

Validate before calling

function randomBytesSafe(n) {
  const size = Number(n);
  if (!Number.isInteger(size) || size < 1) {
    throw new Error(`randomBytes requires an integer >= 1, got ${n}`);
  }
  return crypto.randomBytes(size);
}

Type guard

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

Try / catch

try {
  buf = crypto.randomBytes(len);
} catch (e) {
  if (/invalid size/.test(e.message)) {
    buf = crypto.randomBytes(16); // sensible default
  } else throw e;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


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

Appendix: 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 01ffac6f24)