fix(api): use a dedicated forbidden error for role-authority denials#6517
Merged
Conversation
The role-authority guards returned NewErrRoleInvalid() when the caller lacked authority over the target role — a forbidden condition wrongly reported as a malformed-role-value error, which misleads consumers and obscures telemetry. Replace those guards with a dedicated NewErrRoleForbidden() (still 403) and drop the now-unused NewErrRoleInvalid/ErrRoleInvalid. Also split the UpdateAPIKey guard so a non-member returns 404 via NewErrNamespaceMemberNotFound instead of conflating member-not-found with the authority denial. Fixes: shellhub-io/team#155
|
Claude finished @otavio's task in 12m 27s —— View job Code Review CompleteReviewed 6 files across code quality, security, testing, Go patterns, and architecture — no issues found. The code looks good as-is. Highlights:
To request another review round, comment |
mohlerj523-boop
approved these changes
Jun 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Replaces the misleading
NewErrRoleInvalid()returned by role-authority guardswith a dedicated
NewErrRoleForbidden()error, so an authorization denial is nolonger reported as a malformed-role-value error. HTTP status is unchanged (403).
Why
The guards in
api/servicesreturnedNewErrRoleInvalid()("role is invalid")when the caller lacked authority over the target role — a forbidden
condition, not a validation failure. This conflated two distinct failure modes
under one error, misleading API consumers and obscuring logs/telemetry.
Across both
shellhubandcloud, every call site ofNewErrRoleInvalid()wasan authority guard; there were no genuine "invalid role value" uses, so the
symbol is removed entirely.
Fixes: shellhub-io/team#155
Changes
ErrRoleForbidden+NewErrRoleForbidden()(
ErrCodeForbidden→ 403, matching the existingErrAuthForbiddenfamily);remove the now-dead
ErrRoleInvalid/NewErrRoleInvalid().HasAuthorityguards(
AddNamespaceMember,UpdateNamespaceMember×2,RemoveNamespaceMember).CreateAPIKeyguard; split theUpdateAPIKeyguard so a non-member returnsNewErrNamespaceMemberNotFound(404) instead of conflating member-not-found with the authority denial (403).
UpdateAPIKeymember-not-found case (service + route-layer 404).Coordination
This is the primary of two coordinated PRs. The companion is
shellhub-io/cloud#2407 (same branch name
fix/member-authority-error-semantics)and migrates cloud's call sites. Merge this shellhub PR first — cloud resolves
the
servicespackage from the shellhub module via a localreplacedirective,so removing the symbol here breaks cloud's build until cloud is also merged.
Testing
go test ./api/services/ ./api/routes/ -run 'Member|APIKey|Errors' -tags docker— all authority cases assert
NewErrRoleForbidden(); the new non-member casereturns 404.