fix(plugins): split granted scopes on commas only for comma-dialect entries; refuse dot-segment callback paths

A comma is legal inside an RFC 6749 scope token, so an entry on the default space dialect that declares urn:example:read,write was refused as missing a permission it was granted. Commas now split only when the entry chose scope_separator ','. Callback paths with . or .. segments are refused at admission: browsers and curl normalize them before the request reaches the listener, so the exact-path match 404'd and sign-in timed out.
This commit is contained in:
Teknium
2026-11-26 15:46:52 -08:00
parent bab924f6cf
commit cb54834510
2 changed files with 9 additions and 4 deletions

View File

@@ -61,7 +61,8 @@ class OAuthClient:
_DEFAULTED = {"scope_separator": " ", "redirect_port": 0, "redirect_path": "/callback", "redirect_host": "127.0.0.1"}
_REDIRECT_PATH_RE = re.compile(r"/(?!/)[A-Za-z0-9._~/-]*") # fullmatch; no leading "//" (the server folds it)
# fullmatch; no "//" and no "." / ".." segment, since clients and the server rewrite those and the callback would 404
_REDIRECT_PATH_RE = re.compile(r"(?!.*//)(?!(?:.*/)?\.\.?(?:/|$))/[A-Za-z0-9._~/-]*")
# ── Manifest ───────────────────────────────────────────────────────────────────────────────────────
@@ -125,7 +126,7 @@ def _entry_problems(entry: dict, where: str) -> list[str]:
problems.append(f"{where}.redirect_port: must be a port number from 1024 to 65535")
path = entry.get("redirect_path", "/callback")
if not (isinstance(path, str) and _REDIRECT_PATH_RE.fullmatch(path)):
problems.append(f"{where}.redirect_path: must be a URL path starting with a single / (letters, digits, ._~-/)")
problems.append(f"{where}.redirect_path: must be a URL path starting with / (letters, digits, ._~-/; no // or . / .. segments)")
if entry.get("redirect_host", "127.0.0.1") not in ("127.0.0.1", "localhost"):
problems.append(f"{where}.redirect_host: must be 127.0.0.1 or localhost")
return problems
@@ -332,7 +333,9 @@ def _require_granted_scopes(client: OAuthClient, payload: Mapping[str, Any], key
granted = payload.get("scope")
if not isinstance(granted, str) or not granted.strip():
return # the IdP did not say which scopes it granted
missing = [scope for scope in client.scopes if scope not in re.split(r"[,\s]+", granted)]
# A comma is legal inside an RFC 6749 scope token, so it splits only for an entry that chose the comma dialect.
granted_set = re.split(r"[,\s]+" if client.scope_separator == "," else r"\s+", granted.strip())
missing = [scope for scope in client.scopes if scope not in granted_set]
if missing:
raise _auth_error(key, "not every permission was granted, so nothing was saved. Missing: "
+ ", ".join(missing), "oauth_scope_not_granted")

View File

@@ -417,7 +417,7 @@ def test_exact_redirect_and_comma_scopes_reach_the_idp_and_old_grants_keep_worki
allowed, _ = api
assert plugin.call("google", allowed.url).status == 200
for bad in ("//linear/callback", "/linear/callback\n"): # the HTTP server would fold/strip these
for bad in ("//linear/callback", "/linear/callback\n", "/linear/../callback", "/./callback"): # rewritten in transit
assert any("redirect_path" in p for p in plugin_oauth._entry_problems(
{**_entry(idp), "redirect_path": bad}, "oauth[0]"))
positional = pkce.OAuthPKCEConfig("cid", "https://a.example/auth", "https://a.example/t", ("s",), 8650)
@@ -425,6 +425,8 @@ def test_exact_redirect_and_comma_scopes_reach_the_idp_and_old_grants_keep_worki
old = plugin_oauth.OAuthClient(id="x", authorize_url="https://a.example/auth", token_url="https://a.example/t",
scopes=("s",), client_id_env="X_ID", hosts=("api.example.com",))
comma_token = dataclasses.replace(old, scopes=("urn:example:read,write",)) # one RFC 6749 token, default dialect
plugin_oauth._require_granted_scopes(comma_token, {"scope": "urn:example:read,write"}, "x")
legacy = {k: v for k, v in dataclasses.asdict(old).items()
if k not in ("scope_separator", "redirect_port", "redirect_path", "redirect_host")}
assert old.fingerprint == hashlib.sha256(