mirror of
https://github.com/Luchexx/wacrm.git
synced 2026-11-02 09:19:51 +00:00
Merge pull request #127 from ArnasDon/feat/flows-cron-timing-safe
fix(flows): constant-time compare for cron secret
This commit is contained in:
commit
1a6419b815
@ -1,3 +1,4 @@
|
||||
import { timingSafeEqual } from 'node:crypto'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { supabaseAdmin } from '@/lib/flows/admin-client'
|
||||
import { resolveFallbackPolicy } from '@/lib/flows/fallback'
|
||||
@ -30,8 +31,17 @@ export async function GET(request: Request) {
|
||||
if (!expected) {
|
||||
return NextResponse.json({ error: 'cron not configured' }, { status: 503 })
|
||||
}
|
||||
const supplied = request.headers.get('x-cron-secret')
|
||||
if (supplied !== expected) {
|
||||
// Constant-time compare so an attacker who can hit the endpoint
|
||||
// can't recover the secret byte-by-byte from response-time deltas.
|
||||
// Length pre-check is required by timingSafeEqual (throws otherwise)
|
||||
// and leaks only the length itself, which isn't sensitive.
|
||||
const supplied = request.headers.get('x-cron-secret') ?? ''
|
||||
const suppliedBuf = Buffer.from(supplied)
|
||||
const expectedBuf = Buffer.from(expected)
|
||||
if (
|
||||
suppliedBuf.length !== expectedBuf.length ||
|
||||
!timingSafeEqual(suppliedBuf, expectedBuf)
|
||||
) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user