96 lines
3.4 KiB
Python
96 lines
3.4 KiB
Python
"""Disposable ZITADEL OIDC metadata/JWKS and fail-closed negative checks.
|
|
|
|
No interactive login, account provisioning, MFA or real user sessions.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
from urllib.parse import urlparse
|
|
from urllib.request import Request, urlopen
|
|
|
|
PROXY = os.environ.get("ZITPLANE_CI_PROXY", "http://proxy")
|
|
EXPECTED_ISSUER = "http://localhost:8080"
|
|
HOST = "localhost:8080"
|
|
|
|
|
|
def verify_metadata(discovery: dict) -> str:
|
|
if discovery.get("issuer") != EXPECTED_ISSUER:
|
|
raise RuntimeError("OIDC issuer does not match disposable local origin")
|
|
for field in ("authorization_endpoint", "token_endpoint", "jwks_uri"):
|
|
value = discovery.get(field)
|
|
if not isinstance(value, str):
|
|
raise RuntimeError(f"OIDC {field} missing")
|
|
parsed = urlparse(value)
|
|
if (
|
|
parsed.scheme != "http"
|
|
or parsed.netloc != "localhost:8080"
|
|
or not parsed.path.startswith("/")
|
|
or parsed.username or parsed.password
|
|
):
|
|
raise RuntimeError(f"OIDC {field} not on the allowed origin")
|
|
return urlparse(discovery["jwks_uri"]).path
|
|
|
|
|
|
def verify_jwks(jwks: dict) -> None:
|
|
keys = jwks.get("keys")
|
|
if not isinstance(keys, list) or not keys:
|
|
raise RuntimeError("JWKS must contain at least one public key")
|
|
for key in keys:
|
|
if not isinstance(key, dict) or not key.get("kty"):
|
|
raise RuntimeError("JWKS contains an invalid public key")
|
|
|
|
|
|
def verify_negative_contracts() -> None:
|
|
safe = {
|
|
"issuer": EXPECTED_ISSUER,
|
|
"authorization_endpoint": EXPECTED_ISSUER + "/oauth/v2/authorize",
|
|
"token_endpoint": EXPECTED_ISSUER + "/oauth/v2/token",
|
|
"jwks_uri": EXPECTED_ISSUER + "/oauth/v2/keys",
|
|
}
|
|
for field, dangerous in (
|
|
("issuer", "http://evil.invalid"),
|
|
("jwks_uri", "https://evil.invalid/keys"),
|
|
("authorization_endpoint", "//evil.invalid/authorize"),
|
|
("token_endpoint", "http://localhost:8080.evil.invalid/token"),
|
|
):
|
|
obj = {**safe, field: dangerous}
|
|
try:
|
|
verify_metadata(obj)
|
|
except RuntimeError:
|
|
continue
|
|
raise AssertionError(f"Unsafe OIDC {field} accepted")
|
|
for obj in ({"keys": []}, {"keys": "wrong"}, {"keys": [{}]}):
|
|
try:
|
|
verify_jwks(obj)
|
|
except RuntimeError:
|
|
continue
|
|
raise AssertionError("Unsafe JWKS payload accepted")
|
|
print("ZITPLANE_OIDC_NEGATIVE_CONTRACTS_PASS")
|
|
|
|
|
|
def fetch(path: str) -> bytes:
|
|
if PROXY != "http://proxy" or not path.startswith("/") or path.startswith("//"):
|
|
raise RuntimeError("Only disposable Docker proxy is allowed")
|
|
request = Request(PROXY + path, headers={"Host": HOST, "Accept": "application/json"})
|
|
with urlopen(request, timeout=20) as reply:
|
|
if reply.status != 200:
|
|
raise RuntimeError(f"HTTP {reply.status}: {path}")
|
|
return reply.read()
|
|
|
|
|
|
def main() -> None:
|
|
verify_negative_contracts()
|
|
fetch("/debug/ready")
|
|
print("ZITPLANE_BACKEND_READY_PASS")
|
|
discovery = json.loads(fetch("/.well-known/openid-configuration"))
|
|
jwks_path = verify_metadata(discovery)
|
|
print("ZITPLANE_OIDC_DISCOVERY_PASS")
|
|
verify_jwks(json.loads(fetch(jwks_path)))
|
|
print("ZITPLANE_OIDC_JWKS_PASS")
|
|
print("ZITPLANE_OIDC_INTERACTIVE_E2E_DEFERRED: no disposable OIDC client/user")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|