Desired outcome
A lib/utils/hash.test.ts covering hashIp() and getClientIp().
Why it matters
lib/utils/hash.ts is security-relevant code: it is the only thing standing between a raw visitor IP and what gets written to the database as ip_hash. Its sibling modules lib/utils/urls.ts and lib/utils/pii.ts both have tests; this one has none, so nobody would notice if the pepper check or the header parsing regressed.
Steps
Create lib/utils/hash.test.ts next to the existing tests and cover at least:
hashIp() throws when IP_HASH_PEPPER is unset (set/restore process.env around the case).
hashIp() is deterministic for the same input and pepper, and differs for a different pepper.
hashIp() trims surrounding whitespace, so " 1.2.3.4 " and "1.2.3.4" hash identically.
getClientIp() returns the first entry of a comma-separated x-forwarded-for.
getClientIp() falls back to x-real-ip, and to "unknown" when neither header is present.
Run with npx vitest run lib/utils/hash.test.ts.
Claiming this
Comment below to claim it. A reply usually comes within a day.
Desired outcome
A
lib/utils/hash.test.tscoveringhashIp()andgetClientIp().Why it matters
lib/utils/hash.tsis security-relevant code: it is the only thing standing between a raw visitor IP and what gets written to the database asip_hash. Its sibling moduleslib/utils/urls.tsandlib/utils/pii.tsboth have tests; this one has none, so nobody would notice if the pepper check or the header parsing regressed.Steps
Create
lib/utils/hash.test.tsnext to the existing tests and cover at least:hashIp()throws whenIP_HASH_PEPPERis unset (set/restoreprocess.envaround the case).hashIp()is deterministic for the same input and pepper, and differs for a different pepper.hashIp()trims surrounding whitespace, so" 1.2.3.4 "and"1.2.3.4"hash identically.getClientIp()returns the first entry of a comma-separatedx-forwarded-for.getClientIp()falls back tox-real-ip, and to"unknown"when neither header is present.Run with
npx vitest run lib/utils/hash.test.ts.Claiming this
Comment below to claim it. A reply usually comes within a day.