Skip to content

Commit f8425c0

Browse files
fix: remove cf-connecting-header headers from external override requests
this change removes `cf-connecting-header` headers from requests being sent to external urls during rewrites, this allows such overrides, when run inside a Cloudflare worker to rewrite to urls also hosted on Cloudflare
1 parent dd9face commit f8425c0

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

.changeset/new-beers-worry.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
fix: remove `cf-connecting-header` headers from external override requests
6+
7+
this change removes `cf-connecting-header` headers from requests being sent to
8+
external urls during rewrites, this allows such overrides, when run inside a
9+
Cloudflare worker to rewrite to urls also hosted on Cloudflare

packages/open-next/src/overrides/proxyExternalRequest/fetch.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ const fetchProxy: ProxyExternalRequest = {
55
name: "fetch-proxy",
66
// @ts-ignore
77
proxy: async (internalEvent) => {
8-
const { url, headers, method, body } = internalEvent;
8+
const { url, headers: eventHeaders, method, body } = internalEvent;
9+
10+
const headers = Object.fromEntries(
11+
Object.entries(eventHeaders).filter(
12+
([key]) => key.toLowerCase() !== "cf-connecting-ip",
13+
),
14+
);
15+
916
const response = await fetch(url, {
1017
method,
1118
headers,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import fetchProxy from "@opennextjs/aws/overrides/proxyExternalRequest/fetch.js";
2+
import { vi } from "vitest";
3+
4+
describe("proxyExternalRequest/fetch", () => {
5+
// Note: if the url is hosted on the Cloudflare network we want to make sure that a `cf-connecting-ip` header is not being sent as that causes a DNS error
6+
// (see: https://developers.cloudflare.com/support/troubleshooting/cloudflare-errors/troubleshooting-cloudflare-1xxx-errors/#error-1000-dns-points-to-prohibited-ip)
7+
it("the proxy should remove any cf-connecting-ip headers (with any casing) before passing it to fetch", async () => {
8+
const fetchMock = vi.fn<typeof global.fetch>(async () => new Response());
9+
globalThis.fetch = fetchMock;
10+
11+
const { proxy } = fetchProxy;
12+
13+
await proxy({
14+
headers: {
15+
"header-1": "valid header 1",
16+
"header-2": "valid header 2",
17+
"cf-connecting-ip": "forbidden header 1",
18+
"header-3": "valid header 3",
19+
"CF-Connecting-IP": "forbidden header 2",
20+
"CF-CONNECTING-IP": "forbidden header 3",
21+
"header-4": "valid header 4",
22+
},
23+
});
24+
25+
expect(fetchMock.mock.calls.length).toEqual(1);
26+
27+
const headersPassedToFetch = Object.keys(
28+
fetchMock.mock.calls[0][1]?.headers ?? {},
29+
);
30+
31+
expect(headersPassedToFetch).toContain("header-1");
32+
expect(headersPassedToFetch).toContain("header-2");
33+
expect(headersPassedToFetch).not.toContain("cf-connecting-ip");
34+
expect(headersPassedToFetch).toContain("header-3");
35+
expect(headersPassedToFetch).not.toContain("CF-Connecting-IP");
36+
expect(headersPassedToFetch).not.toContain("CF-CONNECTING-IP");
37+
expect(headersPassedToFetch).toContain("header-4");
38+
});
39+
});

0 commit comments

Comments
 (0)