Branchwell

Become a Pro / website · becomeapro.ai / SECURITY_AUDIT.md

Raw 10.4 KB on b7854f3692d64213a2fce644d0b5101d3e345b03

Show the source

Security Audit Report - AI Resource Pro

Date: 2026-02-04 Scope: Repository codebase + git history scan + npm audit

Executive Summary

This audit identified multiple critical issues that could allow full administrative compromise and exposure of production secrets. The most severe findings include hardcoded/committed secrets in the repository and authentication bypass via a client-set cookie. The API authorization model also relies on a public API key (NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY), which is exposed in client code and therefore does not provide security. Several endpoints log sensitive data, CORS settings are overly permissive, and authentication endpoints lack rate limiting. Dependency vulnerabilities were detected by npm audit and should be addressed.

Immediate actions are required: rotate and revoke exposed secrets, remove them from the repository and git history, and replace the current admin-cookie and API-key authorization scheme with a server-verified session model.

Critical Findings (Fix Immediately)

C-1: Production secrets committed to the repository

Location: /.mcp.json, /client_secret.txt, plus evidence in git history

Details:

  • .mcp.json contains a Supabase service role key (full admin privileges to DB and Auth).
  • client_secret.txt contains an Apple Sign-In client secret JWT.
  • Git history shows previous commits with multiple production secrets (Supabase service role, OpenAI, Upstash, Cloudflare, reCAPTCHA secret, etc.).

Impact: Anyone with repo access can use the service role key to read/modify all data and users. The Apple client secret can be used to impersonate your app with Apple Sign-In. Historic exposure means keys should be considered compromised.

Remediation:

  1. Revoke and rotate all exposed credentials immediately (Supabase service role key, Apple client secret, OpenAI, Upstash, Cloudflare, reCAPTCHA secret, etc.).
  2. Remove secrets from the repo (.mcp.json, client_secret.txt) and ensure they are listed in .gitignore.
  3. Purge secrets from git history using a tool like git filter-repo, then force-push if acceptable for the repo.
  4. Use a secret manager or environment variables for all credentials. Avoid storing secrets in local files tracked by git.

Location: src/lib/auth/supabase-server-auth.ts, src/middleware.ts

Details: validateRequest() treats the presence of the admin_session_active cookie as authenticated admin without any server-side verification. The cookie is also set with httpOnly: false and can be created or modified by client-side JavaScript.

Impact: Any attacker can set admin_session_active=true in their browser and gain admin access to protected routes and APIs. This is a full admin takeover.

Remediation:

  1. Remove the admin_session_active cookie as an authentication mechanism.
  2. Only trust server-verified sessions (Supabase session cookies or server-side JWT validation).
  3. If you must use a custom cookie, sign it (e.g., HMAC) and validate the signature server-side.
  4. Set httpOnly: true, secure: true, and sameSite: strict for sensitive cookies.

C-3: API authorization relies on a public API key

Location: src/middleware/auth.ts, client usage in src/app/admin/* and API routes such as src/app/api/tools/route.ts, src/app/api/news/route.ts

Details: The API key is stored in NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY, which is exposed to browsers. Several admin-level endpoints accept only this API key when admin auth fails (e.g., PUT/DELETE/POST in /api/tools, DELETE in /api/news).

Impact: Anyone can extract the key from client code and perform privileged API actions (create/update/delete content).

Remediation:

  1. Replace NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY with a server-only secret (non-public env var).
  2. Do not accept API key alone for admin actions. Require verified admin sessions or signed JWTs.
  3. If API key access is required for integrations, use scoped keys with narrow permissions and rotate them regularly.

C-4: Stored XSS via unsanitized HTML rendering

Location: src/utils/contentParser.ts and pages using dangerouslySetInnerHTML (e.g., src/app/blog/[slug]/page.tsx, src/app/news/[slug]/page.tsx, src/app/faq/page.tsx)

Details: ContentParser converts markdown to HTML without sanitizing user-controlled content. Multiple pages render the result with dangerouslySetInnerHTML.

Impact: Attackers can inject arbitrary HTML/JS into content (blog/news/faq), enabling session theft, admin cookie manipulation, or account takeover.

Remediation:

  1. Sanitize all HTML before rendering (e.g., DOMPurify on the server or a trusted markdown-to-HTML sanitizer).
  2. Prefer a safe markdown renderer with built-in XSS protection.
  3. Remove dangerouslySetInnerHTML where possible.

High Priority Findings

H-1: Sensitive data logged in production

Location: src/lib/auth/supabase-server-auth.ts, src/app/api/admin/login/route.ts, src/app/api/news/route.ts

Details: Logs include cookies and headers (sometimes full headers in production), and request bodies. These can contain tokens, session IDs, or PII.

Impact: Logs become a secondary leakage vector. Anyone with log access can extract session tokens or sensitive user data.

Remediation:

  1. Remove logging of cookies/headers or redact values.
  2. Avoid logging request bodies containing user data.
  3. Use structured logging with redaction rules.

H-2: Overly permissive CORS on API endpoints

Location: src/app/api/news/route.ts (Access-Control-Allow-Origin: *), src/app/api/admin/login/route.ts (dynamic origin, credentials enabled)

Details: CORS allows requests from any origin on endpoints that also accept privileged actions (POST/PUT/DELETE). This is dangerous when combined with the public API key and weak auth.

Impact: Malicious sites can trigger requests and, in some scenarios, exfiltrate responses or abuse endpoints.

Remediation:

  1. Restrict CORS to a whitelist of trusted origins.
  2. Only include Access-Control-Allow-Credentials: true when needed and with explicit origin checks.
  3. For admin APIs, consider disabling CORS entirely.

H-3: No rate limiting on authentication endpoints

Location: src/app/api/admin/login/route.ts, src/app/api/admin/login-alt/route.ts

Details: authRateLimit exists but is not applied. Login endpoints have no rate limiting, enabling brute-force attacks.

Impact: Increased risk of credential stuffing and brute-force compromises.

Remediation:

  1. Apply authRateLimit to all login/auth endpoints.
  2. Add IP- and account-based throttling.
  3. Add account lockout or exponential backoff on repeated failures.

H-4: Debug endpoints expose internal state

Location: src/app/api/debug/auth-status/route.ts (API-key only), src/app/api/auth/debug/route.ts

Details: Debug endpoints reveal cookies, auth state, environment flags, and user data. Some are protected only by the public API key.

Impact: Attackers can enumerate and gather sensitive internal information, aiding exploitation.

Remediation:

  1. Restrict debug endpoints to internal environments only (or remove entirely in production).
  2. Require verified admin auth for all debug endpoints.
  3. Remove sensitive details from responses.

Medium Priority Findings

M-1: Development auth bypass could be enabled in production

Location: src/lib/auth/supabase-server-auth.ts, src/middleware.ts, src/app/api/admin/dashboard/*

Details: BYPASS_AUTH_IN_DEV enables full auth bypass and mock admin users. If mistakenly enabled in production, all admin routes become public.

Impact: Full admin compromise if env misconfigured.

Remediation:

  1. Add hard checks preventing bypass flags in production builds.
  2. Fail startup if NODE_ENV=production and bypass flags are enabled.

M-2: GET endpoints with side effects (CSRF/abuse risk)

Location: src/app/api/admin/create-test-user/route.ts

Details: This endpoint creates admin users on GET. It is gated by NODE_ENV, but if misconfigured it is remotely exploitable and CSRF-friendly.

Impact: Unauthorized admin creation if environment flags are mis-set.

Remediation:

  1. Remove this endpoint from production builds.
  2. Require POST + strong admin authentication + CSRF protection.

M-3: Cookies not marked HttpOnly for admin session

Location: src/app/api/admin/login/route.ts, src/app/api/admin/login-alt/route.ts

Details: admin_session_active is set with httpOnly: false, allowing JavaScript to read/modify it.

Impact: XSS enables session theft or session forging.

Remediation:

  1. Set httpOnly: true on all auth cookies.
  2. Use signed or server-validated sessions.

Low Priority / Recommendations

Location: /cookies.txt

Details: Local cookie values are stored in the repo. Even if local-only, this sets a bad precedent.

Remediation: Remove cookies.txt from the repo and ensure it is ignored by .gitignore.


L-2: Dependency vulnerabilities from npm audit

Location: package.json, package-lock.json

Details: npm audit reported 10 vulnerabilities (5 high, 4 moderate, 1 low). Key packages include next, eslint-config-next, glob, qs, lodash, react-syntax-highlighter, prismjs.

Remediation:

  1. Update to patched versions where possible (next -> 15.5.11 suggested by audit).
  2. Consider upgrading eslint-config-next (major bump).
  3. Re-run npm audit and address remaining transitive vulnerabilities.

Remediation Checklist (Prioritized)

  1. Rotate/revoke all exposed secrets and purge from git history. (Critical)
  2. Remove admin_session_active cookie auth and replace with server-verified sessions. (Critical)
  3. Replace public API key authorization with server-only secrets or full admin auth. (Critical)
  4. Sanitize HTML content and remove unsafe dangerouslySetInnerHTML usage. (Critical)
  5. Lock down CORS to trusted origins and disable credentials for public endpoints. (High)
  6. Add rate limiting to all authentication endpoints. (High)
  7. Remove or fully lock debug endpoints in production. (High)
  8. Harden environment flags to prevent dev bypass in production. (Medium)
  9. Upgrade vulnerable dependencies per npm audit. (Low/Medium)