T13.9 follow-up 3: read pretty-printed and hand-copied JWKs, report the line and column of a syntax error (D23)

This commit is contained in:
2026-10-16 16:56:11 -07:00
parent c75450a000
commit 83e77ec622
3 changed files with 86 additions and 15 deletions

View File

@@ -399,3 +399,9 @@ 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).
T13.9 follow-up 3 (2026-10-16): the third deploy decoded the base64 to 1,760 characters that start
with a brace, but Python's strict JSON reader refused them (PowerShell accepted the same text).
The loader now accepts control characters such as line breaks inside a string, curly quote marks and
a trailing comma, removes spaces inside the key's number fields, and a real syntax error reports the
line and column (position and rule only, never key text).

View File

@@ -166,6 +166,36 @@ def auth_method() -> str:
return ""
def _parse_json_forgiving(text: str):
"""Parse the key text as JSON, forgiving what copying does to it. Returns
(object, None) or (None, "problem at line L column C: what"). The problem names a
position and a rule, never any text of the key. Forgiven: line breaks and other control
characters inside a string (a long value wrapped over several lines), curly quote
marks, and a comma before a closing brace. Spaces inside the key's own number fields
are removed, because those fields are base64 text."""
attempts = [text]
cleaned = (text.replace("\u201c", '"').replace("\u201d", '"').replace("\u2018", "'").replace("\u2019", "'"))
cleaned = re.sub(r",\s*([}\]])", r"\1", cleaned)
if cleaned != text:
attempts.append(cleaned)
last = ""
for t in attempts:
try:
obj = json.loads(t, strict=False)
except json.JSONDecodeError as exc:
last = f"{exc.msg} at line {exc.lineno} column {exc.colno}"
continue
except ValueError:
last = "unreadable"
continue
if isinstance(obj, dict):
for k in ("n", "e", "d", "p", "q", "dp", "dq", "qi", "x", "y"):
if isinstance(obj.get(k), str):
obj[k] = re.sub(r"\s+", "", obj[k])
return obj, None
return None, last
def _load_jwk() -> dict:
"""OKTA_SYNC_PRIVATE_KEY is the private key as a JWK. Two forms are accepted:
the JSON itself (it starts with "{"), or that JSON encoded as base64. Use base64
@@ -207,21 +237,19 @@ def _load_jwk() -> dict:
"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)
except ValueError:
jwk, problem = _parse_json_forgiving(text)
if problem is not None and not text.startswith("{"):
# 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
try:
squeezed = re.sub(r"\s+", "", text)
again = base64.b64decode(squeezed + "=" * (-len(squeezed) % 4)).decode("utf-8-sig").strip()
jwk, problem = _parse_json_forgiving(again)
except Exception: # noqa: BLE001
pass
if problem is not 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}; {problem})")
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")

View File

@@ -217,11 +217,48 @@ for label, raw in [("PEM text", pem), ("PEM in base64", base64.b64encode(pem.enc
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)
# the pretty-printed, hand-copied shapes that Python's strict reader refuses
def b64s(t):
return base64.b64encode(t.encode()).decode()
pretty = json.dumps(rsa_jwk, indent=2)
wrapped = pretty.replace(rsa_jwk["n"], rsa_jwk["n"][:100] + "\n" + rsa_jwk["n"][100:]) # line break inside a value
wrapped_sp = pretty.replace(rsa_jwk["d"], rsa_jwk["d"][:60] + " \r\n " + rsa_jwk["d"][60:])
curly = pretty.replace('"', "\u201c", 1)
curly = curly.replace('": ', "\u201d: ") # curly quotes after the first
trailing = pretty[:-2] + ",\n}"
for label, text in [("pretty-printed, 36 lines", pretty),
("line break inside the n value", wrapped),
("space and line break inside the d value", wrapped_sp),
("trailing comma before the closing brace", trailing)]:
for how, raw in [("base64", b64s(text)), ("plain", text)]:
os.environ["OKTA_SYNC_PRIVATE_KEY"] = raw
try:
f = FakeOkta(rsa_pub)
okta_sync.fetch_users(http=f.client())
chk(f"forgiven ({how}): {label}", isinstance(f.verified, dict))
except OktaSyncError as e:
chk(f"forgiven ({how}): {label}", False, str(e))
curly_ok = json.dumps(rsa_jwk, indent=2).replace('"kty"', "\u201ckty\u201d")
os.environ["OKTA_SYNC_PRIVATE_KEY"] = b64s(curly_ok)
try:
f = FakeOkta(rsa_pub)
okta_sync.fetch_users(http=f.client())
chk("forgiven: curly quote marks", isinstance(f.verified, dict))
except OktaSyncError as e:
chk("forgiven: curly quote marks", False, str(e))
broken = pretty.replace('"kty": "RSA",', '"kty": "RSA" "x",', 1)
os.environ["OKTA_SYNC_PRIVATE_KEY"] = b64s(broken)
err = fetch_err(FakeOkta()) or ""
chk("a real syntax error names the line and column", "line " in err and "column " in err and "starts with a brace" in err, err)
chk("the position message holds no key text", rsa_jwk["d"][:10] not in err and rsa_jwk["n"][:10] 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 (11 characters, starts with a letter or digit)"),
("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"),
]: