Skip to content

fix: log auth failures in requireApiKey middleware#162

Open
0x-SquidSol wants to merge 1 commit intodcccrypto:mainfrom
0x-SquidSol:fix/log-auth-failures
Open

fix: log auth failures in requireApiKey middleware#162
0x-SquidSol wants to merge 1 commit intodcccrypto:mainfrom
0x-SquidSol:fix/log-auth-failures

Conversation

@0x-SquidSol
Copy link
Copy Markdown
Contributor

Summary

  • Issue: Both 401 return paths in auth.ts (missing key and invalid key) had zero logging — no createLogger was even imported. Sentry only captures 5xx, so auth failures were completely invisible to operators. Could not detect brute-force attempts or correlate failures with suspicious activity.
  • Fix: Adds structured warn-level logging with client IP, request path, and HTTP method for both failure modes.

Changes

  • src/middleware/auth.ts: Import createLogger, add logger.warn() to both 401 paths

Log output example

[2026-04-09T16:44:17.674Z] WARN  [api:auth] Auth failure: invalid x-api-key {"path":"/ws/stats","method":"GET","ip":"192.168.1.1"}
[2026-04-09T16:44:17.677Z] WARN  [api:auth] Auth failure: missing x-api-key {"path":"/ws/stats","method":"GET","ip":"unknown"}

Test plan

  • tsc --noEmit passes
  • vitest run passes (186/186 tests)
  • Auth test suite confirms warn logs are emitted on 401 responses

🤖 Generated with Claude Code

Both 401 return paths (missing key, invalid key) were completely
silent — no logger imported, no logging of any kind. Operators
could not detect brute-force attempts or correlate auth failures
with other suspicious activity.

Adds structured warn-level logging with client IP, request path,
and method for both failure modes. Follows the same pattern used
by the rate-limit middleware.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 9, 2026

Warning

Rate limit exceeded

@0x-SquidSol has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 17 minutes and 20 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 17 minutes and 20 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: cf736383-480c-4cd8-80ee-02b62a102d21

📥 Commits

Reviewing files that changed from the base of the PR and between fb94c29 and e8fad5f.

📒 Files selected for processing (1)
  • src/middleware/auth.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

1 participant