blackbar-redact 0.1.2__tar.gz
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.
- blackbar_redact-0.1.2/LICENSE +21 -0
- blackbar_redact-0.1.2/PKG-INFO +479 -0
- blackbar_redact-0.1.2/README.md +457 -0
- blackbar_redact-0.1.2/pyproject.toml +37 -0
- blackbar_redact-0.1.2/setup.cfg +4 -0
- blackbar_redact-0.1.2/src/blackbar/__init__.py +36 -0
- blackbar_redact-0.1.2/src/blackbar/__main__.py +6 -0
- blackbar_redact-0.1.2/src/blackbar/checks.py +133 -0
- blackbar_redact-0.1.2/src/blackbar/cli.py +270 -0
- blackbar_redact-0.1.2/src/blackbar/core.py +202 -0
- blackbar_redact-0.1.2/src/blackbar/detectors.py +954 -0
- blackbar_redact-0.1.2/src/blackbar/normalize.py +104 -0
- blackbar_redact-0.1.2/src/blackbar/vault.py +279 -0
- blackbar_redact-0.1.2/src/blackbar_redact.egg-info/PKG-INFO +479 -0
- blackbar_redact-0.1.2/src/blackbar_redact.egg-info/SOURCES.txt +26 -0
- blackbar_redact-0.1.2/src/blackbar_redact.egg-info/dependency_links.txt +1 -0
- blackbar_redact-0.1.2/src/blackbar_redact.egg-info/entry_points.txt +2 -0
- blackbar_redact-0.1.2/src/blackbar_redact.egg-info/top_level.txt +1 -0
- blackbar_redact-0.1.2/tests/test_bench.py +88 -0
- blackbar_redact-0.1.2/tests/test_cli.py +285 -0
- blackbar_redact-0.1.2/tests/test_core.py +274 -0
- blackbar_redact-0.1.2/tests/test_detectors.py +327 -0
- blackbar_redact-0.1.2/tests/test_phone.py +145 -0
- blackbar_redact-0.1.2/tests/test_readme.py +132 -0
- blackbar_redact-0.1.2/tests/test_roundtrip.py +145 -0
- blackbar_redact-0.1.2/tests/test_secrets.py +192 -0
- blackbar_redact-0.1.2/tests/test_terms.py +212 -0
- blackbar_redact-0.1.2/tests/test_vault.py +231 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 blackbar contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: blackbar-redact
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: Reversible redaction of sensitive values in text: mask before you paste, unmask the answer.
|
|
5
|
+
Author: blackbar contributors
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/RealAoss/blackbar
|
|
8
|
+
Project-URL: Issues, https://github.com/RealAoss/blackbar/issues
|
|
9
|
+
Keywords: redaction,privacy,pii,anonymization,secrets,llm
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Topic :: Security
|
|
17
|
+
Classifier: Topic :: Text Processing :: Filters
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# blackbar
|
|
24
|
+
|
|
25
|
+
Reversible redaction of sensitive values in text.
|
|
26
|
+
|
|
27
|
+
Mask email addresses, phone numbers, keys and other values before you paste a
|
|
28
|
+
text into an AI assistant, a ticket or a chat. Keep the mapping in a local
|
|
29
|
+
vault. Put the real values back into the answer.
|
|
30
|
+
|
|
31
|
+
> **blackbar does not detect names of people.** It has no language model. It
|
|
32
|
+
> is a set of regular expressions and checksums. A name, a company or a
|
|
33
|
+
> project is masked only if you list it yourself (`terms`). It does not
|
|
34
|
+
> promise to find everything else either. Read the masked text before you
|
|
35
|
+
> send it. See [What it does not do](#what-it-does-not-do).
|
|
36
|
+
|
|
37
|
+
Python 3.10 or newer, standard library only, no network access.
|
|
38
|
+
|
|
39
|
+
## The problem
|
|
40
|
+
|
|
41
|
+
Asking an assistant about a customer ticket, a log or a config file is useful.
|
|
42
|
+
Pasting the ticket also hands over the customer's email address, their phone
|
|
43
|
+
number, the server's IP address and sometimes a password. Deleting those by
|
|
44
|
+
hand is slow, and afterwards the answer talks about "the customer" and you
|
|
45
|
+
have to put everything back yourself.
|
|
46
|
+
|
|
47
|
+
blackbar replaces each value by a stable placeholder such as `[EMAIL_1]`, so
|
|
48
|
+
the text keeps its meaning ("the same address appears twice"), and restores
|
|
49
|
+
the values in whatever comes back, even if the assistant changed the
|
|
50
|
+
placeholder's case or wrapped it in Markdown.
|
|
51
|
+
|
|
52
|
+
## Example: mask, ask, unmask
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
>>> from blackbar import mask, unmask
|
|
56
|
+
>>> ticket = """Customer Maria Example (maria@example.com, +1 202 555 0164) cannot log in.
|
|
57
|
+
... She connects from 198.51.100.23 and her temporary password: Tmp-Example-553
|
|
58
|
+
... Logs are in C:\\Users\\mexample\\AppData\\Local\\acme\\app.log"""
|
|
59
|
+
>>> m = mask(ticket, terms=["Maria Example", "acme"])
|
|
60
|
+
>>> print(m.text)
|
|
61
|
+
Customer [TERM_1] ([EMAIL_1], [PHONE_1]) cannot log in.
|
|
62
|
+
She connects from [IP_1] and her temporary password: [SECRET_1]
|
|
63
|
+
Logs are in C:\Users\[USER_1]\AppData\Local\[TERM_2]\app.log
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
`m.text` is what you paste. Without `terms=[...]` the name and the company
|
|
68
|
+
would have stayed in the text. The assistant answers with placeholders, not
|
|
69
|
+
always spelled the way it received them:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
>>> answer = "Tell **[TERM_1]** to reset it. [email_1]'s account and [PHONE 1] look fine; ignore [EMAIL_9]."
|
|
73
|
+
>>> print(unmask(answer, m.vault))
|
|
74
|
+
Tell **Maria Example** to reset it. maria@example.com's account and +1 202 555 0164 look fine; ignore [EMAIL_9].
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
`[EMAIL_9]` is not in the vault, so it is left alone. `unmask_report` tells
|
|
79
|
+
you about it:
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
>>> from blackbar import unmask_report
|
|
83
|
+
>>> unmask_report(answer, m.vault).unknown
|
|
84
|
+
['[EMAIL_9]']
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Install
|
|
89
|
+
|
|
90
|
+
```sh
|
|
91
|
+
pip install git+https://github.com/RealAoss/blackbar
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
blackbar is not published on a package index; the line above installs it from
|
|
95
|
+
the repository. It has no dependencies. You can also copy `src/blackbar` into
|
|
96
|
+
a project.
|
|
97
|
+
|
|
98
|
+
## Python API
|
|
99
|
+
|
|
100
|
+
### `mask(text, *, kinds=None, terms=None, vault=None) -> Masked`
|
|
101
|
+
|
|
102
|
+
- `kinds`: which detectors to run, by the names shown by `blackbar kinds`.
|
|
103
|
+
`None` is the default set. The words `"default"` and `"all"` are accepted,
|
|
104
|
+
so `kinds=["default", "uuid"]` adds one.
|
|
105
|
+
- `terms`: literal strings to mask. Matched case-insensitively on word
|
|
106
|
+
boundaries, in any script. For Arabic terms the prefix clitics و ف ب ل ك
|
|
107
|
+
and the article ال may be attached and stay outside the placeholder
|
|
108
|
+
(`arabic_clitics=False` turns this off).
|
|
109
|
+
- `vault`: pass the same `Vault` again to keep placeholders stable across
|
|
110
|
+
calls.
|
|
111
|
+
- `min_confidence`: drop findings below a confidence (0 to 1, default 0).
|
|
112
|
+
|
|
113
|
+
The result has `.text`, `.vault`, and `.findings`, a list of `Finding` with
|
|
114
|
+
`kind`, `start`, `end`, `span`, `confidence` and `placeholder`:
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
>>> [(f.kind, f.span, f.confidence) for f in m.findings][:3]
|
|
118
|
+
[('term', (9, 22), 1.0), ('email', (24, 41), 0.95), ('phone', (43, 58), 0.9)]
|
|
119
|
+
>>> m.counts
|
|
120
|
+
{'term': 2, 'email': 1, 'phone': 1, 'ip': 1, 'secret': 1, 'user': 1}
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
The same value gets the same placeholder, also when it is spelled slightly
|
|
125
|
+
differently: email letter case, phone numbers with other spacing, dashes or
|
|
126
|
+
Arabic-Indic digits, IBANs with or without spaces, card numbers with or
|
|
127
|
+
without separators. Different values never share a placeholder.
|
|
128
|
+
|
|
129
|
+
Exactly these spellings are merged, after Unicode NFC (canonical
|
|
130
|
+
composition, so a precomposed `é` and `e` + combining accent are one
|
|
131
|
+
spelling); compatibility forms are never folded, so `x²` and `x2`, or the
|
|
132
|
+
`fi` ligature and `fi`, stay different values:
|
|
133
|
+
|
|
134
|
+
| kind | merged |
|
|
135
|
+
|---|---|
|
|
136
|
+
| `email`, `domain`, `uuid` | letter case (`str.lower`) |
|
|
137
|
+
| `url` | letter case of the scheme and the host |
|
|
138
|
+
| `phone` | spaces and punctuation, Arabic-Indic and Persian digits, a leading `00` read as `+` (more than ten digits) |
|
|
139
|
+
| `iban` | spaces, ASCII letter case |
|
|
140
|
+
| `card`, `mac` | spaces and punctuation (and letter case for `mac`) |
|
|
141
|
+
| `ip` | the canonical form of the address (`2001:0DB8::0:1` = `2001:db8::1`) |
|
|
142
|
+
| `term` | letter case (`str.lower`, so `Straße` and `STRASSE` stay different), Arabic and Hebrew diacritics and tatweel, runs of whitespace |
|
|
143
|
+
| `secret`, `entropy`, `user` | nothing |
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
>>> two = mask("Call +1 202-555-0164 or +1 (202) 555 0164")
|
|
147
|
+
>>> two.text
|
|
148
|
+
'Call [PHONE_1] or [PHONE_1]'
|
|
149
|
+
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
There is a price for that, and it is the one exception to an exact round
|
|
153
|
+
trip: both occurrences come back in the spelling that was seen first.
|
|
154
|
+
`Masked.exact` is `False` when this happened.
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
>>> two.exact
|
|
158
|
+
False
|
|
159
|
+
>>> unmask(two.text, two.vault)
|
|
160
|
+
'Call +1 202-555-0164 or +1 202-555-0164'
|
|
161
|
+
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Arabic text and digits:
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
>>> print(mask("أرسلت العقد لسلمى على ٠٧٧٠٠ ٩٠٠١٢٣", terms=["سلمى"]).text)
|
|
168
|
+
أرسلت العقد ل[TERM_1] على [PHONE_1]
|
|
169
|
+
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Names are not found unless you pass them:
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
>>> mask("Omar Sample called.").text
|
|
176
|
+
'Omar Sample called.'
|
|
177
|
+
>>> mask("Omar Sample called.", terms=["Omar Sample"]).text
|
|
178
|
+
'[TERM_1] called.'
|
|
179
|
+
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### `unmask(text, vault) -> str`
|
|
183
|
+
|
|
184
|
+
Restores the original values. It tolerates what language models do to
|
|
185
|
+
placeholders: other letter case (`[email_1]`), spaces (`[ EMAIL 1 ]`), a dash
|
|
186
|
+
or nothing instead of the underscore, a Markdown-escaped underscore or
|
|
187
|
+
brackets (`\[EMAIL\_1\]`), bold, italics or backticks around or inside the
|
|
188
|
+
brackets, a possessive or plural suffix (`[EMAIL_1]'s`), full-width brackets
|
|
189
|
+
and letters, Arabic-Indic digits in the number.
|
|
190
|
+
|
|
191
|
+
It does not restore a placeholder whose brackets were removed (`EMAIL_1`) or
|
|
192
|
+
replaced by other brackets (`(EMAIL_1)`, `<EMAIL_1>`).
|
|
193
|
+
|
|
194
|
+
`unmask_report(text, vault)` returns `.text`, `.restored` (a count) and
|
|
195
|
+
`.unknown` (placeholders that are not in the vault; they are left untouched).
|
|
196
|
+
|
|
197
|
+
### `Vault`
|
|
198
|
+
|
|
199
|
+
**The vault contains the secrets.** It maps every placeholder to its original
|
|
200
|
+
value. A saved vault file is plain, unencrypted JSON with all of them in it.
|
|
201
|
+
Keep it private, do not commit it, delete it when you are done.
|
|
202
|
+
|
|
203
|
+
blackbar never writes a vault on its own. You choose the path:
|
|
204
|
+
|
|
205
|
+
```python
|
|
206
|
+
>>> import os, tempfile
|
|
207
|
+
>>> from blackbar import Vault
|
|
208
|
+
>>> with tempfile.TemporaryDirectory() as folder:
|
|
209
|
+
... path = os.path.join(folder, "vault.json")
|
|
210
|
+
... m.vault.save(path) # mode 0600 where the OS supports it
|
|
211
|
+
... later = Vault.load(path)
|
|
212
|
+
>>> unmask("[IP_1]", later)
|
|
213
|
+
'198.51.100.23'
|
|
214
|
+
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
On Windows the POSIX mode bits are mostly ignored and the file gets the
|
|
218
|
+
permissions of its folder.
|
|
219
|
+
|
|
220
|
+
`a.merge(b)` adds the entries of another vault. If both vaults used the same
|
|
221
|
+
placeholder for different values, the incoming one is renumbered and the
|
|
222
|
+
method returns the mapping `{placeholder in b: placeholder in a}`.
|
|
223
|
+
|
|
224
|
+
**Keyed mode.** `Vault(key=...)` derives the identifier from
|
|
225
|
+
`HMAC-SHA256(key, kind + NUL + normalised value)` instead of counting.
|
|
226
|
+
Separate runs with the same key then agree on the placeholders without
|
|
227
|
+
sharing a vault:
|
|
228
|
+
|
|
229
|
+
```python
|
|
230
|
+
>>> key = "a long random string that is kept out of the repository"
|
|
231
|
+
>>> a = mask("mail maria@example.com", vault=Vault(key=key))
|
|
232
|
+
>>> b = mask("MARIA@example.com wrote", vault=Vault(key=key))
|
|
233
|
+
>>> a.findings[0].placeholder == b.findings[0].placeholder
|
|
234
|
+
True
|
|
235
|
+
>>> len(a.findings[0].placeholder) # "[EMAIL_" + 10 hex digits + "]"
|
|
236
|
+
18
|
|
237
|
+
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
If the input already contains placeholder-looking text, `mask` leaves it
|
|
241
|
+
alone in both modes and never hands that placeholder out, including every
|
|
242
|
+
form `unmask` would read as it (`[EMAIL_01]` and `[email 1]` both count as
|
|
243
|
+
`[EMAIL_1]`). In numbered mode the next free number is used; in keyed mode
|
|
244
|
+
the value gets a longer prefix of its HMAC (12 hex digits or more), so in
|
|
245
|
+
that run its placeholder differs from the one other runs give it.
|
|
246
|
+
|
|
247
|
+
Each run still needs its own vault to restore its values. The key is never
|
|
248
|
+
written to the vault file. Whoever has the key can test whether a placeholder
|
|
249
|
+
stands for a value they guess; see `SECURITY.md`.
|
|
250
|
+
|
|
251
|
+
### `scan(text, ...)` and `redact(kind, value)`
|
|
252
|
+
|
|
253
|
+
`scan` returns the findings without changing anything. `redact` gives a short
|
|
254
|
+
hint that is safe to print:
|
|
255
|
+
|
|
256
|
+
```python
|
|
257
|
+
>>> from blackbar import scan, redact
|
|
258
|
+
>>> text = "ping 192.0.2.7 or jane@example.com"
|
|
259
|
+
>>> [(f.kind, redact(f.kind, text[f.start:f.end])) for f in scan(text)]
|
|
260
|
+
[('ip', '1***'), ('email', 'j***@e***.com')]
|
|
261
|
+
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## Command line
|
|
265
|
+
|
|
266
|
+
```sh
|
|
267
|
+
blackbar mask ticket.txt --vault vault.json --terms-file terms.txt -o ticket.masked.txt
|
|
268
|
+
blackbar unmask answer.txt --vault vault.json -o answer.restored.txt
|
|
269
|
+
blackbar scan ticket.txt --terms-file terms.txt
|
|
270
|
+
blackbar kinds
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
- `mask [FILE|-] --vault PATH [--kinds LIST] [--exclude LIST] [--terms-file PATH] [--term TEXT] [--min-confidence X] [--key-env NAME] [-o OUT] [--force] [-q]`
|
|
274
|
+
reads the file or standard input, creates or extends the vault, writes the
|
|
275
|
+
masked text to `OUT` or standard output. The summary on standard error
|
|
276
|
+
counts the values and never shows them.
|
|
277
|
+
- `unmask [FILE|-] --vault PATH [-o OUT] [--force] [--strict]` restores.
|
|
278
|
+
Placeholders that are not in the vault are listed on standard error;
|
|
279
|
+
`--strict` makes that exit status 3.
|
|
280
|
+
- `scan [FILE|-] [--kinds ...] [--terms-file ...] [--json] [--fail-on-findings]`
|
|
281
|
+
only reports. Values are shown redacted, never in full.
|
|
282
|
+
- `kinds` lists the kinds; `kinds --aliases` also lists other accepted names
|
|
283
|
+
(`ipv4`, `credit_card`, ...).
|
|
284
|
+
|
|
285
|
+
`--terms-file` is a UTF-8 file with one term per line; lines starting with `#`
|
|
286
|
+
are comments. `--key-env NAME` switches to keyed mode and reads the key from
|
|
287
|
+
the environment variable `NAME`, so that it does not end up in the shell
|
|
288
|
+
history.
|
|
289
|
+
|
|
290
|
+
An existing output file is never overwritten unless you pass `--force`, and
|
|
291
|
+
the output may not be the input or the vault. Input and output are UTF-8 on
|
|
292
|
+
every platform, including the Windows console; line endings are kept as they
|
|
293
|
+
are.
|
|
294
|
+
|
|
295
|
+
For the ticket of the example above, with `Maria Example` and `acme` in
|
|
296
|
+
`terms.txt`, `blackbar scan ticket.txt --terms-file terms.txt` prints
|
|
297
|
+
(line:column, kind, confidence, hint):
|
|
298
|
+
|
|
299
|
+
<!-- scan-output -->
|
|
300
|
+
```text
|
|
301
|
+
1:10 term 1.00 M***
|
|
302
|
+
1:25 email 0.95 m***@e***.com
|
|
303
|
+
1:44 phone 0.90 +***64
|
|
304
|
+
2:19 ip 0.90 1***
|
|
305
|
+
2:61 secret 0.80 *** (15 chars)
|
|
306
|
+
3:22 user 0.85 m***
|
|
307
|
+
3:45 term 1.00 a***
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
In a pipe (`your-assistant` stands for whatever you use):
|
|
311
|
+
|
|
312
|
+
```sh
|
|
313
|
+
cat ticket.txt | blackbar mask --vault vault.json | your-assistant | blackbar unmask --vault vault.json
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
## Detectors
|
|
317
|
+
|
|
318
|
+
Confidence is a rough, hand-assigned number, not a calibrated probability.
|
|
319
|
+
"Default" says whether the kind runs when you do not pass `kinds`.
|
|
320
|
+
|
|
321
|
+
| Kind | Default | What is matched | Strong at | Weak at |
|
|
322
|
+
|---|---|---|---|---|
|
|
323
|
+
| `email` | yes | ASCII addresses with a dotted domain | ordinary addresses, `+tags`, subdomains; skips `logo@2x.png` | non-ASCII local parts; `user@localhost`; obfuscated forms (`a [at] b`); the display name next to an address is not touched; `git@host` is masked as an address |
|
|
324
|
+
| `url` | yes | anything `scheme://...`, and `www.` hosts; the whole URL becomes one placeholder, including `user:pass@` | trailing punctuation, brackets, Markdown links, Arabic punctuation | `example.com/path` without scheme or `www.` is not a URL here; the whole URL is hidden, so the assistant cannot see its path. With `kinds` that leave out `url`, only the password inside `scheme://user:pass@host` is masked, as a secret |
|
|
325
|
+
| `ip` | yes | IPv4 with four parts 0-255 and no leading zeros; IPv6 validated by the standard library | documentation, private and public ranges; `::ffff:` forms; ports and `/24` stay outside | **Version numbers.** `1.2.3.4` after `version`, `v`, `build`, `release`, a `name/` prefix (user agents) or before `-beta` is skipped, but a bare four-part version such as `node 20.11.0.1` is taken for an address (confidence 0.6 or 0.9 depending on the numbers). Loopback, `0.0.0.0` and netmasks are deliberately not masked. Short IPv6 forms like `a::b` have low confidence |
|
|
326
|
+
| `mac` | yes | `aa:bb:cc:dd:ee:ff`, `aa-bb-...`, `aabb.ccdd.eeff` | separators | 12 hex digits without separators are not matched; six dash-separated decimal pairs can be something else (confidence 0.7) |
|
|
327
|
+
| `phone` | yes | `+` international (9-15 digits, `+1`/`+7` exactly 11); `00` international with separators; North American `(202) 555-0164` shapes; grouped national numbers with a leading zero (10-11 digits); any 7-15 digit number right after a phone word (phone, tel, mobile, fax, WhatsApp, call ... on; هاتف, جوال, موبايل, ...); Western, Arabic-Indic and Persian digits | **Precision is preferred.** Dates, times, timestamps, prices, epoch values, ISBNs, and national-format numbers right after order / invoice / ticket / ref / id / tracking / SKU (and Arabic طلب, فاتورة, ...) are not flagged | **This is the hard one.** Unformatted national digits with no phone word (`02079460777`) are missed. A grouped leading-zero number that is something else (`Lot 0412 345 6789`) is flagged at confidence 0.7. Local formats that are neither `+`, leading-zero nor North American are missed without a phone word. National and international spellings of one number get different placeholders. A short number right after a `+` number may be absorbed into it. No per-country numbering plan is used |
|
|
328
|
+
| `card` | yes | 12-19 digits, optional uniform spaces or dashes in card-like groups, Luhn checksum, and a known scheme prefix and length | public test numbers of the main schemes, Arabic-Indic digits | prefix table written from memory; the broad Maestro range has confidence 0.6; any other 16-digit number that happens to pass Luhn with a card prefix is flagged (about 1 in 10 of such numbers) |
|
|
329
|
+
| `iban` | yes | two capitals, two digits, then the country's length, with or without single spaces; mod-97 checksum | exact length per country for the 77 codes in the table | table written from memory; a country that is not in it is accepted on the checksum alone at confidence 0.6; lower-case IBANs are not matched |
|
|
330
|
+
| `secret` | yes | private-key blocks; JSON Web Tokens; `Authorization:` header values and `Bearer <token>`; values of `key=value` / `key: value` / JSON pairs whose key contains a secret word (password, passwd, pwd, passphrase, secret, token, api_key, access_key, private_key, credentials, session id, pin, otp ...); `--password VALUE` flags; `CVV 123`, `PIN 4821`; the password in `scheme://user:pass@host`; token prefixes listed below; Arabic `كلمة المرور: ...` | only the value is masked, the key stays; skips `max_tokens`, `token_type`, `password_policy`, placeholders like `<token>`, `${VAR}`, `None`, function calls and attribute references in code | a secret in a sentence without `:` or `=` ("the password is ...", "the door code is 4821") is missed; an unquoted value stops at `,` `;` `&` unless the pair starts the line; `mysql -pSECRET` style flags are missed; a key named just `key` is not treated as secret; tokens of services that are not in the prefix list are found only next to a secret-looking key |
|
|
331
|
+
| `user` | yes | the user-name part of `C:\Users\NAME`, `/home/NAME`, `/Users/NAME`, `/mnt/c/Users/NAME` | names with a space when a path separator follows; JSON-escaped backslashes | only the name inside the path is masked, not the same name elsewhere in the text (add it to `terms`); `/home/pricing` in a web route written without host is taken for a user; generic accounts (`Public`, `root`, `runner`, ...) are skipped |
|
|
332
|
+
| `term` | with `terms` | the literal strings you pass, case-insensitive, whole words, Unicode-aware; Arabic and Hebrew diacritics and tatweel are ignored; Arabic prefix clitics may be attached | exactly what you list | nothing you did not list. Inflected forms and Arabic suffixes (`شركتنا`) are different words. `acme_corp` does not contain the word `acme` (underscore and digits are word characters). For Arabic `لل` + noun the whole word is masked under its own placeholder |
|
|
333
|
+
| `domain` | no | dotted names ending in a top-level domain from a short list | | list written from memory and incomplete; `config.app`-like code is flagged |
|
|
334
|
+
| `uuid` | no | 8-4-4-4-12 hex | | |
|
|
335
|
+
| `entropy` | no | runs of 24+ base64/URL-safe characters with letters and digits and high Shannon entropy; skips pure hex (hashes, commit ids), UUIDs, `data:` payloads, word-like identifiers | | noisy by nature: long camelCase identifiers and encoded data are flagged. Confidence 0.5 |
|
|
336
|
+
|
|
337
|
+
Not masked by design unless you pass them as `terms`: UUIDs (opt-in kind),
|
|
338
|
+
dates including dates of birth, plain numbers, national identity or social
|
|
339
|
+
security numbers, passport numbers, postal addresses, account numbers that
|
|
340
|
+
are not IBANs.
|
|
341
|
+
|
|
342
|
+
## Dev-set numbers
|
|
343
|
+
|
|
344
|
+
`bench/dataset.py` holds 76 short texts with labelled spans: support tickets,
|
|
345
|
+
logs, emails, chat messages and config snippets in English and Arabic (plus a
|
|
346
|
+
little German, French and Persian), and hard negatives. `python bench/run.py`
|
|
347
|
+
printed this for version 0.1.1, the same as for 0.1.0 (Python 3.14, Windows):
|
|
348
|
+
|
|
349
|
+
<!-- dev-set-numbers -->
|
|
350
|
+
```text
|
|
351
|
+
== dev set (bench/dataset.py): 76 texts, 99 labelled spans ==
|
|
352
|
+
kind gold found exact precision recall
|
|
353
|
+
email 13 13 13 1.00 1.00
|
|
354
|
+
url 9 9 9 1.00 1.00
|
|
355
|
+
ip 12 13 12 0.92 1.00
|
|
356
|
+
mac 3 3 3 1.00 1.00
|
|
357
|
+
phone 22 22 21 0.95 0.95
|
|
358
|
+
card 3 3 3 1.00 1.00
|
|
359
|
+
iban 3 3 3 1.00 1.00
|
|
360
|
+
secret 19 17 17 1.00 0.89
|
|
361
|
+
user 5 5 5 1.00 1.00
|
|
362
|
+
term 10 8 8 1.00 0.80
|
|
363
|
+
all 99 96 94 0.98 0.95
|
|
364
|
+
covered by any mask: 94/99 (0.95)
|
|
365
|
+
round trip: 76/76 (1.00)
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
How to read it, honestly:
|
|
369
|
+
|
|
370
|
+
- **These numbers are optimistic.** The set was written by the same author
|
|
371
|
+
as the detectors and was looked at during development; one rule (`CVV 123`)
|
|
372
|
+
was added after a miss in it. It measures "does the tool do what its author
|
|
373
|
+
intended" far more than "how well does it work on your text". Counts per
|
|
374
|
+
kind are small; one item moves a column by several points.
|
|
375
|
+
- Scoring is strict: kind, start and end must all match a label. "covered"
|
|
376
|
+
counts labelled spans that lie inside any masked span.
|
|
377
|
+
- The seven errors are kept in the set on purpose: two names that were not
|
|
378
|
+
given as terms (the two `term` misses; blackbar cannot find names), two
|
|
379
|
+
secrets written in a sentence, one unformatted phone number, one lot number
|
|
380
|
+
taken for a phone number, one four-part version taken for an IP address.
|
|
381
|
+
- "round trip" is exact restoration of each text. It is 76/76 here because no
|
|
382
|
+
text writes the same value in two spellings.
|
|
383
|
+
- `bench/run.py` also evaluates `bench/blind.py` and prints a second table;
|
|
384
|
+
that result is below.
|
|
385
|
+
|
|
386
|
+
### Blind set
|
|
387
|
+
|
|
388
|
+
`bench/blind.py` was written by someone who never saw the code: 186 texts in
|
|
389
|
+
English and Arabic (tickets, chat, logs, config, invoices), 179 labelled
|
|
390
|
+
values, and 62 texts with nothing to mask but plenty of look-alikes (dates,
|
|
391
|
+
prices, order numbers, versions, hashes). Every value in it is fake. It was run
|
|
392
|
+
once and the detectors were not changed afterwards. For version 0.1.1 it was
|
|
393
|
+
re-run after fixes made once the blind set had been seen (term matching,
|
|
394
|
+
placeholder reservation, normalisation; none aimed at it); every number
|
|
395
|
+
below stayed the same.
|
|
396
|
+
|
|
397
|
+
| kind | labelled | precision | recall |
|
|
398
|
+
|---|---|---|---|
|
|
399
|
+
| email, url, mac, card, iban, user | 95 | 1.00 | 1.00 |
|
|
400
|
+
| ip | 26 | 0.96 | 1.00 |
|
|
401
|
+
| phone | 34 | 0.97 | 0.91 |
|
|
402
|
+
| secret | 24 | 0.96 | 0.96 |
|
|
403
|
+
| all | 179 | 0.98 | 0.98 |
|
|
404
|
+
|
|
405
|
+
Round trip (mask, then unmask, gives back the original text): 186 of 186.
|
|
406
|
+
|
|
407
|
+
The seven errors, in full:
|
|
408
|
+
|
|
409
|
+
- three phone numbers missed: national numbers written as one unbroken run
|
|
410
|
+
of ten or eleven digits with no plus sign, one of them in Arabic-Indic
|
|
411
|
+
digits;
|
|
412
|
+
- one secret missed (a token mentioned in prose without `=` or `:`);
|
|
413
|
+
- three things masked that should not have been: a four-part version number
|
|
414
|
+
(`3.2.1.0`) taken for an IP address, a spaced digit group taken for a phone
|
|
415
|
+
number, and a short number after a secret-looking key.
|
|
416
|
+
|
|
417
|
+
A missed value is the failure that matters here, because it leaves the text
|
|
418
|
+
you share with a real value in it. Read the masked text before you send it.
|
|
419
|
+
|
|
420
|
+
## What it does not do
|
|
421
|
+
|
|
422
|
+
- **No name detection.** Names of people, companies, products, places and
|
|
423
|
+
projects are masked only when you pass them as `terms`. There is no
|
|
424
|
+
language model and no dictionary of names.
|
|
425
|
+
- **No guarantee of completeness.** blackbar finds what its patterns
|
|
426
|
+
describe. Unusual formats, typos, values split across lines, text in
|
|
427
|
+
images or attachments, and whole categories (postal addresses, identity
|
|
428
|
+
numbers, dates of birth, medical or financial details written in words)
|
|
429
|
+
pass through. Treat the output as a first pass and read it.
|
|
430
|
+
- **Regex-based.** It does not understand the text. It can mask something
|
|
431
|
+
harmless that looks like a phone number and miss a password that is
|
|
432
|
+
introduced by a sentence. The detector table lists the known cases.
|
|
433
|
+
- **Context can still identify a person.** Removing the values does not make
|
|
434
|
+
a text anonymous. A job title, a place, a date and a rare event can be
|
|
435
|
+
enough. blackbar is not an anonymisation tool in any legal sense.
|
|
436
|
+
- **The vault holds the secrets.** The vault and any saved vault file contain
|
|
437
|
+
every original value in plain text. Losing the file to someone else undoes
|
|
438
|
+
the masking; losing it yourself means you cannot restore. It is not
|
|
439
|
+
encrypted.
|
|
440
|
+
- **The answer is restored blindly.** `unmask` replaces placeholders wherever
|
|
441
|
+
they are. Read the restored text before you pass it on.
|
|
442
|
+
- **No exact restoration of respelled duplicates.** The same value written in
|
|
443
|
+
two spellings comes back in the first spelling (`Masked.exact`).
|
|
444
|
+
- **No protection of what stays visible on purpose**: key names, path
|
|
445
|
+
structure, ports, the kind and number of values.
|
|
446
|
+
|
|
447
|
+
## Tables written from memory
|
|
448
|
+
|
|
449
|
+
These were written from memory and have not been checked against the official
|
|
450
|
+
registries. Corrections are welcome.
|
|
451
|
+
|
|
452
|
+
- IBAN country lengths (`blackbar.checks.IBAN_LENGTHS`, 77 codes). An unknown
|
|
453
|
+
country is accepted on the checksum alone, with lower confidence.
|
|
454
|
+
- Card scheme prefixes and lengths (`blackbar.checks.card_brand`).
|
|
455
|
+
- Token prefixes for the `secret` kind: `ghp_` `gho_` `ghu_` `ghs_` `ghr_`,
|
|
456
|
+
`github_pat_`, `glpat-`, `AKIA` / `ASIA` + 16, `xoxb-` `xoxp-` `xoxa-`
|
|
457
|
+
`xoxr-` `xoxs-`, `sk-` followed by 20 or more characters, `sk_live_`
|
|
458
|
+
`sk_test_` `rk_live_` `rk_test_`, `AIza` + 35, `npm_` + 36, `hf_`, and the
|
|
459
|
+
`pypi-AgEIcHlwaS5vcmc` prefix. Only prefixes the author was sure of are
|
|
460
|
+
included; many services are missing.
|
|
461
|
+
- Top-level domains for the opt-in `domain` kind (short and incomplete).
|
|
462
|
+
- The reserved phone ranges used in tests and examples (US `555-01xx`, UK
|
|
463
|
+
`020 7946 0xxx` and `07700 900xxx`, Australia `0491 570 xxx`, Germany
|
|
464
|
+
`030 23125 xxx`, France `01 99 00 xx xx`).
|
|
465
|
+
|
|
466
|
+
## Development
|
|
467
|
+
|
|
468
|
+
```sh
|
|
469
|
+
python -m unittest discover -s tests -v
|
|
470
|
+
python bench/run.py --verbose
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
Every value in the tests, the examples and the dev set is invented. See
|
|
474
|
+
`CONTRIBUTING.md` for the rules on test data and `SECURITY.md` for the threat
|
|
475
|
+
model.
|
|
476
|
+
|
|
477
|
+
## License
|
|
478
|
+
|
|
479
|
+
MIT. See `LICENSE`.
|