Metadata-Version: 2.4
Name: omna-pii-mask
Version: 0.2.23
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: Other/Proprietary License
Classifier: Operating System :: MacOS
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Rust
Classifier: Topic :: Security
Classifier: Topic :: Text Processing :: Linguistic
Summary: Omna unified PII/secret detection engine (L1–L6) — compiled Rust kernel with in-process on-device AI. Binary-only.
Keywords: pii,redaction,privacy,ner,secrets,rust,detection
Author-email: gaurav <gaurjin@gmail.com>
License: Proprietary
Requires-Python: >=3.9
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Homepage, https://github.com/gaurjin/omna

# omna-pii-mask

The compiled detection engine behind [Omna](https://github.com/gaurjin/omna)'s
PII features. This is the same unified Rust kernel that ships in the Omna Mac
app and browser extension, exposed to Python as a single self-contained wheel.

It is the backend for `pip install "omna[pii]"` — you normally don't install it
directly; the `omna` package depends on it.

## What it does

A six-layer (L1–L6) detection pipeline over text:

- **Regex + rule layers** — emails, phone numbers, cards, URLs, and 220+ secret
  rules (AWS keys, GitHub tokens, JWTs, …) with entropy checks.
- **Checksum-validated IDs** — Luhn, IBAN, Verhoeff, NHS, and 30+ international
  formats, so a string that merely *looks* like an ID isn't flagged unless it
  validates.
- **India** — Aadhaar (Verhoeff), PAN, GSTIN (checksum), Voter ID, passport,
  driving licence, UPI IDs, vehicle numbers, ABHA, UAN, PIN codes.
- **Name lists** — a first name followed by a surname ("Emma Clarke",
  "Rahul Sharma") from public US Census and Wikidata (CC0) name lists; one word
  alone never counts.
- **On-device AI (L3)** — optional ONNX models that catch contextual PII
  regex can't (e.g. a bare prose name). Two are supported: the OpenAI privacy
  filter (default, ~809 MB, best at names) and a compact
  open-source model (Apache-2.0, ~64 MB, ~30× faster, strong on IDs/addresses/links), or both
  merged. Run **in-process** on the same ONNX Runtime the wheel already links;
  each downloads once on first use and every file is checked against a pinned
  SHA-256. Up to 4 cores, stepping down to 3 after 10 min and 2 after 30 min
  of non-stop work (`OMNA_L3_INTRA_THREADS=n` fixes the count).

## API

```python
import omna_pii_mask

omna_pii_mask.mask("Email john@acme.com", model=False)   # -> {"masked": "...", "tokens": {...}}
omna_pii_mask.detect("SSN 123-45-6789")                   # -> [{"entity": ..., "confidence": ...}, ...]
omna_pii_mask.restore(masked_text, tokens)                # reverse the reversible tokens
omna_pii_mask.mask(text, model=True, smart_model="both")  # L3: "privacy-filter" (default) | "compact" | "both"
omna_pii_mask.download_model(smart_model="compact")       # pre-fetch an L3 model
omna_pii_mask.verify_model(smart_model="compact")         # re-hash its files against the pinned SHA-256
omna_pii_mask.version()                                   # engine fingerprint (version+gitsha)
```

`ruleset=` takes a path to a JSON ruleset: `allowlist` (values never masked),
`custom_rules` (your own patterns) and `mask_off` — categories to let through,
any of `PERSON EMAIL PHONE ADDRESS GOV_ID FINANCIAL HEALTH EMPLOYMENT DIGITAL
BIRTH`. Secrets can never be switched off. An allowlist **pattern** never spares a secret — only an entry
that names one exact value does — and a pattern that matches everything (`.+`)
is refused: `omna_pii_mask.ruleset_check(path)` returns the refused ones as
`{"ignored_allowlist": [...]}`.

```json
{"mask_off": ["PHONE"], "allowlist": ["support@acme\\.com"]}
```

Reversible PII is replaced with stable tokens (`[PERSON_1]`, `[EMAIL_1]`, …).
Secrets/credentials are **always** redacted irreversibly (`[REDACTED:<KIND>]`)
and never written to the reversible token map.

**Audit events (L6).** `mask(...)["audit"]` lists one event per find — `entity`,
`layer`, `risk`, `action`, `confidence`, `span_hash` — and never the value;
`detect(...)` spans carry `risk` and `span_hash` too. `span_hash` is a keyed
fingerprint (HMAC-SHA256 under `audit_key=`, bytes you keep secret, e.g. one per
computer) so the same value can be recognised again without being stored.
Without `audit_key` it is empty: an unkeyed hash of a short value (a 9-digit ID)
could be reversed by hashing every possible value.

```python
omna_pii_mask.mask(text, audit_key=my_32_secret_bytes)["audit"]
```

Span `start`/`end` (in `mask(...)["spans"]` and `detect(...)`) are **UTF-8 byte
offsets**, not Python string indices. Slicing `text[start:end]` is wrong as soon
as a dash, emoji or accented letter comes before the span — use
`text.encode()[start:end].decode()`, or `mask(...)["masked"]`, which is already
correct.

## Distribution

Binary-only, proprietary. Wheels are built `abi3` (one wheel per platform covers
Python 3.9+). No source distribution is published — the Rust source is closed.

