Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/domain.js

Issue 29987585: Issue 7232 - Move third-party request check into adblockpluscore (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add tests Created Jan. 23, 2019, 3:22 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/domain.js » ('j') | test/domain.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 const publicSuffixes = require("../data/publicSuffixList.json"); 20 const publicSuffixes = require("../data/publicSuffixList.json");
21 21
22 /** 22 /**
23 * Checks whether the given hostname is a domain.
24 *
25 * @param {string} hostname
26 * @returns {boolean}
27 */
28 function isDomain(hostname)
29 {
30 // No hostname or IPv4 address, also considering hexadecimal octets.
31 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname))
32 return false;
33
34 // IPv6 address. Since there can't be colons in domains, we can
35 // just check whether there are any colons to exclude IPv6 addresses.
36 return hostname.indexOf(":") == -1;
37 }
38
39 /**
23 * Gets the base domain for the given hostname. 40 * Gets the base domain for the given hostname.
24 * 41 *
25 * @param {string} hostname 42 * @param {string} hostname
26 * @returns {string} 43 * @returns {string}
27 */ 44 */
28 exports.getDomain = hostname => 45 function getDomain(hostname)
29 { 46 {
30 let bits = hostname.split("."); 47 let bits = hostname.split(".");
31 let cutoff = bits.length - 2; 48 let cutoff = bits.length - 2;
32 49
33 for (let i = 0; i < bits.length; i++) 50 for (let i = 0; i < bits.length; i++)
34 { 51 {
35 let offset = publicSuffixes[bits.slice(i).join(".")]; 52 let offset = publicSuffixes[bits.slice(i).join(".")];
36 53
37 if (typeof offset != "undefined") 54 if (typeof offset != "undefined")
38 { 55 {
39 cutoff = i - offset; 56 cutoff = i - offset;
40 break; 57 break;
41 } 58 }
42 } 59 }
43 60
44 if (cutoff <= 0) 61 if (cutoff <= 0)
45 return hostname; 62 return hostname;
46 63
47 return bits.slice(cutoff).join("."); 64 return bits.slice(cutoff).join(".");
48 }; 65 }
66
67 /**
68 * Checks whether a request's origin is different from its document's origin.
69 *
70 * @param {URL} url The request URL.
71 * @param {string} documentHostname The IDNA-encoded hostname of the document.
72 *
73 * @returns {boolean}
74 */
75 function isThirdParty(url, documentHostname)
76 {
77 let requestHostname = url.hostname.replace(/\.+$/, "");
78 documentHostname = documentHostname.replace(/\.+$/, "");
79
80 if (requestHostname == documentHostname)
81 return false;
82
83 if (!isDomain(requestHostname) || !isDomain(documentHostname))
84 return true;
85
86 return getDomain(requestHostname) != getDomain(documentHostname);
87 }
88
89 exports.isThirdParty = isThirdParty;
OLDNEW
« no previous file with comments | « no previous file | test/domain.js » ('j') | test/domain.js » ('J')

Powered by Google App Engine
This is Rietveld