fix(ci): fail-closed normalized service-secret mount contracts and eight negative grants

This commit is contained in:
victorsteele428 committed 2026-11-23 16:35:28 +00:00
1 parent 64d8c0830e
commit daaa8b259a
1 file changed
+176 -7
+176 -7
View File
@@ -318,19 +318,113 @@ def diagnose_normalized_deploy(c: object) -> None:
print(prefix + "_REPLICAS_" + state, flush=True) print(prefix + "_REPLICAS_" + state, flush=True)
def secret_target_class(source: object, target: object) -> str:
"""Classify only known safe forms; never return or log raw input values."""
if type(source) is not str or type(target) is not str:
return "INVALID_TYPE"
if target == source:
return "SOURCE_NAME"
if target == "/run/secrets/" + source:
return "CANONICAL_MOUNT"
return "UNEXPECTED"
def safe_root_owner(value: object) -> bool:
return (type(value) is str and value == "0") or (
type(value) is int and value == 0
)
def safe_readonly_mode(value: object) -> bool:
# Compose default 0444 is decimal 292 in config --format json.
return (type(value) is int and value == 0o444) or (
type(value) is str and value == "0444"
)
def diagnose_normalized_service_grants(c: object) -> None:
"""Known service/index and fixed security classifications only.
Neither source/target strings, grant values, arbitrary keys nor
secret contents ever enter the diagnostic output.
"""
if type(c) is not dict or type(c.get("services")) is not dict:
return
for name, expected in SERVICE_SECRETS.items():
svc = c["services"].get(name)
if type(svc) is not dict:
continue
prefix = "ZITPLANE_P08_SECRET_GRANT_" + name.replace("-", "_").upper()
grants = svc.get("secrets", [])
if type(grants) is not list:
print(prefix + "_LIST_UNEXPECTED_TYPE", flush=True)
continue
print(prefix + "_LIST_" + ("EMPTY" if not grants else "NONEMPTY"),
flush=True)
seen = set()
for index, entry in enumerate(grants):
tag = prefix + "_ITEM_" + str(index) + "_"
if type(entry) is str:
source = entry
print(tag + "STRING", flush=True)
print(tag + "TARGET_SOURCE_NAME", flush=True)
elif type(entry) is dict:
source = entry.get("source")
print(tag + "OBJECT", flush=True)
target_class = secret_target_class(source, entry.get("target"))
print(tag + "TARGET_" + target_class, flush=True)
if set(entry) - {"source", "target", "uid", "gid", "mode"}:
print(tag + "UNKNOWN_METADATA_PRESENT", flush=True)
for key in ("uid", "gid"):
if key in entry:
print(tag + key.upper() + "_" +
("ROOT_DEFAULT" if safe_root_owner(entry[key])
else "UNSAFE"), flush=True)
if "mode" in entry:
print(tag + "MODE_" +
("READONLY_DEFAULT" if safe_readonly_mode(entry["mode"])
else "UNSAFE"), flush=True)
else:
print(tag + "UNEXPECTED_TYPE", flush=True)
continue
if type(source) is not str:
print(tag + "SOURCE_INVALID_TYPE", flush=True)
continue
source_class = ("EXPECTED" if source in expected else
"KNOWN_OTHER" if source in SECRETS else "UNKNOWN")
print(tag + "SOURCE_" + source_class, flush=True)
if source in seen:
print(tag + "DUPLICATE_SOURCE", flush=True)
seen.add(source)
if len(grants) != len(expected) or seen != set(expected):
print(prefix + "_REQUIRED_SET_OR_COUNT_MISMATCH", flush=True)
def service_secret_refs(service: dict) -> list[str]: def service_secret_refs(service: dict) -> list[str]:
"""Only exact authorized symbolic mounts, canonical target, default perms."""
entries = service.get("secrets", []) entries = service.get("secrets", [])
check(type(entries) is list, "SERVICE_SECRET_REFS") check(type(entries) is list, "SERVICE_SECRET_REFS")
values = [] values = []
for entry in entries: for entry in entries:
if isinstance(entry, str): if type(entry) is str:
values.append(entry) source = entry
elif isinstance(entry, dict): elif type(entry) is dict:
check(set(entry).issubset({"source", "target", "uid", "gid", "mode"}) and check(set(entry).issubset({"source", "target", "uid", "gid", "mode"})
entry.get("target") == entry.get("source"), "SERVICE_SECRET_REFS") and "source" in entry and "target" in entry,
values.append(entry.get("source")) "SERVICE_SECRET_REFS")
source = entry["source"]
check(secret_target_class(source, entry["target"]) in
{"SOURCE_NAME", "CANONICAL_MOUNT"}, "SERVICE_SECRET_REFS")
for field in ("uid", "gid"):
check(field not in entry or safe_root_owner(entry[field]),
"SERVICE_SECRET_REFS")
check("mode" not in entry or safe_readonly_mode(entry["mode"]),
"SERVICE_SECRET_REFS")
else: else:
raise ContractError("SERVICE_SECRET_REFS") raise ContractError("SERVICE_SECRET_REFS")
check(type(source) is str and source in SECRETS, "SERVICE_SECRET_REFS")
values.append(source)
check(len(values) == len(set(values)), "SERVICE_SECRET_REFS")
return values return values
@@ -418,7 +512,11 @@ def validate_compose(c: object, p: dict) -> None:
check(not re.search(r"(PASSWORD|SECRET|TOKEN|PAT|MASTERKEY)$", key), check(not re.search(r"(PASSWORD|SECRET|TOKEN|PAT|MASTERKEY)$", key),
"INLINE_SECRET") "INLINE_SECRET")
check(env == env_expected, "ENV_POLICY") check(env == env_expected, "ENV_POLICY")
check(service_secret_refs(service) == list(SERVICE_SECRETS[name]), # Secret mount order is not a security property of normalized
# Compose JSON. Exact per-service identity and count ARE mandatory.
actual = service_secret_refs(service)
required = SERVICE_SECRETS[name]
check(len(actual) == len(required) and set(actual) == set(required),
"SERVICE_SECRET_REFS") "SERVICE_SECRET_REFS")
check(p["required_secret_references"] == list(SECRETS), "POLICY_SECRETS") check(p["required_secret_references"] == list(SECRETS), "POLICY_SECRETS")
@@ -571,6 +669,75 @@ def negative_cases(p: dict, c: dict) -> None:
+ "_REJECTED_PASS", flush=True) + "_REJECTED_PASS", flush=True)
print("ZITPLANE_P08_ALL_8_DEPLOY_NEGATIVE_CASES_PASS", flush=True) print("ZITPLANE_P08_ALL_8_DEPLOY_NEGATIVE_CASES_PASS", flush=True)
# Grant security: test against the REAL normalized grant entry shape.
# Deliberate mutations never contact Docker, read secrets or log strings.
def source_mapping(item: object) -> dict:
return ({"source": item, "target": item} if type(item) is str
else copy.deepcopy(item))
def secret_case(compose: dict, label: str) -> None:
grants = compose["services"]["account"]["secrets"]
if label == "MISSING":
grants.pop()
elif label == "DUPLICATE":
grants.append(copy.deepcopy(grants[0]))
elif label == "EXTRA_AUTHORIZED_ELSEWHERE":
grants.append({"source": "zitadel_masterkey",
"target": "/run/secrets/zitadel_masterkey"})
elif label == "UNKNOWN_SOURCE":
grants.append({"source": "unapproved_symbolic_secret",
"target": "/run/secrets/unapproved_symbolic_secret"})
elif label == "WRONG_TARGET":
grant = source_mapping(grants[0])
grant["target"] = "/tmp/not-approved"
grants[0] = grant
elif label == "UNEXPECTED_METADATA":
grant = source_mapping(grants[0])
grant["privileged_grant"] = True
grants[0] = grant
elif label == "UNSAFE_UID":
grant = source_mapping(grants[0])
grant["uid"] = "1000"
grants[0] = grant
elif label == "WORLD_WRITABLE":
grant = source_mapping(grants[0])
grant["mode"] = 0o666
grants[0] = grant
labels = (
"MISSING", "DUPLICATE", "EXTRA_AUTHORIZED_ELSEWHERE",
"UNKNOWN_SOURCE", "WRONG_TARGET", "UNEXPECTED_METADATA",
"UNSAFE_UID", "WORLD_WRITABLE",
)
for label in labels:
bad_compose = copy.deepcopy(c)
secret_case(bad_compose, label)
try:
validate_all(p, bad_compose)
except ContractError as error:
check(error.code == "SERVICE_SECRET_REFS",
"SECRET_NEGATIVE_WRONG_CLASS_" + label)
else:
raise ContractError("SECRET_NEGATIVE_NOT_REJECTED_" + label)
print("ZITPLANE_P08_SECRET_NEGATIVE_" + label +
"_REJECTED_PASS", flush=True)
print("ZITPLANE_P08_ALL_8_SECRET_NEGATIVE_CASES_PASS", flush=True)
# A canonical mount path is a harmless representation change, not a
# new grant. Prove exact source/target mapping with each known service.
canonical = copy.deepcopy(c)
for service_name, expected in SERVICE_SECRETS.items():
grants = canonical["services"][service_name].get("secrets", [])
for i, grant in enumerate(grants):
normalized = source_mapping(grant)
source = normalized.get("source")
check(type(source) is str and source in expected,
"INERT_GRANT_SOURCE")
normalized["target"] = "/run/secrets/" + source
grants[i] = normalized
validate_all(p, canonical)
print("ZITPLANE_P08_CANONICAL_SECRET_MOUNTS_ACCEPTED_PASS", flush=True)
def workflow_safety() -> None: def workflow_safety() -> None:
body = WORKFLOW_PATH.read_text(encoding="utf-8") body = WORKFLOW_PATH.read_text(encoding="utf-8")
@@ -610,7 +777,9 @@ def main() -> int:
verify_tracked_zero_replica_fixture() verify_tracked_zero_replica_fixture()
diagnose_normalized_runtime_fields(compose) diagnose_normalized_runtime_fields(compose)
diagnose_normalized_deploy(compose) diagnose_normalized_deploy(compose)
diagnose_normalized_service_grants(compose)
validate_all(policy, compose) validate_all(policy, compose)
print("ZITPLANE_P08_NORMALIZED_SECRET_GRANT_POLICY_PASS", flush=True)
print("ZITPLANE_P08_NORMALIZED_DEPLOY_METADATA_POLICY_PASS", flush=True) print("ZITPLANE_P08_NORMALIZED_DEPLOY_METADATA_POLICY_PASS", flush=True)
print("ZITPLANE_P08_NORMALIZED_ZERO_REPLICA_POLICY_PASS", flush=True) print("ZITPLANE_P08_NORMALIZED_ZERO_REPLICA_POLICY_PASS", flush=True)
print("ZITPLANE_P08_NORMALIZED_RUNTIME_DEFAULTS_POLICY_PASS", flush=True) print("ZITPLANE_P08_NORMALIZED_RUNTIME_DEFAULTS_POLICY_PASS", flush=True)