Files
nav-carte/server/utils/rateLimitJson.ts
JulesandClaude Opus 5.5 ff22377619 test(aep-surface): banc qui joue les routes bâties contre un faux serveur local (étape A)
npm run test:routes bâtit l'application, la démarre sur un port local avec un
environnement vide, et joue des requêtes HTTP. NocoDB, Bifrost, Mistral, Resend
et LightRAG sont remplacés par un faux serveur qui enregistre ce qu'il reçoit.
Adresses Resend et Mistral et dossier des limites de débit réglables par
l'environnement (RESEND_API_URL, MISTRAL_API_URL, NAV_RATELIMIT_DIR), valeur
d'aujourd'hui par défaut.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 11:10:56 +01:00

89 lines
2.4 KiB
TypeScript

/**
* Rate limiting via fichiers JSON locaux
* Implémentation recommandée spec F §8 : /tmp/nav-ratelimit/{IP_hash}.json
*
* - IP hashée SHA-256 (RGPD — pas de stockage IP en clair)
* - Fichier JSON par IP, reset automatique si date != today
* - Dossier créé au premier appel si absent
*
* Usage :
* const allowed = await checkRateLimitJson(ip, 'chatbot', 10)
* if (!allowed) throw createError({ statusCode: 429, ... })
*/
import { createHash } from 'crypto'
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
// Réglable pour le banc de routes (un dossier jetable par passage).
const RATELIMIT_DIR = process.env.NAV_RATELIMIT_DIR || '/tmp/nav-ratelimit'
type RateLimitFile = {
[action: string]: { count: number; date: string }
}
function ensureDir() {
if (!existsSync(RATELIMIT_DIR)) {
mkdirSync(RATELIMIT_DIR, { recursive: true })
}
}
function hashIp(ip: string): string {
return createHash('sha256').update(ip).digest('hex')
}
function todayStr(): string {
const d = new Date()
return `${d.getUTCFullYear()}-${String(d.getUTCMonth() + 1).padStart(2, '0')}-${String(d.getUTCDate()).padStart(2, '0')}`
}
function readFile(ipHash: string): RateLimitFile {
const path = join(RATELIMIT_DIR, `${ipHash}.json`)
try {
return JSON.parse(readFileSync(path, 'utf-8'))
} catch {
return {}
}
}
function writeFile(ipHash: string, data: RateLimitFile) {
const path = join(RATELIMIT_DIR, `${ipHash}.json`)
writeFileSync(path, JSON.stringify(data), 'utf-8')
}
/**
* Vérifie et incrémente le compteur pour une IP et une action.
* @param ip Adresse IP du client (sera hashée SHA-256)
* @param action Clé d'action (ex : 'chatbot', 'submit', 'comment')
* @param maxPerDay Nombre max d'appels autorisés par jour
* @returns true si autorisé, false si limite dépassée
*/
export function checkRateLimitJson(
ip: string,
action: string,
maxPerDay: number,
): boolean {
ensureDir()
const ipHash = hashIp(ip)
const today = todayStr()
const data = readFile(ipHash)
const entry = data[action]
if (!entry || entry.date !== today) {
// Nouveau jour ou premier appel : reset et autoriser
data[action] = { count: 1, date: today }
writeFile(ipHash, data)
return true
}
if (entry.count >= maxPerDay) {
return false
}
entry.count++
writeFile(ipHash, data)
return true
}