Skip to content

fix(virtual_lab): secure evaluate_code endpoint with auth, rate limiting, and tests#1032

Open
arnavgogia20 wants to merge 1 commit intoalphaonelabs:mainfrom
arnavgogia20:fix/evaluate-code-auth-rate-limit
Open

fix(virtual_lab): secure evaluate_code endpoint with auth, rate limiting, and tests#1032
arnavgogia20 wants to merge 1 commit intoalphaonelabs:mainfrom
arnavgogia20:fix/evaluate-code-auth-rate-limit

Conversation

@arnavgogia20
Copy link

@arnavgogia20 arnavgogia20 commented Mar 18, 2026

Security Fix: Protect evaluate_code Endpoint

This PR addresses a critical security vulnerability in the evaluate_code endpoint by adding authentication, rate limiting, and improved request handling.


Problem

The evaluate_code endpoint previously:

  • Allowed unauthenticated users to execute arbitrary code via the Piston API
  • Had no enforced rate limiting, making it vulnerable to abuse
  • Could return 500 errors on malformed JSON input

Changes Made

1. Authentication Protection

  • Added @login_required to restrict access to authenticated users only

2. Rate Limiting (Per-user)

  • Implemented lightweight rate limiting using Django cache
  • Limit: 5 requests per user per minute
  • Returns 429 Too Many Requests when exceeded
  • Added logging for rate limit violations

3. Safe JSON Handling

  • Wrapped json.loads(request.body) in a try/except
  • Returns 400 Bad Request for invalid JSON payloads

4. Test Coverage

Added tests to ensure reliability:

  • Unauthenticated access is blocked
  • Rate limiting is enforced correctly
  • Invalid JSON payloads are handled gracefully

External API calls (Piston) are mocked in tests to ensure consistency.


Verification

  • All tests passing (234 tests)
  • Pre-commit hooks pass (black, isort, flake8, etc.)
  • No breaking changes introduced

Notes

  • Implementation uses Django's built-in cache to avoid introducing new dependencies
  • Logic is intentionally kept simple and localized to the view for maintainability

Impact

  • Prevents abuse of external code execution API
  • Improves endpoint reliability and error handling
  • Aligns with secure coding practices for production systems

Please let me know if you'd like any changes or improvements...i'll be happy to iterate!

Security Enhancement for evaluate_code Endpoint

This PR addresses a security vulnerability in the evaluate_code endpoint by introducing authentication, rate limiting, and input validation controls.

Key Changes

Authentication & Rate Limiting (views.py)

  • Added @login_required decorator to restrict endpoint access to authenticated users
  • Implemented per-user rate limiting using Django cache (5 requests per minute)
  • Returns 429 Too Many Requests when limit is exceeded with appropriate logging

Input Validation (views.py)

  • Wrapped JSON parsing in try/except block to gracefully handle invalid JSON payloads
  • Returns 400 Bad Request for malformed JSON with descriptive error message

Test Coverage (tests.py)

  • Added comprehensive test suite with 52 lines covering:
    • Unauthenticated access blocking (302 redirect to login)
    • Rate limiting enforcement across multiple requests
    • Invalid JSON payload handling
  • External Piston API calls are mocked in tests to avoid external dependencies

Build Configuration

  • Updated poetry.toml to enable automatic virtual environment creation
  • Modified pre-commit configuration to run Django tests serially (removed --parallel=4 flag)

Impact

These changes prevent abuse of external code execution functionality, improve error handling reliability, and align with secure coding best practices. All 234 existing tests pass along with pre-commit hooks validation.

…miting

- add login_required protection
- implement per-user rate limiting via Django cache
- add safe JSON parsing with error handling
- log rate limit violations
- add test coverage for auth and throttling
@github-actions
Copy link
Contributor

👀 Peer Review Required

Hi @arnavgogia20! This pull request does not yet have a peer review.

Before this PR can be merged, please request a review from one of your peers:

  • Go to the PR page and click "Reviewers" on the right sidebar.
  • Select a team member or contributor to review your changes.
  • Once they approve, this reminder will be automatically removed.

Thank you for contributing! 🎉

@github-actions github-actions bot added the files-changed: 5 PR changes 5 files label Mar 18, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 18, 2026

Note

.coderabbit.yaml has unrecognized properties

CodeRabbit is using all valid settings from your configuration. Unrecognized properties (listed below) have been ignored and may indicate typos or deprecated fields that can be removed.

⚠️ Parsing warnings (1)
Validation error: Unrecognized key(s) in object: 'tools'
⚙️ Configuration instructions
  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • 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

Walkthrough

The changes implement authentication and rate limiting for the evaluate_code endpoint, introduce comprehensive test coverage for this functionality, adjust Poetry's virtual environment configuration, and modify Django test execution settings to run serially instead of in parallel.

Changes

Cohort / File(s) Summary
Configuration Updates
.pre-commit-config.yaml, poetry.toml
Removed --parallel=4 flag from Django test command to run tests serially; enabled automatic virtual environment creation in Poetry.
Feature Implementation
web/virtual_lab/views.py
Added @login_required decorator for authentication; implemented per-user rate limiting (5 requests/minute) via Django cache with 429 response; added try/except for JSON parsing errors returning 400 response; preserved existing error handling for downstream API failures.
Test Coverage
web/virtual_lab/tests.py
Added EvaluateCodeTests suite with three test cases: authentication requirement verification, rate limiting enforcement (mocked external API), and invalid JSON payload handling.

Sequence Diagram(s)

sequenceDiagram
    participant Client as User/Client
    participant View as Django View<br/>(evaluate_code)
    participant Cache as Django Cache
    participant API as External API<br/>(Piston)

    Client->>View: POST /evaluate_code
    
    alt Not Authenticated
        View-->>Client: Redirect to /accounts/login/
    else Authenticated
        View->>Cache: Check rate limit<br/>(key: user_id)
        alt Rate Limit Exceeded
            Cache-->>View: 429 response
            View-->>Client: 429 + error message
        else Within Limit
            View->>View: Parse JSON payload
            alt Invalid JSON
                View-->>Client: 400 + error message
            else Valid JSON
                View->>Cache: Increment request count
                View->>API: POST code to evaluate
                API-->>View: Response (200/5xx)
                View-->>Client: Response
            end
        end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: adding authentication, rate limiting, and tests to the evaluate_code endpoint, which aligns with all major file modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

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.

Tip

CodeRabbit can approve the review once all CodeRabbit's comments are resolved.

Enable the reviews.request_changes_workflow setting to automatically approve the review once all CodeRabbit's comments are resolved.

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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@web/virtual_lab/tests.py`:
- Line 18: The test currently calls cache.clear(), which wipes the global cache;
instead target and delete only the rate-limit key for the test user. Replace the
cache.clear() call with a deletion of the specific key (e.g., use
cache.delete(rate_limit_key) or cache.pop(rate_limit_key, None)) where
rate_limit_key is the same key your rate limiter uses for this user within the
test setup; keep the rest of the cache untouched to avoid cross-test coupling.

In `@web/virtual_lab/views.py`:
- Around line 158-169: The rate limiter has a race on initialization and a
non-parameterized log; replace the get/set sequence with an atomic
cache.add(cache_key, 1, timeout=60) to initialize only one process, then call
cache.incr(cache_key) for subsequent requests and check the returned value
against the limit (>=5) to return the 429 JsonResponse; also change the logger
call in this branch to parameterized logging via logger.warning("Rate limit
exceeded for user %s", request.user.id) to avoid f-string evaluation and support
structured logging.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: alphaonelabs/coderabbit/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: dcba5c22-e36f-411d-8e5f-cdfe06c47266

📥 Commits

Reviewing files that changed from the base of the PR and between c94caf8 and ba40f0e.

⛔ Files ignored due to path filters (1)
  • poetry.log is excluded by !**/*.log
📒 Files selected for processing (4)
  • .pre-commit-config.yaml
  • poetry.toml
  • web/virtual_lab/tests.py
  • web/virtual_lab/views.py

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

Labels

files-changed: 5 PR changes 5 files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant