Check charset types in linkPreviewFetch.ts

This commit is contained in:
Fedor Indutny 2021-04-02 11:37:54 -07:00 committed by Josh Perez
parent bb412d61c2
commit 2796dfab23

View file

@ -180,6 +180,38 @@ const parseContentLength = (headerValue: string | null): number => {
return Number.isNaN(result) ? Infinity : result;
};
type ValidCharset =
| 'ascii'
| 'utf8'
| 'utf-8'
| 'utf16le'
| 'ucs2'
| 'ucs-2'
| 'base64'
| 'latin1'
| 'binary'
| 'hex';
const VALID_CHARSETS = new Set([
'ascii',
'utf8',
'utf-8',
'utf16le',
'ucs2',
'ucs-2',
'base64',
'latin1',
'binary',
'hex',
]);
const checkCharset = (charSet: string | null): charSet is ValidCharset => {
if (!charSet) {
return false;
}
return VALID_CHARSETS.has(charSet);
};
const emptyHtmlDocument = (): HTMLDocument =>
new DOMParser().parseFromString('', 'text/html');
@ -269,6 +301,9 @@ const getHtmlDocument = async (
// This check exists to satisfy TypeScript; chunk should always be a Buffer.
if (typeof chunk === 'string') {
if (!checkCharset(httpCharset)) {
throw new Error(`Invalid charset: ${httpCharset}`);
}
chunk = Buffer.from(chunk, httpCharset || 'utf8');
}