fix(ci): distinguish safe PATH from credential PAT with bounded OCI env diagnostics and 14 negative cases
This commit is contained in:
1 parent
46e2897380
commit
4452a61b0f
1 file changed
+145
-2
@@ -47,6 +47,23 @@ EXPECTED = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
ENTRYPOINT = ["/app/entrypoint.sh", "node", "apps/login/server.js"]
|
ENTRYPOINT = ["/app/entrypoint.sh", "node", "apps/login/server.js"]
|
||||||
|
# Fixed OCI image-config environment key allowlist. Values are NEVER logged.
|
||||||
|
# The upstream base supplies PATH/NODE_VERSION/YARN_VERSION; the frozen
|
||||||
|
# imported Dockerfile supplies the other keys. Unexpected keys are rejected.
|
||||||
|
SAFE_OCI_ENV_KEYS = frozenset({
|
||||||
|
"PATH", "NODE_VERSION", "YARN_VERSION",
|
||||||
|
"NODE_ENV", "HOSTNAME", "PORT", "NODE_OPTIONS", "SSL_CERT_FILE",
|
||||||
|
"ZITADEL_TLS_ENABLED", "OTEL_SERVICE_NAME",
|
||||||
|
"OTEL_EXPORTER_OTLP_PROTOCOL",
|
||||||
|
})
|
||||||
|
# Match complete underscore-separated credential-bearing segments, not
|
||||||
|
# unanchored substrings: PAT is sensitive; PATH is not a PAT credential.
|
||||||
|
SENSITIVE_OCI_ENV_SEGMENTS = frozenset({
|
||||||
|
"AUTH", "SECRET", "SECRETS", "PASSWORD", "PASSWD",
|
||||||
|
"TOKEN", "PAT", "MASTERKEY", "DATABASE", "DB", "POSTGRES",
|
||||||
|
"PRIVATE", "KEY", "APIKEY", "CREDENTIAL", "CREDENTIALS",
|
||||||
|
"ACCESSKEY", "SESSION", "JWT", "BEARER",
|
||||||
|
})
|
||||||
SOURCE_BLOBS = {
|
SOURCE_BLOBS = {
|
||||||
"tree": "auth/ui/v2",
|
"tree": "auth/ui/v2",
|
||||||
"pnpm_lock": "auth/ui/v2/pnpm-lock.yaml",
|
"pnpm_lock": "auth/ui/v2/pnpm-lock.yaml",
|
||||||
@@ -159,6 +176,60 @@ def expected_labels(lock: dict, git_sha: str, rid: str) -> dict:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def classify_oci_env_key(key: object) -> str:
|
||||||
|
"""Return only a fixed classification: never the raw input key or value."""
|
||||||
|
if type(key) is not str or not key:
|
||||||
|
return "INVALID"
|
||||||
|
if any(part in SENSITIVE_OCI_ENV_SEGMENTS
|
||||||
|
for part in key.upper().split("_")):
|
||||||
|
return "SENSITIVE"
|
||||||
|
if re.fullmatch(r"[A-Z][A-Z0-9_]*", key) is None:
|
||||||
|
return "INVALID"
|
||||||
|
return "SAFE" if key in SAFE_OCI_ENV_KEYS else "UNKNOWN"
|
||||||
|
|
||||||
|
|
||||||
|
def diagnose_oci_image_env(image: object) -> None:
|
||||||
|
"""Only fixed pre-approved benign key names or fixed class labels.
|
||||||
|
|
||||||
|
Image Env values, arbitrary unknown keys, entire docker inspect JSON and
|
||||||
|
credential-bearing names never enter diagnostic output.
|
||||||
|
"""
|
||||||
|
if type(image) is not dict or type(image.get("Config")) is not dict:
|
||||||
|
print("ZITPLANE_P10_OCI_ENV_UNEXPECTED_SHAPE", flush=True)
|
||||||
|
return
|
||||||
|
env = image["Config"].get("Env")
|
||||||
|
if type(env) is not list:
|
||||||
|
print("ZITPLANE_P10_OCI_ENV_UNEXPECTED_SHAPE", flush=True)
|
||||||
|
return
|
||||||
|
legacy_matcher = re.compile(
|
||||||
|
r"(SECRET|PASSWORD|TOKEN|PAT|MASTERKEY|DATABASE|AUTH_|PRIVATE_KEY)",
|
||||||
|
re.I,
|
||||||
|
)
|
||||||
|
seen = set()
|
||||||
|
for entry in env:
|
||||||
|
if type(entry) is not str or "=" not in entry:
|
||||||
|
print("ZITPLANE_P10_OCI_ENV_INVALID_ENTRY_PRESENT", flush=True)
|
||||||
|
continue
|
||||||
|
key = entry.split("=", 1)[0]
|
||||||
|
classification = classify_oci_env_key(key)
|
||||||
|
if classification == "SAFE":
|
||||||
|
# key is a member of the immutable SAFE_OCI_ENV_KEYS set,
|
||||||
|
# so this suffix can never be arbitrary input.
|
||||||
|
print("ZITPLANE_P10_OCI_ENV_KNOWN_" + key + "_PRESENT", flush=True)
|
||||||
|
if legacy_matcher.search(key):
|
||||||
|
print("ZITPLANE_P10_OCI_ENV_LEGACY_MATCH_" + key +
|
||||||
|
"_VERIFIED", flush=True)
|
||||||
|
elif classification == "SENSITIVE":
|
||||||
|
print("ZITPLANE_P10_OCI_ENV_SENSITIVE_KEY_PRESENT", flush=True)
|
||||||
|
elif classification == "UNKNOWN":
|
||||||
|
print("ZITPLANE_P10_OCI_ENV_UNKNOWN_KEY_PRESENT", flush=True)
|
||||||
|
else:
|
||||||
|
print("ZITPLANE_P10_OCI_ENV_INVALID_KEY_PRESENT", flush=True)
|
||||||
|
if key in seen:
|
||||||
|
print("ZITPLANE_P10_OCI_ENV_DUPLICATE_KEY_PRESENT", flush=True)
|
||||||
|
seen.add(key)
|
||||||
|
|
||||||
|
|
||||||
def validate_image(image: object, labels: dict) -> None:
|
def validate_image(image: object, labels: dict) -> None:
|
||||||
require(type(image) is dict, "IMAGE_SHAPE")
|
require(type(image) is dict, "IMAGE_SHAPE")
|
||||||
require(type(image.get("Id")) is str and
|
require(type(image.get("Id")) is str and
|
||||||
@@ -176,10 +247,15 @@ def validate_image(image: object, labels: dict) -> None:
|
|||||||
env = cfg.get("Env", [])
|
env = cfg.get("Env", [])
|
||||||
require(type(env) is list and
|
require(type(env) is list and
|
||||||
all(type(e) is str and "=" in e for e in env), "IMAGE_ENV_TYPE")
|
all(type(e) is str and "=" in e for e in env), "IMAGE_ENV_TYPE")
|
||||||
denied = re.compile(r"(SECRET|PASSWORD|TOKEN|PAT|MASTERKEY|DATABASE|AUTH_|PRIVATE_KEY)", re.I)
|
seen_keys = set()
|
||||||
for e in env:
|
for e in env:
|
||||||
key = e.split("=", 1)[0]
|
key = e.split("=", 1)[0]
|
||||||
require(not denied.search(key), "IMAGE_SECRET_ENV_KEY")
|
classification = classify_oci_env_key(key)
|
||||||
|
require(classification != "SENSITIVE", "IMAGE_SECRET_ENV_KEY")
|
||||||
|
require(classification != "INVALID", "IMAGE_ENV_KEY_FORMAT")
|
||||||
|
require(classification == "SAFE", "IMAGE_UNEXPECTED_ENV_KEY")
|
||||||
|
require(key not in seen_keys, "IMAGE_ENV_DUPLICATE_KEY")
|
||||||
|
seen_keys.add(key)
|
||||||
require("NODE_ENV=production" in env, "IMAGE_PRODUCTION_ENV")
|
require("NODE_ENV=production" in env, "IMAGE_PRODUCTION_ENV")
|
||||||
|
|
||||||
|
|
||||||
@@ -301,6 +377,71 @@ def negative_cases(lock: dict) -> None:
|
|||||||
print("ZITPLANE_P10_OCI_NEGATIVE_" + name + "_PASS", flush=True)
|
print("ZITPLANE_P10_OCI_NEGATIVE_" + name + "_PASS", flush=True)
|
||||||
print("ZITPLANE_P10_ALL_12_PROVENANCE_NEGATIVES_PASS", flush=True)
|
print("ZITPLANE_P10_ALL_12_PROVENANCE_NEGATIVES_PASS", flush=True)
|
||||||
|
|
||||||
|
# A01: original 12 negative fixtures remain unchanged. Demonstrate that
|
||||||
|
# standard PATH passes while complete credential-token segments do not.
|
||||||
|
benign = copy.deepcopy(base)
|
||||||
|
benign["Config"]["Env"].append("PATH=/usr/bin:/bin")
|
||||||
|
validate_image(benign, expected)
|
||||||
|
require(classify_oci_env_key("PATH") == "SAFE" and
|
||||||
|
classify_oci_env_key("LOGIN_CLIENT_PAT") == "SENSITIVE",
|
||||||
|
"A01_ENV_SEGMENT_CONTRACT")
|
||||||
|
print("ZITPLANE_P10_A01_HARMLESS_PATH_ACCEPTED_PASS", flush=True)
|
||||||
|
|
||||||
|
sensitive_cases = (
|
||||||
|
("AUTH_SECRET", "AUTH_SECRET=synthetic"),
|
||||||
|
("ZITADEL_MASTERKEY", "ZITADEL_MASTERKEY=synthetic"),
|
||||||
|
("SERVICE_USER_TOKEN", "ZITADEL_SERVICE_USER_TOKEN=synthetic"),
|
||||||
|
("LOGIN_CLIENT_PAT", "LOGIN_CLIENT_PAT=synthetic"),
|
||||||
|
("DATABASE_PASSWORD", "DATABASE_PASSWORD=synthetic"),
|
||||||
|
("POSTGRES_PASSWORD", "POSTGRES_PASSWORD=synthetic"),
|
||||||
|
("PRIVATE_KEY", "PRIVATE_KEY=synthetic"),
|
||||||
|
("AWS_ACCESS_KEY_ID", "AWS_ACCESS_KEY_ID=synthetic"),
|
||||||
|
("API_TOKEN", "API_TOKEN=synthetic"),
|
||||||
|
)
|
||||||
|
# Labels below are static literals from code, never arbitrary image keys.
|
||||||
|
for label, entry in sensitive_cases:
|
||||||
|
test = copy.deepcopy(benign)
|
||||||
|
test["Config"]["Env"].append(entry)
|
||||||
|
try:
|
||||||
|
validate_image(test, expected)
|
||||||
|
except GateError as error:
|
||||||
|
require(str(error) == "IMAGE_SECRET_ENV_KEY",
|
||||||
|
"A01_SENSITIVE_WRONG_CLASS")
|
||||||
|
else:
|
||||||
|
raise GateError("A01_SENSITIVE_NOT_REJECTED")
|
||||||
|
print("ZITPLANE_P10_A01_SECRET_NEGATIVE_" + label + "_PASS",
|
||||||
|
flush=True)
|
||||||
|
|
||||||
|
env_malformed_cases = (
|
||||||
|
("UNKNOWN_KEY", "IMAGE_UNEXPECTED_ENV_KEY", "CUSTOM_OPTION=synthetic"),
|
||||||
|
("DUPLICATE_PATH", "IMAGE_ENV_DUPLICATE_KEY", "PATH=/sbin"),
|
||||||
|
("MALFORMED_ENTRY", "IMAGE_ENV_TYPE", "PATH"),
|
||||||
|
("INVALID_FORMAT", "IMAGE_ENV_KEY_FORMAT", "PATH-ALIAS=synthetic"),
|
||||||
|
)
|
||||||
|
for label, code, entry in env_malformed_cases:
|
||||||
|
test = copy.deepcopy(benign)
|
||||||
|
test["Config"]["Env"].append(entry)
|
||||||
|
try:
|
||||||
|
validate_image(test, expected)
|
||||||
|
except GateError as error:
|
||||||
|
require(str(error) == code, "A01_ENV_WRONG_CLASS")
|
||||||
|
else:
|
||||||
|
raise GateError("A01_ENV_NOT_REJECTED")
|
||||||
|
print("ZITPLANE_P10_A01_ENV_NEGATIVE_" + label + "_PASS",
|
||||||
|
flush=True)
|
||||||
|
|
||||||
|
wrong_node_env = copy.deepcopy(benign)
|
||||||
|
wrong_node_env["Config"]["Env"][0] = "NODE_ENV=development"
|
||||||
|
try:
|
||||||
|
validate_image(wrong_node_env, expected)
|
||||||
|
except GateError as error:
|
||||||
|
require(str(error) == "IMAGE_PRODUCTION_ENV",
|
||||||
|
"A01_PRODUCTION_ENV_WRONG_CLASS")
|
||||||
|
else:
|
||||||
|
raise GateError("A01_PRODUCTION_ENV_NOT_REJECTED")
|
||||||
|
print("ZITPLANE_P10_A01_ENV_NEGATIVE_DEVELOPMENT_MODE_PASS", flush=True)
|
||||||
|
print("ZITPLANE_P10_A01_ALL_14_EXTRA_ENV_NEGATIVES_PASS", flush=True)
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
p = argparse.ArgumentParser()
|
p = argparse.ArgumentParser()
|
||||||
@@ -330,7 +471,9 @@ def main() -> None:
|
|||||||
current = verify_source(lock)
|
current = verify_source(lock)
|
||||||
verify_builder(lock)
|
verify_builder(lock)
|
||||||
image = docker_inspect("zitplane-login-ci:" + run_id())
|
image = docker_inspect("zitplane-login-ci:" + run_id())
|
||||||
|
diagnose_oci_image_env(image)
|
||||||
validate_image(image, expected_labels(lock, current, run_id()))
|
validate_image(image, expected_labels(lock, current, run_id()))
|
||||||
|
print("ZITPLANE_P10_A01_IMAGE_ENV_KEY_POLICY_PASS", flush=True)
|
||||||
write_material_attestations(lock, image, current)
|
write_material_attestations(lock, image, current)
|
||||||
print("ZITPLANE_P10_IMAGE_ID_NONROOT_ENTRYPOINT_PROVENANCE_PASS", flush=True)
|
print("ZITPLANE_P10_IMAGE_ID_NONROOT_ENTRYPOINT_PROVENANCE_PASS", flush=True)
|
||||||
elif args.negative:
|
elif args.negative:
|
||||||
|
|||||||
Reference in new issue
Block a user