File size: 1,716 Bytes
19e93fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* ============================================================
 * Cloudflare-Konfigurations-Validierung
 * Verhindert den häufigsten Fehler: Token in der Account-ID.
 * ============================================================ */

/** Echte Cloudflare Account-IDs sind exakt 32 Hex-Zeichen. */
export const CF_ACCOUNT_ID_RE = /^[a-f0-9]{32}$/i;

/** Neue scannbare Cloudflare-Credential-Formate (Token, KEINE Account-ID). */
const CF_TOKEN_PREFIXES = /^(cfat_|cfut_|cfk_)/i;

/**
 * true, wenn in CF_ACCOUNT_ID versehentlich ein API-Token steht
 * (z. B. "cfat_…") statt der 32-stelligen hexadezimalen Account-ID.
 */
export function cfAccountLooksLikeToken(accountId: string): boolean {
  return CF_TOKEN_PREFIXES.test(accountId.trim());
}

/** Konfigurationsstatus für /api/health (keine Werte, nur Diagnose). */
export function cloudflareStatus(): { configured: boolean; issue: string | null } {
  const id = process.env.CF_ACCOUNT_ID?.trim() || "";
  const token = process.env.CF_API_TOKEN?.trim() || "";

  if (!id && !token) return { configured: false, issue: null }; // einfach nicht gesetzt
  if (!id) return { configured: false, issue: "CF_ACCOUNT_ID fehlt (32-stellige hex Account-ID aus dem Dashboard)." };
  if (!token) return { configured: false, issue: "CF_API_TOKEN fehlt (API-Token mit Workers-AI-Berechtigung)." };
  if (cfAccountLooksLikeToken(id))
    return { configured: false, issue: "CF_ACCOUNT_ID enthält einen Token (cfat_/cfut_) — dort gehört die 32-stellige hex Account-ID hin." };
  if (!CF_ACCOUNT_ID_RE.test(id))
    return { configured: false, issue: "CF_ACCOUNT_ID hat ein unerwartetes Format (erwartet: 32 Hex-Zeichen)." };

  return { configured: true, issue: null };
}