Skip to content

fix: casbin org matching logic with glob - #1193

Merged
Datron merged 3 commits into
mainfrom
casbin-minor-fixes
Dec 2, 2026
Merged

Datron merged 3 commits into
mainfrom
casbin-minor-fixes

Conversation

@ayushjain17

Copy link
Copy Markdown
Collaborator

Problem

A role granted at org level (e.g. g, user, reader, aarokyasbx_*) did nothing for requests to that org's workspaces (aarokyasbx_backend). The grant only worked if the same role also had a p row at exactly {org}_*. admin happened to work for orgs created through the API, because add_org_policy created a p admin {org}_* row. reader, and any role whose policies are defined at *, were denied, so people had to be granted the role again on every workspace.

Root cause

g() matches domains by exact string. The matcher only checked a grant at p.dom or r.dom, never at the request's org ({org}_*).

Changes

Matcher (model.conf)

  • Registered a custom function orgDomain (org_ws → org_*; * and org_* are returned unchanged). The subject check now also matches a grant at orgDomain(r.dom), so a role granted at {org}_* applies to every workspace in that org, whatever domain the role's policies use.
  • Domain check: globMatch(r.dom, p.dom) → p.dom == orgDomain(r.dom). This gives the same result for every domain shape we allow (*, org_*, org_ws). It also avoids casbin compiling a new glob for every policy row on every request.
  • The object check now runs straight after the domain check, before the role lookups. Inside the clauses, the cheaper checks run first (g3 before g2, the global grant last). Dropped the redundant == true. No decision changes from the reordering: every clause is joined by && and has no side effects.

Org creation / backfill

  • add_org_policy now only adds g, {admin_email}, admin, {org}_*. The per-org p admin {org}_* * * * row isn't needed any more, because the seeded p admin * * * * now applies through the org grant. POST /authz/admin/backfill keeps working and now inserts only the g rows.

Workspace admin sync removed

  • Removed on_workspace_creation / on_workspace_admin_update from Authorizer and their casbin/no-auth implementations, the calls in the workspace create/update handlers, and the workspace backfill endpoint.
  • Behaviour change: creating a workspace no longer grants admin on it to workspace_admin_email, and changing that email no longer moves the grant. Workspace access now has to be granted explicitly, or comes from an org-level grant.

Testing

9 new unit tests in casbin.rs, run against the real model.conf with an in-memory enforcer:

  • an org grant reaches all of that org's workspaces, but not another org, a similarly named org (aarokya vs aarokyasbx), or *;
  • a workspace grant doesn't reach sibling workspaces or the org level;
  • an org-level reader stays read-only, and reads resolve through g3 action groups;
  • policies defined at org level and at workspace level are both scoped correctly;
  • org creation grants admin without creating a per-org p row;
  • * grants behave as before.

The two tests that reproduce the bug fail on the old matcher and pass on the new one.

Rollout notes

  • Existing p admin {org}_* rows are now redundant but harmless. Keep them until this has been stable, because they're what keeps org admins working if this is rolled back.
  • Existing workspace-admin g rows stay as they are.
  • Org routes (/superposition/organisations) still use domain *, so organisation:get and organisation:list remain global-only.

@ayushjain17
ayushjain17 requested a review from a team as a code owner December 1, 2026 12:45
@semanticdiff-com

Copy link
Copy Markdown

Review changes with  SemanticDiff

}

fn org_domain(domain: Dynamic) -> Dynamic {
let domain = domain.into_string().unwrap_or_default();

@Datron Datron Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If domain can't be unwrapped, it will fall back to empty string, and we return that as Dynamic. This could be exploited due to a mistake in configuration or values passed?

Comment on lines +434 to +435
assert_eq!(map(""), "");
assert_eq!(org_domain(Dynamic::from(1_i64)).into_string().unwrap(), "");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be inverted? We should fail on empty strings and wrong parses right?

@ayushjain17
ayushjain17 requested a review from Datron December 2, 2026 04:11
@Datron
Datron added this pull request to the merge queue Dec 2, 2026
Merged via the queue into main with commit bf73140 Dec 2, 2026
19 of 21 checks passed
@Datron
Datron deleted the casbin-minor-fixes branch December 2, 2026 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants