Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .docker/docker-compose-infra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,24 @@ services:
- "9324:9324"

imgproxy:
image: darthsim/imgproxy:v3.26.0
image: darthsim/imgproxy:v4.0.26
ports:
- "5020:8080"
volumes:
- ${PWD}/data:${PWD}/data
environment:
- IMGPROXY_WRITE_TIMEOUT=20
- IMGPROXY_READ_TIMEOUT=20
- IMGPROXY_TIMEOUT=20
- IMGPROXY_READ_REQUEST_TIMEOUT=20
- IMGPROXY_REQUESTS_QUEUE_SIZE=24
- IMGPROXY_LOCAL_FILESYSTEM_ROOT=/
- IMGPROXY_USE_ETAG=true
- IMGPROXY_ENABLE_WEBP_DETECTION=true
- IMGPROXY_USE_LAST_MODIFIED=false
- IMGPROXY_AUTO_WEBP=true
- IMGPROXY_ALLOW_SECURITY_OPTIONS=true
- IMGPROXY_DEVELOPMENT_ERRORS_MODE=true
- IMGPROXY_PRESETS=default=width:3000/height:8192
- IMGPROXY_FORMAT_QUALITY=jpeg=80,avif=62,webp=80
- IMGPROXY_AVIF_SPEED=9

rest-catalog:
image: apache/iceberg-rest-fixture:1.10.1
Expand Down
2 changes: 1 addition & 1 deletion acceptance/specs/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const onePixelPng = new Uint8Array(
)
)
// Minimal ftypmif1 HEIF-family header without heic/avif compatible brands. imgproxy
// v3.26 identifies it as HEIF-like, then rejects it as incompatible with heic/avif.
// identifies it as HEIF-like, then rejects it as an invalid/incompatible source image.
const incompatibleMif1Heif = new Uint8Array(Buffer.from('00000010667479706d69663100000000', 'hex'))

