From 07c91ea5bcb2037e3c7b76261e1eb106b49d223f Mon Sep 17 00:00:00 2001 From: Md Nurnobi Date: Mon, 23 Nov 2026 12:55:45 +0000 Subject: [PATCH] fix(ci): classify Compose normalized runtime defaults and reject explicit executable overrides --- tests/ci/production_contract/validate.py | 107 +++++++++++++++++++++-- 1 file changed, 102 insertions(+), 5 deletions(-) diff --git a/tests/ci/production_contract/validate.py b/tests/ci/production_contract/validate.py index 8948109..4b8e093 100644 --- a/tests/ci/production_contract/validate.py +++ b/tests/ci/production_contract/validate.py @@ -135,6 +135,44 @@ def validate_policy(p: object) -> None: check(p["required_secret_references"] == list(SECRETS), "POLICY_SECRETS") +def diagnose_normalized_runtime_fields(c: object) -> None: + """Report only allowlisted field names, known synthetic service and safe shape. + + Do not print field values, environment, raw JSON or arbitrary keys. This + deliberately runs once on the real Compose-normalized document, never on + the synthetic negative mutations. + """ + if type(c) is not dict or type(c.get("services")) is not dict: + return + for name in ENVS: + service = c["services"].get(name) + if type(service) is not dict: + continue + for field in ("build", "command", "entrypoint", "configs"): + if field not in service: + continue + value = service[field] + if value is None: + shape = "NULL" + elif type(value) is list: + shape = "EMPTY_LIST" if not value else "NONEMPTY_LIST" + elif type(value) is dict: + shape = "EMPTY_DICT" if not value else "NONEMPTY_DICT" + elif type(value) is str: + shape = "EMPTY_STRING" if not value else "STRING" + elif type(value) is bool: + shape = "BOOL" + elif type(value) in (int, float): + shape = "NUMBER" + else: + shape = "OTHER" + print( + "ZITPLANE_P08_NORMALIZED_RUNTIME_FIELD_" + + name.replace("-", "_").upper() + "_" + field.upper() + + "_" + shape, flush=True + ) + + def service_secret_refs(service: dict) -> list[str]: entries = service.get("secrets", []) check(type(entries) is list, "SERVICE_SECRET_REFS") @@ -183,11 +221,29 @@ def validate_compose(c: object, p: dict) -> None: "pid" not in service and "ipc" not in service and "userns_mode" not in service, "PRIVILEGED") - check("build" not in service and "command" not in service and - "entrypoint" not in service and - "configs" not in service, "RUNTIME_BEHAVIOR") - check(set(service).issubset({"image", "deploy", "networks", "environment", "secrets"}), - "SERVICE_FIELDS") + # A Compose config JSON normalization can attach inert optional + # defaults. Accept only a null command/entrypoint (image defaults + # unchanged) or null/empty configs (no mounted config). Any build key, + # non-null command/entrypoint, nonempty configs or unknown key remains + # fail-closed, even for a zero-replica synthetic service. + inert = set() + for field in ("build", "command", "entrypoint", "configs"): + if field not in service: + continue + value = service[field] + if field in ("command", "entrypoint") and value is None: + inert.add(field) + elif field == "configs" and ( + value is None or (type(value) is list and not value) + ): + inert.add(field) + else: + raise ContractError("RUNTIME_BEHAVIOR") + check( + set(service).issubset( + {"image", "deploy", "networks", "environment", "secrets"} | inert + ), "SERVICE_FIELDS" + ) check(service.get("image") == "synthetic.invalid/zitplane/" + {"ingress-policy": "ingress", "zitadel-api": "zitadel-api", @@ -260,6 +316,45 @@ def negative_cases(p: dict, c: dict) -> None: print("ZITPLANE_P08_NEGATIVE_" + label + "_REJECTED_PASS", flush=True) print("ZITPLANE_P08_ALL_25_NEGATIVE_CASES_PASS", flush=True) + # Independently prove inert normalization compatibility: no command + # override is executed and no config is mounted by these null/empty + # defaults. Real nonempty runtime fields below must still be rejected. + inert_compose = copy.deepcopy(c) + inert_service = inert_compose["services"]["ingress-policy"] + inert_service["command"] = None + inert_service["entrypoint"] = None + inert_service["configs"] = [] + validate_all(p, inert_compose) + print("ZITPLANE_P08_INERT_NORMALIZED_DEFAULTS_ACCEPTED_PASS", flush=True) + + runtime_cases = [ + ("NONEMPTY_COMMAND", "RUNTIME_BEHAVIOR", + lambda c: c["services"]["ingress-policy"].__setitem__( + "command", ["sh", "-c", "true"])), + ("NONEMPTY_ENTRYPOINT", "RUNTIME_BEHAVIOR", + lambda c: c["services"]["ingress-policy"].__setitem__( + "entrypoint", ["/bin/sh"])), + ("BUILD_CONTEXT", "RUNTIME_BEHAVIOR", + lambda c: c["services"]["ingress-policy"].__setitem__( + "build", {"context": "."})), + ("NONEMPTY_CONFIGS", "RUNTIME_BEHAVIOR", + lambda c: c["services"]["ingress-policy"].__setitem__( + "configs", [{"source": "unapproved-config"}])), + ] + for label, expected_error, mutate in runtime_cases: + bad_compose = copy.deepcopy(c) + mutate(bad_compose) + try: + validate_all(p, bad_compose) + except ContractError as error: + check(error.code == expected_error, + "RUNTIME_NEGATIVE_WRONG_CLASS_" + label) + else: + raise ContractError("RUNTIME_NEGATIVE_NOT_REJECTED_" + label) + print("ZITPLANE_P08_RUNTIME_NEGATIVE_" + label + + "_REJECTED_PASS", flush=True) + print("ZITPLANE_P08_ALL_4_RUNTIME_NEGATIVE_CASES_PASS", flush=True) + def workflow_safety() -> None: body = WORKFLOW_PATH.read_text(encoding="utf-8") @@ -296,7 +391,9 @@ def main() -> int: return 0 policy = json.loads(POLICY_PATH.read_text(encoding="utf-8")) compose = json.load(sys.stdin) + diagnose_normalized_runtime_fields(compose) validate_all(policy, compose) + print("ZITPLANE_P08_NORMALIZED_RUNTIME_DEFAULTS_POLICY_PASS", flush=True) print("ZITPLANE_P08_CANONICAL_HTTPS_ISSUER_AND_CALLBACK_PASS", flush=True) print("ZITPLANE_P08_LOGIN_CONSOLE_API_H2C_PRIVATE_ROUTING_PASS", flush=True) print("ZITPLANE_P08_HOST_FORWARDED_HEADER_TLS_POLICY_PASS", flush=True)