Skip to content

validateApiKey() compares API keys with plain === instead of a timing-safe comparison #525

Description

@sshdopey

Where: src/lib/apiKeys.ts, validateApiKey() (lines ~126-144).

What's wrong:

export function validateApiKey(key: string): ApiKey | null {
  for (const apiKey of keysStore.values()) {
    if (apiKey.status !== "active") continue;
    if (apiKey.key === key) return apiKey;
    if (apiKey.old_key && apiKey.new_key_expires_at && now < apiKey.new_key_expires_at) {
      if (apiKey.old_key === key) return apiKey;
    }
  }
  return null;
}

Both the current-key and grace-period-old-key comparisons use plain ===.
This is the same class of issue as the admin-key comparisons flagged
separately (apiKeyAuth.ts, graphql/schema.ts) — the codebase has
lib/timing-safe.ts precisely for this purpose but it isn't used here,
even though this function is on the live path for GraphQL API-key auth
(createGraphQLContext calls it directly).

Suggested fix: use timingSafeCompare(apiKey.key, key) (and likewise for
old_key) instead of ===.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave program

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions