CherryHQ/cherry-studio · error · Error

Private key content is empty after cleaning

Error message

Private key content is empty after cleaning

What it means

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.

Source

Thrown at packages/aiCore/src/core/providers/core/utils.ts:81

  // 重新格式化为 64 字符一行
  const formattedContent = keyContent.match(/.{1,64}/g)?.join('\n') || keyContent

  return `-----BEGIN PRIVATE KEY-----\n${formattedContent}\n-----END PRIVATE KEY-----`
}

/**
 * 重新构建 PEM 私钥
 */
function reconstructPemKey(key: string): string {
  // 移除所有空白字符和可能存在的不完整头尾
  let cleanKey = key.replace(/\s+/g, '')
  cleanKey = cleanKey.replace(/-----BEGIN[^-]*-----/g, '')
  cleanKey = cleanKey.replace(/-----END[^-]*-----/g, '')

  // 确保私钥内容不为空
  if (!cleanKey) {
    throw new Error('Private key content is empty after cleaning')
  }

  // 验证是否是有效的 Base64 字符
  if (!/^[A-Za-z0-9+/=]+$/.test(cleanKey)) {
    throw new Error('Private key contains invalid characters (not valid Base64)')
  }

  // 格式化为 64 字符一行
  const formattedKey = cleanKey.match(/.{1,64}/g)?.join('\n') || cleanKey

  return `-----BEGIN PRIVATE KEY-----\n${formattedKey}\n-----END PRIVATE KEY-----`
}

// ==================== 错误类 ====================

/**
 * Provider 创建错误
 * 当创建 provider 实例失败时抛出

View on GitHub (pinned to 726446b54c)

Solutions

  1. Re-copy the full private key including the base64 body between the markers.
  2. Verify the key string length is plausible (a real key is hundreds of chars).
  3. Load the key from the original service-account JSON's private_key field.
Defensive patterns

Strategy: validation

Validate before calling

const stripped = privateKey.replace(/-----[^-]+-----/g, '').replace(/\s+/g, '')
if (!stripped) throw new Error('private key has no base64 body')
formatPrivateKey(privateKey)

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of CherryHQ/cherry-studio@726446b54c (2026-08-12). Data as JSON: /api/errors/cd0858d889b6f5b5. Report an issue: GitHub.