Skip to content

Commit 27501b0

Browse files
Pigbibicodex
andcommitted
fix: screen scope secret markers
Co-Authored-By: Codex <noreply@openai.com>
1 parent ea19ccc commit 27501b0

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

scripts/canonical_typed_identity.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,19 @@ def _text(value: Any, limit: int = 512) -> str:
6060
if any(unicodedata.category(char).startswith("C") for char in value) or normalized_size > limit:
6161
raise IdentityError("invalid text")
6262
return value
63+
def _secret_marker(value: str) -> bool:
64+
return any(pattern.search(value) for pattern in SECRET_PATTERNS)
6365
def _scope(value: Any) -> dict[str, str]:
6466
scope = _obj(value, {"repo", "file", "category"})
6567
repo = _text(scope["repo"], 140)
68+
if _secret_marker(repo):
69+
raise IdentityError("reserved secret marker")
6670
parts = repo.split("/")
6771
if len(parts) != 2 or not OWNER.fullmatch(parts[0]) or "--" in parts[0] or not REPO.fullmatch(parts[1]) or parts[1] in {".", ".."}:
6872
raise IdentityError("invalid repo")
6973
path = _text(scope["file"], 1024)
74+
if _secret_marker(path):
75+
raise IdentityError("reserved secret marker")
7076
if path.startswith("/") or "\\" in path or any(part in {"", ".", ".."} for part in path.split("/")):
7177
raise IdentityError("invalid path")
7278
category = _text(scope["category"], 32)
@@ -81,13 +87,13 @@ def _token(value: Any) -> dict[str, Any]:
8187
if kind == "secret_ref":
8288
ref = _obj(token["value"], {"type", "role", "position"})
8389
typ, role, position = _text(ref["type"], 32), _text(ref["role"], 32), ref["position"]
84-
if any(pattern.search(candidate) for pattern in SECRET_PATTERNS for candidate in (typ, role)):
90+
if any(_secret_marker(candidate) for candidate in (typ, role)):
8591
raise IdentityError("reserved secret marker")
8692
if not re.fullmatch(r"[a-z][a-z0-9_.-]{0,31}", typ) or not re.fullmatch(r"[a-z][a-z0-9_.-]{0,31}", role) or isinstance(position, bool) or not isinstance(position, int) or not 0 <= position <= 1024:
8793
raise IdentityError("invalid secret reference")
8894
return {"kind": kind, "value": {"type": typ, "role": role, "position": position}}
8995
item = _text(token["value"])
90-
if any(pattern.search(item) for pattern in SECRET_PATTERNS):
96+
if _secret_marker(item):
9197
raise IdentityError("reserved secret marker")
9298
if kind == "operator" and item not in OPS or kind == "policy_state" and item not in POLICY or kind == "identifier" and not IDENTIFIER.fullmatch(item):
9399
raise IdentityError("invalid typed token")

tests/test_canonical_typed_identity_r1b.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,11 @@ def test_secret_ref_metadata_rejects_reserved_markers(self):
9292
value = self.payload()
9393
value["predicates"] = [[self.tok("secret_ref", {"type": marker, "role": "auth", "position": 0})]]
9494
self.invalid(value)
95+
def test_scope_rejects_secret_markers_but_allows_ordinary_names(self):
96+
for repo, path in (("owner/ghs_1234567890abcdef", "service/review.py"), ("owner/ASIAABCDEFGHIJKLMNOP", "service/review.py"), ("owner/audit-bridge", "service/ghs_1234567890abcdef.py")):
97+
value = self.payload()
98+
value["scope"]["repo"], value["scope"]["file"] = repo, path
99+
self.invalid(value)
100+
value = self.payload()
101+
value["scope"]["repo"], value["scope"]["file"] = "owner/Eurasia", "service/keyJson.py"
102+
validate_identity(value)

0 commit comments

Comments
 (0)