mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 09:20:03 +01:00
fix(cron): /api/crons/prune-debug-logs fails closed in production
Without this, a Vercel deployment with CRON_SECRET unset would leave the janitor publicly callable — anyone hitting GET /api/crons/prune-debug-logs could delete forensics data. The previous version silently allowed the call when the env var was missing (intended for local dev convenience). Tighten the gate: - if (VERCEL=1 && !CRON_SECRET) → 503 "CRON_SECRET not configured" - if (CRON_SECRET set) → require matching Bearer header (existing behavior) - if (VERCEL unset && !CRON_SECRET) → allow (local dev curl path) Also set CRON_SECRET in Vercel production envs via `vercel env add` (64-char hex, encrypted in the dashboard) so the Vercel-issued cron request matches and runs, and any unauthenticated probe gets 401. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 file changed
+15
-3
@@ -8,15 +8,27 @@ import { logDebug, pruneDebugLogs } from "@/db/queries/debug-logs";
|
||||
* - 30 days for level=info|warn (most rows; not useful long-term)
|
||||
* - 90 days for level=error (postmortem value; longer retention)
|
||||
*
|
||||
* Auth: Vercel Cron sends Authorization: Bearer ${CRON_SECRET}. If
|
||||
* CRON_SECRET is unset (local dev), the endpoint allows the call so devs
|
||||
* can run it manually with `curl localhost:3000/api/crons/prune-debug-logs`.
|
||||
* Auth: Vercel Cron sends Authorization: Bearer ${CRON_SECRET}.
|
||||
* - In production (VERCEL=1): CRON_SECRET MUST be set, or the endpoint
|
||||
* returns 503. Fail-closed so a forgotten env var never leaves the
|
||||
* janitor publicly callable.
|
||||
* - In local dev (VERCEL unset): missing CRON_SECRET allows the call so
|
||||
* devs can run it manually with `curl localhost:3000/api/crons/prune-debug-logs`.
|
||||
*
|
||||
* Logs its own outcome into debug_logs (level=info) so we can see whether
|
||||
* the janitor actually fires every day in production.
|
||||
*/
|
||||
export async function GET(req: NextRequest) {
|
||||
const expected = process.env.CRON_SECRET;
|
||||
const isProd = !!process.env.VERCEL;
|
||||
if (isProd && !expected) {
|
||||
// Fail-closed: production deployments without CRON_SECRET would leave
|
||||
// the endpoint publicly callable. Refuse rather than silently allow.
|
||||
return NextResponse.json(
|
||||
{ error: "CRON_SECRET not configured" },
|
||||
{ status: 503 },
|
||||
);
|
||||
}
|
||||
if (expected) {
|
||||
const header = req.headers.get("authorization");
|
||||
if (header !== `Bearer ${expected}`) {
|
||||
|
||||
Reference in new issue
Block a user