{"record":{"id":"57970da1ec520a98","repo":"gchq/CyberChef","slug":"input-is-not-a-json-web-key","errorCode":null,"errorMessage":"Input is not a JSON Web Key","messagePattern":"Input is not a JSON Web Key","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/JWKToPem.mjs","lineNumber":57,"sourceCode":"     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        const inputJson = JSON.parse(input);\n\n        let keys = [];\n        if (Array.isArray(inputJson)) {\n            // list of keys => transform all keys\n            keys = inputJson;\n        } else if (Array.isArray(inputJson.keys)) {\n            // JSON Web Key Set => transform all keys\n            keys = inputJson.keys;\n        } else if (typeof inputJson === \"object\") {\n            // single key\n            keys.push(inputJson);\n        } else {\n            throw new OperationError(\"Input is not a JSON Web Key\");\n        }\n\n        let output = \"\";\n        for (let i=0; i<keys.length; i++) {\n            const jwk = keys[i];\n            if (typeof jwk.kty !== \"string\") {\n                throw new OperationError(\"Invalid JWK format\");\n            } else if (\"|RSA|EC|\".indexOf(jwk.kty) === -1) {\n                throw new OperationError(`Unsupported JWK key type '${inputJson.kty}'`);\n            }\n\n            const key = r.KEYUTIL.getKey(jwk);\n            const pem = key.isPrivate ? r.KEYUTIL.getPEM(key, \"PKCS8PRV\") : r.KEYUTIL.getPEM(key);\n\n            // PEM ends with '\\n', so a new key always starts on a new line\n            output += pem;\n        }\n","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/JWKToPem.mjs#L39-L75","documentation":"Thrown by JWK to PEM when the parsed input is not shaped like any recognised JSON Web Key container. The operation accepts three shapes: an array of keys, a JSON Web Key Set ({ keys: [...] }), or a single key object. If the parsed value is none of these (a primitive scalar), this fires.","triggerScenarios":"The JSON parses to a primitive: the string \"hello\", a number like 123, a boolean, or null. Also any non-array, non-'keys'-bearing value whose typeof is not 'object'.","commonSituations":"Feeding a raw base64 key blob instead of a JWK. Passing a PEM string by mistake. Input that decodes to a single scalar rather than a key structure.","solutions":["Provide a JWK object with a 'kty' field, a JWKS { keys: [...] }, or an array of JWKs.","If the input is PEM, use the PEM-to-JWT/key path instead of JWK to PEM.","Validate that the parsed value is a non-null object or array before converting."],"exampleFix":"// before: primitive input\nchef.JWKToPem('not-a-key');\n// after: a valid single JWK\nchef.JWKToPem(JSON.stringify({ kty: 'RSA', n: '...', e: 'AQAB' }));","handlingStrategy":"type-guard","validationCode":"function ensureJwkContainer(parsed) {\n  if (Array.isArray(parsed) || (parsed && typeof parsed === 'object' && Array.isArray(parsed.keys)) ||\n      (parsed && typeof parsed === 'object' && typeof parsed.kty === 'string')) return parsed;\n  throw new Error('Input must be a JWK, a JWKS {keys:[...]}, or an array of JWKs');\n}","typeGuard":"function isJwkContainer(p) {\n  if (Array.isArray(p)) return p.every(k => k && typeof k === 'object');\n  if (p && typeof p === 'object') return Array.isArray(p.keys) || typeof p.kty === 'string';\n  return false;\n}","tryCatchPattern":"try {\n  return chef.JWKToPem(input);\n} catch (e) {\n  if (/not a JSON Web Key/.test(e.message)) throw new Error('Supply a JWK/JWKS object, not a scalar');\n  throw e;\n}","preventionTips":["Provide a JWK object, a JWKS, or an array of JWKs - never a bare scalar.","If you have PEM, use the PEM path instead.","Validate the parsed value is a non-null object/array before converting."],"tags":["jwk","crypto","key","input-validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}