grafana/k6 · error

modulus (n) is required

Error message

modulus (n) is required

What it means

Thrown when crypto.subtle.importKey receives a JWK with kty 'RSA' that is missing the 'n' (modulus) member. rsaJWK.validate() (internal/js/modules/k6/webcrypto/jwk.go:313) requires kty, n, and e before the key is parsed; the failure rejects the importKey promise. A private JWK still needs n and e — only primes and CRT fields are optional.

Solutions

  1. Add the 'n' member: the modulus as an unpadded base64url string (256 bytes for RSA-2048, 384 for RSA-3072, 512 for RSA-4096)
  2. Re-export the complete JWK with crypto.subtle.exportKey('jwk', key) or convert the full PEM with a tool like pem-jose/pem-jwk rather than hand-assembling
  3. Validate that kty, n, and e exist and are non-empty before calling importKey

Example fix

// before
const jwk = { kty: 'RSA', e: 'AQAB', d: 'Kk9...' };
const key = await crypto.subtle.importKey('jwk', jwk, { name: 'RSA-OAEP', hash: 'SHA-256' }, true, ['decrypt']);

// after
const jwk = { kty: 'RSA', n: 'vHkg...', e: 'AQAB', d: 'Kk9...' };
const key = await crypto.subtle.importKey('jwk', jwk, { name: 'RSA-OAEP', hash: 'SHA-256' }, true, ['decrypt']);
Defensive patterns

Strategy: validation

Validate before calling

function assertRsaJwkComplete(jwk) {
  const missing = ['n', 'e'].filter((f) => !jwk || typeof jwk[f] !== 'string' || jwk[f] === '');
  if (jwk && jwk.kty !== 'RSA') throw new Error(`expected kty RSA, got ${jwk.kty}`);
  if (missing.length > 0) throw new Error(`RSA JWK is missing required field(s): ${missing.join(', ')}`);
}

assertRsaJwkComplete(jwk);
await crypto.subtle.importKey('jwk', jwk, { name: 'RSA-OAEP', hash: 'SHA-256' }, true, ['encrypt']);

Type guard

function isCompleteRsaJwk(v) {
  return !!v && v.kty === 'RSA'
    && typeof v.n === 'string' && v.n !== ''
    && typeof v.e === 'string' && v.e !== '';
}

Try / catch

try {
  const key = await crypto.subtle.importKey('jwk', jwk, alg, true, usages);
} catch (err) {
  if (/modulus \(n\) is required/.test(String(err))) {
    throw new Error('RSA JWK missing modulus — re-export the full key with exportKey(\'jwk\')');
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling crypto.subtle.importKey('jwk', jwk, { name: 'RSA-OAEP', hash: 'SHA-256' }, true, ['encrypt']) with a JWK like { kty: 'RSA', e: 'AQAB', d: '...' } — private primes are present but the modulus is not.

Common situations: Copying only the private parts (d, p, q, dp, dq, qi) out of a PEM breakdown; a JWK truncated by a config limit or copy-paste; converting a PEM with a tool configured to omit public fields.

Related errors


AI-assisted analysis of grafana/k6@8d06114777 (2026-08-18). Data as JSON: /api/errors/54376863a20663f8. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/webcrypto/jwk.go:314

type rsaJWK struct {
	Kty string `json:"kty"`          // Key Type
	N   string `json:"n"`            // Modulus
	E   string `json:"e"`            // Exponent
	D   string `json:"d,omitempty"`  // Private exponent
	P   string `json:"p,omitempty"`  // First prime factor
	Q   string `json:"q,omitempty"`  // Second prime factor
	Dp  string `json:"dp,omitempty"` // Exponent1
	Dq  string `json:"dq,omitempty"` // Exponent2
	Qi  string `json:"qi,omitempty"` // Coefficient
}

func (jwk *rsaJWK) validate() error {
	if jwk.Kty != "RSA" {
		return fmt.Errorf("invalid key type: %s", jwk.Kty)
	}

	if jwk.N == "" {
		return errors.New("modulus (n) is required")
	}

	if jwk.E == "" {
		return errors.New("exponent (e) is required")
	}

	// TODO: consider validating the other fields in future
	return nil
}

func importRSAJWK(jsonKeyData []byte) (any, CryptoKeyType, int, error) {
	var jwk rsaJWK
	if err := json.Unmarshal(jsonKeyData, &jwk); err != nil {
		return nil, UnknownCryptoKeyType, 0, fmt.Errorf("failed to parse input as RSA JWK key: %w", err)
	}

	if err := jwk.validate(); err != nil {
		return nil, UnknownCryptoKeyType, 0, fmt.Errorf("invalid RSA JWK key: %w", err)

View on GitHub (pinned to 8d06114777)