-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Detect Cloudflare WAF block pages and show a helpful error message in stead of "Received a malformed response from the API" #13574
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
dario-piotrowicz
merged 5 commits into
main
from
dario/13312/malformed-response-block-page
Apr 21, 2026
+315
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9eb90e1
Detect Cloudflare WAF block pages and show a helpful error message in…
dario-piotrowicz e9610fb
Use response headers instead of relying on html response body
dario-piotrowicz 11cbf00
add Ray Id to all non-JSON responses as well
dario-piotrowicz 2b71b33
fix formatting
dario-piotrowicz 0b90fb2
combine changesets into one
dario-piotrowicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "wrangler": patch | ||
| --- | ||
|
|
||
| Detect Cloudflare WAF block pages and include Ray ID in API error messages | ||
|
|
||
| When the Cloudflare WAF blocks an API request, the response is an HTML page rather than JSON. Previously, this caused a confusing "Received a malformed response from the API" error with a truncated HTML snippet. Wrangler now detects WAF block pages and displays a clear error message explaining that the request was blocked by the firewall, along with the Cloudflare Ray ID (when available) for use in support tickets. | ||
|
|
||
| For other non-JSON responses that aren't WAF blocks, the "malformed response" error also now includes the Ray ID to help reference failing requests in support tickets. |
229 changes: 229 additions & 0 deletions
229
packages/wrangler/src/__tests__/cfetch-internal.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| import { COMPLIANCE_REGION_CONFIG_UNKNOWN } from "@cloudflare/workers-utils"; | ||
| import { http, HttpResponse } from "msw"; | ||
| import { describe, it } from "vitest"; | ||
| import { fetchGraphqlResult } from "../cfetch"; | ||
| import { extractWAFBlockRayId, isWAFBlockResponse } from "../cfetch/internal"; | ||
| import { mockAccountId, mockApiToken } from "./helpers/mock-account-id"; | ||
| import { msw } from "./helpers/msw"; | ||
|
|
||
| describe("isWAFBlockResponse", () => { | ||
| it("should detect a WAF-mitigated response", ({ expect }) => { | ||
| const headers = new Headers({ "cf-mitigated": "challenge" }); | ||
| expect(isWAFBlockResponse(headers)).toBe(true); | ||
| }); | ||
|
|
||
| it("should return false when cf-mitigated header is absent", ({ expect }) => { | ||
| const headers = new Headers(); | ||
| expect(isWAFBlockResponse(headers)).toBe(false); | ||
| }); | ||
|
|
||
| it("should return false when cf-mitigated has a different value", ({ | ||
| expect, | ||
| }) => { | ||
| const headers = new Headers({ "cf-mitigated": "other" }); | ||
| expect(isWAFBlockResponse(headers)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("extractWAFBlockRayId", () => { | ||
| it("should extract the Ray ID from the cf-ray header", ({ expect }) => { | ||
| const headers = new Headers({ "cf-ray": "9e8116df4823e2c5" }); | ||
| expect(extractWAFBlockRayId(headers)).toBe("9e8116df4823e2c5"); | ||
| }); | ||
|
|
||
| it("should return undefined when cf-ray header is absent", ({ expect }) => { | ||
| const headers = new Headers(); | ||
| expect(extractWAFBlockRayId(headers)).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("fetchInternal WAF block detection", () => { | ||
| mockAccountId({ accountId: null }); | ||
| mockApiToken(); | ||
|
|
||
| it("should throw a helpful error when the API returns a WAF block response", async ({ | ||
| expect, | ||
| }) => { | ||
| msw.use( | ||
| http.post("*/graphql", async () => { | ||
| return new HttpResponse("blocked", { | ||
| status: 403, | ||
| statusText: "Forbidden", | ||
| headers: { | ||
| "Content-Type": "text/html", | ||
| "cf-mitigated": "challenge", | ||
| "cf-ray": "9e8116df4823e2c5", | ||
| }, | ||
| }); | ||
| }) | ||
| ); | ||
| await expect( | ||
| fetchGraphqlResult(COMPLIANCE_REGION_CONFIG_UNKNOWN, { | ||
| body: JSON.stringify({ query: "{ viewer { __typename } }" }), | ||
| }) | ||
| ).rejects.toThrow( | ||
| "The Cloudflare API responded with a WAF block page instead of the expected JSON response" | ||
| ); | ||
| }); | ||
|
|
||
| it("should include the Ray ID in the error when cf-ray header is present", async ({ | ||
| expect, | ||
| }) => { | ||
| msw.use( | ||
| http.post("*/graphql", async () => { | ||
| return new HttpResponse("blocked", { | ||
| status: 403, | ||
| statusText: "Forbidden", | ||
| headers: { | ||
| "Content-Type": "text/html", | ||
| "cf-mitigated": "challenge", | ||
| "cf-ray": "9e8116df4823e2c5", | ||
| }, | ||
| }); | ||
| }) | ||
| ); | ||
| try { | ||
| await fetchGraphqlResult(COMPLIANCE_REGION_CONFIG_UNKNOWN, { | ||
| body: JSON.stringify({ query: "{ viewer { __typename } }" }), | ||
| }); | ||
| expect.unreachable("should have thrown"); | ||
| } catch (e) { | ||
| const error = e as { notes: { text: string }[] }; | ||
| const rayIdNote = error.notes.find((n: { text: string }) => | ||
| n.text.includes("Cloudflare Ray ID:") | ||
| ); | ||
| expect(rayIdNote).toBeDefined(); | ||
| expect(rayIdNote?.text).toBe("Cloudflare Ray ID: 9e8116df4823e2c5"); | ||
| const supportNote = error.notes.find((n: { text: string }) => | ||
| n.text.includes("open a Cloudflare Support ticket") | ||
| ); | ||
| expect(supportNote?.text).toBe( | ||
| "If the issue persists, please open a Cloudflare Support ticket and include the Ray ID above." | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| it("should still throw a WAF error without the Ray ID note when cf-ray header is absent", async ({ | ||
| expect, | ||
| }) => { | ||
| msw.use( | ||
| http.post("*/graphql", async () => { | ||
| return new HttpResponse("blocked", { | ||
| status: 403, | ||
| statusText: "Forbidden", | ||
| headers: { | ||
| "Content-Type": "text/html", | ||
| "cf-mitigated": "challenge", | ||
| }, | ||
| }); | ||
| }) | ||
| ); | ||
| try { | ||
| await fetchGraphqlResult(COMPLIANCE_REGION_CONFIG_UNKNOWN, { | ||
| body: JSON.stringify({ query: "{ viewer { __typename } }" }), | ||
| }); | ||
| expect.unreachable("should have thrown"); | ||
| } catch (e) { | ||
| const error = e as { text: string; notes: { text: string }[] }; | ||
| expect(error.text).toBe( | ||
| "The Cloudflare API responded with a WAF block page instead of the expected JSON response" | ||
| ); | ||
| const rayIdNote = error.notes.find((n: { text: string }) => | ||
| n.text.includes("Cloudflare Ray ID:") | ||
| ); | ||
| expect(rayIdNote).toBeUndefined(); | ||
| const supportNote = error.notes.find((n: { text: string }) => | ||
| n.text.includes("open a Cloudflare Support ticket") | ||
| ); | ||
| expect(supportNote?.text).toBe( | ||
| "If the issue persists, please open a Cloudflare Support ticket. You can find the Cloudflare Ray ID on the block page in your browser." | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| it("should still throw 'malformed response' for non-WAF HTML responses", async ({ | ||
| expect, | ||
| }) => { | ||
| msw.use( | ||
| http.post("*/graphql", async () => { | ||
| return new HttpResponse( | ||
| "<html><body>Internal Server Error</body></html>", | ||
| { | ||
| status: 500, | ||
| statusText: "Internal Server Error", | ||
| headers: { "Content-Type": "text/html" }, | ||
| } | ||
| ); | ||
| }) | ||
| ); | ||
| await expect( | ||
| fetchGraphqlResult(COMPLIANCE_REGION_CONFIG_UNKNOWN, { | ||
| body: JSON.stringify({ query: "{ viewer { __typename } }" }), | ||
| }) | ||
| ).rejects.toThrow("Received a malformed response from the API"); | ||
| }); | ||
|
|
||
| it("should include the Ray ID in 'malformed response' error when cf-ray header is present", async ({ | ||
| expect, | ||
| }) => { | ||
| msw.use( | ||
| http.post("*/graphql", async () => { | ||
| return new HttpResponse( | ||
| "<html><body>Internal Server Error</body></html>", | ||
| { | ||
| status: 500, | ||
| statusText: "Internal Server Error", | ||
| headers: { | ||
| "Content-Type": "text/html", | ||
| "cf-ray": "abc123def456", | ||
| }, | ||
| } | ||
| ); | ||
| }) | ||
| ); | ||
| try { | ||
| await fetchGraphqlResult(COMPLIANCE_REGION_CONFIG_UNKNOWN, { | ||
| body: JSON.stringify({ query: "{ viewer { __typename } }" }), | ||
| }); | ||
| expect.unreachable("should have thrown"); | ||
| } catch (e) { | ||
| const error = e as { text: string; notes: { text: string }[] }; | ||
| expect(error.text).toBe("Received a malformed response from the API"); | ||
| const rayIdNote = error.notes.find((n: { text: string }) => | ||
| n.text.includes("Cloudflare Ray ID:") | ||
| ); | ||
| expect(rayIdNote).toBeDefined(); | ||
| expect(rayIdNote?.text).toBe("Cloudflare Ray ID: abc123def456"); | ||
| } | ||
| }); | ||
|
|
||
| it("should omit the Ray ID in 'malformed response' error when cf-ray header is absent", async ({ | ||
| expect, | ||
| }) => { | ||
| msw.use( | ||
| http.post("*/graphql", async () => { | ||
| return new HttpResponse( | ||
| "<html><body>Internal Server Error</body></html>", | ||
| { | ||
| status: 500, | ||
| statusText: "Internal Server Error", | ||
| headers: { "Content-Type": "text/html" }, | ||
| } | ||
| ); | ||
| }) | ||
| ); | ||
| try { | ||
| await fetchGraphqlResult(COMPLIANCE_REGION_CONFIG_UNKNOWN, { | ||
| body: JSON.stringify({ query: "{ viewer { __typename } }" }), | ||
| }); | ||
| expect.unreachable("should have thrown"); | ||
| } catch (e) { | ||
| const error = e as { text: string; notes: { text: string }[] }; | ||
| expect(error.text).toBe("Received a malformed response from the API"); | ||
| const rayIdNote = error.notes.find((n: { text: string }) => | ||
| n.text.includes("Cloudflare Ray ID:") | ||
| ); | ||
| expect(rayIdNote).toBeUndefined(); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.