From 5f8ebab5b253e594940daa9d923d69dd1fc456f4 Mon Sep 17 00:00:00 2001 From: Matt Mabrey Date: Fri, 16 Oct 2026 16:24:05 -0700 Subject: [PATCH] T13.9 follow-up 2: name PEM keys, forgive missing braces and double encoding, give a length hint (D23) --- docs/waves/wave-13.md | 4 ++++ server/okta_sync.py | 25 +++++++++++++++++++++---- tests/okta_sync_oauth_check.py | 19 ++++++++++++++++++- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/docs/waves/wave-13.md b/docs/waves/wave-13.md index c819a1e..9636da1 100644 --- a/docs/waves/wave-13.md +++ b/docs/waves/wave-13.md @@ -395,3 +395,7 @@ space or line break, and it gave one message for every failure. The loader now f marks, spaces, line breaks, missing padding, the URL-safe alphabet, a byte-order mark and a one-key JWK Set, and each failure names its step (counts only, no key text). The new checks in `tests/okta_sync_oauth_check.py` fail against the first loader and pass now. + +T13.9 follow-up 2 (2026-10-16): the second deploy decoded the value but the text was not JSON. +The loader now also names a PEM key, adds missing outer braces, unwraps a value that was encoded +twice, and its "not JSON" message gives the length and the kind of first character (counts only). diff --git a/server/okta_sync.py b/server/okta_sync.py index 9807ce6..82090c7 100644 --- a/server/okta_sync.py +++ b/server/okta_sync.py @@ -181,7 +181,7 @@ def _load_jwk() -> dict: raise _fail("OKTA_SYNC_PRIVATE_KEY is not set") if len(raw) >= 2 and raw[0] == raw[-1] and raw[0] in "\"'": raw = raw[1:-1].strip() - if raw.startswith("{"): + if raw.startswith(("{", '"', "-----BEGIN")): text = raw else: compact = re.sub(r"\s+", "", raw) @@ -200,11 +200,28 @@ def _load_jwk() -> dict: text = decoded.decode("utf-8-sig") except UnicodeDecodeError: raise _fail("OKTA_SYNC_PRIVATE_KEY decodes from base64 but the result is not text") from None + text = text.lstrip("\ufeff").strip() + via = "" if raw.startswith(("{", '"', "-----BEGIN")) else " after decoding base64" + if text.startswith("-----BEGIN"): + raise _fail("OKTA_SYNC_PRIVATE_KEY holds a PEM key (-----BEGIN ...). A JWK is needed: " + "JSON with the fields kty, kid, n, e and d") + if text.startswith('"'): + text = "{" + text.rstrip(",") + "}" # the fields were copied without the outer braces try: - jwk = json.loads(text.lstrip("\ufeff")) + jwk = json.loads(text) except ValueError: - raise _fail("OKTA_SYNC_PRIVATE_KEY is not JSON" - + (" after decoding base64" if not raw.startswith("{") else "")) from None + # One more layer? A value that was encoded twice decodes to base64 text. + again = None + if not text.startswith("{"): + try: + again = base64.b64decode(re.sub(r"\s+", "", text) + "=" * (-len(re.sub(r"\s+", "", text)) % 4)) + jwk = json.loads(again.decode("utf-8-sig").strip()) + except Exception: # noqa: BLE001 + again = None + if again is None: + shape = ("starts with a brace" if text.startswith("{") else + "starts with a letter or digit" if text[:1].isalnum() else "starts with a symbol") + raise _fail(f"OKTA_SYNC_PRIVATE_KEY is not JSON{via} ({len(text)} characters, {shape})") from None if isinstance(jwk, dict) and isinstance(jwk.get("keys"), list): if len(jwk["keys"]) != 1: raise _fail("OKTA_SYNC_PRIVATE_KEY is a key set with more than one key; paste one key") diff --git a/tests/okta_sync_oauth_check.py b/tests/okta_sync_oauth_check.py index f3292b0..26b9677 100644 --- a/tests/okta_sync_oauth_check.py +++ b/tests/okta_sync_oauth_check.py @@ -200,11 +200,28 @@ for label, raw in variants.items(): chk(f"forgiven: {label}", isinstance(f.verified, dict) and f.user_calls == 1) except OktaSyncError as e: chk(f"forgiven: {label}", False, str(e)) +# fields copied without the outer braces, and a value encoded twice, are forgiven +inner = json.dumps(rsa_jwk)[1:-1] +for label, raw in [("fields without braces (text)", inner), + ("fields without braces (base64)", base64.b64encode(inner.encode()).decode()), + ("encoded twice", base64.b64encode(b64.encode()).decode())]: + os.environ["OKTA_SYNC_PRIVATE_KEY"] = raw + try: + f = FakeOkta(rsa_pub) + okta_sync.fetch_users(http=f.client()) + chk(f"forgiven: {label}", isinstance(f.verified, dict)) + except OktaSyncError as e: + chk(f"forgiven: {label}", False, str(e)) +pem = "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkq\n-----END PRIVATE KEY-----" +for label, raw in [("PEM text", pem), ("PEM in base64", base64.b64encode(pem.encode()).decode())]: + os.environ["OKTA_SYNC_PRIVATE_KEY"] = raw + err = fetch_err(FakeOkta()) or "" + chk(f"PEM is named: {label}", "PEM" in err and "MIIEvQ" not in err, err) os.environ["OKTA_SYNC_PRIVATE_KEY"] = json.dumps({"keys": [rsa_jwk, rsa_jwk]}) chk("a key set with two keys is refused", "more than one key" in (fetch_err(FakeOkta()) or "")) for label, raw, want in [ ("garbage characters", "!!!not*base64!!!", "not valid base64"), - ("base64 of non-JSON", base64.b64encode(b"hello world").decode(), "not JSON after decoding base64"), + ("base64 of non-JSON", base64.b64encode(b"hello world").decode(), "not JSON after decoding base64 (11 characters, starts with a letter or digit)"), ("base64 of bytes that are not text", base64.b64encode(b"\xff\xfe\xfd\xfc").decode(), "not text"), ("broken JSON", "{ not json", "not JSON"), ]: