Skip to content

fix: Improve API key validation to prevent GitHub PAT acceptance#34

Open
spenceriam wants to merge 1 commit intomainfrom
fix/bug-23-api-key-validation
Open

fix: Improve API key validation to prevent GitHub PAT acceptance#34
spenceriam wants to merge 1 commit intomainfrom
fix/bug-23-api-key-validation

Conversation

@spenceriam
Copy link
Owner

@spenceriam spenceriam commented Nov 3, 2025

Summary

Fixes critical security issue where GitHub PATs and other invalid keys were being accepted by the API key validation.

Changes Made

  • Updated API key format validation to require 'sk-or-v1-' prefix (OpenRouter's current format)
  • Enhanced error messages in ApiKeyInput component for better user feedback
  • Added proper API key validation in ModelSelector before fetching models
  • Updated placeholder text to reflect correct key format

Testing

  • Tested with GitHub PAT - correctly rejected
  • Tested with valid OpenRouter API key - correctly accepted
  • Tested with old 'sk-or-' format keys - correctly rejected
  • Error messages are now more descriptive and helpful

Fixes #23


Note

Require 'sk-or-v1-' API keys across input, storage, and model fetch with clearer errors and updated placeholders.

  • Frontend:
    • components/ApiKeyInput.tsx:
      • Enforce sk-or-v1- prefix before testing keys; update placeholder accordingly.
      • Improve error handling/messages (401/403, 429, network) and clear invalid keys on failure.
    • components/ModelSelector.tsx:
      • Validate API key format via isValidFormat before fetching models; surface specific error messages.
  • Hooks:
    • hooks/useSimpleApiKeyStorage.ts:
      • Update isValidFormat to require sk-or-v1- and expose it to consumers.

Written by Cursor Bugbot for commit dab9d89. This will update automatically on new commits. Configure here.

- Update API key validation to require 'sk-or-v1-' prefix instead of accepting 'sk-or-' or 'sk-'
- Add proper format checking in useSimpleApiKeyStorage hook
- Improve error messages for invalid keys
- Add validation in ModelSelector before fetching models
- Update placeholder text to reflect correct key format

Fixes #23
@vercel
Copy link

vercel bot commented Nov 3, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
open-spec Ready Ready Preview Comment Nov 3, 2025 1:35am

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

This PR is being reviewed by Cursor Bugbot

Details

You are on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

error: 'Invalid API key format. OpenRouter keys must start with "sk-or-v1-"'
})
return false
}
Copy link

Choose a reason for hiding this comment

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

Bug: Stale Invalid API Key Causes Inconsistent State

Early-return on invalid key format sets an error but does not clear a stored invalid API key or notify the parent via onApiKeyValidated(false), unlike other failure paths. This can leave a stale invalid key in sessionStorage and skip parent state updates (especially when autoTest triggers testApiKey), causing inconsistent UI/state compared to other error cases.

Fix in Cursor Fix in Web

@spenceriam spenceriam requested a review from Copilot November 3, 2025 03:44
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request updates the OpenRouter API key validation to enforce a stricter format requirement. The changes tighten validation from accepting multiple key formats (sk-or- or sk-) to only accepting the specific OpenRouter format sk-or-v1-, ensuring proper authentication with the OpenRouter API. Additionally, the version has been bumped to 0.5.5-beta and package-lock.json peer dependencies have been updated.

  • Enforces strict OpenRouter API key format validation (sk-or-v1- prefix)
  • Improves error messages with more specific authentication and network error handling
  • Adds API key format validation check before making API calls

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.

File Description
package-lock.json Version bump to 0.5.5-beta and peer dependency updates
hooks/useSimpleApiKeyStorage.ts Updated API key format validation to require sk-or-v1- prefix and exposed isValidFormat in return value
components/ApiKeyInput.tsx Enhanced validation logic with stricter format checks and improved error messages for authentication, rate limiting, and network issues
components/ModelSelector.tsx Added pre-flight API key format validation and enhanced error handling with specific error types

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.

bug: API key check is not correct/functional

1 participant