Skip to content

feat: add API key authentication to prover server#1880

Merged
sergeytimoshin merged 1 commit intomainfrom
sergey/prover-auth
Jul 21, 2025
Merged

feat: add API key authentication to prover server#1880
sergeytimoshin merged 1 commit intomainfrom
sergey/prover-auth

Conversation

@sergeytimoshin
Copy link
Contributor

@sergeytimoshin sergeytimoshin commented Jul 21, 2025

Enables authentication via X-API-Key or Bearer token headers.
Public /health endpoint remains accessible while all other routes require authentication when PROVER_API_KEY env var is set.

Summary by CodeRabbit

  • New Features

    • Added API key authentication for the prover server, allowing secure access to protected endpoints.
    • Authentication can be configured via environment variables and supports both "X-API-Key" and "Authorization: Bearer" headers.
    • The /health endpoint remains publicly accessible without authentication.
  • Chores

    • Introduced a test script to verify API key authentication behavior and endpoint accessibility.

Enables authentication via X-API-Key or Bearer token headers. Public
/health endpoint remains accessible while all other routes require
authentication when PROVER_API_KEY env var is set.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 21, 2025

Walkthrough

API key authentication was added to the prover server. A middleware was implemented to enforce API key checks on protected endpoints, excluding /health. The server now reads the API key from the environment, applies the middleware, and updates CORS headers. A test script was added to verify authentication behavior via HTTP requests.

Changes

File(s) Change Summary
prover/server/scripts/test_auth.sh New Bash script to test API key authentication on server endpoints.
prover/server/server/auth.go Added API key authentication middleware, helper functions, and conditional path-based enforcement.
prover/server/server/server.go Integrated authentication middleware and updated CORS headers in server startup logic.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    participant AuthMiddleware
    participant Handler

    Client->>Server: HTTP Request (/prove or /health)
    Server->>AuthMiddleware: Pass request
    alt /health endpoint
        AuthMiddleware->>Handler: Bypass authentication
    else Protected endpoint
        AuthMiddleware->>AuthMiddleware: Check API key in headers
        alt API key valid or not required
            AuthMiddleware->>Handler: Forward request
        else API key invalid/missing
            AuthMiddleware->>Client: Respond 401 Unauthorized
        end
    end
    Handler->>Client: Endpoint response
Loading

Estimated code review effort

3 (~40 minutes)

Poem

In the warren where secrets keep,
A key unlocks what lies so deep.
With headers checked and tokens tight,
Only the trusted pass this night.
The server stands with whiskers tall—
"Authenticate, or not at all!"
🐰🔑

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@sergeytimoshin sergeytimoshin changed the title feat: addd API key authentication to prover server feat: add API key authentication to prover server Jul 21, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3edbb81 and 85f603e.

📒 Files selected for processing (3)
  • prover/server/scripts/test_auth.sh (1 hunks)
  • prover/server/server/auth.go (1 hunks)
  • prover/server/server/server.go (2 hunks)
🧰 Additional context used
🪛 Shellcheck (0.10.0)
prover/server/scripts/test_auth.sh

[warning] 36-36: Did you forget to close this double quoted string?

(SC1078)


[error] 40-40: '(' is invalid here. Did you forget to escape it?

(SC1036)


[error] 40-40: Parsing stopped here. Invalid use of parentheses?

(SC1088)

🪛 Gitleaks (8.27.2)
prover/server/scripts/test_auth.sh

11-11: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


35-35: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

🔇 Additional comments (10)
prover/server/scripts/test_auth.sh (1)

1-43: LGTM on the testing approach.

The test script comprehensively covers the authentication scenarios:

  • Public endpoint access without authentication
  • Protected endpoint access without authentication (expecting 401)
  • Protected endpoint access with both X-API-Key and Authorization Bearer headers

The hardcoded API key is acceptable as it's clearly marked as a test value.

prover/server/server/server.go (3)

456-461: LGTM! Clean environment variable handling with appropriate logging.

The implementation properly reads the API key from the environment and provides clear logging messages for both enabled and disabled authentication states.


540-540: LGTM! Proper CORS header configuration.

Adding "X-API-Key" to allowed headers ensures clients can send the authentication header without CORS issues.


548-549: LGTM! Correct middleware ordering.

The authentication middleware is properly applied before the CORS handler, ensuring requests are authenticated before CORS processing. The conditional application based on API key presence is a good design choice.

prover/server/server/auth.go (6)

11-23: LGTM! Well-structured middleware pattern.

The middleware struct and constructor follow Go HTTP middleware conventions properly, providing clean dependency injection of the API key.


25-43: LGTM! Proper authentication flow with comprehensive logging.

The ServeHTTP implementation correctly handles the authentication flow, provides detailed logging for security monitoring, and returns appropriate error responses with helpful messages.


45-56: LGTM! Security-conscious authentication check.

Excellent use of crypto/subtle.ConstantTimeCompare to prevent timing attacks. The logic properly handles the case when no API key is configured (allowing all requests).


58-70: LGTM! Flexible API key extraction supporting multiple header formats.

The implementation correctly supports both X-API-Key and Authorization: Bearer header formats, providing flexibility for different client implementations.


76-88: LGTM! Clean path-based authentication control.

The requiresAuthentication function provides a maintainable way to define public paths. The current implementation correctly excludes /health from authentication requirements.


90-101: LGTM! Efficient conditional middleware application.

The conditional middleware only applies authentication to protected paths, avoiding unnecessary overhead for public endpoints while maintaining security for protected ones.

echo ""
echo "To run this test:"
echo "1. Set PROVER_API_KEY environment variable: export PROVER_API_KEY=test-api-key-12345"
echo "2. Start the prover server
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix unclosed double quote.

The double quote on line 36 is not properly closed, causing a syntax error.

Apply this diff to fix the syntax error:

-echo "2. Start the prover server
+echo "2. Start the prover server"
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 36-36: Did you forget to close this double quoted string?

(SC1078)

🤖 Prompt for AI Agents
In prover/server/scripts/test_auth.sh at line 36, the echo statement has an
unclosed double quote causing a syntax error. Close the double quote at the end
of the string to fix the syntax error.

Comment on lines +40 to +42
echo "- /health: 200 (always accessible)"
echo "- /prove without key: 401 (if API key is set)"
echo "- /prove with key: 400 or other (depends on valid circuit data)"
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix parsing errors with parentheses.

The parentheses in the expected results comments are causing shell parsing errors.

Apply this diff to fix the syntax errors:

-- /health: 200 (always accessible)
-- /prove without key: 401 (if API key is set)
-- /prove with key: 400 or other (depends on valid circuit data)"
+- /health: 200 \(always accessible\)
+- /prove without key: 401 \(if API key is set\)
+- /prove with key: 400 or other \(depends on valid circuit data\)"

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 Shellcheck (0.10.0)

[error] 40-40: '(' is invalid here. Did you forget to escape it?

(SC1036)


[error] 40-40: Parsing stopped here. Invalid use of parentheses?

(SC1088)

🤖 Prompt for AI Agents
In prover/server/scripts/test_auth.sh around lines 40 to 42, the parentheses in
the echo statements are causing shell parsing errors. To fix this, escape the
parentheses by prefixing them with backslashes or enclose the entire string in
single quotes to prevent the shell from misinterpreting them. This will ensure
the echo commands run without syntax errors.

@sergeytimoshin sergeytimoshin merged commit aef5ab4 into main Jul 21, 2025
17 of 19 checks passed
@sergeytimoshin sergeytimoshin deleted the sergey/prover-auth branch July 21, 2025 22:20
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.

2 participants