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

@@ -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"),
]: