privacy-redact 1.0.3 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +137 -217
- package/package.json +2 -1
- package/scripts/copy-assets.mjs +8 -2
- package/src/detect/locales/checksums.js +62 -0
- package/src/detect/locales/compile.js +177 -0
- package/src/detect/locales/createDetector.js +179 -0
- package/src/detect/locales/englishDefaultRules.js +164 -0
- package/src/detect/locales/packs/en.js +23 -0
- package/src/detect/locales/packs/hi.js +33 -0
- package/src/detect/locales/packs/regions.js +174 -0
- package/src/detect/locales/scope.js +24 -0
- package/src/detect/locales/universal.js +59 -0
- package/src/detect/locales/validate.js +78 -0
- package/src/detect/locales/warnings.js +54 -0
- package/src/detect/ocr.js +58 -5
- package/src/detect/ocrFilter.js +31 -0
- package/src/detect/patterns.js +11 -302
- package/src/detect/textRegions.js +57 -19
- package/src/host/assets.js +33 -4
- package/src/index.js +8 -1
- package/src/prefs/languages.js +75 -0
- package/src/review/actions.js +93 -0
- package/src/review/controller.js +308 -73
- package/src/review/element.js +39 -0
- package/src/review/react.js +23 -1
- package/src/review/review.css +69 -43
- package/src/review/template.js +21 -16
package/README.md
CHANGED
|
@@ -1,282 +1,202 @@
|
|
|
1
|
-
<div align="center">
|
|
2
|
-
|
|
3
|
-
<img src="public/favicon.svg" width="84" alt="privacy-redact logo" />
|
|
4
|
-
|
|
5
1
|
# privacy-redact
|
|
6
2
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
**Drop an image or PDF → sensitive info is auto-blurred → click Save.**
|
|
10
|
-
|
|
11
|
-
Cards, emails, phones, addresses, faces, barcodes — gone. Hidden metadata — gone. Nothing ever leaves your device.
|
|
12
|
-
|
|
13
|
-

|
|
14
|
-

|
|
15
|
-
|
|
16
|
-
<img src="docs/before-after.png" alt="Before and after: a fictional pay stub with the SSN, address, email, phone, card, account numbers, face, QR code and barcode blurred" width="100%" />
|
|
17
|
-
|
|
18
|
-
</div>
|
|
19
|
-
|
|
20
|
-
---
|
|
21
|
-
|
|
22
|
-
## Why
|
|
23
|
-
|
|
24
|
-
We paste screenshots into **ChatGPT, Claude, Slack, Discord, GitHub issues and social media** all day. Every one of them can quietly contain a home address, a card number, a face that isn't yours, or a QR code that logs someone in. And the "fix" — drawing black boxes by hand in Paint or Photoshop — is tedious, easy to get wrong, and usually leaves the **EXIF/GPS/camera-serial metadata** untouched.
|
|
3
|
+
[](https://gitcommitshow.github.io/privacy-redact/) [](https://www.npmjs.com/package/privacy-redact)
|
|
25
4
|
|
|
26
|
-
|
|
5
|
+
Detect and redact private data in text, screenshots, and PDFs. It runs locally, and nothing is uploaded.
|
|
27
6
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
> This repository is the library: detection, redaction, and document handling, plus browser components for OCR, faces, barcodes, and PDFs. The page in this repo is a harness for those components, not a brand. A small host that shows each call is in [examples/](examples/README.md).
|
|
33
|
-
|
|
34
|
-
## Screenshots
|
|
7
|
+
```bash
|
|
8
|
+
npm i privacy-redact
|
|
9
|
+
```
|
|
35
10
|
|
|
36
|
-
|
|
37
|
-
| --- | --- |
|
|
38
|
-
| <img src="docs/landing.png" alt="privacy-redact landing page with drop zone" /> | <img src="docs/screenshot-app.png" alt="privacy-redact workspace showing detected items with per-category toggles" /> |
|
|
11
|
+
Requires Node.js 24 or later. Browser components need Chrome or Edge 119+, Firefox 121+, or Safari 17.4+.
|
|
39
12
|
|
|
40
|
-
|
|
13
|
+

|
|
41
14
|
|
|
42
|
-
|
|
15
|
+
## Features
|
|
43
16
|
|
|
44
|
-
|
|
45
|
-
| --- | --- |
|
|
46
|
-
| 🧾 Pay stub (photo, QR, barcode) | <a href="docs/EXAMPLES.md#1--pay-stub--image-with-photo-qr-code-and-barcode"><img src="docs/examples/paystub-comparison.png" width="420" alt="Pay stub before/after"></a> |
|
|
47
|
-
| 💬 Dark-mode Slack chat (keys, password) | <a href="docs/EXAMPLES.md#2--dark-mode-slack-chat--screenshot-with-secrets"><img src="docs/examples/chat-dark-mode-comparison.png" width="420" alt="Chat before/after"></a> |
|
|
48
|
-
| 🪪 Driver's license | <a href="docs/EXAMPLES.md#3--drivers-license--id-card-with-photo-and-barcode"><img src="docs/examples/id-card-comparison.png" width="420" alt="ID card before/after"></a> |
|
|
49
|
-
| 🏥 Medical intake form | <a href="docs/EXAMPLES.md#4--medical-intake-form--labelled-fields-wrapped-lines"><img src="docs/examples/medical-form-comparison.png" width="420" alt="Medical form before/after"></a> |
|
|
50
|
-
| 💳 Checkout page | <a href="docs/EXAMPLES.md#5--checkout-page--card-billing-details-qr"><img src="docs/examples/checkout-comparison.png" width="420" alt="Checkout before/after"></a> |
|
|
51
|
-
| 👨👩👧 Group photo (faces) | <a href="docs/EXAMPLES.md#6--group-photo--faces-only"><img src="docs/examples/group-photo-comparison.png" width="420" alt="Group photo before/after"></a> |
|
|
52
|
-
| 📄 Lease PDF | <a href="docs/EXAMPLES.md#7--lease-pdf-page-2--pdf-with-a-real-text-layer"><img src="docs/examples/lease-pdf-page2-comparison.png" width="420" alt="Lease PDF before/after"></a> |
|
|
17
|
+
Supported media types:
|
|
53
18
|
|
|
54
|
-
|
|
19
|
+
- **Server side** (`privacy-redact`): text, including OCR output from your own engine
|
|
20
|
+
- **Browser side** (`privacy-redact/ui`, `privacy-redact/react`): PNG, JPG, WebP, GIF, BMP, AVIF, and pasted screenshots, plus PDFs. HEIC is not supported
|
|
55
21
|
|
|
56
|
-
|
|
22
|
+
Ways to use it:
|
|
57
23
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
| 🪪 **SSN / national ID** | `123-45-6789`, `SSN: 123456789`, `TIN`, `EIN` | Pattern + invalid-range filtering (`000`, `666`, `9xx`) |
|
|
62
|
-
| ✉️ **Emails** | `jane.doe+work@example.co.uk` | RFC-ish regex |
|
|
63
|
-
| 📞 **Phone numbers** | `(415) 555-2671`, `+1 415 555 2671`, `+44 20 7946 0958` | US + international patterns |
|
|
64
|
-
| 🏠 **Home addresses** | `742 Evergreen Terrace, Apt 4B`, `Springfield, IL 62704`, `PO Box 12`, Canadian & UK postcodes | Street/suffix, City-State-ZIP, PO box rules |
|
|
65
|
-
| 🏦 **Account numbers** | `Account No: 00123456789`, `Routing # 021000021`, `MRN`, `Policy`, IBANs | Label-aware: redacts the *value*, not the label |
|
|
66
|
-
| 🆔 **Aadhaar numbers** | `2341 2341 2346`, `Aadhaar No: 2341-2341-2346` | 12 digits, first digit 2-9, Verhoeff checksum when unlabelled |
|
|
67
|
-
| 🧾 **PAN (India tax ID)** | `ABCPE1234F`, `PAN: ABCPE1234F` | `AAAAA9999A` shape with a valid holder-type letter; labelled values tolerate OCR look-alike swaps |
|
|
68
|
-
| 🎂 **Dates of birth** | `DOB: 04/12/1988`, `Date of Birth March 3, 1990` | Only when labelled – ordinary dates are left alone |
|
|
69
|
-
| 🔑 **API keys & tokens** | `sk-…`, `AKIA…`, `ghp_…`, `xoxb-…`, JWTs, `password: …` | Vendor prefixes + `key=value` |
|
|
70
|
-
| 🙂 **Faces** | Photos, ID pictures, group shots | SSD-MobileNet via TensorFlow.js (tiled scan for small faces) |
|
|
71
|
-
| ▦ **Barcodes & QR** | QR, Code 128/39, EAN/UPC, PDF417, Data Matrix, Aztec | Native `BarcodeDetector` if present, else ZXing with multi-scale retry |
|
|
24
|
+
- **Server only:** call `redactDocument` for text. No browser code is needed.
|
|
25
|
+
- **Server for text, React for the UI:** use `redactDocument` on the server, and mount `PrivacyRedactReview` only where images and PDFs are handled.
|
|
26
|
+
- **Browser only:** the text entry is plain JavaScript and also runs in the page. The review component runs decoding, OCR, face and barcode detection, review, and save in the page, so no server is required.
|
|
72
27
|
|
|
73
|
-
|
|
28
|
+
## Quick start
|
|
74
29
|
|
|
75
|
-
|
|
30
|
+
### Text
|
|
76
31
|
|
|
77
|
-
-
|
|
78
|
-
- 📷 Camera / phone model, 🔢 body & lens **serial numbers**
|
|
79
|
-
- 🕒 Original capture & edit dates
|
|
80
|
-
- 👤 Owner / author / copyright, 🛠️ editing software
|
|
81
|
-
- 📄 PDF title, author, producer, keywords, dates
|
|
82
|
-
- XMP / IPTC blocks, embedded thumbnails, ICC profiles
|
|
32
|
+
`privacy-redact` has no browser dependencies, so it runs in a server, a script, or a worker.
|
|
83
33
|
|
|
84
|
-
|
|
34
|
+
```js
|
|
35
|
+
import { redactDocument } from 'privacy-redact';
|
|
85
36
|
|
|
86
|
-
|
|
37
|
+
const { text, findings } = redactDocument(
|
|
38
|
+
'Card 4242 4242 4242 4242, contact jane@example.com',
|
|
39
|
+
);
|
|
87
40
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
- **Blackout** – maximum-paranoia solid fill
|
|
91
|
-
- **Strength slider**, per-category switches, click-to-toggle on any box, drag-to-add manual boxes
|
|
92
|
-
- **Hold to compare** with the original · multi-page PDFs with page navigation
|
|
41
|
+
console.log(text);
|
|
42
|
+
// The card number and the email are replaced with bullet characters.
|
|
93
43
|
|
|
94
|
-
|
|
44
|
+
console.log(findings);
|
|
45
|
+
// [{ type: 'card', label: 'Credit cards', count: 1 }, { type: 'email', label: 'Emails', count: 1 }]
|
|
46
|
+
```
|
|
95
47
|
|
|
96
|
-
|
|
97
|
-
- Models, WASM and fonts are **bundled** – no CDN calls at runtime. Works fully offline after first load.
|
|
98
|
-
- Production builds ship a strict **Content-Security-Policy** (`connect-src 'self'`, `form-action 'none'`, `object-src 'none'`) so the page is *technically unable* to send your file anywhere.
|
|
99
|
-
- The end-to-end test fails if the app makes even one request to another origin.
|
|
100
|
-
- No analytics, no cookies, no accounts, no telemetry.
|
|
48
|
+
`findings` gives a count per category. The matched values are not returned.
|
|
101
49
|
|
|
102
|
-
|
|
50
|
+
To check only some categories, pass a list. Leave it out to use every text detector the library ships.
|
|
103
51
|
|
|
104
|
-
|
|
52
|
+
```js
|
|
53
|
+
import { redactDocument, TEXT_TYPES } from 'privacy-redact';
|
|
105
54
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
npm run dev # → http://localhost:5173
|
|
55
|
+
redactDocument(text, ['email', 'phone']);
|
|
56
|
+
console.log(TEXT_TYPES); // every category the default run checks
|
|
109
57
|
```
|
|
110
58
|
|
|
111
|
-
|
|
59
|
+
For Hindi documents, build a detector that includes the `hi` pack. OCR then loads `eng+hin`, and Devanagari labels and digits are checked:
|
|
112
60
|
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
|
|
61
|
+
```js
|
|
62
|
+
import { createDetector } from 'privacy-redact';
|
|
63
|
+
|
|
64
|
+
const detector = createDetector({ regions: ['IN'], languages: ['en', 'hi'] });
|
|
65
|
+
const { text, findings, warnings } = detector.redactDocument(hindiFormText);
|
|
116
66
|
```
|
|
117
67
|
|
|
118
|
-
|
|
68
|
+
On the review element, set `languages="en hi"` (or the React `languages={['en', 'hi']}` prop). Hindi is opt-in so English-only hosts keep the smaller default OCR load.
|
|
119
69
|
|
|
120
|
-
###
|
|
70
|
+
### Images and PDFs (React)
|
|
121
71
|
|
|
122
|
-
|
|
72
|
+
```jsx
|
|
73
|
+
import { PrivacyRedactReview } from 'privacy-redact/react';
|
|
123
74
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
75
|
+
export function Attachment({ file, onSave }) {
|
|
76
|
+
return (
|
|
77
|
+
<PrivacyRedactReview
|
|
78
|
+
file={file}
|
|
79
|
+
style="blur"
|
|
80
|
+
strength={0.65}
|
|
81
|
+
onResult={onSave}
|
|
82
|
+
/>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
127
85
|
```
|
|
128
86
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
D --> E[Word boxes → pixel rects]
|
|
141
|
-
C2 --> F[Regions]
|
|
142
|
-
C3 --> F
|
|
143
|
-
E --> F
|
|
144
|
-
F --> G[Live preview<br/>blur / pixelate / blackout]
|
|
145
|
-
G --> H[Export from raw pixels<br/>PNG · JPG · flattened PDF]
|
|
146
|
-
H --> I[Re-open & verify<br/>0 metadata fields]
|
|
87
|
+
`file` is optional. Without it, the person drops a file into the review screen. `style` is `blur`, `pixelate`, or `blackout`. `strength` runs from 0 to 1.
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
function onSave({ file, filename, findings, clean, truncated, transcript }) {
|
|
91
|
+
// file: Blob of the redacted image or flattened PDF
|
|
92
|
+
// filename: the original name plus "-redacted"
|
|
93
|
+
// findings: counts per category that are still turned on
|
|
94
|
+
// clean: true when the saved file re-opened with no metadata left
|
|
95
|
+
// truncated: true when a PDF had more than 60 pages
|
|
96
|
+
// transcript: the redacted text read from the document
|
|
97
|
+
}
|
|
147
98
|
```
|
|
148
99
|
|
|
149
|
-
|
|
150
|
-
2. **Detect.** OCR yields words with bounding boxes. Screenshots are upscaled and dark themes inverted first to help the OCR. Each line is scanned with the rule set in [`src/detect/patterns.js`](src/detect/patterns.js); matches are mapped back to pixels — even partial words like `Email:jane@x.com` are trimmed to just the address.
|
|
151
|
-
3. **Review.** Every detection is a box you can toggle. Categories can be switched off wholesale.
|
|
152
|
-
4. **Export.** The redacted canvas is re-encoded from scratch and re-checked.
|
|
153
|
-
|
|
154
|
-
## 💥 How redaction stays irreversible
|
|
100
|
+
`onResult` runs only when the person clicks Save. Dropping a file or finishing a scan does not call it, so the host decides when the file leaves the page.
|
|
155
101
|
|
|
156
|
-
|
|
102
|
+
### Images and PDFs (plain HTML)
|
|
157
103
|
|
|
158
|
-
|
|
159
|
-
-
|
|
160
|
-
- **PDFs are flattened.** Each page is rasterised, redacted and re-embedded as an image, so the text layer beneath a redaction no longer exists. (The trade-off: exported PDFs aren't text-selectable.)
|
|
161
|
-
- **Nothing is copied byte-for-byte from your source file**, so EXIF, XMP, IPTC, thumbnails and PDF info dictionaries can't tag along. PDFs are written with metadata auto-stamping disabled (no `Producer`, no dates).
|
|
162
|
-
- The e2e suite **OCRs the exported file** and asserts that the SSN, email, phone, card, address and account numbers of the sample document are no longer readable.
|
|
104
|
+
```html
|
|
105
|
+
<privacy-redact-review redact-style="pixelate" strength="0.8"></privacy-redact-review>
|
|
163
106
|
|
|
164
|
-
>
|
|
107
|
+
<script type="module">
|
|
108
|
+
import 'privacy-redact/ui';
|
|
165
109
|
|
|
166
|
-
|
|
110
|
+
document.querySelector('privacy-redact-review')
|
|
111
|
+
.addEventListener('result', (event) => console.log(event.detail.filename));
|
|
112
|
+
</script>
|
|
113
|
+
```
|
|
167
114
|
|
|
168
|
-
|
|
115
|
+
### Browser setup (Vite)
|
|
169
116
|
|
|
170
|
-
|
|
171
|
-
- **OCR isn't perfect.** Tiny fonts, low contrast, unusual fonts, handwriting, rotated or curved text can be missed. A digit misread by OCR can still leave a card *pattern* undetected if it's badly broken up.
|
|
172
|
-
- **English-first.** Address rules are US/Canada/UK oriented; phone rules include international formats; OCR uses the English model.
|
|
173
|
-
- **Faces:** heavily rotated, occluded or tiny faces can be missed; non-faces occasionally trigger. Wide images are scanned in overlapping tiles so small faces are found, at the cost of speed. Toggle boxes to correct.
|
|
174
|
-
- **Barcodes:** without the native `BarcodeDetector` API (Chrome on Windows/Linux, Firefox), 1D codes are located via scan-line heuristics and a structure check that rejects text mistaken for bars. Damaged or very small codes can be missed.
|
|
175
|
-
- **Not detected by design:** license plates, signatures, logos, company names, free-form medical text.
|
|
176
|
-
- **HEIC** photos aren't decodable by browsers – convert to JPG/PNG first.
|
|
177
|
-
- **PDFs** are limited to 60 pages and become image-only.
|
|
178
|
-
- **Performance:** first scan loads ~15 MB of models. Face detection uses WebGL when available; on machines without GPU acceleration it falls back to CPU and can take a while on big images.
|
|
179
|
-
- Filenames aren't scrubbed beyond adding `-redacted`. If your filename contains your name, rename it.
|
|
117
|
+
The review screen reads its OCR, face, and PDF files from `/vendor`. The Vite plugin serves them in development and copies them into the build.
|
|
180
118
|
|
|
181
|
-
|
|
119
|
+
```js
|
|
120
|
+
// vite.config.js
|
|
121
|
+
import { defineConfig } from 'vite';
|
|
122
|
+
import { privacyRedact } from 'privacy-redact/vite';
|
|
182
123
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
├── src/
|
|
187
|
-
│ ├── main.js # UI controller (state, scanning pipeline, export)
|
|
188
|
-
│ ├── style.css
|
|
189
|
-
│ ├── detect/
|
|
190
|
-
│ │ ├── patterns.js # 🧠 the rule engine (pure JS, unit-tested)
|
|
191
|
-
│ │ ├── textRegions.js # OCR words ↔ character ranges ↔ pixel rects, wrapped-line + neighbour merging (pure, unit-tested)
|
|
192
|
-
│ │ ├── ocr.js # Tesseract.js worker wrapper (+ pre-processing)
|
|
193
|
-
│ │ ├── faces.js # TensorFlow.js face detection with tiling + NMS
|
|
194
|
-
│ │ └── barcodes.js # BarcodeDetector / ZXing multi-scale fallback
|
|
195
|
-
│ ├── render/effects.js # blur · pixelate · blackout
|
|
196
|
-
│ └── io/
|
|
197
|
-
│ ├── loader.js # image + PDF → canvases
|
|
198
|
-
│ ├── metadata.js # what's hidden in the file + post-export verification
|
|
199
|
-
│ └── exporter.js # PNG / JPG / flattened PDF writers
|
|
200
|
-
├── scripts/
|
|
201
|
-
│ ├── copy-assets.mjs # vendors OCR/face/PDF assets into public/vendor (postinstall)
|
|
202
|
-
│ ├── make-samples.mjs # generates the fictional sample docs
|
|
203
|
-
│ └── make-before-after.mjs # README hero image
|
|
204
|
-
├── public/samples/ # fictional pay stub, chat, ID, medical form, checkout, group photo, GPS JPG, lease PDF
|
|
205
|
-
├── docs/EXAMPLES.md # before/after gallery (docs/examples/*)
|
|
206
|
-
├── test/ # node --test unit tests (*.test.js)
|
|
207
|
-
│ └── smoke.e2e.test.js # Playwright end-to-end test incl. OCR-of-output check (npm run test:e2e)
|
|
208
|
-
├── desktop/ # optional Electron wrapper
|
|
209
|
-
└── .github/workflows/ # CI + GitHub Pages deploy
|
|
124
|
+
export default defineConfig({
|
|
125
|
+
plugins: [privacyRedact()],
|
|
126
|
+
});
|
|
210
127
|
```
|
|
211
128
|
|
|
212
|
-
|
|
129
|
+
A postinstall step copies those files into the package. Installs that skip install scripts (`npm --ignore-scripts`, or pnpm without an allow-list) leave them out, and scans then fail with "Text reading failed".
|
|
213
130
|
|
|
214
|
-
|
|
215
|
-
npm test # unit tests (rules, box mapping, metadata, redaction cell size) — under a second
|
|
216
|
-
npm run test:e2e:setup # one-time: download Chromium for Playwright
|
|
217
|
-
npm run build && npm run preview & # then:
|
|
218
|
-
npm run test:e2e -- http://localhost:4173/ # full pipeline in headless Chromium
|
|
219
|
-
npm run test:e2e:ci # all of the above in one go: install Chromium, build, serve, test (used by the manual E2E workflow)
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
The e2e test drives the real UI against the **production build** (so the CSP is enforced) and asserts:
|
|
131
|
+
## API
|
|
223
132
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
-
|
|
227
|
-
|
|
228
|
-
-
|
|
133
|
+
| Import | Use |
|
|
134
|
+
| --- | --- |
|
|
135
|
+
| `privacy-redact` | Text detection and redaction. Node-safe. Exports `redactDocument`, `redactPlainText`, `findSensitive`, `TEXT_TYPES`, and `CATEGORIES`. |
|
|
136
|
+
| `privacy-redact/react` | `PrivacyRedactReview` React component. Needs React 18 or later. |
|
|
137
|
+
| `privacy-redact/ui` | The `privacy-redact-review` custom element, for any framework or none. |
|
|
138
|
+
| `privacy-redact/vite` | `privacyRedact()` Vite plugin that serves the OCR, face, and PDF files. |
|
|
229
139
|
|
|
230
|
-
|
|
140
|
+
## What it detects
|
|
231
141
|
|
|
232
|
-
|
|
142
|
+
| Category | Examples |
|
|
143
|
+
| --- | --- |
|
|
144
|
+
| Credit cards | `4242 4242 4242 4242` (Luhn checked) |
|
|
145
|
+
| SSN / national ID | `123-45-6789` |
|
|
146
|
+
| Emails | `jane.doe+work@example.co.uk` |
|
|
147
|
+
| Phone numbers | `(415) 555-2671`, `+44 20 7946 0958` |
|
|
148
|
+
| Home addresses | `742 Evergreen Terrace, Apt 4B`, `Springfield, IL 62704` |
|
|
149
|
+
| Account numbers | `Account No: 00123456789`, `Routing # 021000021`, IBANs |
|
|
150
|
+
| Aadhaar and PAN | `2341 2341 2346`, `ABCPE1234F` |
|
|
151
|
+
| Dates of birth | `DOB: 04/12/1988` (labeled dates only) |
|
|
152
|
+
| API keys and tokens | `sk-...`, `AKIA...`, JWTs, `password: ...` |
|
|
233
153
|
|
|
234
|
-
|
|
235
|
-
- [ ] Batch mode – drop a folder, get a folder
|
|
236
|
-
- [ ] Watch-folder / clipboard-monitor mode for the desktop app
|
|
237
|
-
- [ ] More locales (EU addresses & national IDs, IBAN validation, non-Latin OCR packs)
|
|
238
|
-
- [ ] License-plate & signature detectors
|
|
239
|
-
- [ ] "Keep text selectable" PDF mode using true content-stream redaction
|
|
240
|
-
- [ ] Browser extension: right-click any image → privacy-redact
|
|
241
|
-
- [ ] Installable PWA (a web manifest ships already; offline service worker still to do)
|
|
242
|
-
- [ ] Custom rules (bring your own regex / keywords, e.g. an employee-ID format)
|
|
154
|
+
For images and PDFs, the scan also finds faces, barcodes, and QR codes. More before and after examples are in [docs/EXAMPLES.md](docs/EXAMPLES.md).
|
|
243
155
|
|
|
244
|
-
##
|
|
156
|
+
## How redaction works
|
|
245
157
|
|
|
246
|
-
|
|
158
|
+
- Images are decoded, read with OCR (Tesseract.js), and scanned for faces (face-api) and codes (ZXing). The text goes through the same rules as `redactDocument`.
|
|
159
|
+
- Blur and pixelate average pixels into coarse blocks before any smoothing, so the original detail is discarded. Blackout paints a solid fill.
|
|
160
|
+
- The output is a new set of pixels. Nothing is layered under a box.
|
|
161
|
+
- Nothing is copied from the source file, so EXIF, GPS, XMP, IPTC, thumbnails, and PDF info do not carry over. The saved file is re-opened and checked, and the result reports that as `clean`.
|
|
162
|
+
- PDFs are flattened to images, so the text under a box is gone. The trade-off is that the output is not text-selectable.
|
|
247
163
|
|
|
248
|
-
|
|
164
|
+
## Limitations
|
|
249
165
|
|
|
250
|
-
|
|
166
|
+
privacy-redact is a safety net, not a guarantee. Review the preview before sharing.
|
|
251
167
|
|
|
252
|
-
|
|
168
|
+
- Names, company names, signatures, logos, and license plates are not detected.
|
|
169
|
+
- Detection defaults to English. Address rules cover US, Canada, UK, and India. For Hindi (Devanagari) documents, pass `languages: ['en', 'hi']` to `createDetector` or set `languages="en hi"` on the review element so OCR loads `eng+hin` and Hindi labels are checked.
|
|
170
|
+
- OCR can miss tiny, low-contrast, rotated, or handwritten text. Small or heavily rotated faces can be missed too.
|
|
171
|
+
- HEIC images cannot be decoded in browsers. Convert them to JPG or PNG first.
|
|
172
|
+
- PDFs are limited to 60 pages and are exported as images.
|
|
173
|
+
- The first OCR or face scan loads about 15 MB of models.
|
|
253
174
|
|
|
254
|
-
|
|
175
|
+
## Privacy
|
|
255
176
|
|
|
256
|
-
|
|
177
|
+
- The text API is plain JavaScript and makes no network calls.
|
|
178
|
+
- In the browser, the OCR, face, and PDF files ship with the package and are served from the host's own origin. A scan does not contact another server.
|
|
179
|
+
- The original file stays with the host. The package has no telemetry, analytics, or accounts.
|
|
257
180
|
|
|
258
|
-
|
|
181
|
+
## Roadmap
|
|
259
182
|
|
|
260
|
-
|
|
183
|
+
- Command-line tool that scans a local file and writes a draft for review
|
|
184
|
+
- MCP wrapper around that command, returning counts rather than file contents
|
|
185
|
+
- Text-only install without the OCR, face, and PDF files, for smaller installs
|
|
186
|
+
- Custom detection rules passed in by the host
|
|
187
|
+
- Optional on-device name and organization detection
|
|
188
|
+
- More locales: EU addresses and national IDs, IBAN validation, non-Latin OCR
|
|
189
|
+
- License plate and signature detectors
|
|
190
|
+
- PDF output that keeps text selectable
|
|
261
191
|
|
|
262
|
-
|
|
192
|
+
## Contributing
|
|
263
193
|
|
|
264
|
-
|
|
194
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for setup, tests, and how to add a detection rule. To report a way to recover redacted content or leak data, see [SECURITY.md](SECURITY.md). Use a private report, and only share fictional documents.
|
|
265
195
|
|
|
266
|
-
|
|
267
|
-
| --- | --- | --- |
|
|
268
|
-
| [Tesseract OCR](https://github.com/tesseract-ocr/tesseract) | Recognizes text. The English traineddata vendored with the page is Tesseract language data. | Apache-2.0 |
|
|
269
|
-
| [Tesseract.js](https://github.com/naptha/tesseract.js) and [tesseract.js-core](https://github.com/naptha/tesseract.js-core) | Runs that engine in the browser and in Node, from the vendored worker and WASM. | Apache-2.0 |
|
|
270
|
-
| [TensorFlow](https://github.com/tensorflow/tensorflow) and [TensorFlow.js](https://github.com/tensorflow/tfjs) | Runs the face model. TensorFlow.js is bundled inside face-api and is not loaded from the network. | Apache-2.0 |
|
|
271
|
-
| [face-api.js](https://github.com/justadudewhohacks/face-api.js), continued as [@vladmandic/face-api](https://github.com/vladmandic/face-api) | Finds faces with the SSD MobileNet v1 model shipped in that package. | MIT |
|
|
272
|
-
| [ZXing](https://github.com/zxing/zxing) and [@zxing/library](https://github.com/zxing-js/library) | Decodes barcodes and QR codes when the browser has no `BarcodeDetector`. | Apache-2.0, MIT for the JavaScript port |
|
|
273
|
-
| [PDF.js](https://github.com/mozilla/pdf.js) | Reads and rasterizes PDFs. | Apache-2.0 |
|
|
274
|
-
| [pdf-lib](https://github.com/Hopding/pdf-lib) | Writes the flattened PDF. | MIT |
|
|
275
|
-
| [exifr](https://github.com/MikeKovarik/exifr) | Reads image metadata so the export can be checked. | MIT |
|
|
196
|
+
## Credits
|
|
276
197
|
|
|
277
|
-
|
|
198
|
+
Started from [Hushshot](https://github.com/SYasJ/hushshot) by SYasJ (MIT). Built on [Tesseract.js](https://github.com/naptha/tesseract.js), [face-api](https://github.com/vladmandic/face-api), [ZXing](https://github.com/zxing-js/library), [PDF.js](https://github.com/mozilla/pdf.js), [pdf-lib](https://github.com/Hopding/pdf-lib), and [exifr](https://github.com/MikeKovarik/exifr). Each keeps its own license.
|
|
278
199
|
|
|
279
200
|
## License
|
|
280
201
|
|
|
281
|
-
|
|
282
|
-
|
|
202
|
+
Apache-2.0. See [LICENSE](LICENSE).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "privacy-redact",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Library for detecting private content, redacting it, and handling document metadata. Browser components cover OCR, faces, barcodes, and PDF pages.",
|
|
6
6
|
"exports": {
|
|
@@ -67,6 +67,7 @@
|
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
69
|
"@tesseract.js-data/eng": "^1.0.0",
|
|
70
|
+
"@tesseract.js-data/hin": "^1.0.0",
|
|
70
71
|
"@vladmandic/face-api": "^1.7.15",
|
|
71
72
|
"@zxing/library": "^0.23.0",
|
|
72
73
|
"exifr": "^7.1.3",
|
package/scripts/copy-assets.mjs
CHANGED
|
@@ -4,6 +4,7 @@ import { cpSync, mkdirSync, existsSync, readdirSync } from 'node:fs';
|
|
|
4
4
|
import { dirname, join } from 'node:path';
|
|
5
5
|
import { fileURLToPath } from 'node:url';
|
|
6
6
|
import { OCR_CORE_FILES } from '../src/detect/ocrCore.js';
|
|
7
|
+
import { SHIPPED_OCR_CODES } from '../src/detect/locales/scope.js';
|
|
7
8
|
|
|
8
9
|
const root = join(dirname(fileURLToPath(import.meta.url)), '..');
|
|
9
10
|
/**
|
|
@@ -30,12 +31,17 @@ function copy(src, dest) {
|
|
|
30
31
|
cpSync(src, dest, { recursive: true });
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
// Tesseract OCR: worker + every LSTM-only WASM core the OCR module can pick +
|
|
34
|
+
// Tesseract OCR: worker + every LSTM-only WASM core the OCR module can pick + shipped language models
|
|
34
35
|
copy(nm('tesseract.js/dist/worker.min.js'), out('tesseract/worker.min.js'));
|
|
35
36
|
for (const f of Object.values(OCR_CORE_FILES)) {
|
|
36
37
|
copy(nm(`tesseract.js-core/${f}`), out(`tesseract/${f}`));
|
|
37
38
|
}
|
|
38
|
-
|
|
39
|
+
for (const code of SHIPPED_OCR_CODES) {
|
|
40
|
+
copy(
|
|
41
|
+
nm(`@tesseract.js-data/${code}/4.0.0_best_int/${code}.traineddata.gz`),
|
|
42
|
+
out(`tesseract/${code}.traineddata.gz`),
|
|
43
|
+
);
|
|
44
|
+
}
|
|
39
45
|
|
|
40
46
|
// Face detection model (SSD MobileNet v1)
|
|
41
47
|
const faceModelDir = nm('@vladmandic/face-api/model');
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checksums and PAN shape helpers shared by region packs and the public patterns API.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/** Luhn checksum – used to tell real card numbers from random digit runs. */
|
|
6
|
+
export function luhn(digits) {
|
|
7
|
+
let sum = 0;
|
|
8
|
+
let alt = false;
|
|
9
|
+
for (let i = digits.length - 1; i >= 0; i--) {
|
|
10
|
+
let n = digits.charCodeAt(i) - 48;
|
|
11
|
+
if (n < 0 || n > 9) return false;
|
|
12
|
+
if (alt) { n *= 2; if (n > 9) n -= 9; }
|
|
13
|
+
sum += n;
|
|
14
|
+
alt = !alt;
|
|
15
|
+
}
|
|
16
|
+
return digits.length > 0 && sum % 10 === 0;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const VERHOEFF_D = [
|
|
20
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 0, 6, 7, 8, 9, 5], [2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
|
|
21
|
+
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7], [4, 0, 1, 2, 3, 9, 5, 6, 7, 8], [5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
|
|
22
|
+
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2], [7, 6, 5, 9, 8, 2, 1, 0, 4, 3], [8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
|
|
23
|
+
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
|
|
24
|
+
];
|
|
25
|
+
const VERHOEFF_P = [
|
|
26
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 5, 7, 6, 2, 8, 3, 0, 9, 4], [5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
|
|
27
|
+
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7], [9, 4, 5, 3, 1, 2, 6, 8, 7, 0], [4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
|
|
28
|
+
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5], [7, 0, 4, 6, 9, 1, 3, 2, 5, 8],
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
/** Verhoeff checksum - used to tell real Aadhaar numbers from random 12-digit runs. */
|
|
32
|
+
export function verhoeff(digits) {
|
|
33
|
+
let c = 0;
|
|
34
|
+
for (let i = 0; i < digits.length; i++) {
|
|
35
|
+
const n = digits.charCodeAt(digits.length - 1 - i) - 48;
|
|
36
|
+
if (n < 0 || n > 9) return false;
|
|
37
|
+
c = VERHOEFF_D[c][VERHOEFF_P[i % 8][n]];
|
|
38
|
+
}
|
|
39
|
+
return digits.length > 0 && c === 0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const PAN_DIGIT_TO_LETTER = { 0: 'O', 1: 'I', 2: 'Z', 5: 'S', 6: 'G', 8: 'B' };
|
|
43
|
+
export const PAN_LETTER_TO_DIGIT = { O: '0', D: '0', I: '1', L: '1', Z: '2', S: '5', G: '6', B: '8' };
|
|
44
|
+
export const PAN_TRAILING_NOISE = '|])!/\\';
|
|
45
|
+
export const PAN_HOLDER_TYPES = 'PCHFATBLJG';
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* True if a 10-char value is a PAN once common OCR look-alike swaps are undone.
|
|
49
|
+
* `minExact` is how many characters must already be the right kind without any repair.
|
|
50
|
+
*/
|
|
51
|
+
export function looksLikePan(value, minExact = 0) {
|
|
52
|
+
if (value.length !== 10) return false;
|
|
53
|
+
let exact = 0;
|
|
54
|
+
for (let i = 0; i < 10; i++) {
|
|
55
|
+
const c = value[i];
|
|
56
|
+
const wantLetter = i < 5 || i === 9;
|
|
57
|
+
if (wantLetter ? (c >= 'A' && c <= 'Z') : (c >= '0' && c <= '9')) { exact++; continue; }
|
|
58
|
+
const repaired = wantLetter ? PAN_DIGIT_TO_LETTER[c] : PAN_LETTER_TO_DIGIT[c];
|
|
59
|
+
if (!repaired && !(i === 9 && PAN_TRAILING_NOISE.includes(c))) return false;
|
|
60
|
+
}
|
|
61
|
+
return exact >= minExact;
|
|
62
|
+
}
|