Outbound link previews: more lenient loading

Signed-off-by: Josh Perez <josh.p@signal.org>
This commit is contained in:
Evan Hahn 2020-10-12 16:58:49 -05:00 committed by Josh Perez
parent 75c7c526a6
commit dd57963dab
3 changed files with 5 additions and 54 deletions

View File

@ -20,9 +20,9 @@ const MAX_CONTENT_TYPE_LENGTH_TO_PARSE = 100;
// Though we'll accept HTML of any Content-Length (including no specified length), we
// will only load some of the HTML. So we might start loading a 99 gigabyte HTML page
// but only parse the first 100 kilobytes. However, if the Content-Length is less than
// but only parse the first 500 kilobytes. However, if the Content-Length is less than
// this, we won't waste space.
const MAX_HTML_BYTES_TO_LOAD = 100 * 1024;
const MAX_HTML_BYTES_TO_LOAD = 500 * 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.
@ -280,10 +280,6 @@ const getHtmlDocument = async (
if (hasLoadedMaxBytes) {
break;
}
const hasFinishedLoadingHead = result.body.innerHTML.length > 0;
if (hasFinishedLoadingHead) {
break;
}
}
/* eslint-enable no-restricted-syntax */
} catch (err) {

View File

@ -720,33 +720,6 @@ describe('link preview fetching', () => {
);
});
it('stops reading as soon as the <body> starts', async () => {
const shouldNeverBeCalled = sinon.stub();
const fakeFetch = stub().resolves(
makeResponse({
body: (async function* body() {
yield new TextEncoder().encode(
'<!doctype html><head><title>foo bar</title></head><body>X'
);
shouldNeverBeCalled();
})(),
})
);
assert.propertyVal(
await fetchLinkPreviewMetadata(
fakeFetch,
'https://example.com',
new AbortController().signal
),
'title',
'foo bar'
);
sinon.assert.notCalled(shouldNeverBeCalled);
});
it('handles incomplete bodies', async () => {
const fakeFetch = stub().resolves(
makeResponse({
@ -803,7 +776,7 @@ describe('link preview fetching', () => {
sinon.assert.notCalled(shouldNeverBeCalled);
});
it('stops reading gigantic bodies after 100 kilobytes', async () => {
it('stops reading bodies after 500 kilobytes', async () => {
const shouldNeverBeCalled = sinon.stub();
const fakeFetch = stub().resolves(
@ -813,7 +786,7 @@ describe('link preview fetching', () => {
'<!doctype html><head><title>foo bar</title>'
);
const spaces = new Uint8Array(1024).fill(32);
for (let i = 0; i < 100; i += 1) {
for (let i = 0; i < 500; i += 1) {
yield spaces;
}
shouldNeverBeCalled();

View File

@ -13148,24 +13148,6 @@
"updated": "2019-11-21T06:13:49.384Z",
"reasonDetail": "Used for setting focus only"
},
{
"rule": "DOM-innerHTML",
"path": "ts/linkPreviews/linkPreviewFetch.js",
"line": " const hasFinishedLoadingHead = result.body.innerHTML.length > 0;",
"lineNumber": 212,
"reasonCategory": "usageTrusted",
"updated": "2020-09-09T21:20:16.643Z",
"reasonDetail": "This only deals with a fake DOM used when parsing link preview HTML, and it doesn't even change innerHTML."
},
{
"rule": "DOM-innerHTML",
"path": "ts/linkPreviews/linkPreviewFetch.ts",
"line": " const hasFinishedLoadingHead = result.body.innerHTML.length > 0;",
"lineNumber": 283,
"reasonCategory": "usageTrusted",
"updated": "2020-09-09T21:20:16.643Z",
"reasonDetail": "This only deals with a fake DOM used when parsing link preview HTML, and it doesn't even change innerHTML."
},
{
"rule": "jQuery-wrap(",
"path": "ts/shims/textsecure.js",
@ -13344,4 +13326,4 @@
"reasonCategory": "falseMatch",
"updated": "2020-09-08T23:07:22.682Z"
}
]
]