From 7ab5aa052233607e201faf883a9edc9e3544924b Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Fri, 21 May 2021 12:53:14 +0100 Subject: [PATCH] fix: catch oversize image response --- src/lib/templates/imageFunction.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib/templates/imageFunction.js b/src/lib/templates/imageFunction.js index 7be53b6251..6b39bfb186 100644 --- a/src/lib/templates/imageFunction.js +++ b/src/lib/templates/imageFunction.js @@ -4,6 +4,9 @@ const fetch = require('node-fetch') const imageType = require('image-type') const isSvg = require('is-svg') +// 6MB is hard max Lambda response size +const MAX_RESPONSE_SIZE = 6291456 + function getImageType(buffer) { const type = imageType(buffer) if (type) { @@ -101,6 +104,13 @@ const handler = async (event) => { .resize(width) .toBuffer({ resolveWithObject: true }) + if (imageBuffer.length > MAX_RESPONSE_SIZE) { + return { + statusCode: 400, + body: 'Requested image is too large. Maximum size is 6MB.', + } + } + return { statusCode: 200, headers: {