Skip to main content

Sieve Redact v0.12.0

A command-line tool that removes sensitive information from documents — deterministically, while preserving byte-for-byte integrity of everything else.

CI License: MIT Python Version

English | 日本語

💡 Concept

Personal information in resumes, reports and logs is converted into a second file that is safe to publish and share.

  • Deterministic — Same input plus same rules always produces the same output. Character selection in noise is derived from SHA-256; it depends on nothing else — not on the environment, the clock, or any RNG.
  • Byte-for-byte integrity — Bytes outside a match are left untouched, down to the last byte. CRLF line endings survive. Integrity is verified mechanically before output; --strict refuses to write on failure (exit code 3).
  • No guessing — The user specifies what is sensitive. The engine never guesses.
  • Receipts — The receipt records the facts of processing (positions, lengths, modes, hashes) but never the sensitive body itself. Even "no pattern remains" is reported as a measurement (pattern_appearance_in_output), not a promise.

The name refers to a sieve: drop only what is specified, and leave everything else unharmed, byte for byte.

Companion tool to Sieve Lens (observation of invisible content) — Lens observes, Redact masks.


🧩 The Sieve Series

Sieve is a family of deterministic, zero-dependency engines that share the same design principles (deterministic output, no telemetry, fully local) — each engine observes or masks one specific risk, and they compose:

Package Role Connects to
sieve-core-engine Base logic / plugin host for the ecosystem —
sieve-lens Observes invisible content & hidden prompts in documents → redact via --from-lens
sieve-scope Detects structurally similar code pairs —
sieve-referee Detects paraphrased / reused text pairs —
sieve-redact Masks sensitive regions (text & PNG/JPEG) with receipts ← lens, → tray
sieve-tray Desktop front-end that routes files to the right engine ← all engines

Observation → masking in one pass: Lens finds what a human can't see, Redact removes it with a receipt that proves nothing else changed.

🔥 Features

Eight redaction modes

MODE Operation Content
delete remove the region empty string
mosaic fill per character █ repeated for the original character count
label numbered labels prefix (default [REDACTED]) + sequence number
noise same-class replacement picked deterministically within the same character class (hiragana / katakana / kanji / upper / lower / digit)
decor wrap ARG + original + ARG
replace bulk replace ARG
alias numbered labels for a value's written forms prefix (default [REDACTED]) + sequence number — the value matches through the same 1:1 normalization view as the built-ins, so its other spellings (full-width/half-width, dash variants) are observed as the same value; --rule only (numbering shares the label counter, position-ascending from 1)
same-value one symbol per value prefix (default [REDACTED]) + ordinal — every occurrence of the same value carries the same symbol; the ordinals share one counter with label / alias and a repeat consumes no number; duplicates are judged by (kind, normalized value), so for card the same number spelled with different separators is the same value; works with --rule and --builtin

Built-in matchers (--builtin)

NAME Target
postal-jp Japanese postal codes NNN-NNNN
phone-jp Japanese phone numbers (090-… / 03-… / 050-… / 0120-… / 4-digit area codes 01XX-09XX) and short numbers (104 / 106 / 110 / 117 / 118 / 119 / 171) — long-vowel separators (090ー1234ー5678) count as hyphens
email Email addresses (ASCII)
chars All occurrences of given codepoints (U+200B,U+00AD form)
mynumber Japanese My Number — a maximal digit run of exactly 12 digits whose check digit validates (residue 11, the same formula as sieve-crowd)
corporate-jp Japanese corporate number (13-digit) — a maximal digit run of exactly 13 digits whose leading check digit validates (the Corporate Number Ordinance art. 6 formula; the published example 7000012050002 carries check digit 7)
card Card numbers — a maximal digit run of 13..19 digits that passes the standard Luhn checksum, and the separated spellings: groups of 4 digits joined by one identical separator (space, the dash variants, U+3000, U+30FC), 13..19 digits total, plus the 4-6-5 and 4-6-4 groupings; the replacement covers the separators

postal-jp / phone-jp / email match through a 1:1 normalization view: full-width ASCII forms (123, ABC, -, @) and dash variants (U+2010–2015, U+2212) are folded to their ASCII counterparts before matching, while masking still applies to the original bytes (receipts record normalized: true on such spans). phone-jp and postal-jp additionally fold the long-vowel marks U+30FC (ー) and U+FF70 (ー) to - when they sit between digits — so 090ー1234ー5678 and 100ー0001 are observed like their hyphenated spellings — while katakana long vowels (コーヒー) and email matching stay exact. The v0.11.0 number matchers (mynumber / corporate-jp / card) and the alias MODE use the same 1:1 view, so full-width spellings of the numbers are observed with normalized: true; the runs themselves are exact-length ("ちょうど") — a 12-digit run never matches inside a 13-digit run. For the separated card spellings the chain is judged form-by-form with the Luhn gate, and every group start is tried, so a valid number is not missed inside a longer same-separator run. chars and plain literal rules stay exact.

Image masking (--region)

PNG images (8-bit RGB/RGBA, non-interlaced, non-animated) and RGB JPEG images are masked by explicit regions: --region x,y,w,h:MODE[:ARG] (repeatable; overlapping regions are discarded in declaration order; zero-area or out-of-bounds regions are usage errors; --rule cannot be combined with --region). The output is always PNG — JPEG input is never re-compressed as JPEG, so masking adds no further loss (the decoded pixels are preserved into the PNG).

MODE Effect
delete opaque white fill
mosaic block average over all channels (blocks align to the region origin; ARG = block size, default 8)
noise per-pixel deterministic replacement — SHA-256(img:x:y:R,G,B) drives the new RGB; alpha is kept, so semi-transparent pixels keep their visibility (content changes, visibility does not)
label black band with [REDACTED]n (Pillow's default font)
decor 2px border in the ARG color (RRGGBB hex)
replace ARG text drawn on a white fill (Pillow's default font)

Image receipts set byte_integrity_verified to null (not applicable — the output is always PNG, so file bytes change structurally even for PNG input), record the detected format as input_format (PNG or JPEG), and report pixel integrity outside every region as pixel_integrity_verified; --strict exits 3 when it fails. File metadata (EXIF etc.) is always dropped on save — a privacy-friendly default by design. Output bytes are deterministic across platforms and pinned in CI (with Pillow 12.3.0; future Pillow updates may change encoded bytes, never pixels).

Word-unit and case-insensitive matching (+ / ~ prefixes)

太郎:label also matches the 太郎 inside 田中太郎; 太郎:+label matches only at word boundaries (adjacent characters of the same class, or both alphanumeric, cause rejection).

Taro:~label ignores ASCII case when matching (TARO / taro also match; non-ASCII stays exact). The prefixes compose in any order — 秘密:+~label is word-unit plus case-insensitive. chars with ~ matches both case variants of the listed ASCII letters.

Config files and Lens integration

  • --config rules.cfg: load rules from a file (rule <spec> / builtin <spec> line format).
  • --from-lens catalog.json: generate masking rules from an observation catalog (JSON) produced by Sieve Lens. Invisible characters become deletions; look-alike letters (Cyrillic/Greek → Latin) become replacements based on the twin field. A zero-dependency schema gate validates the catalog strictly and rejects (rc2, with JSON Pointer error paths): duplicate JSON keys, NaN/Infinity, unknown keys, wrong types (including true as an integer) and files over 1 MiB. Nothing is guessed or coerced.

💻 Quick start

git clone https://github.com/neguseatama/sieve-redact.git
cd sieve-redact

# Mosaic a name, label phone numbers
python sieve_redact.py in.txt -o out.txt \
  --rule "田中太郎:mosaic" \
  --builtin phone-jp:label \
  --receipt receipt.json

# A CP932 (Windows-31J) input file; the output stays UTF-8
python sieve_redact.py in_sjis.txt -o out.txt \
  --from-encoding cp932 \
  --builtin card:label

Example receipt (stdout) — in.txt contains x秘密y, run with --rule "秘密:delete":

Sieve Redact v0.12.0 — レシート
入力: 8 バイト -> 出力: 2 バイト
redact 件数: 1
  #1 rule=1 matcher=literal mode=delete pos=1..7 len=6B -> 0B sha256=062a2931da68...
他バイト完全性: 検証済み
出力内パターン残留: none

The sensitive body never appears in the receipt — hashes only. In image mode (--region) byte_integrity_verified is null (not applicable) and pixel integrity is measured by pixel_integrity_verified instead.

Integration with Sieve Lens

# (1) Lens produces an observation catalog
python -m sieve_lens_ext.redact_bridge input.txt catalog.json
# (2) Redact masks everything
python sieve_redact.py input.txt -o clean.txt --from-lens catalog.json

🔬 Determinism guarantees

Character selection in noise is fully determined by:

char = POOL[ SHA-256("<pattern>:<byte_offset>:<original char>").digest()[0] % len(POOL) ]

No seed. No environment dependence. Same input, same output, always. The bundled tests verify exact agreement with this formula by recomputation.

The normalization view (built-in matchers) and the ASCII case folding (~ rules) are per-character 1:1 mappings — they never shift positions, so masking still applies to the original bytes.

With --from-encoding the byte integrity and the receipt's byte counts live in the UTF-8 domain: the decoded input as UTF-8 vs the output (the two sides must share one domain for the check to mean anything). For the default UTF-8 path the decoded text re-encodes to the original bytes, so the receipt is byte-identical to previous versions.

Overlapping matches are resolved deterministically as well: rules are tried in declaration order, each rule scans leftmost-first, later matches that overlap a confirmed region are discarded, and confirmed regions are applied in ascending order of position.


🔬 Tests

python -m unittest discover -s tests -v

381 tests (eight modes, built-in matchers including the separated card spellings, word-unit matching, case-insensitive matching, the normalization view, input encodings, image masking with JPEG input, config files, Lens integration with the schema gate, the Schema Contract v1 conformance corpus replica (JSON layer), CLI boundaries, the sdist contents with a fresh-venv test run, the public-text watch). No additional dependencies.

tests/corpus/schema_contract_v1.json is a sha256-pinned replica of the Sieve Schema Contract v1 conformance corpus (44 accept/reject cases) maintained in the sieve-lens repository, which also hosts the contract text (docs/SCHEMA_CONTRACT_v1.md) and the canonical engine (sieve_lens_ext/schema.py). The replica exercises Redact's JSON layer — strict catalog loading: duplicate keys, NaN/Infinity and a UTF-8 BOM are refused — and the pin turns any silent drift from the canonical copy into a test failure (the sieve-tray vendor-sync precedent).


⚠️ Known limitations

  1. No regex — patterns are literal strings, optionally with ASCII case-insensitive folding (~); no wildcards, classes or quantifiers
  2. No automatic sensitive-data detection — the user specifies what is sensitive (by design)
  3. Text and images — PDF and Office formats are not supported; image masking accepts PNG (8-bit RGB/RGBA) and RGB JPEG input and always writes PNG output; grayscale/CMYK images, other formats (GIF/WebP etc.) and PDF masking are out of scope (observation is Sieve Lens's domain)
  4. Normalization is 1:1 and targeted — the built-in matchers fold U+FF01..FF5E and the dash variants U+2010..2015 / U+2212 (and, for postal-jp / phone-jp only, the long-vowel marks U+30FC / U+FF70 between digits); other compatibility forms (e.g. U+3000 spaces) stay exact
  5. phone-jp shape limits remain — numbers without hyphens other than the fixed short set (104/106/110/117/118/119/171) and unusual digit groupings are out of scope — match them explicitly with --rule
  6. --from-lens rejects unknown keys — catalog keys outside contract v1 (lens/version/observations, per-item kind/codepoint/count/positions/twin) are refused, not ignored; future Lens versions must extend the contract explicitly
  7. Number matchers take digit runs and uniform separators — card spellings whose groups do not share one separator (e.g. 4111 1111-1111-1111), two-character separators, glued digits (44111 1111 …), and the 8+8 / 7+9 groupings do not reach the card matcher; match such spellings explicitly with --rule or --rule VALUE:alias (whose normalization view still folds the dash variants of the given value). Kanji glyph variants (同形異字, e.g. 髙/高) are not folded anywhere — alias matching is 1:1-table exact on kanji

📄 License

MIT. See LICENSE.


👤 Author

  • Kai IWASAKI

Metadata

Release files for sieve-redact 0.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sieve-redact 0.12.0
File Size Uploaded Provenance Actions
sieve_redact-0.12.0.tar.gz 112.6 kB Without provenance Details for sieve_redact-0.12.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sieve-redact 0.12.0
File Interpreter ABI Platform Actions
sieve_redact-0.12.0-py3-none-any.whl Python 3 none any Details for sieve_redact-0.12.0-py3-none-any.whl

Total release size: 147.4 kB

Release files / sieve_redact-0.12.0.tar.gz

Download URL sieve_redact-0.12.0.tar.gz
Size 112.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9082c6a91cdfad0647c75a3c2c4bdb2faa1d4eaaaf6dbf79c78d29036ea564b1
BLAKE2b-256 checksum
How to use checksums
c518b81fa78c2b10ab920ac4068f9b9e869a47ab6338de78a46d82f788079199
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release files / sieve_redact-0.12.0-py3-none-any.whl

Download URL sieve_redact-0.12.0-py3-none-any.whl
Size 34.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d34d260efc3e8080bc7e536d40fc1bbfc69a8c1a6fcf0066c756cb9d737b9bbc
BLAKE2b-256 checksum
How to use checksums
a5a571b43bace78bd702ae44e7ac6b7b3ebc453346d576606d736680abce4523
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Security for sieve-redact 0.12.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page