{"record":{"id":"cd0858d889b6f5b5","repo":"CherryHQ/cherry-studio","slug":"private-key-content-is-empty-after-cleaning","errorCode":null,"errorMessage":"Private key content is empty after cleaning","messagePattern":"Private key content is empty after cleaning","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/aiCore/src/core/providers/core/utils.ts","lineNumber":81,"sourceCode":"\n  // 重新格式化为 64 字符一行\n  const formattedContent = keyContent.match(/.{1,64}/g)?.join('\\n') || keyContent\n\n  return `-----BEGIN PRIVATE KEY-----\\n${formattedContent}\\n-----END PRIVATE KEY-----`\n}\n\n/**\n * 重新构建 PEM 私钥\n */\nfunction reconstructPemKey(key: string): string {\n  // 移除所有空白字符和可能存在的不完整头尾\n  let cleanKey = key.replace(/\\s+/g, '')\n  cleanKey = cleanKey.replace(/-----BEGIN[^-]*-----/g, '')\n  cleanKey = cleanKey.replace(/-----END[^-]*-----/g, '')\n\n  // 确保私钥内容不为空\n  if (!cleanKey) {\n    throw new Error('Private key content is empty after cleaning')\n  }\n\n  // 验证是否是有效的 Base64 字符\n  if (!/^[A-Za-z0-9+/=]+$/.test(cleanKey)) {\n    throw new Error('Private key contains invalid characters (not valid Base64)')\n  }\n\n  // 格式化为 64 字符一行\n  const formattedKey = cleanKey.match(/.{1,64}/g)?.join('\\n') || cleanKey\n\n  return `-----BEGIN PRIVATE KEY-----\\n${formattedKey}\\n-----END PRIVATE KEY-----`\n}\n\n// ==================== 错误类 ====================\n\n/**\n * Provider 创建错误\n * 当创建 provider 实例失败时抛出","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/CherryHQ/cherry-studio/blob/726446b54cd69ffe51a276638672f6d95ca0768c/packages/aiCore/src/core/providers/core/utils.ts#L63-L99","documentation":"reconstructPemKey handles inputs without valid markers: it strips all whitespace and any BEGIN/END fragments, expecting raw base64. If nothing remains after cleaning, the input had no actual key material and it throws. This catches keys that are only markers/whitespace or completely empty after marker removal.","triggerScenarios":"The input contains only PEM markers and whitespace with no base64 body, e.g. '-----BEGIN PRIVATE KEY----------END PRIVATE KEY-----' or a string of only newlines/spaces.","commonSituations":"A truncated key paste (header and footer but body deleted); env var holding only the marker template; a key file that was corrupted/emptied; copy-paste that grabbed only the boundaries.","solutions":["Re-copy the full private key including the base64 body between the markers.","Verify the key string length is plausible (a real key is hundreds of chars).","Load the key from the original service-account JSON's private_key field."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const stripped = privateKey.replace(/-----[^-]+-----/g, '').replace(/\\s+/g, '')\nif (!stripped) throw new Error('private key has no base64 body')\nformatPrivateKey(privateKey)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check the key length is plausible before formatting.","Load the key from the original source rather than re-typing it.","Keep the base64 body intact when copying."],"tags":["auth","private-key","pem","validation"],"backgroundTag":null,"analyzedSha":"726446b54cd69ffe51a276638672f6d95ca0768c","analyzedAt":"2026-08-12T17:30:37.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}