Skip to content

fix: prevent nil pointer panic on invalid X-API-Key in admin endpoints#4881

Merged
kodiakhq[bot] merged 5 commits intomainfrom
4842
Mar 2, 2026
Merged

fix: prevent nil pointer panic on invalid X-API-Key in admin endpoints#4881
kodiakhq[bot] merged 5 commits intomainfrom
4842

Conversation

@thomaspoignant
Copy link
Owner

Description

When accessing admin endpoints (e.g. POST /admin/v1/retriever/refresh) with an invalid X-API-Key, the relay proxy panics with a nil pointer dereference instead of returning 401 Unauthorized.

The root cause is that ErrorHandler in KeyAuthExtendedConfig defaults to nil (copied from echo's DefaultKeyAuthConfig.ErrorHandler which is also nil). When validateXAPIKey encounters an invalid key, it calls config.ErrorHandler(...) without checking for nil first.

Fix:

  • Added nil guards in validateXAPIKey so that if ErrorHandler is nil, the error is returned directly instead of panicking.
  • Explicitly set ErrorHandler: AuthMiddlewareErrHandler on the admin route group config to ensure consistent error handling.
  • Added tests reproducing the panic with a nil ErrorHandler.

Closes issue(s)

Resolve #4842

Checklist

  • I have tested this code
  • I have added unit test to cover this code
  • I have updated the documentation (README.md and /website/docs)
  • I have followed the contributing guide

Made with Cursor

When ErrorHandler is nil in KeyAuthExtendedConfig (which happens when
using echo's DefaultKeyAuthConfig defaults), sending an invalid X-API-Key
causes a nil pointer dereference panic instead of returning 401.

This adds nil checks for ErrorHandler in validateXAPIKey and explicitly
sets AuthMiddlewareErrHandler on the admin route group config.

Closes #4842

Made-with: Cursor
@netlify
Copy link

netlify bot commented Feb 26, 2026

Deploy Preview for go-feature-flag-doc-preview canceled.

Name Link
🔨 Latest commit 9eb5920
🔍 Latest deploy log https://app.netlify.com/projects/go-feature-flag-doc-preview/deploys/69a59cf8fb27840008318c55

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request effectively addresses a nil pointer dereference issue in the KeyAuthExtended middleware when an invalid X-API-Key is provided and no custom ErrorHandler is configured. The fix involves adding nil checks before invoking the ErrorHandler and explicitly setting a custom error handler for admin routes to ensure consistent behavior. New tests have been added to reproduce and verify the fix.

@codecov
Copy link

codecov bot commented Feb 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.81%. Comparing base (d54dd37) to head (9eb5920).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4881      +/-   ##
==========================================
+ Coverage   85.75%   85.81%   +0.05%     
==========================================
  Files         153      153              
  Lines        6537     6541       +4     
==========================================
+ Hits         5606     5613       +7     
+ Misses        699      698       -1     
+ Partials      232      230       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

thomaspoignant and others added 4 commits February 26, 2026 12:46
Add tests for the case where the validator returns a non-nil error
(e.g. a database failure), exercising both the nil ErrorHandler guard
(line 65) and the ErrorHandler delegation (line 67).

This brings patch coverage to 100% for the changes in #4842.

Made-with: Cursor
Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org>
@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 2, 2026

@kodiakhq kodiakhq bot merged commit 057426d into main Mar 2, 2026
25 checks passed
@kodiakhq kodiakhq bot deleted the 4842 branch March 2, 2026 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(bug) Internal Server Error when accessing admin endpoints with an invalid key

1 participant