Skip to content

Commit

Permalink
Fix potential infinite async loop
Browse files Browse the repository at this point in the history
Related issue:
https://bugzilla.mozilla.org/show_bug.cgi?id=1929326

As identified by @Rob--W:
https://bugzilla.mozilla.org/show_bug.cgi?id=1929326#c9

Truncated or otherwise corrupted asset content in extension storage
could lead to infinite async loop causing high CPU usage in uBO and
its workers.

Likely related to the issue of the asset content returned as
`undefined`:
https://github.com/gorhill/uBlock/blob/652f1787878ba434c3a65287afcd84082c409397/src/js/cachestorage.js#L98
  • Loading branch information
gorhill committed Nov 11, 2024
1 parent 15dae35 commit 335d947
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/js/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@
Home: https://github.com/gorhill/uBlock
*/

'use strict';

/******************************************************************************/
import * as sfp from './static-filtering-parser.js';

import µb from './background.js';
import { broadcast } from './broadcast.js';
import cacheStorage from './cachestorage.js';
import { ubolog } from './console.js';
import { i18n$ } from './i18n.js';
import logger from './logger.js';
import * as sfp from './static-filtering-parser.js';
import { orphanizeString, } from './text-utils.js';
import { orphanizeString } from './text-utils.js';
import { ubolog } from './console.js';
import µb from './background.js';

/******************************************************************************/

Expand All @@ -50,6 +47,9 @@ let remoteServerFriendly = false;

/******************************************************************************/

const hasOwnProperty = (o, p) =>
Object.prototype.hasOwnProperty.call(o, p);

const stringIsNotEmpty = s => typeof s === 'string' && s !== '';

const parseExpires = s => {
Expand Down Expand Up @@ -107,8 +107,8 @@ const resourceTimeFromXhr = xhr => {

const resourceTimeFromParts = (parts, time) => {
const goodParts = parts.filter(part => typeof part === 'object');
return goodParts.reduce((acc, part) =>
((part.resourceTime || 0) > acc ? part.resourceTime : acc),
return goodParts.reduce(
(acc, part) => ((part.resourceTime || 0) > acc ? part.resourceTime : acc),
time
);
};
Expand Down Expand Up @@ -246,6 +246,7 @@ const fireNotification = function(topic, details) {
assets.fetch = function(url, options = {}) {
return new Promise((resolve, reject) => {
// Start of executor
/* eslint-disable indent */

const timeoutAfter = µb.hiddenSettings.assetFetchTimeout || 30;
const xhr = new XMLHttpRequest();
Expand Down Expand Up @@ -322,6 +323,7 @@ assets.fetch = function(url, options = {}) {
onErrorEvent.call(xhr);
}

/* eslint-enable indent */
// End of executor
});
};
Expand Down Expand Up @@ -733,7 +735,7 @@ async function assetCacheRead(assetKey, updateReadTime = false) {
}

if ( bin instanceof Object === false ) { return reportBack(''); }
if ( bin.hasOwnProperty(internalKey) === false ) { return reportBack(''); }
if ( hasOwnProperty(bin, internalKey) === false ) { return reportBack(''); }

const entry = assetCacheRegistry[assetKey];
if ( entry === undefined ) { return reportBack(''); }
Expand Down Expand Up @@ -1277,7 +1279,9 @@ async function diffUpdater() {
if ( data.status === 'needtext' ) {
ubolog('Diff updater: need text for', data.assetKey);
assetCacheRead(data.assetKey).then(result => {
data.text = result.content;
// https://bugzilla.mozilla.org/show_bug.cgi?id=1929326#c9
// Must never be set to undefined!
data.text = result.content || '';
data.status = undefined;
checkAndCorrectDiffPath(data);
bc.postMessage(data);
Expand Down

0 comments on commit 335d947

Please sign in to comment.