Skip to content

Commit af8bf4a

Browse files
committed
Escape regexes from user input, remove brave callback, fix light on CDN
1 parent 8d2d289 commit af8bf4a

File tree

2 files changed

+52
-67
lines changed

2 files changed

+52
-67
lines changed

compile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ const templates = [
227227
...LIGHT,
228228
version: VERSION,
229229
scriptName: `cdn_light_${VERSION}`,
230-
baseUrl: "{{nginxHost}}",
230+
baseUrl: "simpleanalyticscdn.com",
231+
apiUrlPrefix: "queue.",
231232
},
232233
},
233234
{

src/default.js

Lines changed: 50 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
var doc = window.document;
3838
var userAgent = nav.userAgent;
3939
var notSending = "Not sending request ";
40+
var notSendingWhen = notSending + "when ";
4041
var fetchedHighEntropyValues = falseVar;
4142
var encodeURIComponentFunc = encodeURIComponent;
4243
var decodeURIComponentFunc = decodeURIComponent;
@@ -85,6 +86,10 @@
8586
return typeof string == "string";
8687
};
8788

89+
var filterRegex = function (item) {
90+
return item.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
91+
};
92+
8893
var attr = function (scriptElement, attribute) {
8994
return scriptElement && scriptElement.getAttribute("data-" + attribute);
9095
};
@@ -208,7 +213,7 @@
208213
var ignore = ignoreSource || !collectMetricByString("ut");
209214

210215
/** if allowparams **/
211-
var paramsRegexList = allowParams.join("|");
216+
var paramsRegexList = allowParams.map(filterRegex).join("|");
212217
var regex = ignore
213218
? "^(" + paramsRegexList + ")="
214219
: "^((utm_)" +
@@ -225,8 +230,8 @@
225230
"^((utm_)" +
226231
(strictUtm ? "" : "?") +
227232
"(source|medium|content|term|campaign)" +
228-
(strictUtm ? "" : "|ref");
229-
(")=");
233+
(strictUtm ? "" : "|ref") +
234+
")=";
230235
/** endif **/
231236

232237
// The prefix "utm_" is optional with "strictUtm" disabled
@@ -251,7 +256,10 @@
251256
try {
252257
if (
253258
ignorePage === path ||
254-
new RegExp(ignorePage.replace(/\*/gi, "(.*)"), "gi").test(path)
259+
new RegExp(
260+
"^" + filterRegex(ignorePage).replace(/\\\*/gi, "(.*)") + "$",
261+
"i"
262+
).test(path)
255263
)
256264
return trueVar;
257265
} catch (error) {
@@ -279,40 +287,38 @@
279287

280288
// Send data via image
281289
var sendData = function (data, callback, onlyThisData) {
282-
return braveCallback(function (isBrave) {
283-
data = onlyThisData ? data : assign(payload, page, data);
290+
data = onlyThisData ? data : assign(payload, page, data);
284291

285-
if (isBrave && !onlyThisData) data.brave = trueVar;
292+
if (nav.brave && !onlyThisData) data.brave = trueVar;
286293

287-
/** if dev **/
288-
data.dev = trueVar;
289-
/** endif **/
294+
/** if dev **/
295+
data.dev = trueVar;
296+
/** endif **/
290297

291-
var image = new Image();
292-
/** if events **/
293-
if (callback) {
294-
image.onerror = callback;
295-
image.onload = callback;
296-
}
297-
/** endif **/
298-
image.src =
299-
fullApiUrl +
300-
"/simple.gif?" +
301-
Object.keys(data)
302-
.filter(function (key) {
303-
return data[key] != undefinedVar;
304-
})
305-
.map(function (key) {
306-
return (
307-
encodeURIComponentFunc(key) +
308-
"=" +
309-
encodeURIComponentFunc(data[key])
310-
);
311-
})
312-
.join("&") +
313-
"&time=" +
314-
Date.now();
315-
});
298+
var image = new Image();
299+
/** if events **/
300+
if (callback) {
301+
image.onerror = callback;
302+
image.onload = callback;
303+
}
304+
/** endif **/
305+
image.src =
306+
fullApiUrl +
307+
"/simple.gif?" +
308+
Object.keys(data)
309+
.filter(function (key) {
310+
return data[key] != undefinedVar;
311+
})
312+
.map(function (key) {
313+
return (
314+
encodeURIComponentFunc(key) +
315+
"=" +
316+
encodeURIComponentFunc(data[key])
317+
);
318+
})
319+
.join("&") +
320+
"&time=" +
321+
Date.now();
316322
};
317323

318324
/** if errorhandling **/
@@ -350,7 +356,6 @@
350356
//
351357

352358
/** if duration **/
353-
var duration = "duration";
354359
var start = now();
355360
/** endif **/
356361

@@ -397,9 +402,11 @@
397402

398403
/** if ignorepages **/
399404
// Customers can ignore certain pages
400-
var ignorePages = convertCommaSeparatedToArray(
401-
overwriteOptions.ignorePages || attr(scriptElement, "ignore-pages")
402-
);
405+
var ignorePages =
406+
["/path*lala"] ||
407+
convertCommaSeparatedToArray(
408+
overwriteOptions.ignorePages || attr(scriptElement, "ignore-pages")
409+
);
403410
/** endif **/
404411

405412
/** if allowparams **/
@@ -430,19 +437,6 @@
430437
attr(scriptElement, "metadata-collector");
431438
/** endif **/
432439

433-
var braveCallback = function (callback) {
434-
if (!nav.brave) callback(falseVar);
435-
else
436-
nav.brave
437-
.isBrave()
438-
.then(function () {
439-
callback(trueVar);
440-
})
441-
.catch(function () {
442-
callback(falseVar);
443-
});
444-
};
445-
446440
// This code could error on (incomplete) implementations, that's why we use try...catch
447441
var timezone;
448442
try {
@@ -530,22 +524,12 @@
530524
/** if ignorednt **/
531525
if (!collectDnt && doNotTrack in nav && nav[doNotTrack] == "1")
532526
return warn(
533-
notSending +
534-
"when " +
535-
doNotTrack +
536-
" is enabled. See " +
537-
docsUrl +
538-
"/dnt"
527+
notSendingWhen + doNotTrack + " is enabled. See " + docsUrl + "/dnt"
539528
);
540529
/** else **/
541530
if (doNotTrack in nav && nav[doNotTrack] == "1")
542531
return warn(
543-
notSending +
544-
"when " +
545-
doNotTrack +
546-
" is enabled. See " +
547-
docsUrl +
548-
"/dnt"
532+
notSendingWhen + doNotTrack + " is enabled. See " + docsUrl + "/dnt"
549533
);
550534
/** endif **/
551535

@@ -595,7 +579,7 @@
595579
/** if duration **/
596580
// t = timeonpage
597581
if (collectMetricByString("t")) {
598-
append[duration] = Math.round((now() - start - msHidden) / thousand);
582+
append.duration = Math.round((now() - start - msHidden) / thousand);
599583
}
600584
msHidden = 0;
601585
start = now();
@@ -694,7 +678,7 @@
694678
/** if ignorepages **/
695679
// Ignore pages specified in data-ignore-pages
696680
if (shouldIgnore(path)) {
697-
warn(notSending + ", ignored " + path);
681+
warn(notSendingWhen + "ignoring " + path);
698682
return;
699683
}
700684
/** endif **/

0 commit comments

Comments
 (0)