Skip to content

security(teams): authorize /teams from the session, not the global users.role_id (#830) - #844

Open
alex-dembele wants to merge 3 commits into
masterfrom
security/830-teams-global-role
Open

alex-dembele wants to merge 3 commits into
masterfrom
security/830-teams-global-role

Conversation

@alex-dembele

Copy link
Copy Markdown
Member

Closes #830

Decision (criterion 1): kept and fixed, not removed

/teams has no UI, no OpenAPI entry and no product claim, and it had never worked: every call returned 404. Removing a module would be a product decision, and the issue allowed either outcome. So this PR repairs it along criteria 2 to 4. If you'd rather drop the module, that is a separate, small PR: delete the 7 routes and the handler. The tables stay, so no migration is needed.

What changed (internal/handler/team_handler.go)

  • The seven in-handler currentUser.Role.Name != "admin" checks are removed (criterion 2). They read the global users.role_id, and they looked the caller up by claims.ID (the JWT id) instead of claims.Sub, which is why everything returned 404. Authorization is the RequireRole("admin") route guard, which reads the caller's role in the active organization.
  • AddTeamMember requires an active membership in the caller's tenant (criterion 3). A foreign user, a withdrawn member, an unknown id and a malformed id all get the same 404 {"error":"User not found"}. Before, a malformed id could reach uuid.MustParse.
  • DeleteTeam removes the members and the team in one transaction (rule 7).
  • Team and member ids are set with uuid.New() instead of relying on the Postgres-only gen_random_uuid() default.

Verification

$ go test ./internal/handler/ -run 'TestTeam' -count=1 -v
--- PASS: TestTeams_Success
--- PASS: TestTeams_NotFound
--- PASS: TestTeams_Unauthorized
--- PASS: TestTeam_TenantIsolationPredicate
ok  	github.com/opendefender/openrisk/internal/handler	0.087s

All three new tests fail against the old handler.

$ go test ./internal/security/... ./internal/handler/... ./internal/service/... ./cmd/... -count=1
ok internal/security/isolation · ok internal/security/routes · ok internal/handler · ok internal/handler/auth · ok internal/service · ok cmd/server

The DoD grep grep -rn 'Role.Name != "admin"' internal/handler/ now returns only the four hits in user_handler.go. PR #832 (#807) removes those, so the grep is empty once both are merged.

Honest remainders

  • The tests run on SQLite. No live Postgres pass for /teams.
  • GetTeam still loads each member's user row one query at a time (N+1). It existed before this PR and is untouched, because this is a security fix.

…ole_id (#830)

Every /teams handler re-checked the global users.role_id and looked the
caller up by the token id (claims.ID) instead of the user id, so each call
answered 404 before the check ran. The RequireRole("admin") route guard
already authorizes these routes from the caller's role in the active
organization; the in-handler checks are removed.

AddTeamMember now also requires the target's membership to be active and
answers a malformed id with the same 404 as an unknown one. DeleteTeam
removes the members and the team in one transaction. Team and member ids
are set explicitly rather than left to a Postgres-only column default.

Signed-off-by: alex-dembele <alexandredembele16@gmail.com>
…#830)

Signed-off-by: alex-dembele <alexandredembele16@gmail.com>
…ry entry (#830)

Keeps this branch from conflicting with #807's edit of the /users entries
directly below.

Signed-off-by: alex-dembele <alexandredembele16@gmail.com>

This branch has not been deployed

No deployments
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.

security(teams): /teams handlers still read the global users.role_id and resolve the caller by token id

1 participant