Skip to content

fix(auth): trim and validate stored api key in resolveApiKey#120

Merged
mrdailey99 merged 2 commits intodevelopfrom
fix/qh-validate-auth-fallback
Apr 15, 2026
Merged

fix(auth): trim and validate stored api key in resolveApiKey#120
mrdailey99 merged 2 commits intodevelopfrom
fix/qh-validate-auth-fallback

Conversation

@mrdailey99
Copy link
Copy Markdown
Collaborator

The stored key was returned as-is with no trim() or prefix check, unlike the env var path which had both. A key with trailing whitespace (e.g. from manual editing) or a pre-pv_k_ format key would be sent to the Quality Hub API unchanged, causing a 401 and triggering local_fallback even when a valid key exists. Tests added for both cases.

The stored key was returned as-is with no trim() or prefix check, unlike
the env var path which had both. A key with trailing whitespace (e.g. from
manual editing) or a pre-pv_k_ format key would be sent to the Quality Hub
API unchanged, causing a 401 and triggering local_fallback even when a
valid key exists. Tests added for both cases.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 15, 2026 06:39
Copy link
Copy Markdown
Contributor

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 PR aligns the “stored credentials” API key path with the environment variable path by trimming whitespace and enforcing the pv_k_ prefix in resolveApiKey(), preventing invalid keys from being sent to the Quality Hub API and incorrectly triggering fallback behavior.

Changes:

  • Trim whitespace from the stored api_key before use.
  • Validate that the stored key starts with pv_k_, otherwise return null.
  • Add unit tests covering whitespace trimming and invalid stored-key prefix handling.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/services/auth/credentials.ts Trims and validates stored API key in resolveApiKey() before returning it.
test/unit/services/auth/credentials.test.ts Adds tests for trimming stored keys and rejecting stored keys without the required prefix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +85 to +86
const storedKey = readStoredCredentials()?.api_key.trim() ?? null;
if (storedKey?.startsWith(KEY_PREFIX)) return storedKey;
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

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

readStoredCredentials() returns parsed JSON without runtime validation, so api_key may be missing or non-string for manually edited/corrupt files. Calling .trim() directly on readStoredCredentials()?.api_key can throw a TypeError when api_key is undefined/null. Consider guarding with ?.api_key?.trim() and/or a typeof api_key === 'string' check before trimming, and returning null for invalid shapes (optionally add a test for missing/non-string api_key).

Copilot uses AI. Check for mistakes.
Use typeof check before calling .trim() so a missing or non-string
api_key field in a manually edited credentials.json cannot throw a
TypeError. Added tests for missing-field and numeric api_key cases.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@mrdailey99 mrdailey99 merged commit b230cd6 into develop Apr 15, 2026
2 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.

2 participants