Skip to content

Commit

Permalink
Lock down server IPC address (#51378)
Browse files Browse the repository at this point in the history
This attempts to avoid IPv6 and IPv4 resolve issues across network
setups by locking down our internal IPC requests to just one address for
consistency. This way there aren't issues when `localhost` resolves to
one or the other in different cases.

x-ref: #49677
  • Loading branch information
ijjk committed Jun 16, 2023
1 parent ab3f707 commit da98193
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/render-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@ export async function initialize(opts: {
return reject(err)
}
})
server.listen(await getFreePort(), opts.hostname)
server.listen(await getFreePort(), '0.0.0.0')
})
}
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/server-ipc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export async function createIpcServer(
)

const ipcPort = await new Promise<number>((resolveIpc) => {
ipcServer.listen(0, server.hostname, () => {
ipcServer.listen(0, '0.0.0.0', () => {
const addr = ipcServer.address()

if (addr && typeof addr === 'object') {
Expand Down
7 changes: 6 additions & 1 deletion packages/next/src/server/lib/server-ipc/invoke-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ export const invokeRequest = async (
const http = require('http') as typeof import('http')

try {
// force to 127.0.0.1 as IPC always runs on this hostname
// to avoid localhost issues
const parsedTargetUrl = new URL(targetUrl)
parsedTargetUrl.hostname = '127.0.0.1'

const invokeReq = http.request(
targetUrl,
parsedTargetUrl.toString(),
{
headers: invokeHeaders,
method: requestInit.method,
Expand Down

0 comments on commit da98193

Please sign in to comment.