Skip to content

Add unit tests for lib/utils/hash.ts (hashIp and getClientIp) #26

Description

@royalpinto007

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions