|
| 1 | +import { createHash, randomBytes } from 'crypto' |
| 2 | +import { Pool as GenericPool } from 'generic-pool' |
| 3 | +import Redis from 'ioredis' |
| 4 | + |
| 5 | +import { ConcurrencyController } from '~/utils/concurrencyController' |
| 6 | + |
| 7 | +/* |
| 8 | + * Daily-rotating salt for Hog-derived distinct_ids. |
| 9 | + * |
| 10 | + * A 128-bit random value per calendar day, stored in Redis with a TTL. Once the TTL expires the |
| 11 | + * salt is gone, so any hash that mixed it in becomes irreversible. This is intentionally independent |
| 12 | + * of cookieless ingestion — it manages its own salt under its own key — so the cookieless code stays |
| 13 | + * untouched and the two never share state. |
| 14 | + */ |
| 15 | + |
| 16 | +// Redis key namespace. Deliberately separate from cookieless (`cookieless_salt:`) — own salt, own data. |
| 17 | +const SALT_KEY_PREFIX = 'hog_distinct_id_salt:' |
| 18 | + |
| 19 | +// Calendar-day validity window: accept any day some timezone could currently be in (UTC−12…UTC+14), |
| 20 | +// plus a 72h buffer. Mirrors the cookieless salt window without sharing its code. |
| 21 | +const MAX_NEGATIVE_TIMEZONE_HOURS = 12 |
| 22 | +const MAX_POSITIVE_TIMEZONE_HOURS = 14 |
| 23 | +const MAX_SUPPORTED_INGESTION_LAG_HOURS = 72 |
| 24 | + |
| 25 | +export type DailySaltResult = { success: true; salt: Buffer } | { success: false; reason: 'date_out_of_range' } |
| 26 | + |
| 27 | +export interface DailySaltProviderConfig { |
| 28 | + saltTtlSeconds: number |
| 29 | + deleteExpiredLocalSaltsIntervalMs: number |
| 30 | +} |
| 31 | + |
| 32 | +export class DailySaltProvider { |
| 33 | + private readonly saltTtlSeconds: number |
| 34 | + private readonly localSaltMap: Record<string, Buffer> = {} |
| 35 | + private readonly mutex = new ConcurrencyController(1) |
| 36 | + private cleanupInterval: NodeJS.Timeout | null = null |
| 37 | + |
| 38 | + constructor( |
| 39 | + config: DailySaltProviderConfig, |
| 40 | + private readonly redisPool: GenericPool<Redis.Redis> |
| 41 | + ) { |
| 42 | + this.saltTtlSeconds = config.saltTtlSeconds |
| 43 | + // Periodically drop expired salts from the local cache; Redis TTLs handle the durable copy. |
| 44 | + this.cleanupInterval = setInterval(this.deleteExpiredLocalSalts, config.deleteExpiredLocalSaltsIntervalMs) |
| 45 | + // unref so the timer never keeps the process alive. |
| 46 | + this.cleanupInterval.unref() |
| 47 | + } |
| 48 | + |
| 49 | + getSaltForDay(yyyymmdd: string, timestampMs: number): Promise<DailySaltResult> { |
| 50 | + if (!isCalendarDateValid(yyyymmdd)) { |
| 51 | + return Promise.resolve({ success: false, reason: 'date_out_of_range' }) |
| 52 | + } |
| 53 | + if (this.localSaltMap[yyyymmdd]) { |
| 54 | + return Promise.resolve({ success: true, salt: this.localSaltMap[yyyymmdd] }) |
| 55 | + } |
| 56 | + |
| 57 | + // Fetch from Redis once per node process per day, behind a mutex so concurrent callers share one round-trip. |
| 58 | + return this.mutex.run({ |
| 59 | + fn: async (): Promise<DailySaltResult> => { |
| 60 | + if (this.localSaltMap[yyyymmdd]) { |
| 61 | + return { success: true, salt: this.localSaltMap[yyyymmdd] } |
| 62 | + } |
| 63 | + |
| 64 | + const key = `${SALT_KEY_PREFIX}${yyyymmdd}` |
| 65 | + const client = await this.redisPool.acquire() |
| 66 | + try { |
| 67 | + const existing = await client.get(key) |
| 68 | + if (existing) { |
| 69 | + const salt = Buffer.from(existing, 'base64') |
| 70 | + this.localSaltMap[yyyymmdd] = salt |
| 71 | + return { success: true, salt } |
| 72 | + } |
| 73 | + |
| 74 | + // Create the day's salt, but don't overwrite a racing writer (SET NX). |
| 75 | + const newSalt = randomBytes(16) |
| 76 | + const setResult = await client.set(key, newSalt.toString('base64'), 'EX', this.saltTtlSeconds, 'NX') |
| 77 | + if (setResult === 'OK') { |
| 78 | + this.localSaltMap[yyyymmdd] = newSalt |
| 79 | + return { success: true, salt: newSalt } |
| 80 | + } |
| 81 | + |
| 82 | + // Lost the race — read the value the winner wrote. |
| 83 | + const retry = await client.get(key) |
| 84 | + if (!retry) { |
| 85 | + throw new Error('Failed to read Hog daily salt from redis') |
| 86 | + } |
| 87 | + const salt = Buffer.from(retry, 'base64') |
| 88 | + this.localSaltMap[yyyymmdd] = salt |
| 89 | + return { success: true, salt } |
| 90 | + } finally { |
| 91 | + await this.redisPool.release(client) |
| 92 | + } |
| 93 | + }, |
| 94 | + priority: timestampMs, |
| 95 | + }) |
| 96 | + } |
| 97 | + |
| 98 | + deleteExpiredLocalSalts = (): void => { |
| 99 | + for (const key in this.localSaltMap) { |
| 100 | + if (!isCalendarDateValid(key)) { |
| 101 | + delete this.localSaltMap[key] |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + deleteAllLocalSalts(): void { |
| 107 | + for (const key in this.localSaltMap) { |
| 108 | + delete this.localSaltMap[key] |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + shutdown(): void { |
| 113 | + if (this.cleanupInterval) { |
| 114 | + clearInterval(this.cleanupInterval) |
| 115 | + this.cleanupInterval = null |
| 116 | + } |
| 117 | + this.deleteAllLocalSalts() |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Derive a per-team daily salt from the random daily salt. Forward-derivable from |
| 123 | + * (dailySalt, teamId, yyyymmdd), but not reversible — sha256 is one-way and the daily salt is |
| 124 | + * random and discarded after its TTL. Mixing in `teamId` isolates teams: leaking one team's |
| 125 | + * derived salt reveals nothing about the daily salt or any other team. |
| 126 | + */ |
| 127 | +export function deriveTeamDailySalt(dailySalt: Buffer, teamId: number, yyyymmdd: string): string { |
| 128 | + return createHash('sha256').update(dailySalt).update(`:${teamId}:${yyyymmdd}`).digest('base64') |
| 129 | +} |
| 130 | + |
| 131 | +export function isCalendarDateValid(yyyymmdd: string): boolean { |
| 132 | + const utcDate = new Date(`${yyyymmdd}T00:00:00Z`) |
| 133 | + const nowUTC = new Date(Date.now()) |
| 134 | + |
| 135 | + const startOfDayMinus12 = new Date(utcDate) |
| 136 | + startOfDayMinus12.setUTCHours(-MAX_NEGATIVE_TIMEZONE_HOURS) |
| 137 | + |
| 138 | + const endOfDayPlus14 = new Date(utcDate) |
| 139 | + endOfDayPlus14.setUTCHours(MAX_POSITIVE_TIMEZONE_HOURS + MAX_SUPPORTED_INGESTION_LAG_HOURS) |
| 140 | + |
| 141 | + return nowUTC >= startOfDayMinus12 && nowUTC < endOfDayPlus14 |
| 142 | +} |
0 commit comments