describeAcceptance(
Expand Down
56 changes: 56 additions & 0 deletions src/storage/renderer/image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,34 @@ describe('ImageRenderer fetch client', () => {
}
)

it('strips text/html from Accept headers forwarded to imgproxy', async () => {
const fetchMock = stubSuccessfulImageFetch()

const { ImageRenderer } = await loadRendererModule()
await new ImageRenderer(createBackend()).setTransformations({ width: 100 }).getAsset(
createRequest({
accept:
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
}),
createRenderOptions()
)

expect((fetchMock.mock.calls[0][1]?.headers as Headers)?.get('accept')).toBe(
'application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8'
)
})

it('omits Accept header when client only sends text/html', async () => {
const fetchMock = stubSuccessfulImageFetch()

const { ImageRenderer } = await loadRendererModule()
await new ImageRenderer(createBackend())
.setTransformations({ width: 100 })
.getAsset(createRequest({ accept: 'text/html;q=0.9' }), createRenderOptions())

expect((fetchMock.mock.calls[0][1]?.headers as Headers)?.get('accept')).toBeNull()
})

it('does not send Vary or forward Accept when the origin format is requested', async () => {
const fetchMock = stubSuccessfulImageFetch()

Expand Down Expand Up @@ -1830,6 +1858,20 @@ describe('ImageRenderer fetch client', () => {
[422, 'webp2vips: unable to read pixels'],
[422, 'gif2vips: unable to read header'],
[422, 'Broken or unsupported SVG image'],
[422, 'SVG syntax error'],
[422, 'Source image format is not supported: psd'],
[
500,
"can't download source image: webp: invalid format\n\n/app/imagedata/factory.go:175 github.com/imgproxy/imgproxy/v4/imagedata.(*Factory).DownloadAsync",
],
[
422,
'Invalid source image\n\n/app/imagetype/errors.go:25 github.com/imgproxy/imgproxy/v4/imagetype.newUnknownFormatError',
],
[
500,
'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',
],
])(
'maps imgproxy source-image validation error %# (%i)',
async (upstreamStatusCode, body, expectedMessage = 'The source image is invalid or unsupported for rendering') => {
Expand Down Expand Up @@ -1871,6 +1913,20 @@ describe('ImageRenderer fetch client', () => {
[429, 'Too many requests', 429, 400, 'Too many requests'],
[429, 'Too many requests \t', 429, 400, 'Too many requests'],
[503, 'Timeout', 503, 400, 'Image request timed out'],
[
503,
'Request was timed out after 20s\n\n/app/server/errors.go:88 github.com/imgproxy/imgproxy/v4/server.newRequestTimeoutError',
503,
400,
'Image request timed out',
],
[
404,
'Source is unreachable\n\n/app/fetcher/errors.go:34 github.com/imgproxy/imgproxy/v4/fetcher.newRequestError',
404,
400,
'Unable to download source image',
],
[422, '<html>upstream debug</html>', 422, 400, 'Invalid image request'],
[502, '<html>upstream failure</html>', 502, 400, 'Internal error'],
[500, 'heif: Memory allocation error', 500, 500, 'Internal error'],
Expand Down
35 changes: 31 additions & 4 deletions src/storage/renderer/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const IMGPROXY_SOURCE_IMAGE_BAD_REQUESTS = [
},
{
message: IMGPROXY_SOURCE_IMAGE_INVALID_OR_UNSUPPORTED_MESSAGE,
pattern: /Source image type not supported/i,
pattern: /Source image (?:type|format) (?:is )?not supported/i,
},
{
message: IMGPROXY_SOURCE_IMAGE_INVALID_OR_UNSUPPORTED_MESSAGE,
Expand Down Expand Up @@ -117,6 +117,10 @@ const IMGPROXY_SOURCE_IMAGE_BAD_REQUESTS = [
message: IMGPROXY_SOURCE_IMAGE_INVALID_OR_UNSUPPORTED_MESSAGE,
pattern: /^Broken or unsupported (?:SVG )?image$/i,
},
{
message: IMGPROXY_SOURCE_IMAGE_INVALID_OR_UNSUPPORTED_MESSAGE,
pattern: /^SVG syntax error$/i,
},
{
message: 'The source image resolution is too large to process',
pattern: /Source image resolution is too big/i,
Expand Down Expand Up @@ -165,7 +169,7 @@ const IMGPROXY_PUBLIC_ERRORS = [
},
{
message: IMGPROXY_REQUEST_TIMED_OUT_MESSAGE,
pattern: /^Timeout$/i,
pattern: /^(?:Timeout|Request was timed out\b.*)$/i,
},
] as const

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

try {
const acceptHeader = formatFollowsAccept ? request.headers['accept'] : undefined
const acceptHeader = formatFollowsAccept
? sanitizeImgProxyAcceptHeader(request.headers['accept'])
: undefined

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

const processingError = getImageProcessingError(
error.response?.status || 500,
errorResponse.trim() || error.message
normalizeImgProxyErrorMessage(errorResponse) || error.message
)
return ERRORS.ImageProcessingError(processingError.statusCode, processingError.message)
}
Expand All @@ -688,6 +694,27 @@ function isValidFocalPointCoordinate(value: number | undefined): value is number
return value !== undefined && Number.isFinite(value) && value >= 0 && value <= 1
}

const IMGPROXY_STACK_TRACE_SEPARATOR = /\r?\n\r?\n(?=\S+\.go:\d+\s)/

function sanitizeImgProxyAcceptHeader(accept: string | string[] | undefined): string | undefined {
if (!accept) {
return undefined
}

// Strip text/html so imgproxy v4 development errors mode never returns an HTML error page.
const raw = Array.isArray(accept) ? accept.join(', ') : accept
const filtered = raw
.split(',')
.map((part) => part.trim())
.filter((part) => part.length > 0 && !/text\/html/i.test(part))

return filtered.length > 0 ? filtered.join(',') : undefined
}

function normalizeImgProxyErrorMessage(raw: string): string {
return raw.split(IMGPROXY_STACK_TRACE_SEPARATOR, 1)[0].trim()
}

function getImageProcessingError(statusCode: number, message: string) {
const sourceImageError = getImgProxySourceImageValidationError(message)
if (sourceImageError) {
Expand Down