Skip to content

fix: validate API key against Resend API before saving#85

Merged
felipefreitag merged 5 commits intoresend:mainfrom
Bensonn5151:fix/login-api-key-validation
Mar 16, 2026
Merged

fix: validate API key against Resend API before saving#85
felipefreitag merged 5 commits intoresend:mainfrom
Bensonn5151:fix/login-api-key-validation

Conversation

@Bensonn5151
Copy link
Contributor

@Bensonn5151 Bensonn5151 commented Mar 13, 2026

The login command accepted any key with the re_ prefix without verifying it against the API. The Resend SDK returns errors in the response object rather than throwing exceptions, so the try/catch block never caught invalid keys.

Check the error field in the response from resend.domains.list() and exit with validation_failed if the API rejects the key.

Fixes #70


Summary by cubic

Validate the API key during login by calling the Resend API and reject invalid keys before saving. This prevents saving bad keys and shows a clear validation error from the API.

  • Bug Fixes
    • Read { error } from resend.domains.list(); on error, fail the spinner, outputError with code validation_failed, and return to avoid the "valid" message and skip writing credentials.
    • Tests mock overrideable resend responses and add an invalid-key case; assert exit code 1, the validation_failed error, and no credentials file.

Written for commit f4a361b. Summary will update on new commits.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="tests/commands/auth/login.test.ts">

<violation number="1" location="tests/commands/auth/login.test.ts:81">
P2: Invalid-key regression test does not verify that credentials are not persisted, leaving the PR’s core guarantee unprotected.</violation>
</file>

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Add one-off context when rerunning by tagging @cubic-dev-ai with guidance or docs links (including llms.txt)
  • Ask questions if you need clarification on any suggestion

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

The login command accepted any key with the re_ prefix without verifying
it against the API. The Resend SDK returns errors in the response object
rather than throwing exceptions, so the try/catch block never caught
invalid keys.

Check the error field in the response from resend.domains.list() and
exit with validation_failed if the API rejects the key.

Fixes resend#70
@Bensonn5151 Bensonn5151 force-pushed the fix/login-api-key-validation branch from e58687c to 0a6944d Compare March 13, 2026 16:24
@kevkevinpal
Copy link
Contributor

I was able to test this, and it looks good to me

Before:
I was able to log in and save credentials with any token prefixed with re_

After

pnpm dev login --json

> resend-cli@1.4.1 dev /mnt/shared_drive/DEVDIR/resend-cli
> tsx src/cli.ts login --json

┌  Resend Authentication
│
●  Existing API key found (source: config). Enter a new key to replace it.
│
◇  How would you like to get your API key?
│  Enter API key manually
│
◇  Enter your Resend API key:
│  ▪▪▪▪▪▪
  ✗ API key validation failed
{
  "error": {
    "message": "API key is invalid",
    "code": "validation_failed"
  }
}
 ELIFECYCLE  Command failed with exit code 1.
 ```

Bensonn5151 and others added 3 commits March 13, 2026 17:51
Without the early return, execution continues to spinner.stop('API key is valid')
even when the API rejects the key.
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 1 file (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/commands/auth/login.ts">

<violation number="1" location="src/commands/auth/login.ts:150">
P2: Validation uses `domains.list()` and now hard-fails on any error, which rejects valid `sending_access` API keys that lack domain-read permissions.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@felipefreitag felipefreitag merged commit a209865 into resend:main Mar 16, 2026
7 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.

Resend login saves API keys without validating against the API

3 participants