Use charset of the response header in the initial TextDecoder (#1646)

This commit is contained in:
nobody 2024-09-23 08:37:26 +02:00
parent 6ec375a39f
commit e13060e929
No known key found for this signature in database
GPG Key ID: 8F6DE3D614FCFD7A
2 changed files with 5 additions and 3 deletions

View File

@ -52,7 +52,7 @@ manipulateDOM._removeCrossOriginAndIntegrityAttr = function (details) {
if (!isAllowlisted && mimeType === 'text/html') { if (!isAllowlisted && mimeType === 'text/html') {
let asciiDecoder, decoder, encoder, charset, isFirstData, filter, data; let initDecoder, decoder, encoder, charset, isFirstData, filter, data;
charset = (/charset\s*=/).test(header.value) && header.value.replace(/^.*?charset\s*=\s*/, '').replace(/["']?/g, ''); charset = (/charset\s*=/).test(header.value) && header.value.replace(/^.*?charset\s*=\s*/, '').replace(/["']?/g, '');
@ -63,7 +63,8 @@ manipulateDOM._removeCrossOriginAndIntegrityAttr = function (details) {
return; return;
} }
asciiDecoder = new TextDecoder('ASCII'); // Use charset of the response header in the initial TextDecoder. ASCII only as fallback.
initDecoder = new TextDecoder(charset === false ? 'ASCII' : charset);
encoder = new TextEncoder(); encoder = new TextEncoder();
isFirstData = true; isFirstData = true;
filter = browser.webRequest.filterResponseData(details.requestId); filter = browser.webRequest.filterResponseData(details.requestId);
@ -92,7 +93,7 @@ manipulateDOM._removeCrossOriginAndIntegrityAttr = function (details) {
uint8View = new Uint8Array(chunk); uint8View = new Uint8Array(chunk);
} }
htmlHead = asciiDecoder.decode(uint8View, {'stream': false}); htmlHead = initDecoder.decode(uint8View, {'stream': false});
// eslint-disable-next-line no-useless-escape // eslint-disable-next-line no-useless-escape
charsetMatch = htmlHead.match(/<meta\s+charset=["']?([^>"'\/]+)["'>\/]/i); charsetMatch = htmlHead.match(/<meta\s+charset=["']?([^>"'\/]+)["'>\/]/i);
if (charsetMatch === null) { if (charsetMatch === null) {

View File

@ -28,6 +28,7 @@
<p>Improved</p> <p>Improved</p>
<ul> <ul>
<li>redirect outdated paths of font-awesome</li> <li>redirect outdated paths of font-awesome</li>
<li>Use charset of the response header in the initial TextDecoder. (<a href="https://codeberg.org/nobody/LocalCDN/issues/1646">#1646</a>)</li>
</ul> </ul>
<p>Mapping</p> <p>Mapping</p>
<ul> <ul>