Skip to content

- redoc CPS issue #1930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
Merged

Conversation

hiteshghuge
Copy link
Collaborator

@hiteshghuge hiteshghuge commented Apr 23, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Adjusted security headers for the /redoc endpoint to allow worker scripts from blob URLs, ensuring proper functionality of the documentation page without affecting other endpoints.
  • Tests

    • Added a test to verify that the /redoc endpoint returns the correct set of security and response headers.

Copy link

coderabbitai bot commented Apr 23, 2025

Walkthrough

The changes introduce special handling for HTTP requests to the /redoc endpoint across multiple server modules. Specifically, the middleware responsible for adding security headers now detects requests to /redoc and temporarily modifies the Content Security Policy (CSP) to allow workers from blob URLs by adding a worker-src 'blob:' directive. This is accomplished by deep copying the existing CSP, modifying it, applying it to the response, and then restoring the original CSP. Additionally, a new integration test verifies that the /redoc endpoint returns the expected security headers.

Changes

File(s) Change Summary
kairon/actions/server.py
kairon/api/app/main.py
kairon/chat/server.py
kairon/events/server.py
Updated the security headers middleware to detect /redoc requests, temporarily modify CSP to allow worker-src 'blob:', and restore CSP after response.
tests/integration_test/services_test.py Added a new test, test_redoc_headers, to assert that the /redoc endpoint returns the expected set of security headers.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    participant Middleware

    Client->>Server: GET /redoc
    Server->>Middleware: Process request
    Middleware->>Middleware: Deep copy CSP
    Middleware->>Middleware: Add worker-src 'blob:' to CSP
    Middleware->>Server: Apply modified CSP to response
    Middleware->>Middleware: Restore original CSP
    Server-->>Client: Response with modified headers
Loading

Poem

In the warren of code, a change hops through,
Redoc's headers now sparkling and new!
Blob workers allowed, just for this path,
Security stays tight—no developer's wrath.
With tests in the meadow, all checks are complete,
This bunny approves—secure and neat!
🐇✨

✨ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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.

Copy link

@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: 0

🧹 Nitpick comments (1)
tests/integration_test/services_test.py (1)

30555-30576: Thoroughly validates /redoc security headers but could be more robust.

The test properly verifies that the /redoc endpoint returns the expected security headers, including the modified Content Security Policy with worker-src blob: which allows RedOc to function correctly. However, the test has some potential issues:

  1. Asserting the exact content-length value (498) makes the test brittle - this value will likely change if the response content is modified.
  2. Hard-coding all header values increases maintenance overhead as any future change to security headers will require updating this test.

Consider making the test more robust with these changes:

def test_redoc_headers():
    response = client.get("/redoc")
    assert response.status_code == 200
-    assert response.headers == {
-        "content-length": "498",
-        "content-type": "text/html; charset=utf-8",
+    # Verify critical security headers
+    assert "worker-src blob:" in response.headers["content-security-policy"]
+    assert response.headers["x-frame-options"] == "SAMEORIGIN"
+    assert response.headers["strict-transport-security"] == "includeSubDomains; preload; max-age=31536000"
+    
+    # Check other expected headers exist without exact value matching
+    expected_headers = [
         "content-type",
         "content-encoding",
-        "vary": "Accept-Encoding",
-        "server": "Secure",
-        "strict-transport-security": "includeSubDomains; preload; max-age=31536000",
-        "x-frame-options": "SAMEORIGIN",
-        "x-xss-protection": "0",
-        "x-content-type-options": "nosniff",
-        "content-security-policy": "default-src 'self'; frame-ancestors 'self'; form-action 'self'; base-uri 'self'; connect-src 'self'; frame-src 'self'; style-src 'self' https: 'unsafe-inline'; img-src 'self' https:; script-src 'self' https: 'unsafe-inline'; worker-src blob:",
-        "referrer-policy": "no-referrer",
-        "cache-control": "must-revalidate",
-        "permissions-policy": "accelerometer=(), autoplay=(), camera=(), document-domain=(), encrypted-media=(), fullscreen=(), vibrate=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), payment=(), picture-in-picture=(), sync-xhr=(), usb=()",
-        "cross-origin-embedder-policy": "require-corp",
-        "cross-origin-opener-policy": "same-origin",
-        "cross-origin-resource-policy": "same-origin",
-        "access-control-allow-origin": "*"
-    }
+        "vary",
+        "server",
+        "x-xss-protection",
+        "x-content-type-options",
+        "referrer-policy",
+        "cache-control",
+        "permissions-policy",
+        "cross-origin-embedder-policy",
+        "cross-origin-opener-policy", 
+        "cross-origin-resource-policy",
+        "access-control-allow-origin"
+    ]
+    for header in expected_headers:
+        assert header in response.headers
+    
+    # Optionally verify response contains expected RedOc content
+    assert "swagger-ui" in response.text or "redoc" in response.text
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b67f564 and 32904f1.

📒 Files selected for processing (5)
  • kairon/actions/server.py (2 hunks)
  • kairon/api/app/main.py (2 hunks)
  • kairon/chat/server.py (2 hunks)
  • kairon/events/server.py (2 hunks)
  • tests/integration_test/services_test.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Analyze (python)
  • GitHub Check: Python CI
🔇 Additional comments (8)
kairon/actions/server.py (2)

3-3: Added import for deep copying CSP.

The copy module is imported to support the deep copying of the Content Security Policy object for the /redoc endpoint.


135-141: Properly modified CSP for /redoc endpoint.

This implementation correctly creates a deep copy of the CSP, adds the necessary worker-src blob: directive specifically for the /redoc endpoint, and then restores the original CSP after applying the headers. This approach ensures other endpoints maintain the stricter CSP.

kairon/chat/server.py (2)

2-2: Added import for deep copying CSP.

The copy module is imported to support the deep copying of the Content Security Policy object for the /redoc endpoint.


111-117: Properly modified CSP for /redoc endpoint.

This implementation correctly creates a deep copy of the CSP, adds the necessary worker-src blob: directive specifically for the /redoc endpoint, and then restores the original CSP after applying the headers. This approach ensures other endpoints maintain the stricter CSP.

kairon/events/server.py (2)

1-1: Added import for deep copying CSP.

The copy module is imported to support the deep copying of the Content Security Policy object for the /redoc endpoint.


90-96: Properly modified CSP for /redoc endpoint.

This implementation correctly creates a deep copy of the CSP, adds the necessary worker-src blob: directive specifically for the /redoc endpoint, and then restores the original CSP after applying the headers. This approach ensures other endpoints maintain the stricter CSP.

kairon/api/app/main.py (2)

1-1: Added import for deep copying CSP.

The copy module is imported to support the deep copying of the Content Security Policy object for the /redoc endpoint.


102-108: Properly modified CSP for /redoc endpoint.

This implementation correctly creates a deep copy of the CSP, adds the necessary worker-src blob: directive specifically for the /redoc endpoint, and then restores the original CSP after applying the headers. This approach ensures other endpoints maintain the stricter CSP.

Copy link
Collaborator

@sushantpatade sushantpatade left a comment

Choose a reason for hiding this comment

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

Approve

Copy link
Collaborator

@sushantpatade sushantpatade left a comment

Choose a reason for hiding this comment

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

Approved

@sushantpatade sushantpatade merged commit 9904c62 into digiteinfotech:master Apr 29, 2025
7 of 8 checks passed
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.

3 participants