import { promises } from 'fs' import type { IncomingMessage, RequestOptions as HttpRequestOptions, ServerResponse, } from 'http' import { Agent as HttpAgent, request as httpRequest } from 'http' import { Agent as HttpsAgent, request as httpsRequest } from 'https' import { mediaType } from 'next/dist/compiled/@hapi/accept' import contentDisposition from 'next/dist/compiled/content-disposition' import { join } from 'path' import { getImageBlurSvg } from '../shared/lib/image-blur-svg' import type { ImageConfigComplete } from '../shared/lib/image-config' import { hasLocalMatch } from '../shared/lib/match-local-pattern' import { hasRemoteMatch } from '../shared/lib/match-remote-pattern' import type { NextConfigComplete, NextConfigRuntime } from './config-shared' import { MockedRequest, MockedResponse } from './lib/mock-request' import type { NextUrlWithParsedQuery } from './request-meta' import { CachedRouteKind, IncrementalCacheKind, type IncrementalCacheValue, type IncrementalResponseCacheEntry, } from './response-cache' import type { CacheHandler } from './lib/incremental-cache' import { sendEtagResponse } from './send-payload' import { getContentType, getExtension } from './serve-static' import * as Log from '../build/output/log' import { isPrivateIp } from './is-private-ip' import { getOrInitDiskLRU } from './lib/disk-lru-cache.external' import { parseUrl, parseReqUrl } from '../lib/url' import type { CacheControl } from './lib/cache-control' import { InvariantError } from '../shared/lib/invariant-error' import { lookup } from 'dns/promises' import { isIP, type LookupFunction } from 'net' import { ADDRCONFIG, type LookupAddress } from 'dns' import { pipeline, Transform, type Readable, type TransformCallback, } from 'stream' import { constants as zlibConstants, createBrotliDecompress, createGunzip, createInflate, createInflateRaw, } from 'zlib' import { imageOptimizerTransform, type ImageOptimizerTransformOptions, type ImageUpstream, } from './image-optimizer/transform' import { extractEtag, getHash, getImageEtag, } from './image-optimizer/extract-etag' import { getMaxAge } from './image-optimizer/get-max-age' import { getPreviouslyCachedImageOrNull } from './image-optimizer/get-previously-cached-image-or-null' import { getImageSize } from './image-optimizer/get-image-size' import { ImageError } from './image-optimizer/image-error' import { NSIO_OUTPUT_MIME_TYPES } from '../shared/lib/image-optimizer-constants' // `next-server.ts` destructures `ImageError` from this module to check // `instanceof` on errors thrown by the transform. export { ImageError } from './image-optimizer/image-error' type XCacheHeader = 'MISS' | 'HIT' | 'STALE' const CACHE_VERSION = 4 const BLUR_IMG_SIZE = 8 // should match `next-image-loader` const BLUR_QUALITY = 70 // should match `next-image-loader` async function initCacheEntries( cacheDir: string ): Promise> { const cacheKeys = await promises.readdir(cacheDir).catch(() => []) const entries: Array<{ key: string; size: number; expireAt: number }> = [] for (const cacheKey of cacheKeys) { try { const { expireAt, buffer } = await readFromCacheDir(cacheDir, cacheKey) entries.push({ key: cacheKey, size: buffer.byteLength, expireAt, }) } catch { // Skip entries that can't be read from disk } } // Sort oldest-first so we can replay them chronologically into LRU return entries.sort((a, b) => a.expireAt - b.expireAt) } export interface ImageParamsResult { href: string isAbsolute: boolean isStatic: boolean width: number quality: number mimeType: string sizes: number[] minimumCacheTTL: number } type ImageUrlParamResult = | { href: string; isAbsolute: boolean } | { errorMessage: string } function validateUrlParam( url: string | string[] | undefined, imageData: ImageConfigComplete, isRedirect = false ): ImageUrlParamResult { // Note - We intentionally skip validation for local/remotePatterns when // validating redirect locations so that we don't break a src img redirect // to a new domain (e.g. github.com => githubusercontent.com). const { domains = [], remotePatterns = [], localPatterns } = imageData if (!isRedirect && domains.length > 0) { Log.warnOnce( 'The "images.domains" configuration is deprecated. Please use "images.remotePatterns" configuration instead.' ) } if (!url) { return { errorMessage: '"url" parameter is required' } } else if (Array.isArray(url)) { return { errorMessage: '"url" parameter cannot be an array' } } if (url.length > 3072) { return { errorMessage: '"url" parameter is too long' } } if (url.startsWith('//')) { return { errorMessage: '"url" parameter cannot be a protocol-relative URL (//)', } } if (url.startsWith('/')) { if ( /\/_next\/image($|\/)/.test( decodeURIComponent(parseUrl(url)?.pathname ?? '') ) ) { return { errorMessage: '"url" parameter cannot be recursive', } } if (!isRedirect && !hasLocalMatch(localPatterns, url)) { return { errorMessage: '"url" parameter is not allowed' } } return { href: url, isAbsolute: false } } let hrefParsed: URL try { hrefParsed = new URL(url) } catch (_error) { return { errorMessage: '"url" parameter is invalid' } } if (!['http:', 'https:'].includes(hrefParsed.protocol)) { return { errorMessage: '"url" parameter is invalid' } } if (!isRedirect && !hasRemoteMatch(domains, remotePatterns, hrefParsed)) { return { errorMessage: '"url" parameter is not allowed' } } return { href: hrefParsed.toString(), isAbsolute: true } } function getSupportedMimeType(options: string[], accept = ''): string { // With no preferences, `mediaType()` returns the client's preferred type. if (options.length === 0) { return '' } const mimeType = mediaType(accept, options) return accept.includes(mimeType) ? mimeType : '' } async function writeToCacheDir( cacheDir: string, cacheKey: string, extension: string, maxAge: number, expireAt: number, buffer: Buffer, etag: string, upstreamEtag: string ) { if (buffer.byteLength === 0) { throw new Error( 'Invariant: cannot write an empty buffer to the image cache' ) } const dir = join(/* turbopackIgnore: true */ cacheDir, cacheKey) const filename = join( /* turbopackIgnore: true */ dir, `${maxAge}.${expireAt}.${etag}.${upstreamEtag}.${extension}` ) await promises.rm(dir, { recursive: true, force: true }).catch(() => {}) await promises.mkdir(dir, { recursive: true }) await promises.writeFile(filename, buffer) } async function readFromCacheDir(cacheDir: string, cacheKey: string) { const dir = join(/* turbopackIgnore: true */ cacheDir, cacheKey) const files = await promises.readdir(dir) const file = files[0] if (!file) { throw new Error( `Invariant: cache entry "${cacheKey}" not found in dir "${cacheDir}"` ) } const [maxAgeSt, expireAtSt, etag, upstreamEtag, extension] = file.split( '.', 5 ) const filePath = join(/* turbopackIgnore: true */ dir, file) const buffer = await promises.readFile(/* turbopackIgnore: true */ filePath) if (buffer.byteLength === 0) { throw new Error(`Invariant: image cache entry "${cacheKey}" is empty`) } const expireAt = Number(expireAtSt) const maxAge = Number(maxAgeSt) return { maxAge, expireAt, etag, upstreamEtag, buffer, extension } } async function deleteFromCacheDir(cacheDir: string, cacheKey: string) { return promises .rm(join(/* turbopackIgnore: true */ cacheDir, cacheKey), { recursive: true, force: true, }) .catch((err) => { Log.error(`Failed to delete cache key ${cacheKey}`, err) }) } export class ImageOptimizerCache { private cacheDir: string private nextConfig: NextConfigRuntime private cacheHandler?: CacheHandler private cacheDiskLRU?: ReturnType private isDiskCacheEnabled?: boolean static validateParams( req: IncomingMessage, query: NextUrlWithParsedQuery['query'], nextConfig: NextConfigRuntime, isDev: boolean ): ImageParamsResult | { errorMessage: string } { const imageData = nextConfig.images const { deviceSizes = [], imageSizes = [], minimumCacheTTL = 14400, formats = ['image/webp'], } = imageData const qualities = nextConfig.images?.qualities const { url, w, q } = query const urlResult = validateUrlParam(url, imageData) if ('errorMessage' in urlResult) { return urlResult } if (!w) { return { errorMessage: '"w" parameter (width) is required' } } else if (Array.isArray(w)) { return { errorMessage: '"w" parameter (width) cannot be an array' } } else if (!/^[0-9]+$/.test(w)) { return { errorMessage: '"w" parameter (width) must be an integer greater than 0', } } if (!q) { return { errorMessage: '"q" parameter (quality) is required' } } else if (Array.isArray(q)) { return { errorMessage: '"q" parameter (quality) cannot be an array' } } else if (!/^[0-9]+$/.test(q)) { return { errorMessage: '"q" parameter (quality) must be an integer between 1 and 100', } } const width = parseInt(w, 10) if (width <= 0 || isNaN(width)) { return { errorMessage: '"w" parameter (width) must be an integer greater than 0', } } const sizes = [...(deviceSizes || []), ...(imageSizes || [])] if (isDev) { sizes.push(BLUR_IMG_SIZE) } const isValidSize = sizes.includes(width) || (isDev && width <= BLUR_IMG_SIZE) if (!isValidSize) { return { errorMessage: `"w" parameter (width) of ${width} is not allowed`, } } const quality = parseInt(q, 10) if (isNaN(quality) || quality < 1 || quality > 100) { return { errorMessage: '"q" parameter (quality) must be an integer between 1 and 100', } } if ( qualities && !qualities.includes(quality) && !(isDev && quality === BLUR_QUALITY) ) { return { errorMessage: `"q" parameter (quality) of ${q} is not allowed`, } } let supportedFormats: string[] = formats || [] if (nextConfig.experimental?.imgOptSafeMode?.nextImage) { // `config.ts` warns about the formats that are filtered out supportedFormats = supportedFormats.filter((format) => NSIO_OUTPUT_MIME_TYPES.includes(format) ) } const mimeType = getSupportedMimeType( supportedFormats, req.headers['accept'] ) const { href, isAbsolute } = urlResult const isStatic = href.startsWith(`${nextConfig.basePath || ''}/_next/static/media`) || href.startsWith( `${nextConfig.basePath || ''}/_next/static/immutable/media` ) return { href, sizes, isAbsolute, isStatic, width, quality, mimeType, minimumCacheTTL, } } static getCacheKey({ href, width, quality, mimeType, }: { href: string width: number quality: number mimeType: string }): string { return getHash([CACHE_VERSION, href, width, quality, mimeType]) } constructor({ distDir, nextConfig, cacheHandler, }: { distDir: string nextConfig: NextConfigRuntime cacheHandler?: CacheHandler }) { this.cacheDir = join(/* turbopackIgnore: true */ distDir, 'cache', 'images') this.nextConfig = nextConfig this.cacheHandler = cacheHandler // Eagerly start LRU initialization for filesystem cache if ( !cacheHandler && nextConfig.images.maximumDiskCacheSize !== 0 && nextConfig.experimental.isrFlushToDisk ) { this.isDiskCacheEnabled = true this.cacheDiskLRU = getOrInitDiskLRU( this.cacheDir, nextConfig.images.maximumDiskCacheSize, initCacheEntries, deleteFromCacheDir ) } } async get(cacheKey: string): Promise { // If a custom cache handler is provided, use it if (this.cacheHandler) { try { const cacheData = await this.cacheHandler.get(cacheKey, { kind: IncrementalCacheKind.IMAGE, isFallback: false, }) if (!cacheData?.value) { return null } if (cacheData.value.kind !== CachedRouteKind.IMAGE) { return null } const now = Date.now() const lastModified = cacheData.lastModified || now const revalidate = typeof cacheData.value.revalidate === 'number' ? cacheData.value.revalidate : this.nextConfig.images.minimumCacheTTL const revalidateAfter = Math.max(revalidate, this.nextConfig.images.minimumCacheTTL) * 1000 + lastModified const isStale = revalidateAfter < now return { value: cacheData.value, revalidateAfter, cacheControl: { revalidate, expire: undefined }, isStale, } } catch (_) { // failed to get from custom cache handler, treat as cache miss } return null } // If the filesystem cache is disabled, return early if (!this.isDiskCacheEnabled) { return null } // Fall back to filesystem cache try { const now = Date.now() const { maxAge, expireAt, etag, upstreamEtag, buffer, extension } = await readFromCacheDir(this.cacheDir, cacheKey) // Promote entry in LRU (mark as recently used) const lru = await this.cacheDiskLRU lru?.get(cacheKey) return { value: { kind: CachedRouteKind.IMAGE, etag, buffer, extension, upstreamEtag, }, revalidateAfter: Math.max(maxAge, this.nextConfig.images.minimumCacheTTL) * 1000 + Date.now(), cacheControl: { revalidate: maxAge, expire: undefined }, isStale: now > expireAt, } } catch (_) { // failed to read from cache dir, treat as cache miss } return null } async set( cacheKey: string, value: IncrementalCacheValue | null, { cacheControl, }: { cacheControl?: CacheControl } ) { if (value?.kind !== CachedRouteKind.IMAGE) { throw new Error('invariant attempted to set non-image to image-cache') } const revalidate = cacheControl?.revalidate if (typeof revalidate !== 'number') { throw new InvariantError('revalidate must be a number for image-cache') } // If a custom cache handler is provided, use it if (this.cacheHandler) { try { // Apply minimumCacheTTL at write time, similar to the implementation in the fallback filesystem cache const effectiveRevalidate = Math.max( revalidate, this.nextConfig.images.minimumCacheTTL ) const valueWithRevalidate = { ...value, revalidate: effectiveRevalidate, } await this.cacheHandler.set(cacheKey, valueWithRevalidate, { kind: IncrementalCacheKind.IMAGE, cacheControl: { revalidate: effectiveRevalidate, expire: cacheControl?.expire, }, }) } catch (err) { Log.error(`Failed to write image to custom cache ${cacheKey}`, err) } return } // If the filesystem cache is disabled, return early if (!this.isDiskCacheEnabled) { return } // Fall back to filesystem cache const expireAt = Math.max(revalidate, this.nextConfig.images.minimumCacheTTL) * 1000 + Date.now() try { const lru = await this.cacheDiskLRU const success = lru?.set(cacheKey, value.buffer.byteLength) if (success === false) { throw new Error( `image of size ${value.buffer.byteLength} could not be tracked by lru cache` ) } await writeToCacheDir( this.cacheDir, cacheKey, value.extension, revalidate, expireAt, value.buffer, value.etag, value.upstreamEtag ) } catch (err) { Log.error(`Failed to write image to cache ${cacheKey}`, err) } } } // Base origin used to resolve the `Location` header of an internal (relative // `src`) image response, which is handled in-process and has no real origin. const INTERNAL_IMAGE_BASE_URL = 'http://n' function isRedirectStatus(statusCode: number) { return [301, 302, 303, 307, 308].includes(statusCode) } function upstreamTimedOut(href: string): ImageError { Log.error('upstream image response timed out for', href) return new ImageError( 504, '"url" parameter is valid but upstream response timed out' ) } const agentOptions = { keepAlive: true, timeout: 7_000, } type ImageRequestOptions = HttpRequestOptions & { imageLookupKey?: string } type AgentGetName = (options?: HttpRequestOptions) => string const getHttpAgentName = ( HttpAgent.prototype as HttpAgent & { getName: AgentGetName } ).getName const getHttpsAgentName = ( HttpsAgent.prototype as HttpsAgent & { getName: AgentGetName } ).getName class ImageHttpAgent extends HttpAgent { getName(options: ImageRequestOptions = {}): string { return `${getHttpAgentName.call(this, options)}:${options.imageLookupKey ?? ''}` } } class ImageHttpsAgent extends HttpsAgent { getName(options: ImageRequestOptions = {}): string { return `${getHttpsAgentName.call(this, options)}:${options.imageLookupKey ?? ''}` } } let protectedHttpAgent: ImageHttpAgent | undefined let protectedHttpsAgent: ImageHttpsAgent | undefined let permissiveHttpAgent: ImageHttpAgent | undefined let permissiveHttpsAgent: ImageHttpsAgent | undefined function getImageAgent( dangerouslyAllowLocalIP: boolean, isHttps: boolean ): ImageHttpAgent | ImageHttpsAgent { if (isHttps) { if (dangerouslyAllowLocalIP) { return (permissiveHttpsAgent ??= new ImageHttpsAgent(agentOptions)) } return (protectedHttpsAgent ??= new ImageHttpsAgent(agentOptions)) } if (dangerouslyAllowLocalIP) { return (permissiveHttpAgent ??= new ImageHttpAgent(agentOptions)) } return (protectedHttpAgent ??= new ImageHttpAgent(agentOptions)) } const BASE_REQ_HEADERS = { accept: '*/*', 'accept-language': '*', 'sec-fetch-mode': 'cors', 'user-agent': 'node', } const HTTP_FETCH_HEADERS = { ...BASE_REQ_HEADERS, 'accept-encoding': 'gzip, deflate', } const HTTPS_FETCH_HEADERS = { ...BASE_REQ_HEADERS, 'accept-encoding': 'br, gzip, deflate', } /** * Resolves `hostname` with the DNS options the socket itself would have used, * so the addresses checked below are the exact set the connection can reach. * Mirrors `lookupAndConnect()` in Node's `lib/net.js`. */ function lookupAsSocketWould(hostname: string): Promise { return lookup(hostname, { all: true, // Node applies ADDRCONFIG whenever no address family is requested, // on every platform except Windows. hints: process.platform === 'win32' ? 0 : ADDRCONFIG, }) } /** * Pins the connection to `addresses` instead of letting it resolve `hostname` * a second time. Checking addresses up front only proves where the name * pointed at that moment; the socket runs its own lookup, so a record that * changes in between (DNS rebinding) would otherwise still reach a private IP. * * Every address in the list was checked, so all of them are handed back to * keep Node's happy eyeballs failover between IPv6 and IPv4 intact. */ export function createPinnedLookup( hostname: string, addresses: LookupAddress[] ): LookupFunction { return (lookupHostname, options, callback) => { if (lookupHostname !== hostname) { callback( new InvariantError( `Image lookup hostname "${lookupHostname}" does not match request hostname "${hostname}"` ), [] ) return } const family = typeof options.family === 'number' ? options.family : 0 const matched = family ? addresses.filter((address) => address.family === family) : addresses if (matched.length === 0) { // Reporting this as an error rather than an empty list is not optional: // Node destructures the first address, so the resulting TypeError is // thrown inside the socket and escapes the request entirely. const err: NodeJS.ErrnoException = new Error( `No IPv${family} address available for ${hostname}` ) err.code = 'ENODATA' callback(err, []) return } if (options.all) { callback(null, matched) } else { callback(null, matched[0].address, matched[0].family) } } } function requestUpstreamImage( url: URL, pinnedLookup: LookupFunction | undefined, imageLookupKey: string | undefined, dangerouslyAllowLocalIP: boolean, signal: AbortSignal ): Promise { return new Promise((resolve, reject) => { const isHttps = url.protocol === 'https:' const request = isHttps ? httpsRequest : httpRequest const options: ImageRequestOptions = { agent: getImageAgent(dangerouslyAllowLocalIP, isHttps), headers: isHttps ? HTTPS_FETCH_HEADERS : HTTP_FETCH_HEADERS, imageLookupKey, lookup: pinnedLookup, signal, } const req = request(url, options, resolve) req.on('error', reject) req.end() }) } class DeflateDecoder extends Transform { private decoder: Transform | undefined constructor() { super() this.decoder = undefined } _transform( chunk: Buffer, encoding: BufferEncoding, callback: TransformCallback ) { if (!this.decoder) { if (chunk.length === 0) { callback() return } const options = { flush: zlibConstants.Z_SYNC_FLUSH, finishFlush: zlibConstants.Z_SYNC_FLUSH, } this.decoder = (chunk[0] & 0x0f) === 0x08 ? createInflate(options) : createInflateRaw(options) this.decoder.on('data', (data: Buffer) => this.push(data)) this.decoder.on('end', () => this.push(null)) this.decoder.on('error', (err) => this.destroy(err)) } this.decoder.write(chunk, encoding, callback) } _final(callback: TransformCallback) { this.decoder?.end() this.decoder = undefined callback() } } // Matches the chain limit of native fetch (undici), so an upstream cannot // force unbounded decoder allocations with the header alone. const MAX_CONTENT_ENCODINGS = 5 function decodeResponseBody(res: IncomingMessage, href: string): Readable { const contentEncoding = res.headers['content-encoding'] if (!contentEncoding) { return res } const codings = contentEncoding.toLowerCase().split(',') if (codings.length > MAX_CONTENT_ENCODINGS) { Log.error( 'upstream image response had too many content encodings for', href ) throw new ImageError( 400, '"url" parameter is valid but upstream response is invalid' ) } const decoders: Transform[] = [] for (let i = codings.length - 1; i >= 0; i--) { switch (codings[i].trim()) { case 'x-gzip': case 'gzip': decoders.push( createGunzip({ flush: zlibConstants.Z_SYNC_FLUSH, finishFlush: zlibConstants.Z_SYNC_FLUSH, }) ) break case 'deflate': decoders.push(new DeflateDecoder()) break case 'br': decoders.push( createBrotliDecompress({ flush: zlibConstants.BROTLI_OPERATION_FLUSH, finishFlush: zlibConstants.BROTLI_OPERATION_FLUSH, }) ) break default: return res } } pipeline([res, ...decoders], () => {}) return decoders[decoders.length - 1] } export async function fetchExternalImage( href: string, dangerouslyAllowLocalIP: boolean, maximumResponseBody: number, count = 3, abortSignal?: AbortSignal ): Promise { const url = new URL(href) // `URL.hostname` keeps the brackets around an IPv6 literal, the socket does not const hostname = url.hostname.replace(/^\[|\]$/g, '') let pinnedLookup: LookupFunction | undefined let imageLookupKey: string | undefined if (!dangerouslyAllowLocalIP) { const literalFamily = isIP(hostname) let addresses: LookupAddress[] if (literalFamily > 0) { // A literal address is connected to as-is, without any DNS resolution addresses = [{ address: hostname, family: literalFamily }] } else { addresses = await lookupAsSocketWould(hostname) pinnedLookup = createPinnedLookup(hostname, addresses) } imageLookupKey = addresses .map((address) => `${address.family}:${address.address}`) .join(',') const privateIps = addresses .map((record) => record.address) .filter((ip) => isPrivateIp(ip)) if (privateIps.length > 0) { Log.error( 'upstream image', href, 'hostname resolved to private IP', 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') } } // The signals are owned here rather than by `requestUpstreamImage()` because // they have to stay readable while the body streams. Node reports an abort // during the body as a plain `ECONNRESET` on the response, so the signals are // the only way to tell a timeout or abort apart from the upstream resetting // the connection. const timeoutSignal = AbortSignal.timeout(agentOptions.timeout) const signal = abortSignal ? AbortSignal.any([timeoutSignal, abortSignal]) : timeoutSignal const toAbortError = (err: unknown) => { // Our own errors (e.g. an oversized body) were not caused by the signal, // even if it happened to abort around the same time if (err instanceof ImageError) return err if (abortSignal?.aborted) return abortSignal.reason if (timeoutSignal.aborted) return upstreamTimedOut(href) return err } let res: IncomingMessage try { res = await requestUpstreamImage( url, pinnedLookup, imageLookupKey, dangerouslyAllowLocalIP, signal ) } catch (err) { throw toAbortError(err) } const statusCode = res.statusCode if (statusCode === undefined) { res.destroy() throw new InvariantError('Expected statusCode on upstream image response') } const locationHeader = res.headers.location if ( isRedirectStatus(statusCode) && locationHeader && URL.canParse(locationHeader, href) ) { // Discard the body rather than draining it. A redirect body is never used, // and draining one would download unlimited bytes from the upstream because // `maximumResponseBody` is only enforced on the final response. res.destroy() if (count === 0) { Log.error('upstream image response had too many redirects', href) throw new ImageError( 508, '"url" parameter is valid but upstream response is invalid' ) } const redirect = new URL(locationHeader, href).href return fetchExternalImage( redirect, dangerouslyAllowLocalIP, maximumResponseBody, count - 1, abortSignal ) } if (statusCode < 200 || statusCode > 299) { res.destroy() Log.error('upstream image response failed for', href, statusCode) throw new ImageError( statusCode, '"url" parameter is valid but upstream response is invalid' ) } const chunks: Buffer[] = [] let totalSize = 0 try { const body = decodeResponseBody(res, href) // Throwing out of this loop destroys the response, so an oversized or // timed out download stops instead of running to completion. for await (const chunk of body as AsyncIterable) { totalSize += chunk.byteLength if (totalSize > maximumResponseBody) { Log.error( 'upstream image response exceeded maximum size for', href, totalSize ) throw new ImageError( 413, '"url" parameter is valid but upstream response is invalid' ) } chunks.push(chunk) } } catch (err) { res.destroy() throw toAbortError(err) } if (totalSize === 0) { Log.error('upstream image response is empty for', href) throw new ImageError( 400, '"url" parameter is valid but upstream response is invalid' ) } const buffer = Buffer.concat(chunks) const contentType = res.headers['content-type'] const cacheControl = res.headers['cache-control'] const etag = extractEtag(res.headers.etag, buffer) return { buffer, contentType, cacheControl, etag } } export async function fetchInternalImage( href: string, _req: IncomingMessage, _res: ServerResponse, handleRequest: ( newReq: IncomingMessage, newRes: ServerResponse, newParsedUrl?: NextUrlWithParsedQuery ) => Promise, imagesConfig: ImageConfigComplete, count = imagesConfig.maximumRedirects, abortSignal?: AbortSignal ): Promise { try { abortSignal?.throwIfAborted() // Coerce HEAD to GET to avoid issues with the image optimizer const method = !_req.method || _req.method === 'HEAD' ? 'GET' : _req.method // The mocked request keeps the requester's socket so that protocol and // remote address detection keep working, but the mocked response must not // reference it. `send` (used by `serveStatic`) watches `res.socket` through // `on-finished` and treats the response as finished as soon as that socket // stops being writable. When the requester disconnected mid-transfer this // tore down the file stream without ever ending the mocked response, and // since `ResponseCache` coalesces every request for the same cache key onto // this single fetch, the key stayed pending for every later requester until // the server restarted. const mocked = { req: new MockedRequest({ url: href, method, headers: {}, socket: _req.socket, }), res: new MockedResponse({ maximumResponseBody: imagesConfig.maximumResponseBody, }), } // Destroying the mocked response rejects `hasStreamed` with the abort // reason, and stops whatever is writing to it (it also aborts the // `request.signal` of a route handler serving the image). const onAbort = () => mocked.res.destroy(abortSignal?.reason) abortSignal?.addEventListener('abort', onAbort, { once: true }) try { // Not awaited in sequence, as `handleRequest` may never settle once the // response is destroyed await Promise.all([ handleRequest(mocked.req, mocked.res, parseReqUrl(href)), mocked.res.hasStreamed, ]) } finally { abortSignal?.removeEventListener('abort', onAbort) } const locationHeader = mocked.res.getHeader('Location') if ( mocked.res.statusCode && isRedirectStatus(mocked.res.statusCode) && locationHeader && URL.canParse(locationHeader, INTERNAL_IMAGE_BASE_URL) ) { if (count === 0) { Log.error('internal image response had too many redirects', href) throw new ImageError( 508, '"url" parameter is valid but internal response is invalid' ) } const urlResult = validateUrlParam(locationHeader, imagesConfig, true) if ('errorMessage' in urlResult) { throw new ImageError(400, urlResult.errorMessage) } if (!urlResult.isAbsolute) { return fetchInternalImage( urlResult.href, _req, _res, handleRequest, imagesConfig, count - 1, abortSignal ) } return fetchExternalImage( urlResult.href, imagesConfig.dangerouslyAllowLocalIP, imagesConfig.maximumResponseBody, count - 1, abortSignal ) } if ( !mocked.res.statusCode || mocked.res.statusCode < 200 || mocked.res.statusCode > 299 ) { Log.error( 'internal image response failed for', href, mocked.res.statusCode ) throw new ImageError( mocked.res.statusCode, '"url" parameter is valid but internal response is invalid' ) } if (mocked.res.buffers.length === 0) { Log.error('internal image response is empty for', href) throw new ImageError( 400, '"url" parameter is valid but internal response is invalid' ) } const buffer = Buffer.concat(mocked.res.buffers) const contentType = mocked.res.getHeader('Content-Type') const cacheControl = mocked.res.getHeader('Cache-Control') const etag = extractEtag(mocked.res.getHeader('ETag'), buffer) return { buffer, contentType, cacheControl, etag } } catch (err) { if ( err instanceof ImageError || (abortSignal?.aborted && err === abortSignal.reason) ) { throw err } if ( err && typeof err === 'object' && 'code' in err && err.code === 'ERR_MAX_BODY_SIZE_EXCEEDED' ) { Log.error('internal image response exceeded maximum size for', href) throw new ImageError( 413, '"url" parameter is valid but internal response is invalid' ) } Log.error('upstream image response failed for', href, err) throw new ImageError( 500, '"url" parameter is valid but upstream response is invalid' ) } } async function makeBlurPlaceholder(buffer: Buffer, contentType: string) { // During `next dev`, we don't want to generate blur placeholders with webpack // because it can delay starting the dev server. Instead, `next-image-loader.js` // will inline a special url to lazily generate the blur placeholder at request time. const meta = await getImageSize(buffer) const blurOpts = { blurWidth: meta.width, blurHeight: meta.height, blurDataURL: `data:${contentType};base64,${buffer.toString('base64')}`, } return { buffer: Buffer.from(unescape(getImageBlurSvg(blurOpts))), contentType: 'image/svg+xml', } } type ImageOptimizerNextConfig = { experimental: Pick< NextConfigComplete['experimental'], | 'imgOptConcurrency' | 'imgOptOperationCache' | 'imgOptMaxInputPixels' | 'imgOptSequentialRead' | 'imgOptTimeoutInSeconds' | 'imgOptMozjpeg' > & Partial> images: Pick< NextConfigComplete['images'], 'dangerouslyAllowSVG' | 'minimumCacheTTL' > } /** * Helper that extracts fields from `NextConfigComplete` into * `ImageOptimizerTransformOptions`. * * `next/experimental/image-optimizer-transform` is a public module, so it * doesn't make sense for it's functions to depend on the structure of * `NextConfigComplete`, which is an internal-only interface for the * post-transformed `next.config.js` file. * * Internally, we would like to call these APIs with the `NextConfigComplete`, * so this transforms it. */ export function getImageOptimizerTransformOptions( nextConfig: ImageOptimizerNextConfig ): ImageOptimizerTransformOptions { return { dangerouslyAllowSVG: nextConfig.images.dangerouslyAllowSVG, minimumCacheTTL: nextConfig.images.minimumCacheTTL, concurrency: nextConfig.experimental.imgOptConcurrency, operationCache: nextConfig.experimental.imgOptOperationCache, maxInputPixels: nextConfig.experimental.imgOptMaxInputPixels, sequentialRead: nextConfig.experimental.imgOptSequentialRead, timeoutInSeconds: nextConfig.experimental.imgOptTimeoutInSeconds, mozjpeg: nextConfig.experimental.imgOptMozjpeg, safeImageOptimization: nextConfig.experimental.imgOptSafeMode?.nextImage, } } export async function imageOptimizer( imageUpstream: ImageUpstream, paramsResult: Pick< ImageParamsResult, 'href' | 'width' | 'quality' | 'mimeType' >, transformOptions: ImageOptimizerTransformOptions, opts: { isDev?: boolean silent?: boolean previousCacheEntry?: IncrementalResponseCacheEntry | null } ): Promise<{ buffer: Buffer contentType: string maxAge: number etag: string upstreamEtag: string error?: unknown }> { // If the upstream image is unchanged since the previous cache entry was // optimized, re-running the transform would produce the same output. const previouslyCachedImage = getPreviouslyCachedImageOrNull( imageUpstream, opts.previousCacheEntry ) const previousContentType = previouslyCachedImage && getContentType(previouslyCachedImage.extension) if (previouslyCachedImage && previousContentType) { return { buffer: previouslyCachedImage.buffer, contentType: previousContentType, maxAge: opts.previousCacheEntry?.cacheControl?.revalidate || Math.max( transformOptions.minimumCacheTTL ?? 0, getMaxAge(imageUpstream.cacheControl) ), etag: previouslyCachedImage.etag, upstreamEtag: previouslyCachedImage.upstreamEtag, } } const result = await imageOptimizerTransform(imageUpstream, paramsResult, { ...transformOptions, logger: opts.silent ? undefined : Log, }) // Images passed through unoptimized (SVG, animated, or failed) are served // as-is rather than wrapped in a blur placeholder. if ( opts.isDev && paramsResult.width <= BLUR_IMG_SIZE && paramsResult.quality === BLUR_QUALITY && result.optimized ) { const placeholder = await makeBlurPlaceholder( result.buffer, result.contentType ) return { ...result, buffer: placeholder.buffer, contentType: placeholder.contentType, etag: getImageEtag(placeholder.buffer), } } return result } function getFileNameWithExtension( url: string, contentType: string | null ): string { const [urlWithoutQueryParams] = url.split('?', 1) const fileNameWithExtension = urlWithoutQueryParams.split('/').pop() if (!contentType || !fileNameWithExtension) { return 'image.bin' } const [fileName] = fileNameWithExtension.split('.', 1) const extension = getExtension(contentType) return `${fileName}.${extension}` } function setResponseHeaders( req: IncomingMessage, res: ServerResponse, url: string, etag: string, contentType: string | null, isStatic: boolean, xCache: XCacheHeader, imagesConfig: ImageConfigComplete, maxAge: number, isDev: boolean ) { res.setHeader('Vary', 'Accept') res.setHeader( 'Cache-Control', isStatic ? 'public, max-age=315360000, immutable' : `public, max-age=${isDev ? 0 : maxAge}, must-revalidate` ) if (sendEtagResponse(req, res, etag)) { // already called res.end() so we're finished return { finished: true } } if (contentType) { res.setHeader('Content-Type', contentType) } const fileName = getFileNameWithExtension(url, contentType) res.setHeader( 'Content-Disposition', contentDisposition(fileName, { type: imagesConfig.contentDispositionType }) ) res.setHeader('Content-Security-Policy', imagesConfig.contentSecurityPolicy) res.setHeader('X-Nextjs-Cache', xCache) return { finished: false } } export function sendResponse( req: IncomingMessage, res: ServerResponse, url: string, extension: string, buffer: Buffer, etag: string, isStatic: boolean, xCache: XCacheHeader, imagesConfig: ImageConfigComplete, maxAge: number, isDev: boolean ) { const contentType = getContentType(extension) const result = setResponseHeaders( req, res, url, etag, contentType, isStatic, xCache, imagesConfig, maxAge, isDev ) if (!result.finished) { res.setHeader('Content-Length', Buffer.byteLength(buffer)) // A response body must not be sent for HEAD requests if (req.method === 'HEAD') { res.end() } else { res.end(buffer) } } }