Skip to content
This repository was archived by the owner on May 12, 2020. It is now read-only.

Commit 8d72272

Browse files
committed
Complete initial implementation of using public suffix list for eTLD+1
1 parent 62991a1 commit 8d72272

37 files changed

Lines changed: 15179 additions & 343 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ tags
3939
# output from sample which can be used for importing
4040
*.dat
4141

42+
# but do inclule the public_suffix_list.dat file, which is used in tests
43+
!public_suffix_list.dat
44+
4245
# Python compiled files
4346
*.pyc
4447

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
.PHONY: clean
66

77
build:
8-
./node_modules/.bin/node-gyp configure && ./node_modules/.bin/node-gyp build
8+
./node_modules/.bin/node-gyp configure && ./node_modules/.bin/node-gyp build
99

1010
test:
1111
./node_modules/node-gyp/gyp/gyp_main.py --generator-output=./build --depth=. -f ninja test/binding.gyp
@@ -26,4 +26,4 @@ perf:
2626
./build/out/Default/perf
2727

2828
clean:
29-
rm -Rf build
29+
rm -Rf build

ad_block_client.cc

Lines changed: 102 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "./cosmetic_filter.h"
1313
#include "./hashFn.h"
1414
#include "./no_fingerprint_domain.h"
15+
#include "etld/matcher.h"
16+
#include "etld/serialization.h"
1517

1618
#include "BloomFilter.h"
1719

@@ -21,6 +23,10 @@ using std::cout;
2123
using std::endl;
2224
#endif
2325

26+
using brave_etld::SerializedBuffer;
27+
using brave_etld::SerializationResult;
28+
using brave_etld::matcher_from_serialization;
29+
2430
std::set<std::string> unknownOptions;
2531

2632
// Fast hash function applicable to 2 byte char checks
@@ -587,6 +593,10 @@ void AdBlockClient::clear() {
587593
delete badFingerprintsHashSet;
588594
badFingerprintsHashSet = nullptr;
589595
}
596+
if (etldMatcher) {
597+
delete etldMatcher;
598+
etldMatcher = nullptr;
599+
}
590600

591601
numFilters = 0;
592602
numCosmeticFilters = 0;
@@ -685,7 +695,8 @@ bool isNoFingerprintDomainHashSetMiss(HashSet<NoFingerprintDomain> *hashSet,
685695
static_cast<int>(host + hostLen - start)));
686696
}
687697

