Skip to content

fix(router): provide request context of original WebSocket Upgrade request to subgraph requests#1957

Merged
SkArchon merged 8 commits intowundergraph:mainfrom
DerZade:main
Jul 21, 2025
Merged

fix(router): provide request context of original WebSocket Upgrade request to subgraph requests#1957
SkArchon merged 8 commits intowundergraph:mainfrom
DerZade:main

Conversation

@DerZade
Copy link
Copy Markdown
Contributor

@DerZade DerZade commented Jun 11, 2025

Motivation and Context

Up until now requests to subgraphs that result out of WebSocket subscription request cannot use the expression context of the corresponding Upgrade request. This means even if authentication succeeds on the Upgrade request, the expression request.auth.isAuthenticated would evaluate to false for the request to the subgraph. In a similar fashion passing claims to the subgraph via headers is not possible as well.

This PR aims to fix this by providing the expression context of the original Upgrade request to all resulting requests to subgraphs :)

image

Checklist

Summary by CodeRabbit

  • New Features

    • Improved support for using authentication context in header expressions for subgraph requests during WebSocket GraphQL subscriptions.
  • Tests

    • Added a new test to verify that authentication context can be referenced in header expressions and that headers are correctly set in subgraph requests during WebSocket subscriptions.

@DerZade DerZade changed the title fix(router): provide expression context of original WS Upgrade reques… fix(router): provide expression context of original WS Upgrade request to corresponding subgraph requests Jun 11, 2025
Comment thread router/core/websocket.go Outdated
@SkArchon SkArchon self-assigned this Jun 12, 2025
@DerZade
Copy link
Copy Markdown
Contributor Author

DerZade commented Jun 23, 2025

@SkArchon anything I can do to move this forward? :)

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jul 8, 2025

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions Bot added the Stale label Jul 8, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 8, 2025

Walkthrough

A new subtest was added to verify that authentication context can be accessed in header expressions for subgraph requests during WebSocket subscriptions. The subscription execution logic was updated to clone the expression context from the original request context into the subscription request context.

Changes

Files/Paths Change Summary
router-tests/websocket_test.go Added a subtest in TestWebSockets verifying auth context usage in header expressions for subgraph requests during WebSocket subscriptions.
router/core/websocket.go Modified executeSubscription to clone expressionContext from original request context into subscription request context.

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between cb15442 and 898c5bd.

📒 Files selected for processing (1)
  • router-tests/websocket_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • router-tests/websocket_test.go
✨ 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.

Copy link
Copy Markdown
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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3dcce2c and aa18b55.

📒 Files selected for processing (2)
  • router-tests/websocket_test.go (3 hunks)
  • router/core/websocket.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
router/core/websocket.go (1)
router/internal/expr/expr.go (1)
  • Context (34-37)
🔇 Additional comments (3)
router-tests/websocket_test.go (3)

4-4: LGTM!

The context import is appropriately added to support the new TestAuthenticator implementation.


86-96: LGTM!

The TestAuthenticator implementation is clean and appropriate for testing. It correctly implements the authentication.Authenticator interface with predictable claims that can be verified in tests.


863-919: Excellent test implementation!

This test case effectively verifies the core functionality described in the PR objectives. It properly:

  • Configures header rules with expressions that reference authentication context
  • Sets up the test authenticator to provide predictable claims
  • Uses global middleware to assert that headers are correctly propagated to subgraph requests
  • Follows established testing patterns with proper cleanup and parallel execution

The test directly validates that expression context from the original WebSocket upgrade request is available for header expressions in subgraph requests.

Comment thread router/core/websocket.go Outdated
@SkArchon
Copy link
Copy Markdown
Contributor

SkArchon commented Jul 8, 2025

Hi @DerZade! I will take a look at this PR and get back to you this week

@DerZade
Copy link
Copy Markdown
Contributor Author

DerZade commented Jul 9, 2025

I rebased and added a nil check as CodeRabbit suggested.

I'm just still not sure if copying the auth part of the expression context is enough 🤔

I feel like also being able to use other headers (apart from auth) is a benefit. So I think copying the whole context is probably not a bad idea :)

@github-actions github-actions Bot removed the Stale label Jul 9, 2025
Copy link
Copy Markdown
Contributor

@SkArchon SkArchon left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution, it looks good, just one comment. I'll need to check on enabling CI in the meantime as well.

Comment thread router/core/websocket.go Outdated
@DerZade DerZade requested a review from jensneuse as a code owner July 13, 2025 22:53
@DerZade DerZade requested a review from SkArchon July 13, 2025 22:54
@DerZade DerZade changed the title fix(router): provide expression context of original WS Upgrade request to corresponding subgraph requests fix(router): provide request context of original WebSocket Upgrade request to subgraph requests Jul 14, 2025
Copy link
Copy Markdown
Member

@jensneuse jensneuse left a comment

Choose a reason for hiding this comment

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

Overall looks like a good PR and useful improvement. Just 2 minor comments.

Comment thread router-tests/websocket_test.go Outdated
Comment thread router/core/websocket.go
@SkArchon SkArchon mentioned this pull request Jul 14, 2025
5 tasks
@SkArchon
Copy link
Copy Markdown
Contributor

SkArchon commented Jul 14, 2025

I will open up a separate PR which will run the CI checks, as there are some limitations for CI on forks atm. Also two quick comments,

  1. I noticed there was a compilation failure when trying out your change today which you have fixed a bit later, do ensure that the code compiles before requesting a review 🙏
  2. Might be a good idea if you used a separate branch other than main on your fork, especially if you want to contribute again later

Copy link
Copy Markdown
Contributor

@SkArchon SkArchon left a comment

Choose a reason for hiding this comment

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

LGTM, will merge after Jen's comments are resolved

@DerZade
Copy link
Copy Markdown
Contributor Author

DerZade commented Jul 16, 2025

Addressed all comments :)

Please squash merge, since this could easily fit in one commit and there are some commits without nice messages 🙈

@SkArchon
Copy link
Copy Markdown
Contributor

SkArchon commented Jul 18, 2025

Hi @DerZade we squash merge into main, since we want a clean commit history. In PR commit comments would potentially help reviewing though.

Also there is an issue in CI which my colleagues are looking into, so will need to wait until its fixed to merge this in.

@SkArchon SkArchon merged commit b4420a1 into wundergraph:main Jul 21, 2025
67 of 72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants