Skip to content

fix(scope): reject empty array from validateScope for a requested scope #518 - #532

Open
alikendir0 wants to merge 1 commit into
node-oauth:masterfrom
alikendir0:fix-validate-scope-empty-array
Open

alikendir0 wants to merge 1 commit into
node-oauth:masterfrom
alikendir0:fix-validate-scope-empty-array

Conversation

@alikendir0

Copy link
Copy Markdown

Summary

If the model's validateScope() returns an empty array, the handlers accept it, because the check is if (!validatedScope) and [] is truthy. With the "filter out invalid scopes" example from the model docs this means a request for a scope the client isn't allowed at all still gets an authorization code, and the token endpoint answers 200 with scope: "".

This changes the check in AbstractGrantType#validateScope and AuthorizeHandler#validateScope so an empty array is rejected with invalid_scope when a scope was actually requested. When no scope was requested, an empty array is still accepted like before. The issue asks for [] to be rejected in every case, but I didn't want to break models that return [] for scope-less requests (scope is optional since #103). If you'd rather have the strict version it's a one-line change, just say so.

@Figo5 offered to send a PR in the issue. I didn't see one after a month so I went ahead, but I'm happy to close this if you'd rather do it yourself.

Linked issue(s)

Fixes #518

Involved parts of the project

  • lib/grant-types/abstract-grant-type.js (authorization_code, client_credentials, password, and extension grants that call validateScope)
  • lib/handlers/authorize-handler.js

The refresh_token grant doesn't call validateScope, so it's untouched. I also updated the JSDoc in lib/model.js and regenerated docs/api with npm run docs:api.

Added tests?

Yes, for AbstractGrantType#validateScope, the authorize handler (error redirect, no code saved) and the token handler (400 invalid_scope, no token saved). The three rejection tests fail on master. Two more tests pin the "no scope requested" case so it keeps working.

OAuth2 standard

RFC 6749 section 3.3 defines scope as one or more scope tokens, so scope: "" in a token response isn't valid. Sections 4.1.2.1 and 5.2 define invalid_scope for the authorization and token endpoints.

Reproduction

Use a model whose validateScope filters the requested scopes:

validateScope(user, client, scope) {
  return scope.filter(s => ['read', 'write'].includes(s))
}

Send an authorization request with scope=admin. On master you get a code, and exchanging it returns 200 with scope: "". With this change the authorization request redirects with error=invalid_scope.

One thing I left alone: with no scope requested and a model returning [], the token response still contains scope: "" (from TokenHandler#updateSuccessResponse). That felt like a separate change, but I can add it here if you want.

Copilot AI balanced review requested due to automatic review settings November 21, 2026 01:09

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

validateScope() returning an empty array is accepted, and the token endpoint responds with scope: ""

2 participants