Skip to content

fix: add CORS logging middleware and redeploy#28

Open
KnellBalm wants to merge 2 commits intomainfrom
fix-cors-logging-redeploy-8522996915867058504
Open

fix: add CORS logging middleware and redeploy#28
KnellBalm wants to merge 2 commits intomainfrom
fix-cors-logging-redeploy-8522996915867058504

Conversation

@KnellBalm
Copy link
Copy Markdown
Owner

@KnellBalm KnellBalm commented Feb 3, 2026

This PR addresses the reported CORS issue by adding a logging middleware to debug future occurrences and forcing a redeployment of the backend service. The investigation confirmed that the reported origin IS present in the configuration, suggesting the deployed version might be stale. This commit ensures the correct configuration is deployed and provides better visibility into CORS rejections.


PR created automatically by Jules for task 8522996915867058504 started by @KnellBalm

Summary by Sourcery

Add middleware to log missing CORS response headers for requests with an Origin header and register it in the FastAPI application to aid debugging of CORS issues.

Enhancements:

  • Introduce CORS logging middleware to record requests where an Origin header is present but CORS headers are missing in the response.
  • Register the CORS logging middleware after the existing CORS middleware to improve observability of CORS configuration problems.

- Added `CORSLoggingMiddleware` to `backend/common/middleware.py` to log CORS failures and origins.
- Registered `CORSLoggingMiddleware` in `backend/main.py`.
- This change will trigger a redeployment to ensure the latest CORS configuration (which correctly includes the user's origin) is active.
- Verified locally with reproduction scripts and existing tests.

Co-authored-by: KnellBalm <90038472+KnellBalm@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Feb 3, 2026

Reviewer's Guide

Adds a dedicated CORS logging middleware to detect responses missing CORS headers when an Origin is present, and wires it into the FastAPI app to aid debugging CORS issues after redeploying the backend.

Sequence diagram for HTTP request flow with new CORSLoggingMiddleware

sequenceDiagram
    actor Client
    participant FastAPIApp as FastAPIApp
    participant CORSMiddleware as CORSMiddleware
    participant CORSLoggingMiddleware as CORSLoggingMiddleware
    participant Endpoint as EndpointHandler

    Client->>FastAPIApp: HTTP request
    activate FastAPIApp

    FastAPIApp->>CORSLoggingMiddleware: dispatch(request)
    activate CORSLoggingMiddleware
    CORSLoggingMiddleware->>CORSMiddleware: call_next(request)
    activate CORSMiddleware
    CORSMiddleware->>Endpoint: call_next(request)
    activate Endpoint
    Endpoint-->>CORSMiddleware: response
    deactivate Endpoint

    CORSMiddleware-->>CORSLoggingMiddleware: response with CORS headers
    deactivate CORSMiddleware

    CORSLoggingMiddleware->>CORSLoggingMiddleware: check origin and access_control_allow_origin
    alt Origin present and CORS header missing
        CORSLoggingMiddleware->>Logger_backend_middleware_cors: warning CORS Missing Header with origin and path
    end

    CORSLoggingMiddleware-->>FastAPIApp: response
    deactivate CORSLoggingMiddleware

    FastAPIApp-->>Client: HTTP response
    deactivate FastAPIApp
Loading

Class diagram for new CORSLoggingMiddleware

classDiagram
    class BaseHTTPMiddleware {
        <<abstract>>
        +dispatch(request, call_next) async
    }

    class CORSLoggingMiddleware {
        +dispatch(request, call_next) async
    }

    class Request {
        +headers
        +url
    }

    class Response {
        +headers
    }

    class Logger {
        +warning(message)
    }

    CORSLoggingMiddleware --|> BaseHTTPMiddleware
    CORSLoggingMiddleware ..> Request : uses
    CORSLoggingMiddleware ..> Response : uses
    CORSLoggingMiddleware ..> Logger : logs via_get_logger
    CORSLoggingMiddleware : +origin
    CORSLoggingMiddleware : +dispatch(request, call_next) async
    CORSLoggingMiddleware :   origin = request.headers.get(origin)
    CORSLoggingMiddleware :   response = await call_next(request)
    CORSLoggingMiddleware :   if origin and no access_control_allow_origin
    CORSLoggingMiddleware :       logger = get_logger(backend.middleware.cors)
    CORSLoggingMiddleware :       logger.warning(CORS Missing Header ...)
Loading

File-Level Changes

Change Details Files
Introduce CORSLoggingMiddleware to log requests where an Origin header is present but the response lacks CORS headers.
  • Define CORSLoggingMiddleware subclassing BaseHTTPMiddleware with an async dispatch method.
  • Capture the Origin header from incoming requests and call the next handler to obtain the response.
  • If an Origin is present and the Access-Control-Allow-Origin header is missing from the response, retrieve a dedicated logger and emit a warning including origin and request path.
backend/common/middleware.py
Register the new CORS logging middleware in the FastAPI application after the existing CORSMiddleware to observe final response headers.
  • Add app.add_middleware(CORSLoggingMiddleware) after CORSMiddleware setup.
  • Document in comments (in Korean) that this middleware is registered after CORS so it wraps the CORS middleware and can inspect final response headers.
backend/main.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider creating the backend.middleware.cors logger at module level instead of inside dispatch to avoid re-instantiating it on every request.
  • You may want to include additional context (e.g., HTTP method, status code, and request headers like Access-Control-Request-Method) in the CORS warning log to make diagnosing intermittent CORS issues easier.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider creating the `backend.middleware.cors` logger at module level instead of inside `dispatch` to avoid re-instantiating it on every request.
- You may want to include additional context (e.g., HTTP method, status code, and request headers like `Access-Control-Request-Method`) in the CORS warning log to make diagnosing intermittent CORS issues easier.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

- Fixed `tests/test_integration.py` to properly mock the environment during testing, resolving the CI failure that prevented deployment.
- Added `CORSLoggingMiddleware` to `backend/common/middleware.py` and registered it in `backend/main.py` to provide visibility into CORS rejections.
- Ensured `backend/main.py` has a clean definition of allowed origins, explicitly including the reported Cloud Run domain.
- Verified imports in middleware to prevent runtime errors.

Co-authored-by: KnellBalm <90038472+KnellBalm@users.noreply.github.com>
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