phiguard
README.md
<div align="center">
# phiguard
**Zero-dependency PHI de-identification, pseudonymization, and compliance guardrails for HL7 FHIR R4/R5, HL7 v2.x, Vietnam MOH QĐ 130/4750 BHYT XML, Japan APPI / JP-Core, and US HIPAA Safe Harbor — 100% offline.**
[](https://github.com/tranhoangtu-it/phiguard/actions/workflows/ci.yml)
[](https://tranhoangtu-it.github.io/phiguard/)
[](package.json)
[](package.json)
[](LICENSE)
```bash
npx phiguard scan ./clinical-fixtures
```
**[Try the 100% Offline Browser Playground](https://tranhoangtu-it.github.io/phiguard/)** — inspect, de-identify, validate, and re-hydrate FHIR R4 Bundles, HL7v2 messages, and Vietnam QĐ 4750 XML directly in your browser with zero uploads and zero network calls.
<p align="center">
<img src="assets/quickstart-demo.svg" alt="phiguard CLI and Compliance Demo" width="820" />
</p>
</div>
---
## Why `phiguard`?
Healthcare engineering teams adopting AI agents and building EHR/HIS pipelines hit four immediate failure modes when using generic regex redactors or heavy Python NLP servers:
1. **Broken Clinical Structure** — Naive `[REDACTED]` replacement corrupts FHIR `Bundle` cross-resource `Reference` links (`Patient/123`), shifts pipe positions in HL7 v2 `PID`/`PV1`/`OBX`, and misses Base64-encoded XML inside `<NOIDUNGFILE>` in Vietnam QĐ 4750 `GIAMDINHHS` claims.
2. **Narrative & Edge-Case Leakage** — Stripping `Patient.name` while leaving `Patient.text.div` XHTML narratives, 5-digit ZIP codes in the 17 restricted HIPAA Safe Harbor ZIP-3 areas (`830xx` -> `00000`), or birth years revealing age >89 fails compliance audits.
3. **Lost Context for Downstream AI** — Replacing every person with `[NAME]` destroys co-reference (`[PATIENT_01]` vs `[PRACTITIONER_01]`), and stripping every date destroys length-of-stay intervals unless dates are shifted deterministically per subject.
4. **Zero Cloud Trust for Clinical Data** — Security teams cannot send real or borderline clinical fixtures to commercial APIs or install 40-package dependency trees just to audit prompts, tests, or MCP payloads.
---
## Key Capabilities
| Capability | US HIPAA Safe Harbor | Vietnam MOH QĐ 130/4750 | Japan APPI / JP-Core |
|---|---|---|---|
| **Checksums & Formats** | SSN, NPI (`80840` Luhn), DEA check-digit, Medicare MBI, 17 ZIP-3 regions | 15-char BHYT (42 prefixes), 12-digit CCCD (`001–096`), `+84`/VN phone, `XML1–XML15` + Base64 `<NOIDUNGFILE>` | 12-digit MyNumber (`個人番号` Mod-11), postal `〒`, JP phone, `患者氏名` / `カルテ番号` |
| **FHIR R4/R5 AST & Bulk NDJSON** | Consistent `id`/`Reference` rewriting (JSON Bundles & `.ndjson` `$export` streams), `text.div` XHTML & Base64 `Attachment.data` scrubbing, `meta.security`, `age > 89 -> 1900` | BHYT/CCCD identifiers, localized clinic labels & narrative scrubbing | `JP-Core` patient/practitioner names, Karte IDs, `MyNumber` identifiers |
| **HL7 v2.x (`ADT`/`ORU`)** | Preserves `MSH` delimiters; scrubs `PID`, `NK1`, `GT1`, `IN1`, `PV1`, `OBR`, `OBX`, `NTE` | Free-text Vietnamese PHI inside `OBX-5` / `NTE-3` | Free-text Japanese PHI inside `OBX-5` / `NTE-3` |
| **De-identification Modes** | `redact`, `pseudonymize` (HMAC-SHA256 `TokenVault`), `date-shift` (exact interval preservation) | Same + round-trip Base64 XML preservation | Same + consistent entity tokens |
| **CI/CD & AI Agent Integration** | SARIF 2.1.0, Markdown, JUnit XML, JSON, `--fail-on`, `--min-score`, GitHub Action `@v1`, Stdio MCP Server | Same | Same |
---
## Quick Start
```bash
# Audit files or directories across all profiles (exit 1 if >= high severity)
npx phiguard scan ./fixtures --profile all --fail-on high
# Export SARIF 2.1.0 or Markdown compliance reports
npx phiguard scan ./fixtures --sarif -o phiguard.sarif
npx phiguard scan ./fixtures --markdown --summary-only -o COMPLIANCE.md
# De-identify FHIR R4 Bundle with deterministic date shifting & HMAC-signed TokenVault
npx phiguard clean patient-bundle.json \
--profile hipaa \
--mode date-shift \
--secret "$VAULT_SECRET" \
--vault-out vault.json \
-o patient-bundle.safe.json
# Restore authorized records using the HMAC-verified TokenVault
npx phiguard reidentify patient-bundle.safe.json --vault vault.json --secret "$VAULT_SECRET"
# Clean FHIR Bulk Data ($export .ndjson) preserving cross-line Patient/Encounter references
npx phiguard clean bulk-export.ndjson --profile hipaa --mode pseudonymize -o bulk-export.safe.ndjson
# Clean clinical EHR CSV/TSV exports while preserving numeric vitals/labs & per-patient date intervals
npx phiguard clean patient-cohort.csv --profile hipaa --mode date-shift -o patient-cohort.safe.csv
# Batch-deidentify an entire directory of clinical files sharing a single TokenVault
npx phiguard clean-dir ./clinical-fixtures -o ./clinical-safe --vault-out vault.json
# Validate FHIR R4 structural referential integrity + HIPAA Safe Harbor compliance
npx phiguard validate patient-bundle.safe.json --profile hipaa
# Clean Vietnam MOH QD 4750 BHYT XML (including Base64 NOIDUNGFILE payloads)
npx phiguard clean claim-4750.xml --profile vn-byt --mode pseudonymize -o claim-4750.safe.xml
# Install Git pre-commit (--staged) + Claude Code PreToolUse PHI blocker + .mcp.json
npx phiguard guard install
# Generate a dynamic Shields.io compliance badge for your README
npx phiguard scan ./fixtures --badge
```
---
## Preventative Hooks (`phiguard guard` & `.pre-commit-hooks.yaml`)
Stop unredacted PHI before it enters git history or an AI coding assistant transcript:
- **Pre-commit Framework (`.pre-commit-hooks.yaml`)** — supports `repo: https://github.com/tranhoangtu-it/phiguard` (`phiguard` and `phiguard-hipaa` hooks).
- **Git `pre-commit` Hook** — runs `npx --no-install phiguard scan --staged --fail-on high` automatically on staged clinical fixtures, JSON, HL7, and XML files.
- **Claude Code `PreToolUse` Hook (`phiguard guard-check`)** — evaluates `Read|Edit|Write|MultiEdit|Bash` payloads on stdin and exits with code `2` if high/critical unredacted PHI is present.
- **`.mcp.json` Registration** — configures the stdio `phiguard` MCP server alongside existing servers without overwriting them.
---
## MCP Server (Claude Desktop, Cursor, VS Code, Codex, Gemini CLI)
Expose all 5 local-first guardrails (`phiguard_scan`, `phiguard_deidentify`, `phiguard_reidentify`, `phiguard_validate_fhir`, `phiguard_rules`) over stdio JSON-RPC 2.0:
```json
{
"mcpServers": {
"phiguard": {
"command": "npx",
"args": ["-y", "phiguard", "mcp"]
}
}
}
```
Or manage/install alongside your existing stack via [`mcphub`](https://github.com/tranhoangtu-it/mcphub) and [`mcpman`](https://github.com/tranhoangtu-it/mcpman).
---
## GitHub Action (`@v1`)
```yaml
- name: PHI Compliance Gate
uses: tranhoangtu-it/phiguard@v1
with:
target: "./src"
profile: "hipaa"
fail-on: "high"
sarif-file: "phiguard.sarif"
```
---
## TypeScript / JavaScript SDK (Node & Browser)
```ts
import { deidentify, reidentify, scan, validateFhirHipaa } from "phiguard";
const audit = scan(fhirBundleJson, { profile: "hipaa" });
const { output, vault } = deidentify(fhirBundleJson, {
profile: "hipaa",
mode: "pseudonymize",
secret: "clinic-hmac-key",
});
const validation = validateFhirHipaa(output, "hipaa");
console.log(validation.hipaaCompliant, validation.score); // true, 100
const restored = reidentify(output, vault, "clinic-hmac-key");
```
---
## Related Open-Source Security & Healthcare Tools
- **[`dicomguard`](https://github.com/tranhoangtu-it/dicomguard)** — Zero-dependency binary DICOM Part 10 inspector, NEMA PS3.15 de-identifier, burned-in PHI risk detector & MCP server ([Live Playground](https://tranhoangtu-it.github.io/dicomguard/)).
- **[`clawleak`](https://github.com/tranhoangtu-it/clawleak)** — Find, losslessly redact, and block plaintext API keys/secrets leaked into AI coding agents (`0` deps, 100% offline).
- **[`mcphub`](https://github.com/tranhoangtu-it/mcphub)** — Curated hub & CLI for verified MCP servers.
- **[`mcpman`](https://github.com/tranhoangtu-it/mcpman)** — Universal package manager & security auditor for MCP servers.
---
## License
MIT. See [LICENSE](./LICENSE).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues