Files
CASAN/AINative_OKR_CASAN5/packages/casan-harness/scripts/bash/pii-mask.py
T
thanhnvandClaude Opus 4.8 7101af9fd4 feat(plan-01): Phase 4c — hard cutoff, remove .specify compat facade
Repoint every remaining literal `.specify/...` / `docs/input` reference to the real
package/domain location and delete all compat symlinks. The harness now runs purely via
packages/casan-harness/... with no .specify facade; .specify holds ONLY runtime state
(logs/, agentops/alerts.log, level5/central-governance).

Refs fixed (Phase 0.5 only caught `$VAR/.specify/` — these were bare/`__file__`/literal):
- secrets-scan.sh: scan-target excludes -> packages/casan-harness/... (+ apps/okr/domain/corpus)
- loop_common.py: loop-policy.yaml -> harness config (package-relative)
- evidence-pack-build.py: judge-gate test + traceability-matrix.py -> harness/sibling
- phase10-traceability: REQ -> $CASAN_DOMAIN_ROOT/input
- run-casan-pipeline.mjs: model-fallback/drift-detect/rollback-manager -> HARNESS_BASH,
  golden -> GOLDEN_PLAN (apps/okr/domain), with .specify/logs state kept
- casan-step.mjs: requirement fallback restored to docs/input for hermetic sandboxes
- descriptive config (tool-registry/harness-package/drift-policy/hallucination/risk-registry/
  loop-policy.schema + docstrings) repointed for accuracy
- policy-bundle.yaml: 8 policy paths -> packages/casan-harness/...; manifest regenerated +
  re-signed (POLICY_HASHES_VALID files=8, POLICY_SIGNATURE_VALID)

Removed 22 .specify code/config symlinks + docs/input symlink.
Full gate via packages path, NO facade: PASS=64 FAIL=0 SKIP=3.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 12:28:37 +09:00

86 lines
3.0 KiB
Python
Executable File

#!/usr/bin/env python3
"""CASAN H4 PII masker driven by packages/casan-harness/security/pii-rules.yaml.
Reads content on stdin, applies every `action: mask` rule from the rules
file, and writes the masked content to stdout. Type-specific replacement
tokens are preserved so downstream evidence stays stable
(***MASKED_EMAIL***, ***MASKED_PHONE***, ***MASKED_ID***).
This makes pii-rules.yaml the source of truth for PII masking instead of
dead config: editing/removing a rule changes runtime behavior.
"""
import re
import sys
REPLACEMENT_BY_TYPE = {
"email": "***MASKED_EMAIL***",
"phone": "***MASKED_PHONE***",
"personal_id": "***MASKED_ID***",
"address": "***MASKED_ADDRESS***",
}
def load_rules(path):
rules, cur = [], {}
with open(path, encoding="utf-8") as fh:
for raw in fh:
s = raw.strip()
m = re.match(r"-\s*id:\s*(\S+)", s)
if m:
if cur:
rules.append(cur)
cur = {"id": m.group(1)}
continue
m = re.match(r'type:\s*"?([^"\s]+)"?', s)
if m:
cur["type"] = m.group(1)
continue
m = re.match(r'regex:\s*"(.*)"\s*$', s)
if m:
# YAML double-quoted: collapse \\ -> \ to recover the real regex.
cur["regex"] = m.group(1).replace("\\\\", "\\")
continue
m = re.match(r"action:\s*(\S+)", s)
if m:
cur["action"] = m.group(1)
continue
if cur:
rules.append(cur)
return rules
def main():
data = sys.stdin.read()
# SEC-08 (M-06): FAIL CLOSED. Previously a missing rules file, an unreadable
# file, or a broken rule regex all emitted the RAW data — so a mask rule that
# failed to load silently leaked the PII it was meant to hide. Now any such
# condition emits NOTHING and exits non-zero: no unmasked content ever escapes.
if len(sys.argv) < 2:
sys.stderr.write("PII_MASK_FAIL no rules file provided (fail-closed)\n")
return 1
try:
rules = load_rules(sys.argv[1])
except OSError as exc:
sys.stderr.write(f"PII_MASK_FAIL rules file unreadable (fail-closed): {exc}\n")
return 1
# Pre-compile every mask rule; a broken regex is fatal (that PII type would
# otherwise pass through unmasked). Validate all BEFORE emitting anything.
compiled = []
for rule in rules:
if rule.get("action") != "mask" or "regex" not in rule:
continue
token = REPLACEMENT_BY_TYPE.get(rule.get("type", ""), "***MASKED***")
try:
compiled.append((re.compile(rule["regex"]), token))
except re.error as exc:
sys.stderr.write(f"PII_MASK_FAIL bad regex in rule {rule.get('id')} (fail-closed): {exc}\n")
return 1
for rx, token in compiled:
data = rx.sub(token, data)
sys.stdout.write(data)
return 0
if __name__ == "__main__":
raise SystemExit(main())