Skip to content

Commit

Permalink
Add test to detect case of improper deserialization
Browse files Browse the repository at this point in the history
Related commit:
- gorhill@8f46107
  • Loading branch information
gorhill committed Jan 12, 2022
1 parent a7ef330 commit 0bc0af9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
6 changes: 6 additions & 0 deletions platform/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion platform/npm/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
19 changes: 18 additions & 1 deletion platform/npm/tests/snfe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
25 changes: 8 additions & 17 deletions src/js/static-net-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -3881,15 +3873,15 @@ 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;
try {
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;
Expand All @@ -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 = {
Expand Down

0 comments on commit 0bc0af9

Please sign in to comment.