Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(oauth): @default union scope ceilings (#64087)

Matt Brooker14e89cda123a
diff --git a/posthog/scopes.py b/posthog/scopes.py
index 85643c530463..1d29ebf1870c 100644
--- a/posthog/scopes.py
+++ b/posthog/scopes.py
@@ -274,11 +274,36 @@ def filter_to_unprivileged_scopes(scopes: Iterable[object]) -> list[str]:
return result
+# Sentinel element in `OAuthApplication.scopes` meaning "the `UNPRIVILEGED_SCOPES`
+# default *plus* the other listed scopes" — lets an app ride the broad default and add
+# a few explicit (e.g. privileged) extras without enumerating the whole set, and keeps
+# auto-tracking unprivileged scopes added later. Starts with `@` so it can never collide
+# with a real `obj:action` scope. Not grantable itself: it's stripped from the resolved
+# ceiling, and `filter_to_unprivileged_scopes` drops it so a self-registering app can't
+# inject it to widen its own ceiling.
+DEFAULT_CEILING_SENTINEL = "@default"
+
+
+def resolve_ceiling(app_scopes: Iterable[str]) -> frozenset[str] | None:
+ """An app's explicit scope ceiling, or `None` when it has none (empty `scopes`,
+ which falls back to the `UNPRIVILEGED_SCOPES` default). A `@default` sentinel
+ expands to `UNPRIVILEGED_SCOPES` unioned with the other listed scopes; without it,
+ a non-empty ceiling stays an exhaustive allow-list. Entries are stripped so a
+ fat-fingered `" @default"` still resolves (real scopes never have whitespace)."""
+ app = {s.strip() for s in (app_scopes or [])}
+ app.discard("")
+ if not app:
+ return None
+ if DEFAULT_CEILING_SENTINEL in app:
+ return frozenset(UNPRIVILEGED_SCOPES | (app - {DEFAULT_CEILING_SENTINEL}))
+ return frozenset(app)
+
+
def effective_ceiling(app_scopes: Iterable[str]) -> frozenset[str]:
"""The scope set a request resolves against: the explicit `app_scopes` ceiling,
or the broad `UNPRIVILEGED_SCOPES` default when the app has none."""
- app = frozenset(app_scopes or [])
- return app if app else UNPRIVILEGED_SCOPES
+ ceiling = resolve_ceiling(app_scopes)
+ return ceiling if ceiling is not None else UNPRIVILEGED_SCOPES
def scopes_within_ceiling(
@@ -296,6 +321,8 @@ def scopes_within_ceiling(
- OIDC + introspection (`ALWAYS_ALLOWED_SCOPES`) are always granted.
- An explicit `app_scopes` ceiling is an exhaustive allow-list: anything
outside it is rejected, including `*`.
+ - A `@default` sentinel in `app_scopes` resolves to `UNPRIVILEGED_SCOPES` plus
+ the other listed scopes (see `resolve_ceiling`).
- An empty `app_scopes` falls back to the broad `UNPRIVILEGED_SCOPES` default.
`allow_wildcard_under_empty_ceiling` is the only resolution difference between
@@ -304,12 +331,12 @@ def scopes_within_ceiling(
(the default) since it never granted wildcard, so an unseeded ceiling must not
silently become one.
"""
- app = frozenset(app_scopes or [])
+ ceiling = resolve_ceiling(app_scopes)
to_check = set(requested) - ALWAYS_ALLOWED_SCOPES
if not to_check:
return True
- if app:
- return "*" not in to_check and to_check.issubset(app)
+ if ceiling is not None:
+ return "*" not in to_check and to_check.issubset(ceiling)
allowed = UNPRIVILEGED_SCOPES | {"*"} if allow_wildcard_under_empty_ceiling else UNPRIVILEGED_SCOPES
return to_check.issubset(allowed)
@@ -327,13 +354,13 @@ def scopes_outside_ceiling(
Returns a sorted list, empty when every requested scope is grantable.
"""
- app = frozenset(app_scopes or [])
+ ceiling = resolve_ceiling(app_scopes)
to_check = set(requested) - ALWAYS_ALLOWED_SCOPES
if not to_check:
return []
- if app:
+ if ceiling is not None:
# `*` is never grantable under an explicit ceiling, even if listed.
- return sorted(s for s in to_check if s == "*" or s not in app)
+ return sorted(s for s in to_check if s == "*" or s not in ceiling)
allowed = UNPRIVILEGED_SCOPES | {"*"} if allow_wildcard_under_empty_ceiling else UNPRIVILEGED_SCOPES
return sorted(to_check - allowed)
@@ -353,15 +380,15 @@ def narrow_scopes_to_ceiling(original: Iterable[str], app_scopes: Iterable[str])
caller should reject with `invalid_grant` and force re-authorization).
"""
original_list = list(original)
- app = set(app_scopes or [])
- if not app:
+ ceiling = resolve_ceiling(app_scopes)
+ if ceiling is None:
return original_list
original_set = set(original_list)
if "*" in original_set:
return original_list
- narrowed = (original_set & app) | (original_set & ALWAYS_ALLOWED_SCOPES)
+ narrowed = (original_set & ceiling) | (original_set & ALWAYS_ALLOWED_SCOPES)
if not narrowed:
return None
return sorted(narrowed)
diff --git a/posthog/test/test_scopes.py b/posthog/test/test_scopes.py
index 5319797a6a4e..1e6726337ac1 100644
--- a/posthog/test/test_scopes.py
+++ b/posthog/test/test_scopes.py
@@ -246,6 +246,23 @@ class TestScopesWithinCeiling(SimpleTestCase):
# Provisioning never grandfathered `*`; an unseeded ceiling must not grant it.
("wildcard_rejected_under_empty_ceiling", ["*"], [], False),
("unprivileged_allowed_under_empty_ceiling", ["query:read", "insight:write"], [], True),
+ # `@default` sentinel: the unprivileged default plus the other listed scopes.
+ ("default_sentinel_grants_unprivileged", ["query:read", "insight:write"], ["@default"], True),
+ (
+ "default_sentinel_grants_listed_privileged_extra",
+ ["llm_gateway:read", "query:read"],
+ ["@default", "llm_gateway:read"],
+ True,
+ ),
+ (
+ "default_sentinel_rejects_unlisted_privileged",
+ ["llm_gateway:write"],
+ ["@default", "llm_gateway:read"],
+ False,
+ ),
+ ("wildcard_rejected_under_default_sentinel", ["*"], ["@default"], False),
+ ("sentinel_itself_not_grantable", ["@default"], ["@default"], False),
+ ("default_sentinel_tolerates_whitespace", ["query:read", "insight:write"], [" @default "], True),
]
)
def test_resolution(self, _name: str, requested: list[str], app_scopes: list[str], expected: bool) -> None:
@@ -268,9 +285,24 @@ class TestScopesWithinCeiling(SimpleTestCase):
assert scopes_within_ceiling(["*"], [], allow_wildcard_under_empty_ceiling=True) is True
assert scopes_within_ceiling(["*"], [], allow_wildcard_under_empty_ceiling=False) is False
- def test_effective_ceiling(self) -> None:
- assert effective_ceiling([]) == UNPRIVILEGED_SCOPES
- assert effective_ceiling(["query:read", "insight:read"]) == frozenset({"query:read", "insight:read"})
+ @parameterized.expand(
+ [
+ ("empty_falls_back_to_unprivileged", [], UNPRIVILEGED_SCOPES),
+ ("explicit_list_is_exhaustive", ["query:read", "insight:read"], frozenset({"query:read", "insight:read"})),
+ (
+ "default_sentinel_expands_to_unprivileged_plus_extras",
+ ["@default", "llm_gateway:read"],
+ UNPRIVILEGED_SCOPES | {"llm_gateway:read"},
+ ),
+ (
+ "sentinel_and_extras_tolerate_whitespace",
+ [" @default ", "llm_gateway:read "],
+ UNPRIVILEGED_SCOPES | {"llm_gateway:read"},
+ ),
+ ]
+ )
+ def test_effective_ceiling(self, _name: str, app_scopes: list[str], expected: frozenset[str]) -> None:
+ assert effective_ceiling(app_scopes) == expected
class TestScopesOutsideCeiling(SimpleTestCase):
@@ -281,6 +313,12 @@ class TestScopesOutsideCeiling(SimpleTestCase):
("privileged_rejected_without_ceiling", ["llm_gateway:read"], [], ["llm_gateway:read"]),
("wildcard_rejected_under_explicit_ceiling", ["query:read", "*"], ["query:read"], ["*"]),
("oidc_never_rejected", ["openid", "insight:write"], ["query:read"], ["insight:write"]),
+ (
+ "default_sentinel_isolates_unlisted_privileged",
+ ["llm_gateway:write", "query:read"],
+ ["@default", "llm_gateway:read"],
+ ["llm_gateway:write"],
+ ),
]
)
def test_resolution(self, _name: str, requested: list[str], app_scopes: list[str], expected: list[str]) -> None:
@@ -288,7 +326,12 @@ class TestScopesOutsideCeiling(SimpleTestCase):
def test_inverse_of_within_ceiling(self) -> None:
# The two helpers must never disagree: empty offender list iff within ceiling.
- cases = [(["query:read", "insight:write"], ["query:read"]), (["query:read"], []), (["*"], [])]
+ cases = [
+ (["query:read", "insight:write"], ["query:read"]),
+ (["query:read"], []),
+ (["*"], []),
+ (["llm_gateway:write", "query:read"], ["@default", "llm_gateway:read"]),
+ ]
for requested, app_scopes in cases:
within = scopes_within_ceiling(requested, app_scopes, allow_wildcard_under_empty_ceiling=True)
outside = scopes_outside_ceiling(requested, app_scopes, allow_wildcard_under_empty_ceiling=True)
@@ -316,6 +359,14 @@ class TestNarrowScopesToCeiling(SimpleTestCase):
["query:read"],
["openid"],
),
+ # `@default` ceiling narrows to the unprivileged default plus listed extras,
+ # dropping a hidden scope (`wizard_session:read`) the default doesn't cover.
+ (
+ "default_sentinel_keeps_unprivileged_and_extras",
+ ["query:read", "llm_gateway:read", "wizard_session:read"],
+ ["@default", "llm_gateway:read"],
+ ["llm_gateway:read", "query:read"],
+ ),
]
)
def test_resolution(
@@ -337,6 +388,8 @@ class TestFilterToUnprivilegedScopes(SimpleTestCase):
),
("empty_in_empty_out", [], []),
("all_dropped_yields_empty", ["llm_gateway:read", "garbage"], []),
+ # A self-registering app can't inject the ceiling sentinel to widen itself.
+ ("drops_default_sentinel", ["@default", "insight:read"], ["insight:read"]),
]
)
def test_resolution(self, _name: str, given: list[str], expected: list[str]) -> None: