mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2026-02-15 07:28:59 +00:00
Simplify link preview image fetching
Co-authored-by: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com>
This commit is contained in:
@@ -35,6 +35,8 @@ const MAX_CONTENT_TYPE_LENGTH_TO_PARSE = 100;
|
||||
// this, we won't waste space.
|
||||
const MAX_HTML_BYTES_TO_LOAD = 1000 * 1024;
|
||||
|
||||
const MAX_IMAGE_BYTES_TO_LOAD = 1024 * 1024;
|
||||
|
||||
// `<title>x` is 8 bytes. Nothing else (meta tags, etc) will even fit, so we can ignore
|
||||
// it. This is mostly to protect us against empty response bodies.
|
||||
const MIN_HTML_CONTENT_LENGTH = 8;
|
||||
@@ -42,7 +44,6 @@ const MIN_HTML_CONTENT_LENGTH = 8;
|
||||
// Similar to the above. We don't want to show tiny images (even though the more likely
|
||||
// case is that the Content-Length is 0).
|
||||
const MIN_IMAGE_CONTENT_LENGTH = 8;
|
||||
const MAX_IMAGE_CONTENT_LENGTH = 1024 * 1024;
|
||||
const VALID_IMAGE_MIME_TYPES: Set<MIMEType> = new Set([
|
||||
IMAGE_GIF,
|
||||
IMAGE_ICO,
|
||||
@@ -455,6 +456,7 @@ export async function fetchLinkPreviewMetadata(
|
||||
'User-Agent': USER_AGENT,
|
||||
},
|
||||
signal: abortSignal,
|
||||
size: Math.max(MAX_HTML_BYTES_TO_LOAD, MAX_IMAGE_BYTES_TO_LOAD),
|
||||
},
|
||||
logger
|
||||
);
|
||||
@@ -567,7 +569,7 @@ export async function fetchLinkPreviewImage(
|
||||
headers: {
|
||||
'User-Agent': USER_AGENT,
|
||||
},
|
||||
size: MAX_IMAGE_CONTENT_LENGTH,
|
||||
size: MAX_IMAGE_BYTES_TO_LOAD,
|
||||
signal: abortSignal,
|
||||
},
|
||||
logger
|
||||
@@ -603,12 +605,6 @@ async function processImageResponse(
|
||||
logger.warn('fetchLinkPreviewImage: Content-Length is too short; bailing');
|
||||
return null;
|
||||
}
|
||||
if (contentLength > MAX_IMAGE_CONTENT_LENGTH) {
|
||||
logger.warn(
|
||||
'fetchLinkPreviewImage: Content-Length is too large or is unset; bailing'
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
const { type: contentType } = parseContentType(
|
||||
response.headers.get('Content-Type')
|
||||
|
||||
@@ -1319,32 +1319,6 @@ describe('link preview fetching', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('returns null if the response is too large', async () => {
|
||||
const fakeFetch = stub().resolves(
|
||||
new Response(await readFixtureImage('kitten-1-64-64.jpg'), {
|
||||
headers: {
|
||||
'Content-Type': 'image/jpeg',
|
||||
'Content-Length': '123456789',
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
assert.isNull(
|
||||
await fetchLinkPreviewImage(
|
||||
fakeFetch,
|
||||
'https://example.com/img',
|
||||
new AbortController().signal,
|
||||
logger
|
||||
)
|
||||
);
|
||||
|
||||
sinon.assert.calledOnce(warn);
|
||||
sinon.assert.calledWith(
|
||||
warn,
|
||||
'fetchLinkPreviewImage: Content-Length is too large or is unset; bailing'
|
||||
);
|
||||
});
|
||||
|
||||
it('returns null if the Content-Type is not a valid image', async () => {
|
||||
const fixture = await readFixtureImage('kitten-1-64-64.jpg');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user