Unverified Commit 5452439f authored by Rishi's avatar Rishi Committed by GitHub

fix(next/image): Improve error message for private IP (SSRF) rejections (#91686)

Co-authored-by: 's avatarSteven <steven@ceriously.com>
parent 07f76411
...@@ -843,6 +843,8 @@ const nextConfig = { ...@@ -843,6 +843,8 @@ const nextConfig = {
module.exports = nextConfig module.exports = nextConfig
``` ```
This might be necessary when hosting Next.js in a VPC with split-horizon DNS and you receive status 400 Bad Request. Only enable once you understand the SSRF risk.
### Maximum Redirects (Breaking change) ### Maximum Redirects (Breaking change)
The default for `images.maximumRedirects` has changed from unlimited to 3 redirects maximum. The default for `images.maximumRedirects` has changed from unlimited to 3 redirects maximum.
......
...@@ -917,6 +917,8 @@ module.exports = { ...@@ -917,6 +917,8 @@ module.exports = {
} }
``` ```
This might be necessary when hosting Next.js in a VPC with split-horizon DNS and you receive status 400 Bad Request. Only enable once you understand the SSRF risk.
#### `dangerouslyAllowSVG` #### `dangerouslyAllowSVG`
`dangerouslyAllowSVG` allows you to serve SVG images. `dangerouslyAllowSVG` allows you to serve SVG images.
......
...@@ -882,8 +882,9 @@ export async function fetchExternalImage( ...@@ -882,8 +882,9 @@ export async function fetchExternalImage(
Log.error( Log.error(
'upstream image', 'upstream image',
href, href,
'resolved to private ip', 'hostname resolved to private IP',
JSON.stringify(privateIps) JSON.stringify(privateIps),
'If this is expected and you understand SSRF risk, use images.dangerouslyAllowLocalIP = true to continue.'
) )
throw new ImageError(400, '"url" parameter is not allowed') throw new ImageError(400, '"url" parameter is not allowed')
} }
......
...@@ -5,6 +5,54 @@ import { ...@@ -5,6 +5,54 @@ import {
} from 'next/dist/server/image-optimizer' } from 'next/dist/server/image-optimizer'
describe('fetchExternalImage', () => { describe('fetchExternalImage', () => {
describe('private IP / SSRF guard', () => {
it('should reject a literal private IP hostname with a generic error message', async () => {
const fetchMock = jest.fn()
global.fetch = fetchMock
const error = await fetchExternalImage(
'http://192.168.0.1/private.jpg',
false,
50_000_000
).catch((e) => e)
expect(error).toBeInstanceOf(ImageError)
expect((error as ImageError).statusCode).toBe(400)
expect((error as ImageError).message).toBe(
'"url" parameter is not allowed'
)
expect(fetchMock).not.toHaveBeenCalled()
})
it('should allow a literal private IP when dangerouslyAllowLocalIP is true', async () => {
global.fetch = jest.fn().mockResolvedValue({
ok: true,
status: 200,
body: new ReadableStream({
start(controller) {
controller.enqueue(new Uint8Array([1, 2, 3]))
controller.close()
},
}),
headers: {
get: jest.fn((header: string) => {
if (header === 'Content-Type') return 'image/jpeg'
return null
}),
},
})
const result = await fetchExternalImage(
'http://192.168.0.1/private.jpg',
true,
50_000_000
)
expect(result.buffer).toBeInstanceOf(Buffer)
expect(global.fetch).toHaveBeenCalled()
})
})
describe('response size limit', () => { describe('response size limit', () => {
it('should throw error when response has no body', async () => { it('should throw error when response has no body', async () => {
global.fetch = jest.fn().mockResolvedValue({ global.fetch = jest.fn().mockResolvedValue({
......
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