Skip to content

Commit 29035d7

Browse files
fix: set Host header on edge functions in HTTPS (#5681)
1 parent 0457388 commit 29035d7

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/lib/edge-functions/proxy.mjs

+9-1
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ export const initializeProxy = async ({
131131
}
132132

133133
const featureFlags = ['edge_functions_bootstrap_failure_mode']
134+
const forwardedHost = `localhost:${passthroughPort}`
134135

135136
req[headersSymbol] = {
136137
[headers.FeatureFlags]: getFeatureFlagsHeader(featureFlags),
137-
[headers.ForwardedHost]: `localhost:${passthroughPort}`,
138+
[headers.ForwardedHost]: forwardedHost,
138139
[headers.Functions]: functionNames.join(','),
139140
[headers.InvocationMetadata]: getInvocationMetadataHeader(invocationMetadata),
140141
[headers.IP]: LOCAL_HOST,
@@ -145,6 +146,13 @@ export const initializeProxy = async ({
145146
req[headersSymbol][headers.DebugLogging] = '1'
146147
}
147148

149+
// If we're using a different port for passthrough requests, which is the
150+
// case when the CLI is running on HTTPS, use it on the Host header so
151+
// that the request URL inside the edge function is something accessible.
152+
if (mainPort !== passthroughPort) {
153+
req[headersSymbol].host = forwardedHost
154+
}
155+
148156
return `http://${LOCAL_HOST}:${isolatePort}`
149157
}
150158
}

tests/integration/200.command.dev.test.cjs

+17-6
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ export const handler = async function () {
214214
path: 'public/index.html',
215215
content: 'index',
216216
})
217+
.withContentFile({
218+
path: 'public/origin.html',
219+
content: 'origin',
220+
})
217221
.withRedirectsFile({
218222
redirects: [{ from: `/api/*`, to: `/.netlify/functions/:splat`, status: '200' }],
219223
})
@@ -226,15 +230,21 @@ export const handler = async function () {
226230
})
227231
.withEdgeFunction({
228232
handler: async (req, { next }) => {
229-
if (!req.url.includes('?ef=true')) {
230-
return
233+
if (req.url.includes('?ef=true')) {
234+
// eslint-disable-next-line n/callback-return
235+
const res = await next()
236+
const text = await res.text()
237+
238+
return new Response(text.toUpperCase(), res)
231239
}
232240

233-
// eslint-disable-next-line n/callback-return
234-
const res = await next()
235-
const text = await res.text()
241+
if (req.url.includes('?ef=fetch')) {
242+
const url = new URL('/origin', req.url)
243+
const res = await fetch(url)
244+
const text = await res.text()
236245

237-
return new Response(text.toUpperCase(), res)
246+
return new Response(text.toUpperCase(), res)
247+
}
238248
},
239249
name: 'hello',
240250
})
@@ -248,6 +258,7 @@ export const handler = async function () {
248258
const options = { https: { rejectUnauthorized: false } }
249259
t.is(await got(`https://localhost:${port}`, options).text(), 'index')
250260
t.is(await got(`https://localhost:${port}?ef=true`, options).text(), 'INDEX')
261+
t.is(await got(`https://localhost:${port}?ef=fetch`, options).text(), 'ORIGIN')
251262
t.deepEqual(await got(`https://localhost:${port}/api/hello`, options).json(), {
252263
rawUrl: `https://localhost:${port}/api/hello`,
253264
})

0 commit comments

Comments
 (0)