Repository navigation
[TT-19132] Check the Origin of GraphQL and Streams WebSocket handshakes - #8986
Conversation
📚 Documentation previewThis PR changes generated documentation (component: cc @TykTechnologies/pms — advisory review requested; this does not block merging. Rendered docs diffdiff -ruN out-base/gateway-config.mdx out-head/gateway-config.mdx
--- out-base/gateway-config.mdx 2026-11-26 14:29:23.953015403 +0000
+++ out-head/gateway-config.mdx 2026-11-26 14:29:25.875712017 +0000
@@ -3157,3 +3157,12 @@
A token with no iss claim is matched by key ID (kid) alone.
It exists for compatibility with legacy records that predate issuer becoming mandatory. Not recommended for production use: without the issuer check, an identity provider that shares signing keys across tenants cannot be distinguished by tenant.
+### allow_unsafe_websocket_origin
+ENV: <b>TYK_GW_ALLOWUNSAFEWEBSOCKETORIGIN</b><br />
+Type: `bool`<br />
+
+AllowUnsafeWebSocketOrigin turns off the Origin check on WebSocket handshakes for GraphQL and Streams APIs, so a handshake from any origin is accepted (default: false).
+It exists for compatibility with browser clients on other origins that connected before the check was added.
+Not recommended: any website can then open a WebSocket to these APIs through a visitor's browser and read the responses.
+To allow a browser client, add its origin to the API's CORS allowed origins instead.
+
|
|
This PR introduces a security enhancement by adding an A handshake is accepted when:
Handshakes from non-compliant origins are rejected with a Files Changed AnalysisThe changes are well-structured across 13 files, with 680 additions and 13 deletions. The core validation logic is encapsulated in a new file, Architecture & Impact Assessment
Scope Discovery & Context ExpansionThis change introduces a potential breaking change for existing browser-based clients that rely on WebSocket connections for GraphQL subscriptions or Streams from a different origin than the Gateway. For instance, API playgrounds (including the Tyk Dashboard's) or single-page applications will now require their origin to be explicitly added to the API's CORS Metadata
Powered by Visor from Probelabs Last updated: 2026-11-26T14:30:27.055Z | Triggered by: pr_updated | Commit: 4ec312e 💡 TIP: You can chat with Visor using |
✅ Security Check PassedNo security issues found – changes LGTM. ✅ Architecture Check PassedNo architecture issues found – changes LGTM. ✅ Security Check PassedNo security issues found – changes LGTM. \n\n✅ Architecture Check PassedNo architecture issues found – changes LGTM. \n\n \n\n✅ Quality Check PassedNo quality issues found – changes LGTM. Powered by Visor from Probelabs Last updated: 2026-11-26T14:30:21.117Z | Triggered by: pr_updated | Commit: 4ec312e 💡 TIP: You can chat with Visor using |
CI: the remaining failures are not caused by this pull request
The difference is in action pins in gromit-managed workflow files, for example: Why this pull request isn't the cause:
What I didn't check: I compared only against recent runs of the Drift Check workflow, not every open pull request. I also didn't compare #8977's generated files line by line with this diff. The two aggregate checks that show red ("Unit Tests & Linting" and "Aggregated CI Status") are from the run made while this pull request was a draft. In that run the |
|
API Changes --- prev.txt 2026-11-26 14:29:44.972367192 +0000
+++ current.txt 2026-11-26 14:29:39.114304692 +0000
@@ -8304,6 +8304,12 @@
// A token with no iss claim is matched by key ID (kid) alone.
// It exists for compatibility with legacy records that predate issuer becoming mandatory. Not recommended for production use: without the issuer check, an identity provider that shares signing keys across tenants cannot be distinguished by tenant.
AllowUnsafeIDPMissingIssuer bool `json:"allow_unsafe_idp_missing_issuer"`
+
+ // AllowUnsafeWebSocketOrigin turns off the Origin check on WebSocket handshakes for GraphQL and Streams APIs, so a handshake from any origin is accepted (default: false).
+ // It exists for compatibility with browser clients on other origins that connected before the check was added.
+ // Not recommended: any website can then open a WebSocket to these APIs through a visitor's browser and read the responses.
+ // To allow a browser client, add its origin to the API's CORS allowed origins instead.
+ AllowUnsafeWebSocketOrigin bool `json:"allow_unsafe_websocket_origin"`
}
Config is the configuration object used by Tyk to set up various parameters.
@@ -10590,6 +10596,12 @@
OAS oas.OAS
StripListenPath model.StripListenPathFunc
+
+ // WebSocketOriginAllowed reports whether a WebSocket handshake may
+ // proceed. Bento's http_server output accepts every origin unless its own
+ // cors setting is on, so the Gateway checks the origin first. When nil,
+ // handshakes are refused.
+ WebSocketOriginAllowed func(*http.Request) bool
}
APISpec is a subset of gateway.APISpec for the values the middleware
consumes. |
Adds WebSocketOriginChecker to internal/httputil. It accepts a handshake with no Origin, the Gateway's own origin (using the existing trusted-proxy rules), or an origin the API's CORS settings allow, including wildcards. Nothing uses it yet, so Gateway behaviour is unchanged. Code: internal/httputil/websocket_origin.go Criteria (shared check only; endpoints are wired in later commits): - 3 own origin and CORS allowed origins accepted: TestWebSocketOriginChecker - 4 no Origin accepted: TestWebSocketOriginChecker - trusted proxy, wildcard and subdomain pattern cases: TestWebSocketOriginChecker
Adds allow_unsafe_websocket_origin (TYK_GW_ALLOWUNSAFEWEBSOCKETORIGIN), false by default, and logs a startup warning when it is on. Nothing reads it yet, so Gateway behaviour is unchanged apart from the warning. Code: config/config.go, gateway/server.go (afterConfSetup), cli/linter/schema.json Criteria: - 6 (default only) option is false by default: TestAfterConfSetup_WarnsWhenWebSocketOriginCheckIsOff - 7 startup warning when on: TestAfterConfSetup_WarnsWhenWebSocketOriginCheckIsOff
The GraphQL upgrader accepted every origin, and Tyk's CORS middleware does not protect WebSocket handshakes. It now refuses, with 403, a handshake whose Origin is neither the Gateway's own origin nor allowed by the API's CORS settings, and logs the refusal with the API ID and origin. Handshakes with no Origin, and every handshake when allow_unsafe_websocket_origin is on, are accepted as before. The check uses the inbound request, because the request the engine receives already carries the upstream's Host. Code: gateway/websocket_origin.go, gateway/reverse_proxy.go (handleGraphQL), gateway/model_apispec.go Criteria: - 2 other origin refused with 403: TestGraphQLWebSocket_OriginCheck - 3 own origin and CORS allowed origin connect (GraphQL): TestGraphQLWebSocket_OriginCheck - 4 no Origin connects (GraphQL): TestGraphQLWebSocket_OriginCheck - 6 option on accepts any origin (GraphQL): TestGraphQLWebSocket_OriginCheck - 8 refusal logged with API ID and origin (GraphQL): TestGraphQLWebSocket_OriginCheck
Bento's http_server output accepts every origin unless its own cors setting is on. HandleFuncAdapter now runs the Gateway's origin check before Bento handles a WebSocket handshake, and refuses with 403 an origin that is neither the Gateway's own nor allowed by the API's CORS settings. When Bento's cors is on, it still runs afterwards, so an origin must pass both. Non-WebSocket Streams requests are unchanged. Code: ee/middleware/streams/util.go (HandleFuncAdapter.HandleFunc), ee/middleware/streams/model.go, gateway/mw_streaming_ee.go Criteria: - 1 other origin refused with 403, no message: TestStreamsWebSocket_OriginCheck - 3 own origin connects and receives messages, CORS allowed origin connects: TestStreamsWebSocket_OriginCheck - 4 no Origin connects (Streams): TestStreamsWebSocket_OriginCheck - 5 Bento cors: both checks must pass: TestStreamsWebSocket_OriginCheck - 6 option on accepts any origin (Streams): TestStreamsWebSocket_OriginCheck - 8 refusal logged with API ID and origin (Streams): TestStreamsWebSocket_OriginCheck
Moves the graphql-go-tools import into its own group, as the repository's gci formatter requires. Test-only change; behaviour is unchanged. This push also starts the plugin-compiler image build, which was skipped while the pull request was a draft and is not re-triggered by marking it ready, so the release tests could not find the image for the merge commit. Code: gateway/websocket_origin_test.go
Declares inreq and outreq together, as SonarCloud rule go:S8209 asks for consecutive parameters of the same type. Behaviour is unchanged. Code: gateway/reverse_proxy.go (handleOutboundRequest)
f00e07d to
4ec312e
Compare
|
SentinelOne CNS Hardcoded Secret Detector SentinelOne CNS is a cloud-agnostic, agentless CSPM & CWPP solution that continuously detects and prevents vulnerabilities that have the highest probability of being exploited in Azure, AWS, Google Cloud, and Kubernetes. |
🎯 Recommended Merge TargetsBased on JIRA ticket TT-19132: [Security] Gateway: Streams and GraphQL WebSocket endpoints accept connections from any Origin Fix Version: Tyk 5.13.9Required:
Fix Version: Tyk 5.17.0
Required:
Fix Version: Tyk 5.8.21Required:
📋 Workflow
|
|
rafalgolarz
left a comment
There was a problem hiding this comment.
Looks good: WebSocket Origin check for GraphQL and Streams handshakes, Jira TT-19132 acceptance criteria covered by tests, Go and API tests green. [head 4ec312e]



Description
The Gateway now checks the
Originheader of WebSocket handshakes for GraphQL APIs and Streamsws_pathendpoints. Browsers don't apply CORS to WebSocket handshakes, so the API's CORS settings didn't protect these endpoints before.A handshake is accepted when one of these is true:
Originheader. Server-to-server and CLI clients keep working.Host, or the forwarded headers when the request comes from a proxy inhttp_server_options.trusted_proxy_cidrs.allowed_originsallows the origin. Wildcards count, and an empty list allows every origin, as for HTTP CORS.Any other origin gets
403 Forbidden, and the Gateway logs the refusal with the API ID and the origin.For a Streams output with Bento's own
corsconfigured, an origin must pass both the Gateway's check and Bento's.A new Gateway option,
allow_unsafe_websocket_origin(TYK_GW_ALLOWUNSAFEWEBSOCKETORIGIN), isfalseby default. When it'strue, the check is off and handshakes are accepted from any origin, as before. The Gateway logs a warning at startup when it's on.Commits, one per task:
internal/httputil/websocket_origin.go: a shared check. It reusesCanonicalOrigin,ExternalOriginWithTrustedProxiesand thers/corsorigin matching that Tyk's CORS middleware already uses.config/config.go,gateway/server.go,cli/linter/schema.json: the new option and its startup warning.gateway/reverse_proxy.go,gateway/websocket_origin.go: the GraphQL upgrader checks the inbound request. The request the graph engine receives already carries the upstream'sHost, so it can't be used for the "own origin" comparison.ee/middleware/streams/util.go:HandleFuncAdapterruns the check before Bento handles a WebSocket handshake. Bento isn't forked.Related Issue
TT-19132
Motivation and Context
Both WebSocket upgraders accepted every origin. The acceptance criteria and the decisions behind this behaviour are on the ticket.
Behaviour change. A browser client on another origin that connects today without CORS configured is refused after this change. That includes GraphQL subscriptions in the Dashboard's API playground: queries and mutations there go through the Dashboard backend and aren't affected, but subscriptions open a WebSocket from the browser on the Dashboard's origin. To allow such a client, add its origin to the API's CORS allowed origins.
allow_unsafe_websocket_originrestores the old behaviour for the whole Gateway, and isn't recommended.How This Has Been Tested
TestStreamsWebSocket_OriginCheckTestGraphQLWebSocket_OriginCheckTestWebSocketOriginCheckerOriginconnectscorson, both checks must passTestStreamsWebSocket_OriginCheckallow_unsafe_websocket_originis off by default; when on, any origin connectsTestAfterConfSetup_WarnsWhenWebSocketOriginCheckIsOffand both integration testsTestAfterConfSetup_WarnsWhenWebSocketOriginCheckIsOffTestWebSocketOriginCheckercovers no origin, own origin (plain and TLS), scheme mismatch, case,null, malformed and repeatedOriginheaders, a non-ASCII origin, exact CORS origins, CORS turned off,*, an empty list, subdomain patterns and a lookalike domain, a trusted proxy, and forwarded headers from an untrusted client.Before the fix, the GraphQL and Streams refusal cases returned
101. The Streams "Bento allows, Gateway doesn't" case also connected.Run locally on Go 1.26.5 against
masterate66db61f4:go test ./internal/httputil/go test ./gateway/ -run 'TestGraphQLWebSocket_OriginCheck|TestAfterConfSetup_WarnsWhenWebSocketOriginCheckIsOff'go test -tags dev ./gateway/ -run TestStreamsWebSocket_OriginCheckgo test -tags dev ./ee/...Not covered yet: manual QA in a browser and in the Dashboard playground; GraphQL UDG, supergraph and
tyk://looped APIs (the tests use proxy-only v2, which goes through the same code path); and CORS set through OAS on a Dashboard-created Streams API.Screenshots (if appropriate)
None: there's no UI change.
Types of changes
Checklist
go.modisn't changed. The documentation (criterion 9) will follow separately.Ticket Details
TT-19132
Generated at: 2026-11-26 14:28:29