688-
bool isHostAnchoredHashSetMiss(const char *input, int inputLen,
698+
bool isHostAnchoredHashSetMiss(const AdBlockClient * client,
699+
const char *input, int inputLen,
689700
HashSet<Filter> *hashSet,
690701
const char *inputHost,
691702
int inputHostLen,
@@ -709,7 +720,9 @@ bool isHostAnchoredHashSetMiss(const char *input, int inputLen,
709720
if (*(start - 1) == '.') {
710721
Filter *filter = hashSet->Find(Filter(start,
711722
static_cast<int>(inputHost + inputHostLen - start),
712-
nullptr, start, inputHostLen - (start - inputHost)));
723+
nullptr,
724+
start,
725+
inputHostLen - (start - inputHost)));
713726
if (filter && filter->matches(input, inputLen,
714727
contextOption, contextDomain)) {
715728
if (foundFilter) {
@@ -722,8 +735,10 @@ bool isHostAnchoredHashSetMiss(const char *input, int inputLen,
722735
}
723736

724737
Filter *filter = hashSet->Find(Filter(start,
725-
static_cast<int>(inputHost + inputHostLen - start), nullptr,
726-
start, inputHostLen));
738+
static_cast<int>(inputHost + inputHostLen - start),
739+
nullptr,
740+
start,
741+
inputHostLen));
727742
if (!filter) {
728743
return true;
729744
}
@@ -749,7 +764,7 @@ bool AdBlockClient::matches(const char *input, FilterOption contextOption,
749764
if (contextDomain) {
750765
contextDomainLen = static_cast<int>(strlen(contextDomain));
751766
if (isThirdPartyHost(contextDomain, contextDomainLen,
752-
inputHost, static_cast<int>(inputHostLen))) {
767+
inputHost, static_cast<int>(inputHostLen), etldMatcher)) {
753768
contextOption =
754769
static_cast<FilterOption>(contextOption | FOThirdParty);
755770
} else {
@@ -799,7 +814,7 @@ bool AdBlockClient::matches(const char *input, FilterOption contextOption,
799814
if (!hasMatch) {
800815
bloomFilterMiss = bloomFilter
801816
&& !bloomFilter->substringExists(input, AdBlockClient::kFingerprintSize);
802-
hostAnchoredHashSetMiss = isHostAnchoredHashSetMiss(input, inputLen,
817+
hostAnchoredHashSetMiss = isHostAnchoredHashSetMiss(this, input, inputLen,
803818
hostAnchoredHashSet, inputHost, inputHostLen,
804819
contextOption, contextDomain);
805820
if (bloomFilterMiss && hostAnchoredHashSetMiss) {
@@ -871,8 +886,9 @@ bool AdBlockClient::matches(const char *input, FilterOption contextOption,
871886
&& !exceptionBloomFilter->substringExists(input,
872887
AdBlockClient::kFingerprintSize);
873888
bool hostAnchoredExceptionHashSetMiss =
874-
isHostAnchoredHashSetMiss(input, inputLen, hostAnchoredExceptionHashSet,
875-
inputHost, inputHostLen, contextOption, contextDomain);
889+
isHostAnchoredHashSetMiss(this, input, inputLen,
890+
hostAnchoredExceptionHashSet, inputHost, inputHostLen,
891+
contextOption, contextDomain);
876892

877893
// Now that we have a matching rule, we should check if no exception rule
878894
// hits, if none hits, we should block
@@ -932,7 +948,7 @@ bool AdBlockClient::findMatchingFilters(const char *input,
932948
if (contextDomain) {
933949
contextDomainLen = static_cast<int>(strlen(contextDomain));
934950
if (isThirdPartyHost(contextDomain, contextDomainLen,
935-
inputHost, static_cast<int>(inputHostLen))) {
951+
inputHost, static_cast<int>(inputHostLen), etldMatcher)) {
936952
contextOption =
937953
static_cast<FilterOption>(contextOption | FOThirdParty);
938954
} else {
@@ -967,7 +983,7 @@ bool AdBlockClient::findMatchingFilters(const char *input,
967983
}
968984

969985
if (!*matchingFilter) {
970-
isHostAnchoredHashSetMiss(input, inputLen,
986+
isHostAnchoredHashSetMiss(this, input, inputLen,
971987
hostAnchoredHashSet, inputHost, inputHostLen,
972988
contextOption, contextDomain, matchingFilter);
973989
}
@@ -996,9 +1012,9 @@ bool AdBlockClient::findMatchingFilters(const char *input,
9961012
}
9971013

9981014
if (!*matchingExceptionFilter) {
999-
isHostAnchoredHashSetMiss(input, inputLen, hostAnchoredExceptionHashSet,
1000-
inputHost, inputHostLen, contextOption, contextDomain,
1001-
matchingExceptionFilter);
1015+
isHostAnchoredHashSetMiss(this, input, inputLen,
1016+
hostAnchoredExceptionHashSet, inputHost, inputHostLen, contextOption,
1017+
contextDomain, matchingExceptionFilter);
10021018
}
10031019

10041020
if (!*matchingExceptionFilter) {
@@ -1041,7 +1057,7 @@ void setFilterBorrowedMemory(Filter *filters, int numFilters) {
10411057
}
10421058

10431059
// Parses the filter data into a few collections of filters and enables
1044-
// efficent querying.
1060+
// efficient querying.
10451061
bool AdBlockClient::parse(const char *input, bool preserveRules) {
10461062
// If the user is parsing and we have regex support,
10471063
// then we can determine the fingerprints for the bloom filter.
@@ -1351,6 +1367,7 @@ bool AdBlockClient::parse(const char *input, bool preserveRules) {
13511367
&simpleCosmeticFilters,
13521368
preserveRules);
13531369
if (!f.hasUnsupportedOptions()) {
1370+
f.setEtldMatcher(etldMatcher);
13541371
switch (f.filterType & FTListTypesMask) {
13551372
case FTException:
13561373
if (f.filterType & FTHostOnly) {
@@ -1427,6 +1444,14 @@ bool AdBlockClient::parse(const char *input, bool preserveRules) {
14271444
return true;
14281445
}
14291446

1447+
void AdBlockClient::parsePublicSuffixRules(const char *input) {
1448+
if (etldMatcher != nullptr) {
1449+
delete etldMatcher;
1450+
}
1451+
1452+
etldMatcher = new Matcher(std::string(input));
1453+
}
1454+
14301455
// Fills the specified buffer if specified, returns the number of characters
14311456
// written or needed
14321457
int serializeFilters(char * buffer, size_t bufferSizeAvail,
@@ -1528,10 +1553,20 @@ char * AdBlockClient::serialize(int *totalSize,
15281553
&noFingerprintAntiDomainExceptionHashSetSize);
15291554
}
15301555

1556+
1557+
SerializedBuffer serializedMatcherBuffer;
1558+
int serializedMatcherBufSize;
1559+
if (etldMatcher == nullptr) {
1560+
etldMatcher = new Matcher();
1561+
}
1562+
SerializationResult matcherSerializationResult = etldMatcher->Serialize();
1563+
serializedMatcherBuffer = matcherSerializationResult.buffer;
1564+
serializedMatcherBufSize = static_cast<int>(serializedMatcherBuffer.size());
1565+
15311566
// Get the number of bytes that we'll need
15321567
char sz[512];
15331568
*totalSize += 1 + snprintf(sz, sizeof(sz),
1534-
"%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x",
1569+
"%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x",
15351570
numFilters,
15361571
numExceptionFilters, adjustedNumCosmeticFilters, adjustedNumHtmlFilters,
15371572
numNoFingerprintFilters, numNoFingerprintExceptionFilters,
@@ -1540,13 +1575,16 @@ char * AdBlockClient::serialize(int *totalSize,
15401575
numNoFingerprintDomainOnlyExceptionFilters,
15411576
numNoFingerprintAntiDomainOnlyExceptionFilters,
15421577
numHostAnchoredFilters, numHostAnchoredExceptionFilters,
1543-
bloomFilter ? bloomFilter->getByteBufferSize() : 0, exceptionBloomFilter
1544-
? exceptionBloomFilter->getByteBufferSize() : 0,
1545-
hostAnchoredHashSetSize, hostAnchoredExceptionHashSetSize,
1546-
noFingerprintDomainHashSetSize,
1547-
noFingerprintAntiDomainHashSetSize,
1548-
noFingerprintDomainExceptionHashSetSize,
1549-
noFingerprintAntiDomainExceptionHashSetSize);
1578+
bloomFilter ? bloomFilter->getByteBufferSize() : 0,
1579+
exceptionBloomFilter ? exceptionBloomFilter->getByteBufferSize() : 0,
1580+
hostAnchoredHashSetSize,
1581+
hostAnchoredExceptionHashSetSize,
1582+
noFingerprintDomainHashSetSize,
1583+
noFingerprintAntiDomainHashSetSize,
1584+
noFingerprintDomainExceptionHashSetSize,
1585+
noFingerprintAntiDomainExceptionHashSetSize,
1586+
serializedMatcherBufSize);
1587+
15501588
*totalSize += serializeFilters(nullptr, 0, filters, numFilters) +
15511589
serializeFilters(nullptr, 0, exceptionFilters, numExceptionFilters) +
15521590
serializeFilters(nullptr, 0, cosmeticFilters, adjustedNumCosmeticFilters) +
@@ -1574,6 +1612,7 @@ char * AdBlockClient::serialize(int *totalSize,
15741612
*totalSize += noFingerprintAntiDomainHashSetSize;
15751613
*totalSize += noFingerprintDomainExceptionHashSetSize;
15761614
*totalSize += noFingerprintAntiDomainExceptionHashSetSize;
1615+
*totalSize += serializedMatcherBufSize;
15771616

15781617
// Allocate it
15791618
int pos = 0;
@@ -1653,6 +1692,12 @@ char * AdBlockClient::serialize(int *totalSize,
16531692
delete[] noFingerprintAntiDomainExceptionHashSetBuffer;
16541693
}
16551694

1695+
if (serializedMatcherBufSize > 0) {
1696+
memcpy(buffer + pos, serializedMatcherBuffer.c_str(),
1697+
serializedMatcherBufSize);
1698+
pos += serializedMatcherBufSize;
1699+
}
1700+
16561701
return buffer;
16571702
}
16581703

@@ -1703,24 +1748,32 @@ bool AdBlockClient::deserialize(char *buffer) {
17031748
noFingerprintDomainHashSetSize = 0,
17041749
noFingerprintAntiDomainHashSetSize = 0,
17051750
noFingerprintDomainExceptionHashSetSize = 0,
1706-
noFingerprintAntiDomainExceptionHashSetSize = 0;
1751+
noFingerprintAntiDomainExceptionHashSetSize = 0,
1752+
etldMatcherBufSize = 0;
17071753
int pos = 0;
17081754
sscanf(buffer + pos,
1709-
"%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x",
1755+
"%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x",
17101756
&numFilters,
1711-
&numExceptionFilters, &numCosmeticFilters, &numHtmlFilters,
1712-
&numNoFingerprintFilters, &numNoFingerprintExceptionFilters,
1757+
&numExceptionFilters,
1758+
&numCosmeticFilters,
1759+
&numHtmlFilters,
1760+
&numNoFingerprintFilters,
1761+
&numNoFingerprintExceptionFilters,
17131762
&numNoFingerprintDomainOnlyFilters,
17141763
&numNoFingerprintAntiDomainOnlyFilters,
17151764
&numNoFingerprintDomainOnlyExceptionFilters,
17161765
&numNoFingerprintAntiDomainOnlyExceptionFilters,
1717-
&numHostAnchoredFilters, &numHostAnchoredExceptionFilters,
1718-
&bloomFilterSize, &exceptionBloomFilterSize,
1719-
&hostAnchoredHashSetSize, &hostAnchoredExceptionHashSetSize,
1766+
&numHostAnchoredFilters,
1767+
&numHostAnchoredExceptionFilters,
1768+
&bloomFilterSize,
1769+
&exceptionBloomFilterSize,
1770+
&hostAnchoredHashSetSize,
1771+
&hostAnchoredExceptionHashSetSize,
17201772
&noFingerprintDomainHashSetSize,
17211773
&noFingerprintAntiDomainHashSetSize,
17221774
&noFingerprintDomainExceptionHashSetSize,
1723-
&noFingerprintAntiDomainExceptionHashSetSize);
1775+
&noFingerprintAntiDomainExceptionHashSetSize,
1776+
&etldMatcherBufSize);
17241777
pos += static_cast<int>(strlen(buffer + pos)) + 1;
17251778

17261779
filters = new Filter[numFilters];
@@ -1769,40 +1822,53 @@ bool AdBlockClient::deserialize(char *buffer) {
17691822
pos += exceptionBloomFilterSize;
17701823
if (!initHashSet(&hostAnchoredHashSet,
17711824
buffer + pos, hostAnchoredHashSetSize)) {
1772-
return false;
1825+
return false;
17731826
}
17741827
pos += hostAnchoredHashSetSize;
17751828
if (!initHashSet(&hostAnchoredExceptionHashSet,
17761829
buffer + pos, hostAnchoredExceptionHashSetSize)) {
1777-
return false;
1830+
return false;
17781831
}
17791832
pos += hostAnchoredExceptionHashSetSize;
17801833

17811834

17821835
if (!initHashSet(&noFingerprintDomainHashSet,
17831836
buffer + pos, noFingerprintDomainHashSetSize)) {
1784-
return false;
1837+
return false;
17851838
}
17861839
pos += noFingerprintDomainHashSetSize;
17871840

17881841
if (!initHashSet(&noFingerprintAntiDomainHashSet,
17891842
buffer + pos, noFingerprintAntiDomainHashSetSize)) {
1790-
return false;
1843+
return false;
17911844
}
17921845
pos += noFingerprintAntiDomainHashSetSize;
17931846

17941847
if (!initHashSet(&noFingerprintDomainExceptionHashSet,
17951848
buffer + pos, noFingerprintDomainExceptionHashSetSize)) {
1796-
return false;
1849+
return false;
17971850
}
17981851
pos += noFingerprintDomainExceptionHashSetSize;
17991852

18001853
if (!initHashSet(&noFingerprintAntiDomainExceptionHashSet,
18011854
buffer + pos, noFingerprintAntiDomainExceptionHashSetSize)) {
1802-
return false;
1855+
return false;
18031856
}
18041857
pos += noFingerprintAntiDomainExceptionHashSetSize;
18051858

1859+
if (etldMatcher != nullptr) {
1860+
delete etldMatcher;
1861+
}
1862+
1863+
if (etldMatcherBufSize == 0) {
1864+
etldMatcher = new Matcher();
1865+
} else {
1866+
SerializedBuffer serializedmatcherBuffer = std::string(
1867+
buffer + pos, etldMatcherBufSize);
1868+
Matcher newMatcher = matcher_from_serialization(serializedmatcherBuffer);
1869+
etldMatcher = new Matcher(newMatcher);
1870+
}
1871+
18061872
return true;
18071873
}
18081874

ad_block_client.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <string>
1010
#include <set>
1111
#include "./filter.h"
12+
#include "etld/matcher.h"
1213

1314
class CosmeticFilter;
1415
class BloomFilter;
@@ -24,8 +25,8 @@ class AdBlockClient {
2425
~AdBlockClient();
2526

2627
void clear();
27-
// bool parse(const char *input);
2828
bool parse(const char *input, bool preserveRules = false);
29+
void parsePublicSuffixRules(const char *input);
2930
bool matches(const char *input,
3031
FilterOption contextOption = FONoFilterOption,
3132
const char *contextDomain = nullptr);
@@ -108,6 +109,7 @@ class AdBlockClient {
108109
template<class T>
109110
bool initHashSet(HashSet<T>**, char *buffer, int len);
110111
char *deserializedBuffer;
112+
brave_etld::Matcher* etldMatcher = nullptr;
111113
};
112114

113115
extern std::set<std::string> unknownOptions;

0 commit comments

Comments
 (0)