Skip to content

Commit 8df875b

Browse files
authored
feat: bump imgproxy to v4 and adapt error mapping (#1788)
## What kind of change does this PR introduce? feat ## What is the current behavior? Local setup runs imgproxy `v3.26`. Storage forwards the client full `Accept` header and maps known imgproxy errors but some v4 error messages and development-mode stack traces do not match those mappings. ## What is the new behavior? Local setup runs imgproxy `v4.0.26` with updated configuration. Storage removes `text/html` from the `Accept` header sent to imgproxy so development errors stay in a format it can interpret. It also strips stack traces before mapping errors and recognizes additional v4 invalid-image and timeout messages to return clearer controlled errors to clients. Signed-off-by: Ferhat Elmas <elmas.ferhat@gmail.com>
1 parent 9e7630a commit 8df875b

4 files changed

Lines changed: 94 additions & 9 deletions

File tree

‎.docker/docker-compose-infra.yml‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,22 +172,24 @@ services:
172172
- "9324:9324"
173173

174174
imgproxy:
175-
image: darthsim/imgproxy:v3.26.0
175+
image: darthsim/imgproxy:v4.0.26
176176
ports:
177177
- "5020:8080"
178178
volumes:
179179
- ${PWD}/data:${PWD}/data
180180
environment:
181-
- IMGPROXY_WRITE_TIMEOUT=20
182-
- IMGPROXY_READ_TIMEOUT=20
181+
- IMGPROXY_TIMEOUT=20
182+
- IMGPROXY_READ_REQUEST_TIMEOUT=20
183183
- IMGPROXY_REQUESTS_QUEUE_SIZE=24
184184
- IMGPROXY_LOCAL_FILESYSTEM_ROOT=/
185185
- IMGPROXY_USE_ETAG=true
186-
- IMGPROXY_ENABLE_WEBP_DETECTION=true
186+
- IMGPROXY_USE_LAST_MODIFIED=false
187+
- IMGPROXY_AUTO_WEBP=true
187188
- IMGPROXY_ALLOW_SECURITY_OPTIONS=true
188189
- IMGPROXY_DEVELOPMENT_ERRORS_MODE=true
189190
- IMGPROXY_PRESETS=default=width:3000/height:8192
190191
- IMGPROXY_FORMAT_QUALITY=jpeg=80,avif=62,webp=80
192+
- IMGPROXY_AVIF_SPEED=9
191193

192194
rest-catalog:
193195
image: apache/iceberg-rest-fixture:1.10.1

‎acceptance/specs/render.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const onePixelPng = new Uint8Array(
2525
)
2626
)
2727
// Minimal ftypmif1 HEIF-family header without heic/avif compatible brands. imgproxy
28-
// v3.26 identifies it as HEIF-like, then rejects it as incompatible with heic/avif.
28+
// identifies it as HEIF-like, then rejects it as an invalid/incompatible source image.
2929
const incompatibleMif1Heif = new Uint8Array(Buffer.from('00000010667479706d69663100000000', 'hex'))
3030

3131
describeAcceptance(

‎src/storage/renderer/image.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,34 @@ describe('ImageRenderer fetch client', () => {
393393
}
394394
)
395395

396+
it('strips text/html from Accept headers forwarded to imgproxy', async () => {
397+
const fetchMock = stubSuccessfulImageFetch()
398+
399+
const { ImageRenderer } = await loadRendererModule()
400+
await new ImageRenderer(createBackend()).setTransformations({ width: 100 }).getAsset(
401+
createRequest({
402+
accept:
403+
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
404+
}),
405+
createRenderOptions()
406+
)
407+
408+
expect((fetchMock.mock.calls[0][1]?.headers as Headers)?.get('accept')).toBe(
409+
'application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8'
410+
)
411+
})
412+
413+
it('omits Accept header when client only sends text/html', async () => {
414+
const fetchMock = stubSuccessfulImageFetch()
415+
416+
const { ImageRenderer } = await loadRendererModule()
417+
await new ImageRenderer(createBackend())
418+
.setTransformations({ width: 100 })
419+
.getAsset(createRequest({ accept: 'text/html;q=0.9' }), createRenderOptions())
420+
421+
expect((fetchMock.mock.calls[0][1]?.headers as Headers)?.get('accept')).toBeNull()
422+
})
423+
396424
it('does not send Vary or forward Accept when the origin format is requested', async () => {
397425
const fetchMock = stubSuccessfulImageFetch()
398426

@@ -1830,6 +1858,20 @@ describe('ImageRenderer fetch client', () => {
18301858
[422, 'webp2vips: unable to read pixels'],
18311859
[422, 'gif2vips: unable to read header'],
18321860
[422, 'Broken or unsupported SVG image'],
1861+
[422, 'SVG syntax error'],
1862+
[422, 'Source image format is not supported: psd'],
1863+
[
1864+
500,
1865+
"can't download source image: webp: invalid format\n\n/app/imagedata/factory.go:175 github.com/imgproxy/imgproxy/v4/imagedata.(*Factory).DownloadAsync",
1866+
],
1867+
[
1868+
422,
1869+
'Invalid source image\n\n/app/imagetype/errors.go:25 github.com/imgproxy/imgproxy/v4/imagetype.newUnknownFormatError',
1870+
],
1871+
[
1872+
500,
1873+
'bad seek to 1024\nheif: Invalid input: Unspecified: File size too small. (2.0)\n\n/app/vips/vips.go:136 github.com/imgproxy/imgproxy/v4/vips.Error',
1874+
],
18331875
])(
18341876
'maps imgproxy source-image validation error %# (%i)',
18351877
async (upstreamStatusCode, body, expectedMessage = 'The source image is invalid or unsupported for rendering') => {
@@ -1871,6 +1913,20 @@ describe('ImageRenderer fetch client', () => {
18711913
[429, 'Too many requests', 429, 400, 'Too many requests'],
18721914
[429, 'Too many requests \t', 429, 400, 'Too many requests'],
18731915
[503, 'Timeout', 503, 400, 'Image request timed out'],
1916+
[
1917+
503,
1918+
'Request was timed out after 20s\n\n/app/server/errors.go:88 github.com/imgproxy/imgproxy/v4/server.newRequestTimeoutError',
1919+
503,
1920+
400,
1921+
'Image request timed out',
1922+
],
1923+
[
1924+
404,
1925+
'Source is unreachable\n\n/app/fetcher/errors.go:34 github.com/imgproxy/imgproxy/v4/fetcher.newRequestError',
1926+
404,
1927+
400,
1928+
'Unable to download source image',
1929+
],
18741930
[422, '<html>upstream debug</html>', 422, 400, 'Invalid image request'],
18751931
[502, '<html>upstream failure</html>', 502, 400, 'Internal error'],
18761932
[500, 'heif: Memory allocation error', 500, 500, 'Internal error'],

‎src/storage/renderer/image.ts‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const IMGPROXY_SOURCE_IMAGE_BAD_REQUESTS = [
7777
},
7878
{
7979
message: IMGPROXY_SOURCE_IMAGE_INVALID_OR_UNSUPPORTED_MESSAGE,
80-
pattern: /Source image type not supported/i,
80+
pattern: /Source image (?:type|format) (?:is )?not supported/i,
8181
},
8282
{
8383
message: IMGPROXY_SOURCE_IMAGE_INVALID_OR_UNSUPPORTED_MESSAGE,
@@ -117,6 +117,10 @@ const IMGPROXY_SOURCE_IMAGE_BAD_REQUESTS = [
117117
message: IMGPROXY_SOURCE_IMAGE_INVALID_OR_UNSUPPORTED_MESSAGE,
118118
pattern: /^Broken or unsupported (?:SVG )?image$/i,
119119
},
120+
{
121+
message: IMGPROXY_SOURCE_IMAGE_INVALID_OR_UNSUPPORTED_MESSAGE,
122+
pattern: /^SVG syntax error$/i,
123+
},
120124
{
121125
message: 'The source image resolution is too large to process',
122126
pattern: /Source image resolution is too big/i,
@@ -165,7 +169,7 @@ const IMGPROXY_PUBLIC_ERRORS = [
165169
},
166170
{
167171
message: IMGPROXY_REQUEST_TIMED_OUT_MESSAGE,
168-
pattern: /^Timeout$/i,
172+
pattern: /^(?:Timeout|Request was timed out\b.*)$/i,
169173
},
170174
] as const
171175

@@ -583,7 +587,9 @@ export class ImageRenderer extends Renderer {
583587
let assetBody: Readable | undefined
584588

585589
try {
586-
const acceptHeader = formatFollowsAccept ? request.headers['accept'] : undefined
590+
const acceptHeader = formatFollowsAccept
591+
? sanitizeImgProxyAcceptHeader(request.headers['accept'])
592+
: undefined
587593

588594
const response = await this.getClient().get(url.join('/'), {
589595
signal: options.signal,
@@ -674,7 +680,7 @@ export class ImageRenderer extends Renderer {
674680

675681
const processingError = getImageProcessingError(
676682
error.response?.status || 500,
677-
errorResponse.trim() || error.message
683+
normalizeImgProxyErrorMessage(errorResponse) || error.message
678684
)
679685
return ERRORS.ImageProcessingError(processingError.statusCode, processingError.message)
680686
}
@@ -688,6 +694,27 @@ function isValidFocalPointCoordinate(value: number | undefined): value is number
688694
return value !== undefined && Number.isFinite(value) && value >= 0 && value <= 1
689695
}
690696

697+
const IMGPROXY_STACK_TRACE_SEPARATOR = /\r?\n\r?\n(?=\S+\.go:\d+\s)/
698+
699+
function sanitizeImgProxyAcceptHeader(accept: string | string[] | undefined): string | undefined {
700+
if (!accept) {
701+
return undefined
702+
}
703+
704+
// Strip text/html so imgproxy v4 development errors mode never returns an HTML error page.
705+
const raw = Array.isArray(accept) ? accept.join(', ') : accept
706+
const filtered = raw
707+
.split(',')
708+
.map((part) => part.trim())
709+
.filter((part) => part.length > 0 && !/text\/html/i.test(part))
710+
711+
return filtered.length > 0 ? filtered.join(',') : undefined
712+
}
713+
714+
function normalizeImgProxyErrorMessage(raw: string): string {
715+
return raw.split(IMGPROXY_STACK_TRACE_SEPARATOR, 1)[0].trim()
716+
}
717+
691718
function getImageProcessingError(statusCode: number, message: string) {
692719
const sourceImageError = getImgProxySourceImageValidationError(message)
693720
if (sourceImageError) {

0 commit comments

Comments
 (0)