Skip to content

fix(anonymize): salt WIDs with BIOBLOCK_STUDY_SALT - #264

Open
PRAteek-singHWY wants to merge 2 commits into
healthyinc:devfrom
PRAteek-singHWY:fix/salt-anonymized-ids
Open

PRAteek-singHWY wants to merge 2 commits into
healthyinc:devfrom
PRAteek-singHWY:fix/salt-anonymized-ids

Conversation

@PRAteek-singHWY

@PRAteek-singHWY PRAteek-singHWY commented Oct 17, 2026 •

Copy link
Copy Markdown

Fixes #262

What was wrong

WIDs in the spreadsheet anonymizer were WID_ + the first 8 hex of sha256(patientId), with nothing secret in it. Patient IDs follow patterns like P0042, so anyone with an anonymized file could hash the likely IDs and get the real ones back in under a second. For personal uploads the input was the wallet address, which is public.

For example, a row with Patient ID P0042 comes out as WID_999595a9, and this gets it back:

const crypto = require("crypto");
const wid = (id) => "WID_" + crypto.createHash("sha256").update(id).digest("hex").substring(0, 8);
for (let n = 0; n <= 99999; n++) {
  const guess = "p" + String(n).padStart(4, "0");
  if (wid(guess) === "WID_999595a9") console.log(guess); // p0042
}

Change

In workers/anonymizeWorker.js, the hash now includes BIOBLOCK_STUDY_SALT, built the same way as stable_hash in python_backend/services/text_anonymization.py (salt:PATIENT_ID:value). The WID format stays WID_ + 8 lowercase hex, and the same Patient ID still gets the same WID within a study, so rows stay linked. Without the salt, a WID can't be recomputed from a guessed ID.

If BIOBLOCK_STUDY_SALT isn't set, the worker refuses to anonymize and returns an error naming the variable, the same way the Python side fails closed. It doesn't fall back to an unsalted hash.

Also added BIOBLOCK_STUDY_SALT to javascript_backend/.env.example, with a note to use the same value as python_backend.

Heads up: any deployment needs BIOBLOCK_STUDY_SALT set in the JS backend's environment, and WIDs for new uploads won't match ones produced before this change.

Tests

Added three tests in tests/controllers/anonymizeController.test.js:

  • the WID for P0042 is not the unsalted SHA-256 of it
  • the same Patient ID gets the same WID with the same salt, and a different WID with a different salt
  • with no salt set, the upload is refused and the error names BIOBLOCK_STUDY_SALT

The test files set a test salt if none is set, like the Python tests do.

cd javascript_backend
npm install
npx mocha tests/controllers/anonymizeController.test.js --exit   # 62 passing
npm test                                                         # 10 passing

The three new tests fail on current dev and pass with this change. Prettier check is clean on the changed JS files.

WIDs were a plain SHA-256 of the Patient ID, so they could be reversed
by hashing guessed IDs. Salt them with BIOBLOCK_STUDY_SALT the same way
the Python anonymizers do, and refuse to anonymize if it isn't set.
@PRAteek-singHWY

Copy link
Copy Markdown
Author

Hi @karthiksathishjeemain @pradeeban whenever you get a chance, this is ready for review and closes #262.
Happy to iterate based on review.
Thank you.

@karthiksathishjeemain

Copy link
Copy Markdown
Member

Hey @PRAteek-singHWY thanks for the PR. Sure, will get back to you once I review it!!

@PRAteek-singHWY

Copy link
Copy Markdown
Author

Thanks @karthiksathishjeemain, no rush at all.

Just a heads up so the red checks don't confuse things: prettier and security_audit are failing on dev as well, not because of this PR. The tests pass.

Also, after this change the JS worker needs BIOBLOCK_STUDY_SALT set (the same one the Python backend uses), otherwise it won't anonymize. I added it to .env.example.

Looking forward to learn.

Thank you.

@karthiksathishjeemain

Copy link
Copy Markdown
Member

hey @PRAteek-singHWY sorry for the delay in the review. The PR looks good. Can you please resolve a small bug which I have mentioned below?

Missing input normalization — WIDs can diverge for the same patient (Bug)

The Python stable_hash normalizes with .strip().lower() before hashing:
normalized = value.strip().lower()
digest = hashlib.sha256(f"{salt}:{entity_type}:{normalized}".encode("utf-8"))

The JS generateAnonymizedId does not:
const hash = crypto.createHash("sha256").update(${salt}:PATIENT_ID:${input}).digest("hex");

If the same patient appears with different casing in a spreadsheet (e.g. P0042 in one row, p0042 in another), the JS side will produce two different WIDs for the same patient. The Python side would correctly collapse them. The PR description says the hash is "built the same way as stable_hash" — it should match that claim. Fix would be adding .trim().toLowerCase() to input before hashing:

const normalized = input.trim().toLowerCase();
const hash = crypto.createHash("sha256").update(${salt}:PATIENT_ID:${normalized}).digest("hex");

Trim and lowercase the input in generateAnonymizedId, the same as
stable_hash in python_backend. Patient IDs were already normalized
before the call, but wallet addresses were not, so a checksummed and
a lowercase address gave different WIDs.
@PRAteek-singHWY

Copy link
Copy Markdown
Author

Thanks @karthiksathishjeemain, for the review, Fixed this in 3bb95f2.

and no worries about delay in review.

Based on ur guidance

I've moved the normalization into generateAnonymizedId itself, so it now applies .trim().toLowerCase() before hashing, matching the normalization used by stable_hash.

While looking into this, I noticed that the Patient ID path was already trimming and lowercasing IDs before passing them to the function. So P0042 and p0042 in a sheet were already producing the same WID. The inconsistency was actually in personal uploads, where wallet addresses were passed as-is. This meant a checksummed address and its lowercase equivalent could produce different WIDs.

Moving the normalization into the function handles both cases consistently, regardless of the input path.

I've also added two tests:

  • One verifies that the WID matches the stable_hash construction for " P0042 ".
  • Another verifies that mixed-case and lowercase versions of the same wallet address produce an identical WID. This test fails without the fix.

The JS tests pass. The red coverage check is from the Python test test_store_with_complex_metadata, which is failing on dev too (#273 fixes it), and security_audit is the same failure as on dev.

The hash itself now matches Python exactly. The only intentional difference is the output format (WID_ + lowercase versus Python's PATIENT_ID_ + uppercase), which I've left unchanged to avoid breaking existing WIDs and tests.

Happy to change the output format too if that's preferred
& iterate based on any further suggestions, this has been great learning.

Thank you.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants