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

fix: fix handling of corrupted images #205

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 11 additions & 20 deletions src/upload.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import fs from 'fs';
import express from 'express';
import multer from 'multer';
import sharp from 'sharp';
import { capture } from '@snapshot-labs/snapshot-sentry';
import { rpcError, rpcSuccess } from './utils';
import { rpcError, rpcSuccess, processImage } from './utils';
import uploadToProviders from './providers/';
import { IMAGE_PROVIDERS } from './providers/utils';

const MAX_INPUT_SIZE = 1024 * 1024;
const MAX_IMAGE_DIMENSION = 1500;

const router = express.Router();
const upload = multer({
Expand All @@ -22,31 +20,24 @@ router.post('/upload', async (req, res) => {
if (err) return rpcError(res, 400, err.message);
if (!req.file) return rpcError(res, 400, 'No file submitted');

const transformer = sharp()
.resize({
width: MAX_IMAGE_DIMENSION,
height: MAX_IMAGE_DIMENSION,
fit: 'inside'
})
.webp({ lossless: true });

const buffer = await fs
.createReadStream(req.file?.path as string)
.pipe(transformer)
.toBuffer();
let processedImage: Buffer;
try {
processedImage = await processImage(req.file?.path);
} catch (e: any) {
if (e.message === 'Input buffer contains unsupported image format') {
return rpcError(res, 415, 'Unsupported file type');
}
throw e;
}

const result = await uploadToProviders(IMAGE_PROVIDERS, 'image', buffer);
const result = await uploadToProviders(IMAGE_PROVIDERS, 'image', processedImage);
const file = {
cid: result.cid,
provider: result.provider
};

return rpcSuccess(res, file);
} catch (e: any) {
if (e.message === 'Input buffer contains unsupported image format') {
return rpcError(res, 415, 'Unsupported file type');
}

capture(e);
return rpcError(res, 500, e);
} finally {
Expand Down
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import sharp from 'sharp';
import fs from 'fs';
import { createHash } from 'crypto';
import { Response } from 'express';

export const MAX = 10e4;
const MAX_IMAGE_DIMENSION = 1500;

export function rpcSuccess(res: Response, result: any, id = '') {
res.json({
Expand All @@ -26,3 +29,15 @@ export function rpcError(res: Response, code: number, e: Error | string, id = nu
export function sha256(input: string | Buffer) {
return createHash('sha256').update(input).digest('hex');
}

export async function processImage(path: string) {
const transformer = sharp({ failOnError: false })
.resize({
width: MAX_IMAGE_DIMENSION,
height: MAX_IMAGE_DIMENSION,
fit: 'inside'
})
.webp({ lossless: true });

return await fs.createReadStream(path).pipe(transformer).toBuffer();
}
Binary file added test/unit/providers/fixtures/corrupted.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions test/unit/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sharp from 'sharp';
import path from 'path';
import { processImage } from '../../src/utils';

describe('utils', () => {
describe('processImage()', () => {
const valid_image_path = path.join(__dirname, './providers/fixtures/sample.webp');
const corrupted_image_path = path.join(__dirname, './providers/fixtures/corrupted.jpeg');

it('returns a buffer', () => {
expect(processImage(valid_image_path)).resolves.toBeInstanceOf(Buffer);
});

it('resizes the image to fit the max dimensions', async () => {
const result = await processImage(valid_image_path);
const metadata = await sharp(result).metadata();

expect(metadata.width).toBe(1500);
return expect(metadata.height).toBe(1004);
});

describe('on a corrupted image', () => {
it('ignores errors and returns the image buffer', async () => {
expect(processImage(corrupted_image_path)).resolves.toBeInstanceOf(Buffer);
});
});
});
});
Loading