Skip to content

fix(auth): name invalid OAuth scopes and re-authenticate when scopes change (#552) - #659

Merged
petebacondarwin merged 4 commits into
cloudflare:mainfrom
TheVicky1:fix/oauth-scope-validation-552
Nov 23, 2026
Merged

petebacondarwin merged 4 commits into
cloudflare:mainfrom
TheVicky1:fix/oauth-scope-validation-552

Conversation

@TheVicky1

@TheVicky1 TheVicky1 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Why

Fixes #552.

When requesting custom scopes during cf auth login:

  1. If any requested scopes are invalid, cf previously threw a generic message: One or more of the requested scopes are not valid cf OAuth scopes. without naming which scope failed validation.
  2. If a user was already logged in and ran cf auth login --scopes <new_scopes> without --force, cf previously ignored --scopes, printed You are already logged in, and exited 0. This silently dropped the requested permissions.

What changed

  • In src/lib/oauth/index.ts, login() now checks individual scopes against validateScopeKeys() and names all invalid scopes in the error message (e.g. Invalid cf OAuth scope(s): foo, bar.).
  • In src/commands/auth/login.ts, validate explicit scopes before checking the current login. When the requested and stored scope sets differ, including when requesting fewer scopes, start a fresh OAuth login without requiring --force. Equivalent sets reuse the current credentials, ignoring order, duplicates, and automatically added offline_access.
  • Added comprehensive unit tests in auth.test.ts and device-login.test.ts.
  • Added a patch changeset.

Blast Radius

Only cf auth login with --scopes is affected. Login without --scopes and login with an equivalent scope set behave unchanged.

Verification

  • pnpm --filter cf exec vp test run src/__tests__/lib/auth.test.ts passed (19 tests).
  • pnpm --filter cf exec vp test run -t "requesting new scopes|holds the requested scopes" src/__tests__/commands/auth/device-login.test.ts passed.
  • Follow-up: all 160 auth tests and the root pnpm check passed.

@TheVicky1
TheVicky1 requested a review from a team as a code owner November 17, 2026 12:44
@github-actions

Copy link
Copy Markdown
Contributor

Hi @TheVicky1. Thanks for contributing to cf!

During the release candidate phase, we’re being very intentional about the changes we accept so that we can prioritise stability for GA. We’re limiting pull requests to contributions coordinated with the team in advance, so this PR has been automatically closed.

We’d still love to hear about your use case, so please make sure it’s captured in a cf issue. If the proposed change fits our priorities, we’d be happy to take another look at this PR.

@github-actions github-actions Bot closed this Nov 17, 2026
@TheVicky1

Copy link
Copy Markdown
Contributor Author

Hi @petebacondarwin!

I saw the auto-close notice regarding the RC phase. I've prepared a clean, focused fix for #552:

  • Names the specific invalid OAuth scopes instead of a generic message.
  • Prevents silently dropping --scopes when already logged in without --force.
  • Includes unit tests in auth.test.ts and device-login.test.ts and a patch changeset.

The branch and tests are ready. Could you please take a look when you have a moment and vouch for me or reopen the PR? Thank you!

@clanky-the-clanker

clanky-the-clanker Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

This pull request's reviews are posted as pull request reviews: see the review of 6a934329.

@petebacondarwin

Copy link
Copy Markdown
Collaborator

clanker review

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 710f03f5.

This review, of 6a934329

💬 Approved with comments — One WARNING: the new error message recommends --profile <name>, which cf auth login rejects. One SUGGESTION about checking scope names before the already-logged-in check.

Findings: 1 warning · 1 suggestion (2 are inline comments on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #659.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## WARNING (1)

### [Code Quality] The error hint recommends `--profile <name>`, but `cf auth login` rejects `--profile` (`assertNoProfileFlag` at login.ts:74); point to `cf auth create <name>` instead.
- **File:** packages/cli/src/commands/auth/login.ts:122

## SUGGESTION (1)

### [Code Quality] Scope names are checked only inside `login()`, after this check, so a mistyped scope on an existing login is reported as missing and the user is told to use `--force`.
- **File:** packages/cli/src/commands/auth/login.ts:117

📋 Code Quality: ⚠️ 1 warning · 1 suggestion

The new error hint recommends a flag the command rejects, and a mistyped scope is reported as missing when the user is already logged in.

Recommendation: 📝 Minor Comments

  • [WARNING] The error hint recommends --profile <name>, but cf auth login rejects --profile (assertNoProfileFlag at login.ts:74); point to cf auth create <name> instead. (packages/cli/src/commands/auth/login.ts:122)
  • [SUGGESTION] Scope names are checked only inside login(), after this check, so a mistyped scope on an existing login is reported as missing and the user is told to use --force. (packages/cli/src/commands/auth/login.ts:117)
View full review
  • --profile hint is wrong (packages/cli/src/commands/auth/login.ts:122). The handler starts with assertNoProfileFlag(argv, "login", ...) at line 74. That call rejects --profile on cf auth login and points to cf auth create <name>, so following the new hint leads straight to another error.
  • Invalid scopes are reported as missing (login.ts:115-125). The already-logged-in check runs before login() checks scope names. A typo such as --scopes workerz:write first gives "run --force". The --force run then fails with Invalid cf OAuth scope(s). Checking scope names before the already-logged-in check would give the correct error in one step.

✅ All other checks passed

🚀 Release: ✅ LGTM

The patch changeset describes both behavior changes in cf auth login: invalid scopes are named, and requesting new scopes on an existing login now requires --force. Release risk is moderate because the change is limited to scope handling in the login flow.


🔒 Security: ✅ LGTM

No security concerns in the scope check or error-message changes.


📖 AGENTS.md: ✅ LGTM

The changes follow the repository conventions, and a changeset is included.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 6a934329

Comment thread packages/cli/src/commands/auth/login.ts Resolved
Comment thread packages/cli/src/commands/auth/login.ts Resolved
@TheVicky1

Copy link
Copy Markdown
Contributor Author

Addressed both findings in commit 710f03f: early scope validation ensures invalid scope names fail before checking existing credentials.

Please review it, Thanks!

@petebacondarwin

Copy link
Copy Markdown
Collaborator

clanker review

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Out of date: this review is of 710f03f5; the pull request is now at 456e88dd. Someone with write access can comment clanky review (or clanker review or @clanky-the-clanker review) to review the new commits.

💬 Approved with comments — Both earlier findings are fixed. The only remaining note is an INFO about changeset wording.

Findings: 1 info (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📝 Documentation: 💬 1 info

The changeset describes an error message as a prompt.

Recommendation: 💬 Optional Nits

  • [INFO] The changeset says the command will "prompt for --force", but it exits with an error telling the user to rerun with --force. (.changeset/oauth-scope-validation.md:9)
View full review

The changeset says the command will "prompt for --force". It actually exits with an error that tells the user to rerun with --force.


✅ All other checks passed

🚀 Release: ✅ LGTM

The patch changeset documents both changes: invalid scope names are now listed in the error, and requesting new scopes on an existing login now requires --force. The change is limited to scope handling in the login flow.


🔒 Security: ✅ LGTM

Scope names are checked against a fixed allowlist. The error messages echo only the user's own CLI input, to the local terminal. Requiring --force for new scopes makes the CLI stricter, not looser.


📖 AGENTS.md: ✅ LGTM

The change follows the repository conventions and includes a changeset.


📋 Code Quality: ✅ LGTM

  • The --profile hint is fixed: the error at login.ts:125-128 now points to cf auth create <name>, which matches the assertNoProfileFlag message.
  • The scope check order is fixed: assertValidScopeKeys runs at login.ts:81-83, before the already-logged-in branch, and a test covers it.
  • login() still checks the scope names too. Checking them twice is harmless and keeps login() safe for other callers.

⚡ Performance: ✅ LGTM

No performance concerns.


Reviewed commit: 710f03f5

Comment thread .changeset/oauth-scope-validation.md Resolved
@petebacondarwin

Copy link
Copy Markdown
Collaborator

In src/commands/auth/login.ts, if the user is already logged in and --scopes specifies scopes not present in the current credentials, cf now halts with an error informing the user that re-authenticating with new scopes requires --force (or --profile ), rather than silently exiting 0. If the existing credentials already satisfy the requested scopes, it succeeds as before.

Rather than requiring the user to run again with --force we should just trigger a new login flow (an implicit force) since we know the user is requesting new scopes.

@petebacondarwin petebacondarwin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Rather than requiring the user to run again with --force we should just trigger a new login flow (an implicit force) since we know the user is requesting new scopes.

@TheVicky1
TheVicky1 force-pushed the fix/oauth-scope-validation-552 branch from 710f03f to 3988b9c Compare November 19, 2026 06:39
@pkg-pr-new

pkg-pr-new Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
npm i https://pkg.pr.new/cf@659

commit: 456e88d

@TheVicky1

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback, @petebacondarwin!

I've updated the implementation:

Requesting new scopes now directly initiates a new OAuth login flow (implicit --force) instead of halting.
Invalid scopes are still caught and reported early.
Unit tests, snapshots, and the changeset have been updated.
Rebased onto the latest main.

Ready for another look when you have time!

@petebacondarwin petebacondarwin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

All looks great except for the subset check.

Comment thread packages/cli/src/commands/auth/login.ts Resolved
@petebacondarwin petebacondarwin changed the title fix(auth): name invalid OAuth scopes and require --force for new scopes (#552) fix(auth): name invalid OAuth scopes and re-authenticate when scopes change (#552) Nov 23, 2026
@petebacondarwin
petebacondarwin removed the request for review from dario-piotrowicz November 23, 2026 10:13
@petebacondarwin
petebacondarwin added this pull request to the merge queue Nov 23, 2026
Merged via the queue into cloudflare:main with commit 30bfe56 Nov 23, 2026
10 checks passed
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.

auth login: requesting new scopes on an existing login silently succeeds; invalid-scope error doesn't name the scope

3 participants