Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update handling for relative files in image-optimizer #17998

Merged
merged 14 commits into from Oct 19, 2020
30 changes: 15 additions & 15 deletions packages/next/next-server/server/image-optimizer.ts
Expand Up @@ -32,6 +32,7 @@ export async function imageOptimizer(
const { headers } = req
const { url, w, q } = parsedUrl.query
const mimeType = mediaType(headers.accept, MIME_TYPES) || ''
let href: string

if (!url) {
res.statusCode = 400
Expand All @@ -43,27 +44,31 @@ export async function imageOptimizer(
return { finished: true }
}

let absoluteUrl: URL | undefined
let relativeUrl: string | undefined
let isAbsolute: boolean

if (url.startsWith('/')) {
ijjk marked this conversation as resolved.
Show resolved Hide resolved
relativeUrl = url
href = url
isAbsolute = false
} else {
let hrefParsed: URL

try {
absoluteUrl = new URL(url)
hrefParsed = new URL(url)
isAbsolute = true
href = hrefParsed.toString()
ijjk marked this conversation as resolved.
Show resolved Hide resolved
} catch (_error) {
res.statusCode = 400
res.end('"url" parameter is invalid')
return { finished: true }
}

if (!['http:', 'https:'].includes(absoluteUrl.protocol)) {
if (!['http:', 'https:'].includes(hrefParsed.protocol)) {
res.statusCode = 400
res.end('"url" parameter is invalid')
return { finished: true }
}

if (!domains.includes(absoluteUrl.hostname)) {
if (!domains.includes(hrefParsed.hostname)) {
res.statusCode = 400
res.end('"url" parameter is not allowed')
return { finished: true }
Expand Down Expand Up @@ -112,7 +117,6 @@ export async function imageOptimizer(
return { finished: true }
}

const href = (absoluteUrl && absoluteUrl.toString()) || relativeUrl
const hash = getHash([CACHE_VERSION, href, width, quality, mimeType])
const imagesDir = join(distDir, 'cache', 'images')
const hashDir = join(imagesDir, hash)
Expand Down Expand Up @@ -140,8 +144,8 @@ export async function imageOptimizer(
let upstreamType: string | null
let maxAge: number

if (absoluteUrl) {
const upstreamRes = await fetch(absoluteUrl.toString())
if (isAbsolute) {
const upstreamRes = await fetch(href)
ijjk marked this conversation as resolved.
Show resolved Hide resolved

if (!upstreamRes.ok) {
res.statusCode = upstreamRes.status
Expand All @@ -158,7 +162,7 @@ export async function imageOptimizer(
const _req: any = {
headers: req.headers,
method: req.method,
url: relativeUrl,
url: href,
}
const resBuffers: Buffer[] = []
const mockRes: any = new Stream.Writable()
Expand Down Expand Up @@ -186,14 +190,10 @@ export async function imageOptimizer(
await server.getRequestHandler()(
_req,
mockRes,
nodeUrl.parse(relativeUrl!, true)
nodeUrl.parse(href!, true)
ijjk marked this conversation as resolved.
Show resolved Hide resolved
)
res.statusCode = mockRes.statusCode

// make sure to 404 for non-static file requests
if (!mockRes.servedStatic) {
throw new Error('non-static file requested')
}
upstreamBuffer = Buffer.concat(resBuffers)
upstreamType = mockRes.getHeader('Content-Type')
maxAge = getMaxAge(mockRes.getHeader('Cache-Control'))
Expand Down
2 changes: 0 additions & 2 deletions packages/next/next-server/server/next-server.ts
Expand Up @@ -1692,10 +1692,8 @@ export default class Server {
}

try {
;(res as any).servedStatic = true
await serveStatic(req, res, path)
} catch (err) {
;(res as any).servedStatic = false
if (err.code === 'ENOENT' || err.statusCode === 404) {
this.render404(req, res, parsedUrl)
} else if (err.statusCode === 412) {
Expand Down