Unverified Commit 6549fd7c authored by Steven's avatar Steven Committed by GitHub

[16.3.x] next/image: reject empty image on read/write to disk cache (#98186)

Backports https://github.com/vercel/next.js/pull/97278 to 16.3
parent d9eac96e
...@@ -1466,5 +1466,7 @@ ...@@ -1466,5 +1466,7 @@
"1465": "\\`experimental.useCache\\` cannot be disabled when \\`cacheComponents\\` is enabled, because Cache Components relies on the \\`\"use cache\"\\` directive. Please remove it from %s.", "1465": "\\`experimental.useCache\\` cannot be disabled when \\`cacheComponents\\` is enabled, because Cache Components relies on the \\`\"use cache\"\\` directive. Please remove it from %s.",
"1466": "TypeScript %s does not provide the compiler API required by Next.js. Set %s to true in your Next.js config to use the TypeScript CLI, or install TypeScript 6 instead.", "1466": "TypeScript %s does not provide the compiler API required by Next.js. Set %s to true in your Next.js config to use the TypeScript CLI, or install TypeScript 6 instead.",
"1467": "TypeScript %s does not provide the compiler API required by Next.js. Set %s back to true in your Next.js config to use the TypeScript CLI, or install TypeScript 6 instead.", "1467": "TypeScript %s does not provide the compiler API required by Next.js. Set %s back to true in your Next.js config to use the TypeScript CLI, or install TypeScript 6 instead.",
"1468": "Invalid file path: %s" "1468": "Invalid file path: %s",
"1469": "Invariant: image cache entry \"%s\" is empty",
"1470": "Invariant: cannot write an empty buffer to the image cache"
} }
...@@ -188,6 +188,12 @@ async function writeToCacheDir( ...@@ -188,6 +188,12 @@ async function writeToCacheDir(
etag: string, etag: string,
upstreamEtag: 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 dir = join(/* turbopackIgnore: true */ cacheDir, cacheKey)
const filename = join( const filename = join(
/* turbopackIgnore: true */ /* turbopackIgnore: true */
...@@ -216,6 +222,9 @@ async function readFromCacheDir(cacheDir: string, cacheKey: string) { ...@@ -216,6 +222,9 @@ async function readFromCacheDir(cacheDir: string, cacheKey: string) {
) )
const filePath = join(/* turbopackIgnore: true */ dir, file) const filePath = join(/* turbopackIgnore: true */ dir, file)
const buffer = await promises.readFile(/* turbopackIgnore: true */ filePath) 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 expireAt = Number(expireAtSt)
const maxAge = Number(maxAgeSt) const maxAge = Number(maxAgeSt)
return { maxAge, expireAt, etag, upstreamEtag, buffer, extension } return { maxAge, expireAt, etag, upstreamEtag, buffer, extension }
......
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