Skip to content

Add ein_hash Column (SHA-256 of EIN) to importers for PII-Safe Queries #243

Description

@vjuliaife

Description:
The importers.ein column stores a plaintext Employer Identification Number, which is PII subject to data minimization requirements. Many queries only need to verify that a given EIN matches an importer (e.g., deduplication checks, lookup by EIN) — they don't need the raw value. Adding an ein_hash column that stores the SHA-256 hex digest of the EIN allows equality lookups without exposing the plaintext, enabling the raw ein column to be encrypted or removed in a future PII hardening pass.

Acceptance Criteria:

  • Add column via migration: ALTER TABLE importers ADD COLUMN IF NOT EXISTS ein_hash TEXT;
  • Update the CREATE TABLE IF NOT EXISTS importers DDL in db.ts to include ein_hash TEXT for fresh deployments
  • Add CREATE UNIQUE INDEX IF NOT EXISTS idx_importers_ein_hash ON importers(ein_hash) WHERE ein_hash IS NOT NULL; to enforce uniqueness and enable O(1) lookup
  • Write a one-time backfill migration that computes encode(sha256(ein::bytea), 'hex') for all existing rows where ein IS NOT NULL and sets ein_hash
  • Update the importer registration handler in importers.ts to compute and store ein_hash using crypto.createHash('sha256').update(ein).digest('hex') before inserting
  • Replace any WHERE ein = $1 queries in importers.ts with WHERE ein_hash = $1 (where the caller passes a pre-hashed value) to avoid plaintext EIN in query params
  • Document in db.ts that ein is scheduled for encryption (referencing the AES-GCM security issue) while ein_hash handles lookup

Relevant Files:

  • apps/api/src/db.ts — add ein_hash column to importers DDL and backfill migration
  • apps/api/src/routes/importers.ts — update registration and lookup queries

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave programdatabaseAuto-created by repotool publish-foldermigrationsAuto-created by repotool publish-folderschemaAuto-created by repotool publish-folder

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions