Skip to content
This repository has been archived by the owner. It is now read-only.

Add an option to cancel a request #197

Merged
merged 5 commits into from Mar 19, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -9,6 +9,7 @@
#include <fstream>
#include "./bad_fingerprint.h"
#include "./data_file_version.h"
#include "./filter.h"
#include "./filter_list.h"
#include "./lists/regions.h"
#include "./lists/malware.h"
@@ -135,51 +136,65 @@ void AdBlockClientWrap::Init(Local<Object> exports) {
// filter options
Local<Object> filterOptions = Object::New(isolate);
filterOptions->Set(String::NewFromUtf8(isolate, "noFilterOption"),
Int32::New(isolate, 0));
Int32::New(isolate, FONoFilterOption));
filterOptions->Set(String::NewFromUtf8(isolate, "script"),
Int32::New(isolate, 01));
Int32::New(isolate, FOScript));
filterOptions->Set(String::NewFromUtf8(isolate, "image"),
Int32::New(isolate, 02));
Int32::New(isolate, FOImage));
filterOptions->Set(String::NewFromUtf8(isolate, "stylesheet"),
Int32::New(isolate, 04));
Int32::New(isolate, FOStylesheet));
filterOptions->Set(String::NewFromUtf8(isolate, "object"),
Int32::New(isolate, 010));
Int32::New(isolate, FOObject));
filterOptions->Set(String::NewFromUtf8(isolate, "xmlHttpRequest"),
Int32::New(isolate, 020));
Int32::New(isolate, FOXmlHttpRequest));
filterOptions->Set(String::NewFromUtf8(isolate, "objectSubrequest"),
Int32::New(isolate, 040));
Int32::New(isolate, FOObjectSubrequest));
filterOptions->Set(String::NewFromUtf8(isolate, "subdocument"),
Int32::New(isolate, 0100));
Int32::New(isolate, FOSubdocument));
filterOptions->Set(String::NewFromUtf8(isolate, "document"),
Int32::New(isolate, 0200));
Int32::New(isolate, FODocument));
filterOptions->Set(String::NewFromUtf8(isolate, "other"),
Int32::New(isolate, 0400));
Int32::New(isolate, FOOther));
filterOptions->Set(String::NewFromUtf8(isolate, "xbl"),
Int32::New(isolate, 01000));
Int32::New(isolate, FOXBL));
filterOptions->Set(String::NewFromUtf8(isolate, "collapse"),
Int32::New(isolate, 02000));
Int32::New(isolate, FOCollapse));
filterOptions->Set(String::NewFromUtf8(isolate, "doNotTrack"),
Int32::New(isolate, 04000));
Int32::New(isolate, FODoNotTrack));
filterOptions->Set(String::NewFromUtf8(isolate, "elemHide"),
Int32::New(isolate, 010000));
Int32::New(isolate, FOElemHide));
filterOptions->Set(String::NewFromUtf8(isolate, "thirdParty"),
Int32::New(isolate, 020000));
Int32::New(isolate, FOThirdParty));
filterOptions->Set(String::NewFromUtf8(isolate, "notThirdParty"),
Int32::New(isolate, 040000));
Int32::New(isolate, FONotThirdParty));
filterOptions->Set(String::NewFromUtf8(isolate, "ping"),
Int32::New(isolate, 0100000));
Int32::New(isolate, FOPing));
filterOptions->Set(String::NewFromUtf8(isolate, "popup"),
Int32::New(isolate, 0200000));
Int32::New(isolate, FOPopup));
filterOptions->Set(String::NewFromUtf8(isolate, "redirect"),
Int32::New(isolate, FORedirect));
filterOptions->Set(String::NewFromUtf8(isolate, "csp"),
Int32::New(isolate, FOCSP));
filterOptions->Set(String::NewFromUtf8(isolate, "font"),
Int32::New(isolate, 02000000));
Int32::New(isolate, FOFont));
filterOptions->Set(String::NewFromUtf8(isolate, "media"),
Int32::New(isolate, 04000000));
Int32::New(isolate, FOMedia));
filterOptions->Set(String::NewFromUtf8(isolate, "webrtc"),
Int32::New(isolate, 010000000));
Int32::New(isolate, FOWebRTC));
filterOptions->Set(String::NewFromUtf8(isolate, "genericblock"),
Int32::New(isolate, FOGenericBlock));
filterOptions->Set(String::NewFromUtf8(isolate, "generichide"),
Int32::New(isolate, FOGenericHide));
filterOptions->Set(String::NewFromUtf8(isolate, "empty"),
Int32::New(isolate, 0100000000));
Int32::New(isolate, FOEmpty));
filterOptions->Set(String::NewFromUtf8(isolate, "websocket"),
Int32::New(isolate, 0200000000));
Int32::New(isolate, FOWebsocket));
filterOptions->Set(String::NewFromUtf8(isolate, "important"),
Int32::New(isolate, FOImportant));
filterOptions->Set(String::NewFromUtf8(isolate, "explicitcancel"),
Int32::New(isolate, FOExplicitCancel));
filterOptions->Set(String::NewFromUtf8(isolate, "unknown"),
Int32::New(isolate, FOUnknown));

// Adblock lists
Local<Object> lists = Object::New(isolate);
@@ -322,6 +322,8 @@ void Filter::parseOption(const char *input, int len) {
*pFilterOption = static_cast<FilterOption>(*pFilterOption | FOWebsocket);
} else if (!strncmp(pStart, "important", len)) {
*pFilterOption = static_cast<FilterOption>(*pFilterOption | FOImportant);
} else if (!strncmp(pStart, "explicitcancel", len)) {
*pFilterOption = static_cast<FilterOption>(*pFilterOption | FOExplicitCancel);
} else {
*pFilterOption = static_cast<FilterOption>(*pFilterOption | FOUnknown);
std::string option(pStart, len);
@@ -445,12 +447,12 @@ bool Filter::matchesOptions(const char *input, FilterOption context,
// blocking a the HTTP level, don't block here because we don't have enough
// information
if (context != FONoFilterOption) {
if ((filterOption & ~FOThirdParty) != FONoFilterOption
if ((filterOption & ~BehavioralFilterOnly) != FONoFilterOption
&& !(filterOption & FOResourcesOnly & context)) {
return false;
}

if ((antiFilterOption & ~FOThirdParty) != FONoFilterOption
if ((antiFilterOption & ~BehavioralFilterOnly) != FONoFilterOption
&& (antiFilterOption & FOResourcesOnly & context)) {
return false;
}
@@ -5,7 +5,6 @@

#ifndef FILTER_H_
#define FILTER_H_

#include <stdint.h>
#include <string.h>
#include "./base.h"
@@ -72,14 +71,18 @@ enum FilterOption {
FOWebsocket = 0200000000,
// important means to ignore all exception filters (those prefixed with @@).
FOImportant = 0400000000,
// Cancel the request instead of using a 200 OK response
FOExplicitCancel = 01000000000,

FOUnknown = 04000000000,
FOResourcesOnly = FOScript|FOImage|FOStylesheet|FOObject|FOXmlHttpRequest|
FOObjectSubrequest|FOSubdocument|FODocument|FOOther|FOXBL|FOFont|FOMedia|
FOWebRTC|FOWebsocket|FOPing,
FOUnsupportedSoSkipCheck = FOPopup|FOCSP|FOElemHide|FOGenericHide|
FOGenericBlock|FOEmpty|FOUnknown,
FOUnsupportedButIgnore = FORedirect|FOImportant
// Non matching related filters, alters behavior
BehavioralFilterOnly = FORedirect|FOImportant|FOExplicitCancel|
FOThirdParty|FONotThirdParty
};

class Filter {
@@ -18,7 +18,15 @@ const filterPredicateWithPossibleLogging = (rule, filterPredicate = I) => {
/**
* Mapping rule which reformats rules
*/
const mapRule = (rule) => rule
const mapRule = (rule) => {
if (rule === '||imasdk.googleapis.com^$third-party') {
return '||imasdk.googleapis.com^$third-party,explicitcancel'
}
if (rule === '/ima3.js') {
return '/ima3.js$explicitcancel'
}
return rule
}

/**
* Given a list of inputs returns a filtered list of rules that should be used.
@@ -31,6 +39,12 @@ const sanitizeABPInput = (input, filterPredicate = I) =>
.filter((rule) =>
filterPredicateWithPossibleLogging(rule, filterPredicate))
.map(mapRule)
// TODO: This can be removed a few versions after 0.62.x.
// It was just added to be backwards compatible with 0.61.x.
.concat([
'||imasdk.googleapis.com/$third-party',
'^ima3.js'
])
.join('\n')

module.exports = {
@@ -38,5 +38,10 @@ describe('filtering', function () {
const rules = '&ad_channel=\n&ad_classid=\n&ad_height=\n&ad_keyword='
assert(sanitizeABPInput(`${filteredOutRule}\n&ad_channel=\n${filteredOutRule}\n&ad_classid=\n&ad_height=\n&ad_keyword=`, predicate) === rules)
})
it('Converts ima list to add forcecancel option', function () {
const rule = '||imasdk.googleapis.com^$third-party'
const mapToRule = '||imasdk.googleapis.com^$third-party,explicitcancel'
assert.strictEqual(sanitizeABPInput(rule), mapToRule)
})
})
})
@@ -963,7 +963,7 @@ TEST(misc, misc2) {
TEST(serializationTests, serializationTests2) {
AdBlockClient client;
client.parse(
"||googlesyndication.com$third-party\n@@||googlesyndication.ca");
"||googlesyndication.com$third-party\n@@||googlesyndication.ca\na$explicitcancel");
int size;
char * buffer = client.serialize(&size);

@@ -989,7 +989,8 @@ TEST(serializationTests, serializationTests2) {
CHECK(client2.hostAnchoredExceptionHashSet->Exists(f3));
CHECK(!client.hostAnchoredExceptionHashSet->Exists(f4));
CHECK(!client2.hostAnchoredExceptionHashSet->Exists(f4));

CHECK(client.noFingerprintFilters[0].filterOption & FOExplicitCancel);
CHECK(client2.noFingerprintFilters[0].filterOption & FOExplicitCancel);
delete[] buffer;
}

@@ -1063,4 +1064,22 @@ TEST(matchesWithFilterInfo, basic) {
CHECK(matchingExceptionFilter)
CHECK(!strcmp(matchingFilter->data, "googlesyndication.com/safeframe/"));
CHECK(!strcmp(matchingExceptionFilter->data, "safeframe"));

// Preserves explicitcancel and important options when matching
client.clear();
client.parse("||brianbondy.com^$explicitcancel");
bool matches = (client.matches("https://brianbondy.com/t", FOScript, "test.com",
&matchingFilter, &matchingExceptionFilter));
CHECK(matches)
CHECK(matchingFilter)
CHECK(matchingFilter->filterOption & FOExplicitCancel)
CHECK(!matchingExceptionFilter)

client.clear();
client.parse("||brianbondy.com^$important");
CHECK(client.matches("https://brianbondy.com/t", FOScript, "test.com",
&matchingFilter, &matchingExceptionFilter))
CHECK(matchingFilter)
CHECK(matchingFilter->filterOption & FOImportant)
CHECK(!matchingExceptionFilter)
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.