This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
/
trackers.js
161 lines (152 loc) · 6.22 KB
/
trackers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const trackers = require('../src/trackers')
const trackerLists = require('./data/tracker-lists')
trackers.addLists(trackerLists)
describe('getTrackerData', () => {
describe('find tracker data', () => {
let trackerTests = [
// matched rule with no exceptions or first party => block
{
action: 'block',
urlToCheck: 'https://geo.yahoo.com/asdf',
siteUrl: 'https://example.com',
requestType: 'image',
expectedOwner: 'Oath',
expectedReason: 'matched rule - block',
firstParty: false,
expectedRule: 'geo\\.yahoo\\.com',
redirectUrl: false,
matchedRuleException: false
},
// request from a site owned by the same company => ignore
{
action: 'ignore',
urlToCheck: 'https://geo.yahoo.com/asdf',
siteUrl: 'https://news.aol.com',
requestType: 'image',
expectedOwner: 'Oath',
expectedReason: 'first party',
firstParty: true,
expectedRule: 'geo\\.yahoo\\.com',
redirectUrl: false,
matchedRuleException: false
},
// matched a rule with a surrogate => rediect
{
action: 'redirect',
urlToCheck: 'https://ads.a.yahoo.com/?b=asdf',
siteUrl: 'https://example.com',
requestType: 'image',
expectedOwner: 'Oath',
expectedReason: 'matched rule - surrogate',
firstParty: false,
expectedRule: 'a\\.yahoo\\.com($|[?/])',
redirectUrl: 'data:application/javascript;base64,KGZ1bmN0aW9uKCkge30p',
matchedRuleException: false
},
// request matches a rule exception => ignore
{
action: 'ignore',
urlToCheck: 'https://ads.a.yahoo.com/?b=asdf',
siteUrl: 'https://a.example2.com',
requestType: 'image',
expectedOwner: 'Oath',
expectedReason: 'matched rule - exception',
firstParty: false,
expectedRule: 'a\\.yahoo\\.com($|[?/])',
redirectUrl: 'data:application/javascript;base64,KGZ1bmN0aW9uKCkge30p',
matchedRuleException: true
},
// request from the same domain => ignore
{
action: 'ignore',
urlToCheck: 'https://ads.a.yahoo.com/?b=asdf',
siteUrl: 'https://news.yahoo.com',
requestType: 'image',
expectedOwner: 'Oath',
expectedReason: 'first party',
firstParty: true,
expectedRule: 'a\\.yahoo\\.com($|[?/])',
redirectUrl: 'data:application/javascript;base64,KGZ1bmN0aW9uKCkge30p',
matchedRuleException: false
},
// rule with a action 'ignore' => ignore
{
action: 'ignore',
urlToCheck: 'https://b.yahoo.com/ads?ad=asdf',
siteUrl: 'https://example.com',
requestType: 'image',
expectedOwner: 'Oath',
expectedReason: 'matched rule - ignore',
firstParty: false,
expectedRule: 'b\\.yahoo\\.com\\/.*\\?ad=asdf',
redirectUrl: false,
matchedRuleException: false
},
// request without a rule, tracker definition default action is block => block
{
action: 'block',
urlToCheck: 'https://asdf.yahoo.com/tracker1/asdf',
siteUrl: 'https://example.com',
requestType: 'script',
expectedOwner: 'Oath',
expectedReason: 'default block',
firstParty: false,
expectedRule: null,
redirectUrl: false,
matchedRuleException: false
},
// tracker with default ignore => ignore
{
action: 'ignore',
urlToCheck: 'https://example.com/tracker1/asdf',
siteUrl: 'https://aol.com',
requestType: 'script',
expectedOwner: 'Example',
expectedReason: 'default ignore',
firstParty: false,
expectedRule: null,
redirectUrl: false,
matchedRuleException: false
}
]
trackerTests.forEach((test) => {
it(`should block ${test.urlToCheck}`, () => {
let tracker = trackers.getTrackerData(
test.urlToCheck,
test.siteUrl,
{url: test.urlToCheck, type: test.requestType})
expect(tracker.action).toEqual(test.action)
expect(tracker.reason).toEqual(test.expectedReason)
if (test.expectedRule) {
expect(tracker.matchedRule.rule.toString()).toEqual(new RegExp(test.expectedRule, 'gi').toString())
}
expect(tracker.firstParty).toEqual(test.firstParty)
expect(tracker.redirectUrl).toEqual(test.redirectUrl)
expect(tracker.matchedRuleException).toEqual(test.matchedRuleException)
})
})
let nonTrackerTests = [
// non tracker requests
{
urlToCheck: 'http://somecdn.com/jquery',
siteUrl: 'https://example.com',
requestType: 'script'
},
// malformed urls
{
urlToCheck: 'http://%20%20s.src%20%3D/',
siteUrl: 'https://example.com',
requestType: 'image'
}
]
nonTrackerTests.forEach((test) => {
it(`should not block ${test.urlToCheck}`, () => {
let tracker = trackers.getTrackerData(
test.urlToCheck,
test.siteUrl,
{url: test.urlToCheck, type: test.requestType})
expect(tracker).toEqual(null)
})
})
})
})