Repository navigation
fix(anonymize): salt WIDs with BIOBLOCK_STUDY_SALT - #264
PRAteek-singHWY wants to merge 2 commits into
Conversation
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.
|
Hi @karthiksathishjeemain @pradeeban whenever you get a chance, this is ready for review and closes #262. |
|
Hey @PRAteek-singHWY thanks for the PR. Sure, will get back to you once I review it!! |
|
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 Looking forward to learn. Thank you. |
|
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: The JS generateAnonymizedId does not: 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(); |
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.
|
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:
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 Thank you. |
Fixes #262
What was wrong
WIDs in the spreadsheet anonymizer were
WID_+ the first 8 hex ofsha256(patientId), with nothing secret in it. Patient IDs follow patterns likeP0042, 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
P0042comes out asWID_999595a9, and this gets it back:Change
In
workers/anonymizeWorker.js, the hash now includesBIOBLOCK_STUDY_SALT, built the same way asstable_hashinpython_backend/services/text_anonymization.py(salt:PATIENT_ID:value). The WID format staysWID_+ 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_SALTisn'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_SALTtojavascript_backend/.env.example, with a note to use the same value aspython_backend.Heads up: any deployment needs
BIOBLOCK_STUDY_SALTset 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:P0042is not the unsalted SHA-256 of itBIOBLOCK_STUDY_SALTThe test files set a test salt if none is set, like the Python tests do.
The three new tests fail on current
devand pass with this change. Prettier check is clean on the changed JS files.