From 0bc0af9d8daf213a8251485df238f2d6b57f41af Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 12 Jan 2022 09:03:38 -0500 Subject: [PATCH] Add test to detect case of improper deserialization Related commit: - https://github.com/gorhill/uBlock/commit/8f461072f576cdf72c088a952ef342281a7c44d6 --- platform/nodejs/index.js | 6 ++++++ platform/npm/package.json | 2 +- platform/npm/tests/snfe.js | 19 ++++++++++++++++++- src/js/static-net-filtering.js | 25 ++++++++----------------- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/platform/nodejs/index.js b/platform/nodejs/index.js index 87cc45123..bdd0e5cf0 100644 --- a/platform/nodejs/index.js +++ b/platform/nodejs/index.js @@ -218,6 +218,12 @@ class StaticNetFilteringEngine { return snfe.hasQuery(details); } + filterQuery(details) { + const directives = snfe.filterQuery(fctx.fromDetails(details)); + if ( directives === undefined ) { return; } + return { redirectURL: fctx.redirectURL, directives }; + } + isBlockImportant() { return snfe.isBlockImportant(); } diff --git a/platform/npm/package.json b/platform/npm/package.json index d4c2d6c12..73caefc2a 100644 --- a/platform/npm/package.json +++ b/platform/npm/package.json @@ -1,6 +1,6 @@ { "name": "@gorhill/ubo-core", - "version": "0.1.23", + "version": "0.1.24", "description": "To create a working instance of uBlock Origin's static network filtering engine", "type": "module", "main": "index.js", diff --git a/platform/npm/tests/snfe.js b/platform/npm/tests/snfe.js index 25c4f9f31..f0ee86b57 100644 --- a/platform/npm/tests/snfe.js +++ b/platform/npm/tests/snfe.js @@ -231,8 +231,25 @@ describe('SNFE', () => { const serialized = await engine.serialize(); await engine.deserialize(serialized); }); - }); + // https://github.com/gorhill/uBlock/commit/8f461072f576cdf72c088a952ef342281a7c44d6 + it('should correctly remove query parameter following deserialization', async () => { + await engine.useLists([ + { name: 'custom', raw: '*$removeparam=/^utm_/' }, + ]); + const request = { + originURL: 'https://www.example.com/?utm_source=1', + type: 'document', + url: 'https://www.example.com/?utm_source=1', + }; + let result = engine.filterQuery(request); + assert.strictEqual(result.redirectURL, 'https://www.example.com/'); + const serialized = await engine.serialize(); + await engine.deserialize(serialized); + result = engine.filterQuery(request); + assert.strictEqual(result.redirectURL, 'https://www.example.com/'); + }); + }); describe('Filter matching', () => { beforeEach(async () => { diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index eaa09d38f..e26097dfb 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -440,7 +440,7 @@ function filterRefsToSelfie() { refs.push({ t: 2, v }); continue; } - if ( v instanceof Object === false ) { + if ( typeof v !== 'object' || v === null ) { refs.push({ t: 0, v }); continue; } @@ -3772,13 +3772,9 @@ FilterContainer.prototype.optimize = function(throttle = 0) { /******************************************************************************/ -FilterContainer.prototype.toSelfie = function(storage, path) { - if ( - storage instanceof Object === false || - storage.put instanceof Function === false - ) { - return Promise.resolve(); - } +FilterContainer.prototype.toSelfie = async function(storage, path) { + if ( typeof storage !== 'object' || storage === null ) { return; } + if ( typeof storage.put !== 'function' ) { return; } const bucketsToSelfie = ( ) => { const selfie = []; @@ -3844,12 +3840,8 @@ FilterContainer.prototype.serialize = async function() { /******************************************************************************/ FilterContainer.prototype.fromSelfie = async function(storage, path) { - if ( - storage instanceof Object === false || - storage.get instanceof Function === false - ) { - return; - } + if ( typeof storage !== 'object' || storage === null ) { return; } + if ( typeof storage.get !== 'function' ) { return; } this.reset(); @@ -3881,7 +3873,7 @@ FilterContainer.prototype.fromSelfie = async function(storage, path) { }; const details = results[0]; - if ( details instanceof Object === false ) { return false; } + if ( typeof details !== 'object' || details === null ) { return false; } if ( typeof details.content !== 'string' ) { return false; } if ( details.content === '' ) { return false; } let selfie; @@ -3889,7 +3881,7 @@ FilterContainer.prototype.fromSelfie = async function(storage, path) { selfie = JSON.parse(details.content); } catch (ex) { } - if ( selfie instanceof Object === false ) { return false; } + if ( typeof selfie !== 'object' || selfie === null ) { return false; } if ( selfie.version !== this.selfieVersion ) { return false; } this.processedFilterCount = selfie.processedFilterCount; this.acceptedCount = selfie.acceptedCount; @@ -3900,7 +3892,6 @@ FilterContainer.prototype.fromSelfie = async function(storage, path) { return true; }; - FilterContainer.prototype.unserialize = async function(s) { const selfie = new Map(JSON.parse(s)); const storage = {