open-pseudonym 0.1.0__py3-none-any.whl
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.
- open_pseudonym/__init__.py +24 -0
- open_pseudonym/__main__.py +5 -0
- open_pseudonym/_data.py +34 -0
- open_pseudonym/browser.py +62 -0
- open_pseudonym/checks.py +104 -0
- open_pseudonym/cli.py +304 -0
- open_pseudonym/conformance.py +149 -0
- open_pseudonym/data/patterns.json +321 -0
- open_pseudonym/data/pools.json +1070 -0
- open_pseudonym/data/profiles/browse_and_fill.json +74 -0
- open_pseudonym/data/profiles/contacts.json +55 -0
- open_pseudonym/data/profiles/draft_reply.json +77 -0
- open_pseudonym/data/profiles/logs.json +69 -0
- open_pseudonym/data/profiles/money_math.json +71 -0
- open_pseudonym/data/profiles/schedule.json +74 -0
- open_pseudonym/data/profiles/secrets_only.json +43 -0
- open_pseudonym/data/profiles/strict.json +77 -0
- open_pseudonym/data/profiles/summarize.json +67 -0
- open_pseudonym/data/profiles/training_data.json +70 -0
- open_pseudonym/data/profiles/web_research.json +67 -0
- open_pseudonym/dates.py +112 -0
- open_pseudonym/detect.py +184 -0
- open_pseudonym/drbg.py +43 -0
- open_pseudonym/egress.py +266 -0
- open_pseudonym/fakes.py +397 -0
- open_pseudonym/fpe.py +104 -0
- open_pseudonym/handles.py +162 -0
- open_pseudonym/jcs.py +47 -0
- open_pseudonym/jws.py +110 -0
- open_pseudonym/mcp/__init__.py +169 -0
- open_pseudonym/ml.py +98 -0
- open_pseudonym/policy.py +235 -0
- open_pseudonym/proxy/__init__.py +5 -0
- open_pseudonym/proxy/app.py +509 -0
- open_pseudonym/proxy/sse.py +50 -0
- open_pseudonym/restore.py +238 -0
- open_pseudonym/session.py +733 -0
- open_pseudonym/types.py +50 -0
- open_pseudonym/vault.py +223 -0
- open_pseudonym-0.1.0.dist-info/METADATA +73 -0
- open_pseudonym-0.1.0.dist-info/RECORD +46 -0
- open_pseudonym-0.1.0.dist-info/WHEEL +5 -0
- open_pseudonym-0.1.0.dist-info/entry_points.txt +2 -0
- open_pseudonym-0.1.0.dist-info/licenses/LICENSE +202 -0
- open_pseudonym-0.1.0.dist-info/licenses/NOTICE +5 -0
- open_pseudonym-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""Open Pseudonym: keep personal data away from AI models without making them worse at the task.
|
|
2
|
+
|
|
3
|
+
The short version:
|
|
4
|
+
|
|
5
|
+
from open_pseudonym import Session, Source
|
|
6
|
+
s = Session("draft_reply")
|
|
7
|
+
masked = s.mask(text) # what the model may see
|
|
8
|
+
reply = s.restore(model_reply) # what the person reads
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from .egress import EgressLeak, EgressLog, Leak, verify_log
|
|
12
|
+
from .handles import HandleRefused, Sink, Source
|
|
13
|
+
from .jws import Signer
|
|
14
|
+
from .policy import LEVELS, Policy, PolicyError, Rule, load_level, load_profile, profile_names, suggest_profile
|
|
15
|
+
from .session import Masked, RefusedData, Replacement, Resolved, Session
|
|
16
|
+
from .vault import Vault, VaultError
|
|
17
|
+
|
|
18
|
+
__version__ = "0.1.0"
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"LEVELS", "EgressLeak", "EgressLog", "HandleRefused", "Leak", "Masked", "Policy", "PolicyError", "RefusedData",
|
|
22
|
+
"Replacement", "Resolved", "Rule", "Session", "Signer", "Sink", "Source", "Vault", "VaultError",
|
|
23
|
+
"load_level", "load_profile", "profile_names", "suggest_profile", "verify_log", "__version__",
|
|
24
|
+
]
|
open_pseudonym/_data.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""Loads the shared data files (fake-value pools, detection patterns, starter profiles).
|
|
2
|
+
|
|
3
|
+
The canonical copies live in the repository's top-level `data/` folder. `tools/sync_data.py` copies them into each
|
|
4
|
+
SDK so the installed packages carry them too; CI fails if the copies drift.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
from functools import lru_cache
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
DATA_DIR = Path(__file__).resolve().parent / "data"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@lru_cache(maxsize=None)
|
|
17
|
+
def pools() -> dict:
|
|
18
|
+
return json.loads((DATA_DIR / "pools.json").read_text(encoding="utf-8"))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@lru_cache(maxsize=None)
|
|
22
|
+
def patterns() -> list[dict]:
|
|
23
|
+
return json.loads((DATA_DIR / "patterns.json").read_text(encoding="utf-8"))["patterns"]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def profile_names() -> list[str]:
|
|
27
|
+
return sorted(p.stem for p in (DATA_DIR / "profiles").glob("*.json"))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def profile_doc(name: str) -> dict:
|
|
31
|
+
path = DATA_DIR / "profiles" / f"{name}.json"
|
|
32
|
+
if not path.is_file():
|
|
33
|
+
raise KeyError(f"no built-in profile called {name!r}; try one of {profile_names()}")
|
|
34
|
+
return json.loads(path.read_text(encoding="utf-8"))
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Filling secret handles in a real browser (Playwright), only on the site they belong to.
|
|
2
|
+
|
|
3
|
+
filler = HandleFiller(session)
|
|
4
|
+
text = await filler.read(page) # what the model may see of this page
|
|
5
|
+
await filler.fill(page.locator("#code"), "{{OTP_1}}")
|
|
6
|
+
shot = await filler.screenshot(page) # filled fields are blacked out
|
|
7
|
+
|
|
8
|
+
The site check uses the URL of the frame that holds the field, read from the browser itself, so an injected
|
|
9
|
+
instruction cannot talk its way past it. After filling we check that the value is not showing anywhere in the page
|
|
10
|
+
text; if it is, the field is cleared and the fill is refused. Needs the [browser] extra.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
from playwright.async_api import Locator, Page
|
|
17
|
+
except ImportError as e: # the optional dependency is not installed
|
|
18
|
+
raise ImportError('open_pseudonym.browser needs Playwright: pip install "open-pseudonym[browser]"') from e
|
|
19
|
+
|
|
20
|
+
from .handles import HandleRefused, Sink, Source
|
|
21
|
+
from .session import Session
|
|
22
|
+
|
|
23
|
+
TOOL = "browser_type"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class HandleFiller:
|
|
27
|
+
def __init__(self, session: Session, tool: str = TOOL):
|
|
28
|
+
self.session = session
|
|
29
|
+
self.tool = tool
|
|
30
|
+
self._filled: list[Locator] = []
|
|
31
|
+
|
|
32
|
+
async def read(self, page: Page, source: Source | None = None) -> str:
|
|
33
|
+
"""The page's visible text, masked. Secrets found on the page get handles bound to the page's own site."""
|
|
34
|
+
text = await page.inner_text("body")
|
|
35
|
+
return self.session.mask(text, source or Source(site=page.url)).text
|
|
36
|
+
|
|
37
|
+
async def fill(self, field: Locator, handle: str) -> None:
|
|
38
|
+
"""Put the real value behind `handle` into `field`, if the field's frame is on an allowed site."""
|
|
39
|
+
frame_url = await field.evaluate("el => el.ownerDocument.location.href")
|
|
40
|
+
real = self.session.resolve_args({"value": handle}, Sink(tool=self.tool, site=frame_url)).args["value"]
|
|
41
|
+
await field.fill(real)
|
|
42
|
+
if await self._showing(field.page, real):
|
|
43
|
+
await field.fill("")
|
|
44
|
+
raise HandleRefused(handle, "the value became visible on the page")
|
|
45
|
+
self._filled.append(field)
|
|
46
|
+
|
|
47
|
+
@staticmethod
|
|
48
|
+
async def _showing(page: Page, real: str) -> bool:
|
|
49
|
+
for frame in page.frames:
|
|
50
|
+
try:
|
|
51
|
+
if real in await frame.inner_text("body"):
|
|
52
|
+
return True
|
|
53
|
+
except Exception: # a frame that went away or has no body cannot be showing anything
|
|
54
|
+
continue
|
|
55
|
+
return False
|
|
56
|
+
|
|
57
|
+
async def screenshot(self, page: Page, **kw) -> bytes:
|
|
58
|
+
"""A screenshot with every field we filled covered, so a vision model never sees the value."""
|
|
59
|
+
return await page.screenshot(mask=list(self._filled), **kw)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
__all__ = ["HandleFiller", "HandleRefused"]
|
open_pseudonym/checks.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"""Checksums and sanity checks that turn a pattern match into a confident detection.
|
|
2
|
+
|
|
3
|
+
A 16-digit run is only a card if the Luhn digit adds up; nine digits after "routing" are only a routing number if
|
|
4
|
+
the ABA weights do. These checks remove most false alarms (order numbers, tracking ids) for free.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from ._data import pools
|
|
10
|
+
from .types import digits_of
|
|
11
|
+
|
|
12
|
+
# IBAN lengths per country (ISO 13616 registry). Unknown countries are rejected rather than guessed.
|
|
13
|
+
IBAN_LENGTHS = {
|
|
14
|
+
"AD": 24, "AE": 23, "AT": 20, "BE": 16, "BG": 22, "BH": 22, "BR": 29, "CH": 21, "CY": 28, "CZ": 24, "DE": 22,
|
|
15
|
+
"DK": 18, "EE": 20, "ES": 24, "FI": 18, "FO": 18, "FR": 27, "GB": 22, "GE": 22, "GI": 23, "GL": 18, "GR": 27,
|
|
16
|
+
"HR": 21, "HU": 28, "IE": 22, "IL": 23, "IS": 26, "IT": 27, "JO": 30, "KW": 30, "KZ": 20, "LB": 28, "LI": 21,
|
|
17
|
+
"LT": 20, "LU": 20, "LV": 21, "MC": 27, "MT": 31, "NL": 18, "NO": 15, "PK": 24, "PL": 28, "PS": 29, "PT": 25,
|
|
18
|
+
"QA": 29, "RO": 24, "RS": 22, "SA": 24, "SE": 24, "SI": 19, "SK": 24, "SM": 27, "TR": 26, "UA": 29, "VA": 22,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def luhn_ok(digits: str) -> bool:
|
|
23
|
+
total = 0
|
|
24
|
+
for i, ch in enumerate(reversed(digits)):
|
|
25
|
+
d = ord(ch) - 48
|
|
26
|
+
if i % 2 == 1:
|
|
27
|
+
d *= 2
|
|
28
|
+
if d > 9:
|
|
29
|
+
d -= 9
|
|
30
|
+
total += d
|
|
31
|
+
return total % 10 == 0
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def luhn_digit(partial: str) -> str:
|
|
35
|
+
"""The check digit that makes partial + digit pass Luhn."""
|
|
36
|
+
for d in "0123456789":
|
|
37
|
+
if luhn_ok(partial + d):
|
|
38
|
+
return d
|
|
39
|
+
raise AssertionError("unreachable")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def card_ok(value: str) -> bool:
|
|
43
|
+
d = digits_of(value)
|
|
44
|
+
if not 13 <= len(d) <= 19 or not luhn_ok(d) or len(set(d)) == 1:
|
|
45
|
+
return False
|
|
46
|
+
if d[0] in "3456":
|
|
47
|
+
return True
|
|
48
|
+
return d[0] == "2" and 2221 <= int(d[:4]) <= 2720
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def iban_numeric(iban: str) -> int:
|
|
52
|
+
moved = iban[4:] + iban[:4]
|
|
53
|
+
return int("".join(str(int(ch, 36)) for ch in moved))
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def iban_ok(value: str) -> bool:
|
|
57
|
+
s = "".join(value.split()).upper()
|
|
58
|
+
if IBAN_LENGTHS.get(s[:2]) != len(s) or not s.isascii() or not s.isalnum():
|
|
59
|
+
return False
|
|
60
|
+
return iban_numeric(s) % 97 == 1
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def aba_sum(d: str) -> int:
|
|
64
|
+
return 3 * (int(d[0]) + int(d[3]) + int(d[6])) + 7 * (int(d[1]) + int(d[4]) + int(d[7])) + (
|
|
65
|
+
int(d[2]) + int(d[5]) + int(d[8])
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def aba_ok(value: str) -> bool:
|
|
70
|
+
d = digits_of(value)
|
|
71
|
+
if len(d) != 9:
|
|
72
|
+
return False
|
|
73
|
+
prefix = int(d[:2])
|
|
74
|
+
if not (prefix <= 12 or 21 <= prefix <= 32 or 61 <= prefix <= 72 or prefix == 80):
|
|
75
|
+
return False
|
|
76
|
+
return aba_sum(d) % 10 == 0
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def ssn_ok(value: str) -> bool:
|
|
80
|
+
d = digits_of(value)
|
|
81
|
+
if len(d) != 9:
|
|
82
|
+
return False
|
|
83
|
+
area, group, serial = d[:3], d[3:5], d[5:]
|
|
84
|
+
return area not in ("000", "666") and area[0] != "9" and group != "00" and serial != "0000"
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def phone_ok(value: str) -> bool:
|
|
88
|
+
return 10 <= len(digits_of(value)) <= 15
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def name_ok(value: str) -> bool:
|
|
92
|
+
stop = _stopwords()
|
|
93
|
+
tokens = value.split()
|
|
94
|
+
return bool(tokens) and all(t.lower() not in stop and len(t) >= 2 for t in tokens)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
_STOP: frozenset[str] | None = None
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _stopwords() -> frozenset[str]:
|
|
101
|
+
global _STOP
|
|
102
|
+
if _STOP is None:
|
|
103
|
+
_STOP = frozenset(w.lower() for w in pools()["name_stopwords"])
|
|
104
|
+
return _STOP
|
open_pseudonym/cli.py
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
"""The `opn` command.
|
|
2
|
+
|
|
3
|
+
opn profiles list the starter profiles
|
|
4
|
+
opn keygen make a vault key and a receipt signing key (owner-only files)
|
|
5
|
+
opn mask --profile training_data a.txt b.txt mask files with one shared vault (fakes stay consistent)
|
|
6
|
+
opn restore masked.txt put real values back (needs the same vault and key)
|
|
7
|
+
opn verify LOGDIR check a folder of receipts: signatures, chain, digests, leaks
|
|
8
|
+
opn proxy --profile draft_reply run the local proxy on 127.0.0.1:8787
|
|
9
|
+
opn mcp-gateway --profile browse_and_fill --source-site acme.com --tool-site type=https://acme.com -- <command>
|
|
10
|
+
opn demo see what the model would see for a sample email
|
|
11
|
+
opn conformance vectors/ run the shared test vectors
|
|
12
|
+
|
|
13
|
+
Keys live in ~/.config/open-pseudonym unless you say otherwise. They are never printed.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import argparse
|
|
19
|
+
import os
|
|
20
|
+
import sys
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
|
|
23
|
+
from . import __version__
|
|
24
|
+
from .handles import Source
|
|
25
|
+
from .jws import Signer, public_key_from_pem, public_pem
|
|
26
|
+
from .policy import LEVELS, load_profile, profile_names
|
|
27
|
+
from .session import Session
|
|
28
|
+
from .vault import Vault
|
|
29
|
+
|
|
30
|
+
DEMO = """From: Dana Reyes <dana.reyes@gmail.com>
|
|
31
|
+
Hi Sam,
|
|
32
|
+
|
|
33
|
+
Can we move Thursday's call to March 12, 2026? Call me at (617) 555-0142.
|
|
34
|
+
Your Acme verification code is 482913. It expires in 10 minutes.
|
|
35
|
+
|
|
36
|
+
Thanks,
|
|
37
|
+
Dana"""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def key_dir(args) -> Path:
|
|
41
|
+
if getattr(args, "key_dir", None):
|
|
42
|
+
return Path(args.key_dir)
|
|
43
|
+
base = os.environ.get("XDG_CONFIG_HOME") or os.path.join(os.path.expanduser("~"), ".config")
|
|
44
|
+
return Path(base) / "open-pseudonym"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _write_private(path: Path, data: bytes) -> None:
|
|
48
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
49
|
+
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
50
|
+
with os.fdopen(fd, "wb") as fh:
|
|
51
|
+
fh.write(data)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def cmd_keygen(args) -> int:
|
|
55
|
+
d = key_dir(args)
|
|
56
|
+
made = []
|
|
57
|
+
if not (d / "vault.key").exists():
|
|
58
|
+
_write_private(d / "vault.key", os.urandom(32))
|
|
59
|
+
made.append("vault.key")
|
|
60
|
+
if not (d / "signing.pem").exists():
|
|
61
|
+
signer = Signer.generate()
|
|
62
|
+
_write_private(d / "signing.pem", signer.to_pem())
|
|
63
|
+
(d / "signing.pub.pem").write_bytes(public_pem(signer.public_key))
|
|
64
|
+
made.append("signing.pem")
|
|
65
|
+
print(f"keys in {d}: " + (", ".join(made) + " created" if made else "already there, nothing changed"))
|
|
66
|
+
return 0
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def load_vault_key(args) -> bytes:
|
|
70
|
+
path = Path(args.key_file) if args.key_file else key_dir(args) / "vault.key"
|
|
71
|
+
if not path.exists():
|
|
72
|
+
sys.exit(f"no vault key at {path}; run `opn keygen` first")
|
|
73
|
+
key = path.read_bytes()
|
|
74
|
+
if len(key) != 32:
|
|
75
|
+
sys.exit(f"{path} is not a 32-byte key")
|
|
76
|
+
return key
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def load_signer(args) -> Signer:
|
|
80
|
+
path = key_dir(args) / "signing.pem"
|
|
81
|
+
if not path.exists():
|
|
82
|
+
sys.exit(f"no signing key at {path}; run `opn keygen` first")
|
|
83
|
+
return Signer.from_pem(path.read_bytes())
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _session(args, vault: Vault | None) -> Session:
|
|
87
|
+
return Session(args.profile, vault)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _inputs(files: list[str]):
|
|
91
|
+
if not files:
|
|
92
|
+
yield "-", sys.stdin.read()
|
|
93
|
+
for f in files:
|
|
94
|
+
yield f, Path(f).read_text(encoding="utf-8")
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def cmd_mask(args) -> int:
|
|
98
|
+
key = load_vault_key(args)
|
|
99
|
+
vault_path = Path(args.vault)
|
|
100
|
+
vault = Vault.load(vault_path, key) if vault_path.exists() else Vault()
|
|
101
|
+
s = _session(args, vault)
|
|
102
|
+
source = Source(sender=args.source_sender, site=args.source_site) if (args.source_sender or args.source_site) else None
|
|
103
|
+
for name, text in _inputs(args.files):
|
|
104
|
+
m = s.mask(text, source)
|
|
105
|
+
if args.out_dir and name != "-":
|
|
106
|
+
out = Path(args.out_dir) / Path(name).name
|
|
107
|
+
out.parent.mkdir(parents=True, exist_ok=True)
|
|
108
|
+
out.write_text(m.text, encoding="utf-8")
|
|
109
|
+
else:
|
|
110
|
+
sys.stdout.write(m.text)
|
|
111
|
+
for n in m.notes:
|
|
112
|
+
print("note: " + n, file=sys.stderr)
|
|
113
|
+
s.vault.save(vault_path, key)
|
|
114
|
+
return 0
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def cmd_restore(args) -> int:
|
|
118
|
+
s = Session("strict", Vault.load(args.vault, load_vault_key(args)))
|
|
119
|
+
for _, text in _inputs(args.files):
|
|
120
|
+
sys.stdout.write(s.restore(text, reveal_handles=args.reveal))
|
|
121
|
+
return 0
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def cmd_verify(args) -> int:
|
|
125
|
+
from .egress import verify_log
|
|
126
|
+
|
|
127
|
+
pub_path = Path(args.pub) if args.pub else key_dir(args) / "signing.pub.pem"
|
|
128
|
+
pub = public_key_from_pem(pub_path.read_bytes())
|
|
129
|
+
vault = Vault.load(args.vault, load_vault_key(args)) if args.vault else None
|
|
130
|
+
report = verify_log(args.logdir, pub, vault, expect_count=args.expect_count, expect_last=args.expect_last,
|
|
131
|
+
expect_session=args.expect_session)
|
|
132
|
+
print(f"receipts: {report['receipts']} (last: {report['last']})")
|
|
133
|
+
if vault is not None:
|
|
134
|
+
print(f"payloads re-checked against the vault: {report['rechecked']}, known values found: {report['leaks']}")
|
|
135
|
+
if report["problems"]:
|
|
136
|
+
print("problems:")
|
|
137
|
+
for p in report["problems"]:
|
|
138
|
+
print(" " + p)
|
|
139
|
+
return 1
|
|
140
|
+
print("every receipt is signed and chained, and every stored payload matches its digest")
|
|
141
|
+
if args.expect_count is None and args.expect_last is None:
|
|
142
|
+
print("note: without --expect-count or --expect-last a log cut short at the end cannot be told apart")
|
|
143
|
+
return 0
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def cmd_proxy(args) -> int:
|
|
147
|
+
try:
|
|
148
|
+
import uvicorn
|
|
149
|
+
|
|
150
|
+
from .proxy import ProxyConfig, create_app
|
|
151
|
+
except ImportError:
|
|
152
|
+
sys.exit('the proxy needs extra packages: pip install "open-pseudonym[proxy]"')
|
|
153
|
+
fill = dict(item.split("=", 1) for item in args.fill_tool)
|
|
154
|
+
cfg = ProxyConfig(
|
|
155
|
+
profile=args.profile, allowed_profiles=tuple(args.allow_profile), openai_upstream=args.openai_upstream,
|
|
156
|
+
anthropic_upstream=args.anthropic_upstream, log_dir=args.log_dir, signer=load_signer(args),
|
|
157
|
+
vault_dir=args.vault_dir, vault_key=load_vault_key(args) if args.vault_dir else None, fill_tools=fill,
|
|
158
|
+
)
|
|
159
|
+
if args.host not in ("127.0.0.1", "localhost", "::1"):
|
|
160
|
+
print("warning: the proxy has no login; anyone who can reach it and guess a session id can read that "
|
|
161
|
+
"session's restores. Keep it on 127.0.0.1 unless something in front of it checks who is calling.",
|
|
162
|
+
file=sys.stderr)
|
|
163
|
+
print(f"open-pseudonym proxy on http://{args.host}:{args.port} (profile {args.profile}); "
|
|
164
|
+
+ (f"receipts in {args.log_dir}" if args.log_dir else "no receipts (add --log-dir to keep them)"),
|
|
165
|
+
file=sys.stderr)
|
|
166
|
+
uvicorn.run(create_app(cfg), host=args.host, port=args.port, log_level="warning")
|
|
167
|
+
return 0
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def cmd_mcp_gateway(args) -> int:
|
|
171
|
+
import anyio
|
|
172
|
+
|
|
173
|
+
try:
|
|
174
|
+
from mcp import StdioServerParameters
|
|
175
|
+
|
|
176
|
+
from .mcp import run_gateway
|
|
177
|
+
except ImportError:
|
|
178
|
+
sys.exit('the MCP gateway needs the MCP SDK: pip install "open-pseudonym[mcp]"')
|
|
179
|
+
if not args.command:
|
|
180
|
+
sys.exit("give the upstream server's command after --, for example: opn mcp-gateway -- npx some-server")
|
|
181
|
+
vault = None
|
|
182
|
+
if args.vault:
|
|
183
|
+
key = load_vault_key(args)
|
|
184
|
+
vault = Vault.load(args.vault, key) if Path(args.vault).exists() else Vault()
|
|
185
|
+
s = Session(args.profile, vault)
|
|
186
|
+
params = StdioServerParameters(command=args.command[0], args=args.command[1:])
|
|
187
|
+
tool_sites = dict(item.split("=", 1) for item in args.tool_site)
|
|
188
|
+
source = Source(site=args.source_site) if args.source_site else None
|
|
189
|
+
try:
|
|
190
|
+
anyio.run(lambda: run_gateway(params, s, tool_sites=tool_sites,
|
|
191
|
+
source_for=(lambda tool, result: source) if source else None))
|
|
192
|
+
finally:
|
|
193
|
+
if args.vault:
|
|
194
|
+
s.vault.save(args.vault, load_vault_key(args))
|
|
195
|
+
return 0
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def cmd_demo(args) -> int:
|
|
199
|
+
s = Session(args.profile)
|
|
200
|
+
m = s.mask(DEMO, Source(sender="no-reply@acme.com"))
|
|
201
|
+
print("You typed:\n" + DEMO + "\n")
|
|
202
|
+
print("The model sees:\n" + m.text + "\n")
|
|
203
|
+
for n in m.notes:
|
|
204
|
+
print("Note to the model: " + n)
|
|
205
|
+
first = m.text.split("Hi ")[1].split(",")[0]
|
|
206
|
+
reply = f"Sure {first}, Thursday works. I'll use {{{{OTP_1}}}} to sign in."
|
|
207
|
+
print("\nThe model answers:\n" + reply)
|
|
208
|
+
print("\nYou read:\n" + s.restore(reply))
|
|
209
|
+
print("\nEgress check on what the model saw: " + ("clean" if not s.check_egress(m.text) else "LEAK"))
|
|
210
|
+
return 0
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def cmd_profiles(args) -> int:
|
|
214
|
+
print("Levels (a simple switch for people):")
|
|
215
|
+
for n, (name, label, text) in LEVELS.items():
|
|
216
|
+
print(f" {n} {label:10} ({name}): {text}")
|
|
217
|
+
print("\nTask profiles:")
|
|
218
|
+
for name in profile_names():
|
|
219
|
+
print(f" {name:16} {load_profile(name).description}")
|
|
220
|
+
return 0
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def cmd_conformance(args) -> int:
|
|
224
|
+
from .conformance import main as run
|
|
225
|
+
|
|
226
|
+
return run([args.vectors])
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def main(argv: list[str] | None = None) -> int:
|
|
230
|
+
p = argparse.ArgumentParser(prog="opn", description="Open Pseudonym: protect personal data sent to AI models.")
|
|
231
|
+
p.add_argument("--version", action="version", version=f"open-pseudonym {__version__}")
|
|
232
|
+
p.add_argument("--key-dir", help="where keys live (default ~/.config/open-pseudonym)")
|
|
233
|
+
sub = p.add_subparsers(dest="cmd", required=True)
|
|
234
|
+
|
|
235
|
+
sub.add_parser("profiles", help="list the starter profiles").set_defaults(fn=cmd_profiles)
|
|
236
|
+
sub.add_parser("keygen", help="create a vault key and a signing key").set_defaults(fn=cmd_keygen)
|
|
237
|
+
|
|
238
|
+
m = sub.add_parser("mask", help="mask files or stdin")
|
|
239
|
+
m.add_argument("files", nargs="*")
|
|
240
|
+
m.add_argument("--profile", default="training_data")
|
|
241
|
+
m.add_argument("--vault", default="opn.vault", help="vault file, created if missing (default opn.vault)")
|
|
242
|
+
m.add_argument("--key-file")
|
|
243
|
+
m.add_argument("--out-dir")
|
|
244
|
+
m.add_argument("--source-sender")
|
|
245
|
+
m.add_argument("--source-site")
|
|
246
|
+
m.set_defaults(fn=cmd_mask)
|
|
247
|
+
|
|
248
|
+
r = sub.add_parser("restore", help="restore files or stdin")
|
|
249
|
+
r.add_argument("files", nargs="*")
|
|
250
|
+
r.add_argument("--vault", default="opn.vault")
|
|
251
|
+
r.add_argument("--key-file")
|
|
252
|
+
r.add_argument("--reveal", action="store_true", help="show secrets behind handles too")
|
|
253
|
+
r.set_defaults(fn=cmd_restore)
|
|
254
|
+
|
|
255
|
+
v = sub.add_parser("verify", help="verify a receipts folder")
|
|
256
|
+
v.add_argument("logdir")
|
|
257
|
+
v.add_argument("--pub", help="public key PEM (default: signing.pub.pem in the key folder)")
|
|
258
|
+
v.add_argument("--vault", help="also re-run the egress check on stored payloads with this vault")
|
|
259
|
+
v.add_argument("--key-file")
|
|
260
|
+
v.add_argument("--expect-count", type=int, help="the number of requests you know were sent")
|
|
261
|
+
v.add_argument("--expect-last", help="the digest of the last receipt you kept (sha-256:...)")
|
|
262
|
+
v.add_argument("--expect-session", help="the session id every receipt must belong to")
|
|
263
|
+
v.set_defaults(fn=cmd_verify)
|
|
264
|
+
|
|
265
|
+
x = sub.add_parser("proxy", help="run the local proxy")
|
|
266
|
+
x.add_argument("--profile", default="strict")
|
|
267
|
+
x.add_argument("--allow-profile", action="append", default=[], help="profile apps may pick with x-opn-profile")
|
|
268
|
+
x.add_argument("--host", default="127.0.0.1")
|
|
269
|
+
x.add_argument("--port", type=int, default=8787)
|
|
270
|
+
x.add_argument("--openai-upstream", default="https://api.openai.com")
|
|
271
|
+
x.add_argument("--anthropic-upstream", default="https://api.anthropic.com")
|
|
272
|
+
x.add_argument("--log-dir", help="write receipts (and payloads) here")
|
|
273
|
+
x.add_argument("--vault-dir", help="persist session vaults here, encrypted")
|
|
274
|
+
x.add_argument("--key-file")
|
|
275
|
+
x.add_argument("--fill-tool", action="append", default=[], metavar="TOOL=SITE",
|
|
276
|
+
help="let the proxy fill handles for this tool, which acts on SITE")
|
|
277
|
+
x.set_defaults(fn=cmd_proxy)
|
|
278
|
+
|
|
279
|
+
g = sub.add_parser("mcp-gateway", help="put an MCP server behind Open Pseudonym (stdio)")
|
|
280
|
+
g.add_argument("--profile", default="strict")
|
|
281
|
+
g.add_argument("--source-site", help="secrets read through this server belong to this site (binds handles)")
|
|
282
|
+
g.add_argument("--tool-site", action="append", default=[], metavar="TOOL=SITE",
|
|
283
|
+
help="this tool acts on SITE, so handles bound to SITE may be filled into it")
|
|
284
|
+
g.add_argument("--vault", help="keep the vault in this file between runs")
|
|
285
|
+
g.add_argument("--key-file")
|
|
286
|
+
g.add_argument("command", nargs=argparse.REMAINDER)
|
|
287
|
+
g.set_defaults(fn=cmd_mcp_gateway)
|
|
288
|
+
|
|
289
|
+
d = sub.add_parser("demo", help="show masking on a sample email")
|
|
290
|
+
d.add_argument("--profile", default="draft_reply")
|
|
291
|
+
d.set_defaults(fn=cmd_demo)
|
|
292
|
+
|
|
293
|
+
c = sub.add_parser("conformance", help="run the shared test vectors")
|
|
294
|
+
c.add_argument("vectors")
|
|
295
|
+
c.set_defaults(fn=cmd_conformance)
|
|
296
|
+
|
|
297
|
+
args = p.parse_args(argv)
|
|
298
|
+
if getattr(args, "command", None) and args.command[:1] == ["--"]:
|
|
299
|
+
args.command = args.command[1:]
|
|
300
|
+
return args.fn(args) or 0
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
if __name__ == "__main__":
|
|
304
|
+
sys.exit(main())
|