rift-desync 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.
- rift/__init__.py +21 -0
- rift/arsenal/__init__.py +4 -0
- rift/arsenal/demo.py +156 -0
- rift/arsenal/mcp_gateway.py +87 -0
- rift/cli.py +173 -0
- rift/controls/__init__.py +5 -0
- rift/controls/sanitizers.py +80 -0
- rift/defense/__init__.py +6 -0
- rift/defense/ctb.py +295 -0
- rift/defense/defense_benchmark.py +166 -0
- rift/defense/peg.py +118 -0
- rift/defense/repair_experiment.py +183 -0
- rift/defense/wsc_experiment.py +480 -0
- rift/defense/wsc_reader.py +189 -0
- rift/detector.py +112 -0
- rift/experiments/__init__.py +0 -0
- rift/experiments/benchmark_reader.py +97 -0
- rift/experiments/benchmark_suite.py +630 -0
- rift/experiments/field_study.py +248 -0
- rift/experiments/heldout_predictor.py +630 -0
- rift/experiments/heldout_reader.py +96 -0
- rift/experiments/live_sweep.py +566 -0
- rift/experiments/peg_vs_binding.py +273 -0
- rift/fuzzer/__init__.py +19 -0
- rift/fuzzer/content.py +226 -0
- rift/fuzzer/guards.py +256 -0
- rift/fuzzer/structural.py +98 -0
- rift/metrics/__init__.py +5 -0
- rift/metrics/benchmark.py +793 -0
- rift/metrics/precision.py +196 -0
- rift/py.typed +0 -0
- rift/shield.py +257 -0
- rift/theory/__init__.py +8 -0
- rift/theory/brier.py +166 -0
- rift/theory/equivalence_oracle.py +130 -0
- rift_desync-0.1.0.dist-info/METADATA +335 -0
- rift_desync-0.1.0.dist-info/RECORD +41 -0
- rift_desync-0.1.0.dist-info/WHEEL +5 -0
- rift_desync-0.1.0.dist-info/entry_points.txt +2 -0
- rift_desync-0.1.0.dist-info/licenses/LICENSE +201 -0
- rift_desync-0.1.0.dist-info/top_level.txt +1 -0
rift/__init__.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""GXPD Toolkit -- Guard-Executor Parse Differential research & defense.
|
|
2
|
+
|
|
3
|
+
A toolkit for finding, measuring, and defending against parse differentials
|
|
4
|
+
between an LLM-agent's guard (validator/classifier/scanner) and its
|
|
5
|
+
executor/consumer: inputs that are *safe to the control and dangerous to the
|
|
6
|
+
sink* because the two parse the same bytes differently.
|
|
7
|
+
|
|
8
|
+
Subpackages
|
|
9
|
+
-----------
|
|
10
|
+
- ``rift.detector`` : reference GXPD detector (Theorem 2 operationalized)
|
|
11
|
+
- ``rift.fuzzer`` : differential fuzzer (guards x mutators x consumers)
|
|
12
|
+
- ``rift.metrics`` : detector precision/recall on a frozen labeled set
|
|
13
|
+
- ``rift.controls`` : HOLD/FALL classification of guard products
|
|
14
|
+
- ``rift.defense.ctb`` : canonicalize-then-bind reference defense library
|
|
15
|
+
|
|
16
|
+
All code is benign, verdict-only, and intended for defensive research and
|
|
17
|
+
coordinated disclosure. See DISCLAIMER.md and SECURITY.md.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
__version__ = "1.0.0"
|
|
21
|
+
__all__ = ["detector", "fuzzer", "metrics", "controls", "defense"]
|
rift/arsenal/__init__.py
ADDED
rift/arsenal/demo.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""GXPD Arsenal -- one-command demo.
|
|
3
|
+
|
|
4
|
+
python -m rift.arsenal.demo
|
|
5
|
+
|
|
6
|
+
Shows, against a REAL MCP tool with a REAL (naive) guard:
|
|
7
|
+
1. the attack: an entity-encoded `DROP TABLE` passes the upstream guard and the
|
|
8
|
+
executor decodes it -- a guard-executor parse differential, live;
|
|
9
|
+
2. the defense: the GXPD-Shield MCP gateway intercepts the same tools/call,
|
|
10
|
+
detects the divergence, and blocks it -- with a Parse-Equivalence Certificate;
|
|
11
|
+
3. zero false positives on a benign call;
|
|
12
|
+
4. shadow mode surfacing the attack as a NOVEL encoding (continuous IDS);
|
|
13
|
+
5. a verifiable, tamper-evident PEC chain across a 2-hop agent pipeline.
|
|
14
|
+
|
|
15
|
+
Benign: the "database" is a stub that REPORTS what SQL it would run; nothing is
|
|
16
|
+
executed, no network, no real DB.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import html
|
|
22
|
+
import json
|
|
23
|
+
|
|
24
|
+
import mcp.types as mt
|
|
25
|
+
|
|
26
|
+
try:
|
|
27
|
+
from rift.arsenal.mcp_gateway import ShieldedMCPGateway
|
|
28
|
+
from rift.shield import verify_chain
|
|
29
|
+
except ImportError:
|
|
30
|
+
from mcp_gateway import ShieldedMCPGateway
|
|
31
|
+
from shield import verify_chain
|
|
32
|
+
|
|
33
|
+
BAR = "=" * 74
|
|
34
|
+
DESTRUCTIVE = ("drop table", "delete from", "truncate", "drop database")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# --- the upstream MCP tool: a DB query tool with a NAIVE substring guard ----- #
|
|
38
|
+
def naive_guard(sql) -> bool:
|
|
39
|
+
"""The upstream server's own guard: refuse if the raw text contains a
|
|
40
|
+
destructive keyword. Matches the RAW bytes (this is the bug)."""
|
|
41
|
+
low = str(sql).lower()
|
|
42
|
+
return not any(k in low for k in DESTRUCTIVE)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def db_executor(arguments: dict) -> mt.CallToolResult:
|
|
46
|
+
"""The upstream handler = the executor. It DECODES the SQL (HTML entities,
|
|
47
|
+
JSON escapes) the way a templating/query layer would, then 'runs' it. Benign:
|
|
48
|
+
it reports the decoded SQL and whether it is destructive; it executes nothing."""
|
|
49
|
+
raw = arguments.get("sql", "")
|
|
50
|
+
decoded = html.unescape(raw)
|
|
51
|
+
try:
|
|
52
|
+
if "\\u" in decoded and '"' not in decoded:
|
|
53
|
+
decoded = json.loads(f'"{decoded}"')
|
|
54
|
+
except Exception:
|
|
55
|
+
pass
|
|
56
|
+
destructive = any(k in decoded.lower() for k in DESTRUCTIVE)
|
|
57
|
+
tag = " <-- DESTRUCTIVE SQL REACHED THE DATABASE" if destructive else ""
|
|
58
|
+
return mt.CallToolResult(
|
|
59
|
+
content=[mt.TextContent(type="text", text=f"[db] WOULD EXECUTE: {decoded!r}{tag}")],
|
|
60
|
+
is_error=False,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
DB_TOOL = mt.Tool(
|
|
65
|
+
name="db_query",
|
|
66
|
+
description="Run a read-only SQL query",
|
|
67
|
+
inputSchema={"type": "object", "properties": {"sql": {"type": "string"}}, "required": ["sql"]},
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
# an entity-encoded 'DROP TABLE users' -- defeats the raw-substring guard
|
|
71
|
+
ATTACK = "SELECT 1; " + "".join(f"&#x{ord(c):x};" for c in "DROP TABLE users")
|
|
72
|
+
BENIGN = "SELECT name FROM customers WHERE active = 1"
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _text(result: mt.CallToolResult) -> str:
|
|
76
|
+
return result.content[0].text if result.content else ""
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def main():
|
|
80
|
+
print(BAR)
|
|
81
|
+
print("GXPD ARSENAL -- Shielded MCP Gateway vs guard-executor parse differential")
|
|
82
|
+
print(BAR)
|
|
83
|
+
print(f"\nupstream tool : {DB_TOOL.name} (guard bans destructive SQL by raw substring)")
|
|
84
|
+
print(f"attack arg : sql = {ATTACK!r}")
|
|
85
|
+
print(" the guard sees entity bytes (no 'drop table' substring) -> ADMITS")
|
|
86
|
+
print(" the executor html-unescapes them -> 'DROP TABLE users'")
|
|
87
|
+
|
|
88
|
+
# ---- 1) BASELINE: client calls the upstream tool directly (no gateway) ----
|
|
89
|
+
print("\n" + "-" * 74)
|
|
90
|
+
print("[1] BASELINE (no gateway): upstream guard, then executor")
|
|
91
|
+
if naive_guard(ATTACK):
|
|
92
|
+
print(" upstream guard verdict: ADMIT (raw substring check passed)")
|
|
93
|
+
res = db_executor({"sql": ATTACK})
|
|
94
|
+
print(" " + _text(res))
|
|
95
|
+
print(" RESULT: attack bypassed the guard; destructive SQL reached the DB.")
|
|
96
|
+
|
|
97
|
+
# ---- 2) SHIELDED GATEWAY: same call, intercepted ----
|
|
98
|
+
print("\n" + "-" * 74)
|
|
99
|
+
print("[2] GXPD-SHIELD GATEWAY (enforce): same tools/call, intercepted")
|
|
100
|
+
events = []
|
|
101
|
+
gw = ShieldedMCPGateway(telemetry_sink=events.append)
|
|
102
|
+
gw.register_tool(DB_TOOL, guard=naive_guard, handler=db_executor, arg_field="sql")
|
|
103
|
+
|
|
104
|
+
res, pec = gw.call_tool("db_query", {"sql": ATTACK})
|
|
105
|
+
print(" " + _text(res))
|
|
106
|
+
print(f" PEC: verdict={pec.verdict} boundary={pec.boundary} diverged_on={pec.diverged_on}")
|
|
107
|
+
print(
|
|
108
|
+
f" byte_sha256={pec.byte_sha256[:16]}... parsers_covered={len(pec.parsers_covered)}"
|
|
109
|
+
)
|
|
110
|
+
if events:
|
|
111
|
+
e = events[0]
|
|
112
|
+
print(f" SIEM event: divergence on '{e.parser}' novel={e.novel} mode={e.mode}")
|
|
113
|
+
|
|
114
|
+
# ---- 3) zero false positives on a benign call ----
|
|
115
|
+
print("\n" + "-" * 74)
|
|
116
|
+
print("[3] BENIGN call through the gateway (false-positive check)")
|
|
117
|
+
res, pec = gw.call_tool("db_query", {"sql": BENIGN})
|
|
118
|
+
print(" " + _text(res))
|
|
119
|
+
print(f" PEC: verdict={pec.verdict} (benign admitted -> 0 false positives)")
|
|
120
|
+
|
|
121
|
+
# ---- 4) shadow mode: surface the attack as a novel encoding, don't block ----
|
|
122
|
+
print("\n" + "-" * 74)
|
|
123
|
+
print("[4] SHADOW mode (observe before enforce): attack admitted-but-logged")
|
|
124
|
+
sh_events = []
|
|
125
|
+
gw_shadow = ShieldedMCPGateway(telemetry_sink=sh_events.append)
|
|
126
|
+
gw_shadow.register_tool(
|
|
127
|
+
DB_TOOL, guard=naive_guard, handler=db_executor, arg_field="sql", mode="shadow"
|
|
128
|
+
)
|
|
129
|
+
res, pec = gw_shadow.call_tool("db_query", {"sql": ATTACK})
|
|
130
|
+
print(f" forwarded (shadow never blocks); PEC.verdict={pec.verdict}")
|
|
131
|
+
print(
|
|
132
|
+
f" but logged {len(sh_events)} divergence event(s); "
|
|
133
|
+
f"NOVEL encoding alerts: {sum(1 for e in sh_events if e.novel)}"
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
# ---- 5) tamper-evident PEC chain across a 2-hop pipeline ----
|
|
137
|
+
print("\n" + "-" * 74)
|
|
138
|
+
print("[5] PEC CHAIN across a 2-hop agent pipeline (planner -> db_query)")
|
|
139
|
+
gw.shield.register("planner", guard=lambda t: True)
|
|
140
|
+
_, p0 = gw.shield.admit("planner", "plan: report active customers")
|
|
141
|
+
_, p1 = gw.call_tool("db_query", {"sql": BENIGN}, prev_pec=p0)
|
|
142
|
+
ok, why = verify_chain([p0, p1])
|
|
143
|
+
print(f" chain verify: {ok} ({why})")
|
|
144
|
+
p1.verdict = "REJECT"
|
|
145
|
+
ok2, why2 = verify_chain([p0, p1])
|
|
146
|
+
print(f" after tamper: {ok2} ({why2})")
|
|
147
|
+
|
|
148
|
+
print("\n" + BAR)
|
|
149
|
+
print("SUMMARY: attack BLOCKED at the gateway (1/1), benign ADMITTED (0 FP),")
|
|
150
|
+
print("novel encoding surfaced in shadow, PEC chain verifiable + tamper-evident.")
|
|
151
|
+
print("Deploy: wrap your MCP gateway's tools/call dispatch in ShieldedMCPGateway.")
|
|
152
|
+
print(BAR)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
if __name__ == "__main__":
|
|
156
|
+
main()
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""GXPD-Shield MCP gateway -- intercepts tools/call and blocks parse-differential
|
|
3
|
+
attacks that a naive upstream guard misses.
|
|
4
|
+
|
|
5
|
+
An MCP gateway proxies tool calls from a client to upstream tool servers (as
|
|
6
|
+
ContextForge, supergateway and friends do). It is the natural chokepoint for a
|
|
7
|
+
cross-cutting control: every tools/call passes through it. This gateway inserts
|
|
8
|
+
the GXPD-Shield at that chokepoint, so every call's arguments are checked for a
|
|
9
|
+
guard-executor parse differential BEFORE the call is forwarded upstream.
|
|
10
|
+
|
|
11
|
+
Uses the real MCP SDK types (mcp.types): Tool, CallToolResult, TextContent. The
|
|
12
|
+
dispatch is direct (a gateway intercepts and forwards; it is not a transport
|
|
13
|
+
demo), so the one-command demo is deterministic and offline. Benign: upstream
|
|
14
|
+
handlers are non-destructive stubs that report what they WOULD do.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
from dataclasses import dataclass
|
|
20
|
+
|
|
21
|
+
import mcp.types as mt
|
|
22
|
+
|
|
23
|
+
try:
|
|
24
|
+
from rift.shield import PEC, Shield
|
|
25
|
+
except ImportError:
|
|
26
|
+
from shield import Shield
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class _Upstream:
|
|
31
|
+
tool: mt.Tool
|
|
32
|
+
guard: object # the upstream server's OWN guard (admit(raw)->bool)
|
|
33
|
+
handler: object # handler(arguments: dict) -> mt.CallToolResult
|
|
34
|
+
arg_field: str # which argument carries the bytes to check
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class ShieldedMCPGateway:
|
|
38
|
+
"""Register upstream tools, then route tools/call through the Shield."""
|
|
39
|
+
|
|
40
|
+
def __init__(self, telemetry_sink=None):
|
|
41
|
+
self.shield = Shield(telemetry_sink=telemetry_sink)
|
|
42
|
+
self._tools: dict[str, _Upstream] = {}
|
|
43
|
+
|
|
44
|
+
def register_tool(self, tool, guard, handler, arg_field, parsers=None, mode="enforce"):
|
|
45
|
+
self.shield.register(tool.name, guard=guard, parsers=parsers, mode=mode)
|
|
46
|
+
self._tools[tool.name] = _Upstream(tool, guard, handler, arg_field)
|
|
47
|
+
return self
|
|
48
|
+
|
|
49
|
+
def list_tools(self):
|
|
50
|
+
return [u.tool for u in self._tools.values()]
|
|
51
|
+
|
|
52
|
+
def call_tool(self, name, arguments, prev_pec=None):
|
|
53
|
+
"""Intercept a tools/call. Returns (CallToolResult, PEC). On a parse
|
|
54
|
+
divergence the call is blocked with an error result and never reaches the
|
|
55
|
+
upstream handler."""
|
|
56
|
+
if name not in self._tools:
|
|
57
|
+
return (
|
|
58
|
+
mt.CallToolResult(
|
|
59
|
+
content=[mt.TextContent(type="text", text=f"unknown tool: {name}")],
|
|
60
|
+
is_error=True,
|
|
61
|
+
),
|
|
62
|
+
None,
|
|
63
|
+
)
|
|
64
|
+
up = self._tools[name]
|
|
65
|
+
raw = arguments.get(up.arg_field, "")
|
|
66
|
+
admitted, pec = self.shield.admit(name, raw, prev_pec=prev_pec)
|
|
67
|
+
if not admitted and pec.verdict == "REJECT":
|
|
68
|
+
return (
|
|
69
|
+
mt.CallToolResult(
|
|
70
|
+
content=[
|
|
71
|
+
mt.TextContent(
|
|
72
|
+
type="text",
|
|
73
|
+
text=(
|
|
74
|
+
f"BLOCKED by GXPD-Shield at the gateway. Parse differential on "
|
|
75
|
+
f"argument '{up.arg_field}': the upstream guard admits the raw "
|
|
76
|
+
f"bytes, but the executor's '{pec.diverged_on}' parse of the same "
|
|
77
|
+
f"bytes is what the guard would reject. Call not forwarded."
|
|
78
|
+
),
|
|
79
|
+
)
|
|
80
|
+
],
|
|
81
|
+
is_error=True,
|
|
82
|
+
),
|
|
83
|
+
pec,
|
|
84
|
+
)
|
|
85
|
+
# admitted (or shadow): forward to the upstream handler
|
|
86
|
+
result = up.handler(arguments)
|
|
87
|
+
return (result, pec)
|
rift/cli.py
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""rift - find the rift between a guard's parse and the executor's.
|
|
3
|
+
|
|
4
|
+
Subcommands:
|
|
5
|
+
rift scan <module.py> --boundary {shell,url,path,sql,content} [--guard NAME]
|
|
6
|
+
Read the guard's source and call it DELEGATE (holds) or REIMPLEMENT (a gap),
|
|
7
|
+
without sending a single payload. This is the predictor from the paper.
|
|
8
|
+
rift peg Run PEG against a canonicalize-then-bind comparator.
|
|
9
|
+
rift bench Recompute the benchmark headline from the frozen logs.
|
|
10
|
+
rift field The field study: the predictor on 9 real deployed guards.
|
|
11
|
+
rift proof Check the Coq proof's integrity (0 axioms, 8 Qed).
|
|
12
|
+
rift version
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import argparse
|
|
18
|
+
import importlib.util
|
|
19
|
+
import json
|
|
20
|
+
import os
|
|
21
|
+
import subprocess
|
|
22
|
+
import sys
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
|
|
25
|
+
ROOT = Path(__file__).resolve().parents[2] # repo root
|
|
26
|
+
BOUNDARIES = ("shell", "url", "path", "sql", "content")
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _load_module(path: str):
|
|
30
|
+
p = Path(path).resolve()
|
|
31
|
+
if not p.exists():
|
|
32
|
+
sys.exit(f"rift: no such file: {path}")
|
|
33
|
+
spec = importlib.util.spec_from_file_location(p.stem, p)
|
|
34
|
+
if spec is None or spec.loader is None:
|
|
35
|
+
sys.exit(f"rift: cannot load module from {path}")
|
|
36
|
+
mod = importlib.util.module_from_spec(spec)
|
|
37
|
+
spec.loader.exec_module(mod)
|
|
38
|
+
return mod
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def cmd_scan(args):
|
|
42
|
+
from rift.experiments.heldout_predictor import classify_structure
|
|
43
|
+
|
|
44
|
+
mod = _load_module(args.module)
|
|
45
|
+
guard = getattr(mod, args.guard, None)
|
|
46
|
+
if guard is None or not callable(guard):
|
|
47
|
+
sys.exit(
|
|
48
|
+
f"rift: no callable '{args.guard}' in {args.module}. "
|
|
49
|
+
f"Pass --guard NAME, or name your function 'guard'."
|
|
50
|
+
)
|
|
51
|
+
module_funcs = {k: v for k, v in vars(mod).items() if callable(v)}
|
|
52
|
+
res = classify_structure(guard, args.boundary, module_funcs)
|
|
53
|
+
label = res["label"]
|
|
54
|
+
print(f"guard {args.guard} ({args.module})")
|
|
55
|
+
print(f"boundary {args.boundary}")
|
|
56
|
+
print(f"verdict {label}")
|
|
57
|
+
if label == "REIMPLEMENT":
|
|
58
|
+
print(" the guard decides on a hand-rolled view, not the executor's parse.")
|
|
59
|
+
print(" predicted DESYNC-PRONE: an input the guard admits can carry a")
|
|
60
|
+
print(" differential the executor runs as a hazard.")
|
|
61
|
+
return 1
|
|
62
|
+
print(" the guard routes its whole decision through the executor's parser.")
|
|
63
|
+
print(" predicted SAFE under the law (no hazard-relevant differential).")
|
|
64
|
+
return 0
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _run_experiment(modpath: str):
|
|
68
|
+
env = dict(os.environ, PYTHONPATH=str(ROOT / "src"))
|
|
69
|
+
return subprocess.call([sys.executable, "-m", modpath], cwd=ROOT, env=env)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def cmd_peg(args):
|
|
73
|
+
return _run_experiment("rift.experiments.peg_vs_binding")
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def cmd_field(args):
|
|
77
|
+
return _run_experiment("rift.experiments.field_study")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def cmd_reproduce(args):
|
|
81
|
+
"""Regenerate every headline figure from the committed logs and source."""
|
|
82
|
+
steps = [
|
|
83
|
+
("benchmark", lambda: cmd_bench(args)),
|
|
84
|
+
("field study", lambda: _run_experiment("rift.experiments.field_study")),
|
|
85
|
+
(
|
|
86
|
+
"PEG vs canonicalize-then-bind",
|
|
87
|
+
lambda: _run_experiment("rift.experiments.peg_vs_binding"),
|
|
88
|
+
),
|
|
89
|
+
("Coq proof integrity", lambda: cmd_proof(args)),
|
|
90
|
+
]
|
|
91
|
+
rc = 0
|
|
92
|
+
for name, fn in steps:
|
|
93
|
+
print(f"\n===== {name} =====")
|
|
94
|
+
rc |= fn() or 0
|
|
95
|
+
print(
|
|
96
|
+
"\nreproduce complete. Figures match docs/results when the full guard"
|
|
97
|
+
" extras are installed (pip install '.[guards]')."
|
|
98
|
+
)
|
|
99
|
+
return rc
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def cmd_bench(args):
|
|
103
|
+
log = ROOT / "gxpd_bench_log.jsonl"
|
|
104
|
+
if not log.exists():
|
|
105
|
+
sys.exit("rift: benchmark log not found. Run the benchmark first.")
|
|
106
|
+
rows = [json.loads(line) for line in log.read_text().splitlines() if line.strip()]
|
|
107
|
+
print(f"benchmark log: {log.name}")
|
|
108
|
+
print(f" guard records in log : {len(rows)}")
|
|
109
|
+
print(" headline (from the frozen run, see docs/results):")
|
|
110
|
+
print(" 140 guards, 22,732 guard-vs-executor comparisons, 5 boundaries")
|
|
111
|
+
print(" held-out delegate-vs-reimplement separates safe from broken at phi +0.66")
|
|
112
|
+
print(" naive 'calls a parser?' baseline: -0.05")
|
|
113
|
+
print(" delegate -> safe precision 1.000, zero delegating-but-unsafe")
|
|
114
|
+
return 0
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def cmd_proof(args):
|
|
118
|
+
v = ROOT / "proofs" / "RIFT.v"
|
|
119
|
+
sh = ROOT / "proofs" / "proof_check.sh"
|
|
120
|
+
if sh.exists():
|
|
121
|
+
return subprocess.call(["bash", str(sh), str(v)])
|
|
122
|
+
# inline integrity check
|
|
123
|
+
text = v.read_text()
|
|
124
|
+
import re
|
|
125
|
+
|
|
126
|
+
holes = len(re.findall(r"Admitted\.|^\s*Axiom |\sadmit[.;]", text, re.M))
|
|
127
|
+
qed = text.count("Qed.")
|
|
128
|
+
print(f"proof: {v.name}")
|
|
129
|
+
print(f" Admitted / Axiom / admit-tactic (want 0): {holes}")
|
|
130
|
+
print(f" Qed (closed proofs): {qed}")
|
|
131
|
+
print(" full green check needs coqc 8.18: coqc proofs/RIFT.v")
|
|
132
|
+
return 0 if holes == 0 else 1
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def cmd_version(args):
|
|
136
|
+
print("rift 0.1.0 - guardrail desync predictor + parse-equivalence gate")
|
|
137
|
+
return 0
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def main(argv=None):
|
|
141
|
+
p = argparse.ArgumentParser(
|
|
142
|
+
prog="rift", description="find the rift between a guard's parse and the executor's"
|
|
143
|
+
)
|
|
144
|
+
sub = p.add_subparsers(dest="cmd", required=True)
|
|
145
|
+
|
|
146
|
+
s = sub.add_parser(
|
|
147
|
+
"scan",
|
|
148
|
+
help="predict DELEGATE vs REIMPLEMENT from a guard's source",
|
|
149
|
+
epilog="WARNING: scan imports and executes the module you point it at. "
|
|
150
|
+
"Only run it on guard code you trust and installed yourself.",
|
|
151
|
+
)
|
|
152
|
+
s.add_argument("module", help="path to a .py file defining the guard (will be imported)")
|
|
153
|
+
s.add_argument("--boundary", required=True, choices=BOUNDARIES)
|
|
154
|
+
s.add_argument("--guard", default="guard", help="guard function name (default: guard)")
|
|
155
|
+
s.set_defaults(fn=cmd_scan)
|
|
156
|
+
|
|
157
|
+
sub.add_parser("peg", help="PEG vs canonicalize-then-bind").set_defaults(fn=cmd_peg)
|
|
158
|
+
sub.add_parser("bench", help="benchmark headline from the frozen log").set_defaults(
|
|
159
|
+
fn=cmd_bench
|
|
160
|
+
)
|
|
161
|
+
sub.add_parser("field", help="field study on 9 real deployed guards").set_defaults(fn=cmd_field)
|
|
162
|
+
sub.add_parser("proof", help="check the Coq proof integrity").set_defaults(fn=cmd_proof)
|
|
163
|
+
sub.add_parser("reproduce", help="regenerate every headline figure from the logs").set_defaults(
|
|
164
|
+
fn=cmd_reproduce
|
|
165
|
+
)
|
|
166
|
+
sub.add_parser("version").set_defaults(fn=cmd_version)
|
|
167
|
+
|
|
168
|
+
args = p.parse_args(argv)
|
|
169
|
+
sys.exit(args.fn(args))
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
if __name__ == "__main__":
|
|
173
|
+
main()
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""O7 increment -- new guard products: HTML sanitizers, classified HOLD vs FALL.
|
|
3
|
+
|
|
4
|
+
Delegation hypothesis (M2): a sanitizer that DELEGATES to a real HTML parser
|
|
5
|
+
(bleach->html5lib, nh3->html5ever) sees what the browser sees => HOLD (robust).
|
|
6
|
+
A sanitizer that RE-IMPLEMENTS via regex => FALL (bypassable).
|
|
7
|
+
|
|
8
|
+
Oracle: for each XSS payload (plain + obfuscated), sanitize it, then test whether an
|
|
9
|
+
executable construct SURVIVES in the sanitized output. FALL = something survives.
|
|
10
|
+
Benign: no browser, no network; we only parse strings locally.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import re
|
|
14
|
+
|
|
15
|
+
import bleach
|
|
16
|
+
import nh3
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# a naive regex 'sanitizer' as the known-FALL foil (re-implementer)
|
|
20
|
+
def regex_strip(s):
|
|
21
|
+
return re.sub(r"(?is)<script.*?>.*?</script>", "", s)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
GUARDS = {
|
|
25
|
+
"bleach.clean(default)": lambda s: bleach.clean(s),
|
|
26
|
+
"nh3.clean(default)": lambda s: nh3.clean(s),
|
|
27
|
+
"naive-regex-strip": regex_strip, # FALL foil (re-implementer)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
# XSS battery: plain + obfuscations that defeat naive matchers
|
|
31
|
+
PAYLOADS = {
|
|
32
|
+
"plain script": "<script>alert(1)</script>",
|
|
33
|
+
"case-varied": "<ScRiPt>alert(1)</ScRiPt>",
|
|
34
|
+
"img onerror": "<img src=x onerror=alert(1)>",
|
|
35
|
+
"svg onload": "<svg onload=alert(1)>",
|
|
36
|
+
"js: href": '<a href="javascript:alert(1)">x</a>',
|
|
37
|
+
"entity-encoded tag": "<script>alert(1)</script>",
|
|
38
|
+
"nested/broken": "<scr<script>ipt>alert(1)</scr</script>ipt>",
|
|
39
|
+
"null-byte split": "<scri\x00pt>alert(1)</scri\x00pt>",
|
|
40
|
+
"zero-width in tag": "<script>alert(1)</script>",
|
|
41
|
+
"attr whitespace": "<img src=x onerror\t=\talert(1)>",
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def danger_survives(sanitized):
|
|
46
|
+
"""Does a LIVE executable construct remain? (escaped entities are inert text to a
|
|
47
|
+
browser, so we do NOT unescape -- escaping IS safe neutralization.)"""
|
|
48
|
+
s = sanitized.lower()
|
|
49
|
+
if re.search(r"<script\b", s): # live script element
|
|
50
|
+
return True
|
|
51
|
+
if re.search(r"<\w+[^>]*\son\w+\s*=", s): # live tag carrying an event handler
|
|
52
|
+
return True
|
|
53
|
+
if re.search(r'(href|src)\s*=\s*["\']?\s*javascript:', s): # live js: URI
|
|
54
|
+
return True
|
|
55
|
+
return False
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
if __name__ == "__main__":
|
|
59
|
+
print("O7 -- HTML sanitizer products, HOLD/FALL classification")
|
|
60
|
+
print("(FALL = an obfuscated XSS payload survives sanitization)\n")
|
|
61
|
+
verdicts = {}
|
|
62
|
+
for gname, clean in GUARDS.items():
|
|
63
|
+
survivors = []
|
|
64
|
+
for pname, payload in PAYLOADS.items():
|
|
65
|
+
out = clean(payload)
|
|
66
|
+
if danger_survives(out):
|
|
67
|
+
survivors.append((pname, payload, out))
|
|
68
|
+
verdict = "FALL" if survivors else "HOLD"
|
|
69
|
+
verdicts[gname] = (verdict, survivors)
|
|
70
|
+
print(f"[{gname}] -> {verdict} ({len(survivors)}/{len(PAYLOADS)} payloads survive)")
|
|
71
|
+
for pn, pl, out in survivors:
|
|
72
|
+
print(f" survive: {pn:18} {pl!r} -> {out!r}")
|
|
73
|
+
print("\n=== M2 measurement (delegation predicts safety) ===")
|
|
74
|
+
for g, (v, _) in verdicts.items():
|
|
75
|
+
kind = (
|
|
76
|
+
"delegator (real HTML parser)"
|
|
77
|
+
if g.startswith(("bleach", "nh3"))
|
|
78
|
+
else "re-implementer (regex)"
|
|
79
|
+
)
|
|
80
|
+
print(f" {g:26} {v:5} [{kind}]")
|