Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
e002ad68
Unverified
Commit
e002ad68
authored
Sep 30, 2026
by
Sebastian "Sebbie" Silbermann
Committed by
GitHub
Sep 30, 2026
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[lts-active] fix(next/image): Pin DNS resolution when fetching external images (#214)
parent
4c20699e
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
1088 additions
and
188 deletions
+1088
-188
image-optimizer.ts
packages/next/src/server/image-optimizer.ts
+361
-54
next.config.js
test/e2e/testmode/next.config.js
+3
-0
testmode.test.ts
test/e2e/testmode/testmode.test.ts
+24
-0
create-pinned-lookup.test.ts
test/unit/image-optimizer/create-pinned-lookup.test.ts
+65
-0
fetch-external-image.test.ts
test/unit/image-optimizer/fetch-external-image.test.ts
+635
-134
No files found.
packages/next/src/server/image-optimizer.ts
View file @
e002ad68
This diff is collapsed.
Click to expand it.
test/e2e/testmode/next.config.js
View file @
e002ad68
/** @type {import('next').NextConfig} */
/** @type {import('next').NextConfig} */
module
.
exports
=
{
module
.
exports
=
{
images
:
{
remotePatterns
:
[
new
URL
(
'https://example.com/**'
)],
},
experimental
:
{
experimental
:
{
testProxy
:
true
,
testProxy
:
true
,
},
},
...
...
test/e2e/testmode/testmode.test.ts
View file @
e002ad68
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
createProxyServer
}
from
'next/experimental/testmode/proxy'
import
{
createProxyServer
}
from
'next/experimental/testmode/proxy'
const
PNG_1X1
=
Buffer
.
from
(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII='
,
'base64'
)
describe
(
'testmode'
,
()
=>
{
describe
(
'testmode'
,
()
=>
{
const
{
next
,
skipped
}
=
nextTestSetup
({
const
{
next
,
skipped
}
=
nextTestSetup
({
files
:
__dirname
,
files
:
__dirname
,
...
@@ -17,6 +22,14 @@ describe('testmode', () => {
...
@@ -17,6 +22,14 @@ describe('testmode', () => {
beforeEach
(
async
()
=>
{
beforeEach
(
async
()
=>
{
proxyServer
=
await
createProxyServer
({
proxyServer
=
await
createProxyServer
({
onFetch
:
async
(
testData
,
request
)
=>
{
onFetch
:
async
(
testData
,
request
)
=>
{
if
(
request
.
method
===
'GET'
&&
request
.
url
===
'https://example.com/image.png'
)
{
return
new
Response
(
PNG_1X1
,
{
headers
:
{
'content-type'
:
'image/png'
},
})
}
if
(
if
(
request
.
method
===
'GET'
&&
request
.
method
===
'GET'
&&
[
[
...
@@ -139,4 +152,15 @@ describe('testmode', () => {
...
@@ -139,4 +152,15 @@ describe('testmode', () => {
expect
(
text
).
toEqual
(
'test1'
)
expect
(
text
).
toEqual
(
'test1'
)
})
})
})
})
describe
(
'image optimizer'
,
()
=>
{
it
(
'should intercept external image requests'
,
async
()
=>
{
const
response
=
await
fetchForTest
(
'/_next/image?url=https%3A%2F%2Fexample.com%2Fimage.png&w=64&q=75'
)
expect
(
response
.
status
).
toBe
(
200
)
expect
(
response
.
headers
.
get
(
'content-type'
)).
toMatch
(
/^image
\/
/
)
})
})
})
})
test/unit/image-optimizer/create-pinned-lookup.test.ts
0 → 100644
View file @
e002ad68
/* eslint-env jest */
import
{
ADDRCONFIG
,
type
LookupAddress
,
type
LookupOptions
}
from
'dns'
import
{
createPinnedLookup
}
from
'next/dist/server/image-optimizer'
const
HOSTNAME
=
'example.com'
const
IPV4
:
LookupAddress
=
{
address
:
'93.184.216.34'
,
family
:
4
}
const
IPV6
:
LookupAddress
=
{
address
:
'2606:2800:220:1:248:1893:25c8:1946'
,
family
:
6
,
}
function
callLookup
(
options
:
LookupOptions
,
{
hostname
=
HOSTNAME
,
addresses
=
[
IPV4
,
IPV6
]
}
=
{}
)
{
const
calls
:
Array
<
{
err
:
NodeJS
.
ErrnoException
|
null
result
:
string
|
LookupAddress
[]
family
?:
number
}
>
=
[]
createPinnedLookup
(
HOSTNAME
,
addresses
)(
hostname
,
options
,
(
err
,
result
,
family
)
=>
calls
.
push
({
err
,
result
,
family
})
)
expect
(
calls
).
toHaveLength
(
1
)
return
calls
[
0
]
}
describe
(
'createPinnedLookup'
,
()
=>
{
it
(
'should hand back every pinned address for the options a socket uses'
,
()
=>
{
const
{
err
,
result
}
=
callLookup
({
all
:
true
,
hints
:
ADDRCONFIG
})
expect
(
err
).
toBeNull
()
expect
(
result
).
toEqual
([
IPV4
,
IPV6
])
})
it
(
'should report ENODATA rather than an empty list when the family matches nothing'
,
()
=>
{
// An empty list is not recoverable: node destructures the first address, so
// the resulting TypeError is thrown inside the socket and escapes the request
const
{
err
,
result
}
=
callLookup
(
{
all
:
true
,
family
:
6
},
{
addresses
:
[
IPV4
]
}
)
expect
(
err
?.
code
).
toBe
(
'ENODATA'
)
expect
(
result
).
toEqual
([])
})
it
(
'should report ENODATA when the family matches nothing and all is unset'
,
()
=>
{
const
{
err
}
=
callLookup
({
family
:
6
},
{
addresses
:
[
IPV4
]
})
expect
(
err
?.
code
).
toBe
(
'ENODATA'
)
})
it
(
'should return a single address of the requested family'
,
()
=>
{
const
{
err
,
result
,
family
}
=
callLookup
({
family
:
6
})
expect
(
err
).
toBeNull
()
expect
({
result
,
family
}).
toEqual
({
result
:
IPV6
.
address
,
family
:
6
})
})
it
(
'should refuse to resolve a hostname other than the pinned one'
,
()
=>
{
const
{
err
}
=
callLookup
({
all
:
true
},
{
hostname
:
'attacker.example'
})
expect
(
err
?.
message
).
toContain
(
'does not match request hostname'
)
})
})
test/unit/image-optimizer/fetch-external-image.test.ts
View file @
e002ad68
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment