fix(api)!: return 403 for authorization failures, reserve 401 for missing session - #41658
fix(api)!: return 403 for authorization failures, reserve 401 for missing session#41658surjeetkumar8006 wants to merge 2 commits into
Conversation
…sing session (RocketChat#41589) - Map error-unauthorized and error-not-authorized to 403 Forbidden in ApiClass - Align Router.ts body validation errorType to 'error-invalid-params' - Clean up permissions middleware dead branch - Update rooms endpoints permission checks and schemas from 401 to 403 Closes RocketChat#41589
|
Looks like this PR is not ready to merge, because of the following issues:
Please fix the issues and try again If you have any trouble, please check the PR guidelines |
🦋 Changeset detectedLatest commit: 7b2e48c The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
|
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (5)
WalkthroughThe API now returns 403 for authorization failures when a session exists. Missing sessions continue to use 401. Body-validation failures now use ChangesAPI error contracts
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related issues
Possibly related PRs
Suggested labels: Suggested reviewers: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Warning |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2b7abb0fc1
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| if (findResult.broadcast && !(await hasPermissionAsync(this.user, 'view-broadcast-member-list', findResult._id))) { | ||
| return API.v1.unauthorized(); | ||
| return API.v1.forbidden(); |
There was a problem hiding this comment.
For rooms.membersOrderedByRole, the response spec above this action still only registers 200/400/401/404. When this broadcast-room permission branch is hit under TEST_MODE, the typed router looks up a validator for status 403 and throws Missing response validator... instead of returning the intended forbidden response; it also leaves the generated route metadata without the new 403. Add 403: validateForbiddenErrorResponse to this endpoint's response map along with the behavior change.
Useful? React with 👍 / 👎.
| { | ||
| success: false, | ||
| errorType: 'invalid-params', | ||
| errorType: 'error-invalid-params', |
There was a problem hiding this comment.
Update body-validation expectations
When packages/http-router/src/Router.spec.ts runs, the two invalid body cases still assert errorType is invalid-params; this line now emits error-invalid-params, so the existing package test fails even though the behavior change is intentional. Please update those expectations in the same change, or preserve the old emitted value.
Useful? React with 👍 / 👎.
Proposal
Fixes REST API HTTP status code semantics for authentication vs authorization failures and aligns validation errorType naming consistency across the typed router, resolving #41589.
Endpoints
ApiClass, Router, permissionsMiddleware, rooms.getMembers, rooms.hide, rooms.bannedUsers
Notes
Reserve 401 status strictly for unauthenticated requests (!user / missing session).
Remap permission denials (error-unauthorized / error-not-authorized) to 403 (forbidden).
Align Router.ts body validation error type from invalid-params to error-invalid-params matching ajvQuery failure responses.
Clean up dead applyBreakingChanges branch in permissions.ts middleware and update rooms.ts response schemas to validateForbiddenErrorResponse.
No changeset (API semantics alignment).
Task: #41589
Summary by CodeRabbit
Refactor: Standardized REST API error handling to return 403 Forbidden for authorization failures and reserved 401 Unauthorized strictly for unauthenticated missing-session requests.
Bug Fixes: Aligned body validation errorType in Hono typed router to 'error-invalid-params' for consistency with query validation errors across all REST endpoints.
Summary by CodeRabbit
error-invalid-paramserror type.