1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-05 11:37:01 +02:00

Add test to detect case of improper deserialization

Related commit:
- 8f461072f5
This commit is contained in:
Raymond Hill 2022-01-12 09:03:38 -05:00
parent a7ef3300fe
commit 0bc0af9d8d
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
4 changed files with 33 additions and 19 deletions

View File

@ -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();
}

View File

@ -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",

View File

@ -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 () => {

View File

@ -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 = {