Unverified Commit e002ad68 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[lts-active] fix(next/image): Pin DNS resolution when fetching external images (#214)

parent 4c20699e
This diff is collapsed.
/** @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,
}, },
......
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\//)
})
})
}) })
/* 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')
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment