configure salt rotation period using env vars. Closed #3427

This commit is contained in:
Francis Cao 2026-02-05 08:56:12 -08:00
parent 5267222a67
commit e21c1c83bb
2 changed files with 13 additions and 3 deletions

View file

@ -1,10 +1,10 @@
import { startOfHour, startOfMonth } from 'date-fns';
import { startOfHour } from 'date-fns';
import { isbot } from 'isbot';
import { serializeError } from 'serialize-error';
import { z } from 'zod';
import clickhouse from '@/lib/clickhouse';
import { COLLECTION_TYPE, EVENT_TYPE } from '@/lib/constants';
import { hash, secret, uuid } from '@/lib/crypto';
import { getSalt, hash, secret, uuid } from '@/lib/crypto';
import { getClientInfo, hasBlockedIp } from '@/lib/detect';
import { createToken, parseToken } from '@/lib/jwt';
import { fetchWebsite } from '@/lib/load';
@ -130,7 +130,8 @@ export async function POST(request: Request) {
const createdAt = timestamp ? new Date(timestamp * 1000) : new Date();
const now = Math.floor(Date.now() / 1000);
const sessionSalt = hash(startOfMonth(createdAt).toUTCString());
const saltRotation = process.env.SALT_ROTATION || 'month';
const sessionSalt = getSalt(saltRotation, createdAt);
const visitSalt = hash(startOfHour(createdAt).toUTCString());
const sessionId = id ? uuid(sourceId, id) : uuid(sourceId, ip, userAgent, sessionSalt);

View file

@ -1,4 +1,5 @@
import crypto from 'node:crypto';
import { startOfDay, startOfMonth, startOfWeek } from 'date-fns';
import { v4, v5, v7 } from 'uuid';
const ALGORITHM = 'aes-256-gcm';
@ -67,3 +68,11 @@ export function uuid(...args: any) {
export function createAuthKey() {
return crypto.randomBytes(16).toString('hex');
}
export function getSalt(saltRotation: string, createdAt: Date): string {
return hash(
(saltRotation === 'day' ? startOfDay : saltRotation === 'week' ? startOfWeek : startOfMonth)(
createdAt,
).toUTCString(),
);
}