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] Oembed request not respecting payload limit #24418

Merged
merged 2 commits into from
Feb 7, 2022
Merged
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
15 changes: 12 additions & 3 deletions app/oembed/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const getUrlContent = async function (urlObj, redirectCount = 5) {

const url = URL.format(data.urlObj);

const chunks = [];
const sizeLimit = 250000;

const response = await fetch(url, {
compress: true,
Expand All @@ -108,17 +108,26 @@ const getUrlContent = async function (urlObj, redirectCount = 5) {
'User-Agent': `${settings.get('API_Embed_UserAgent')} Rocket.Chat/${Info.version}`,
'Accept-Language': settings.get('Language') || 'en',
},
size: 250000, // max size of the response body
size: sizeLimit, // max size of the response body, this was not working as expected so I'm also manually verifying that on the iterator
...(settings.get('Allow_Invalid_SelfSigned_Certs') && {
agent: getUnsafeAgent(parsedUrl.protocol),
}),
});

let totalSize = 0;
const chunks = [];
for await (const chunk of response.body) {
totalSize += chunk.length;
chunks.push(chunk);

if (totalSize > sizeLimit) {
SystemLogger.info({ msg: 'OEmbed request size exceeded', url });
break;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't found any issue on breaking the async iterator loop this way.
not sure if this could produce a memory though

}
}

const buffer = Buffer.concat(chunks);

return {
headers: Object.fromEntries(response.headers),
body: toUtf8(response.headers.get('content-type'), buffer),
Expand Down Expand Up @@ -209,7 +218,7 @@ OEmbed.getUrlMetaWithCache = async function (url, withFragment) {
try {
await OEmbedCache.createWithIdAndData(url, data);
} catch (_error) {
SystemLogger.error('OEmbed duplicated record', url);
SystemLogger.error({ msg: 'OEmbed duplicated record', url });
}
return data;
}
Expand Down