{"record":{"id":"8839862c397f31e1","repo":"grafana/k6","slug":"failed-to-parse-input-as-ec-jwk-key-w","errorCode":null,"errorMessage":"failed to parse input as EC JWK key: %w","messagePattern":"failed to parse input as EC JWK key: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/webcrypto/jwk.go","lineNumber":224,"sourceCode":"\t}\n\n\texported.Set(\"crv\", curveParams.Name)\n\tcurveBits := curveParams.BitSize\n\n\texported.Set(\"x\", base64URLEncode(x.Bytes()))\n\texported.Set(\"y\", base64URLEncode(y.Bytes()))\n\n\tif d != nil {\n\t\texported.Set(\"d\", encodeCurveBigInt(d, curveBits))\n\t}\n\n\treturn exported, nil\n}\n\nfunc importECDSAJWK(_ EllipticCurveKind, jsonKeyData []byte) (any, CryptoKeyType, error) {\n\tvar jwkKey ecJWK\n\tif err := json.Unmarshal(jsonKeyData, &jwkKey); err != nil {\n\t\treturn nil, UnknownCryptoKeyType, fmt.Errorf(\"failed to parse input as EC JWK key: %w\", err)\n\t}\n\n\tif err := jwkKey.validate(); err != nil {\n\t\treturn nil, UnknownCryptoKeyType, fmt.Errorf(\"invalid EC JWK key: %w\", err)\n\t}\n\n\tcrv, err := pickEllipticCurve(jwkKey.Crv)\n\tif err != nil {\n\t\treturn nil, UnknownCryptoKeyType, fmt.Errorf(\"failed to parse elliptic curve: %w\", err)\n\t}\n\n\tx, err := base64URLDecode(jwkKey.X)\n\tif err != nil {\n\t\treturn nil, UnknownCryptoKeyType, fmt.Errorf(\"failed to decode X coordinate: %w\", err)\n\t}\n\n\ty, err := base64URLDecode(jwkKey.Y)\n\tif err != nil {","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/webcrypto/jwk.go#L206-L242","documentation":"importECDSAJWK first unmarshals the JWK bytes into the internal ecJWK struct; this error means that JSON unmarshaling failed. In k6 the JWK is passed as a JavaScript object which is re-serialized with json.Marshal, so a valid object almost never fails here — the failure comes from fields having the wrong JSON type (ecJWK expects kty/crv/x/y/d to be strings). The wrapped %w carries Go's precise 'json: cannot unmarshal ...' message.","triggerScenarios":"crypto.subtle.importKey('jwk', keyData, {name:'ECDSA', namedCurve:...}, ...) where keyData is a JSON string instead of an object (double encoding makes the top level a string), or where x/y/crv/d are numbers/arrays/objects instead of strings, or where keyData is an array.","commonSituations":"Passing JSON.stringify-ed JWKs into importKey (habit from Node's crypto where raw strings are used); JWKs from external services that emit coordinates as byte arrays; script authors building JWKs programmatically with numeric fields.","solutions":["Pass a plain JavaScript object as keyData, not a JSON string","Ensure kty, crv, x, y and d are all strings","If the JWK arrives as text, JSON.parse it before calling importKey","Log the wrapped cause after 'failed to parse input as EC JWK key:' to identify the offending field"],"exampleFix":"// before\nconst key = await crypto.subtle.importKey('jwk', JSON.stringify(jwk), alg, true, ['verify']);\n// after\nconst key = await crypto.subtle.importKey('jwk', jwk, alg, true, ['verify']);","handlingStrategy":"validation","validationCode":"function isPlainObject(v) { return v !== null && typeof v === 'object' && !Array.isArray(v); }\nfunction looksLikeEcJwk(v) {\n  return isPlainObject(v) && ['kty','crv','x','y'].every(f => v[f] === undefined || typeof v[f] === 'string');\n}\n// before import:\nif (!looksLikeEcJwk(jwk)) throw new Error('pass a JWK object with string fields, not JSON text');","typeGuard":"function isEcJwkShape(v) {\n  return v !== null && typeof v === 'object' && !Array.isArray(v) &&\n    Object.entries(v).every(([k, val]) =>\n      ['kty','crv','x','y','d'].includes(k) ? typeof val === 'string' : true);\n}","tryCatchPattern":"try {\n  key = await crypto.subtle.importKey('jwk', jwk, alg, true, usages);\n} catch (e) {\n  if (e.message.includes('failed to parse input as EC JWK key')) {\n    throw new Error('JWK must be a plain object with string fields (got: ' + e.message + ')');\n  }\n  throw e;\n}","preventionTips":["Always pass the parsed object to importKey; never JSON.stringify it first","If reading from a file/env var, JSON.parse once at load time","Treat any non-string kty/crv/x/y/d as a build error in fixtures"],"tags":["webcrypto","jwk","import","ecdsa","json","k6"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}