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

Delta Between Two Patch Sets: lib/domain.js

Issue 29987585: Issue 7232 - Move third-party request check into adblockpluscore (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created Jan. 23, 2019, 2:52 a.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « 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')
LEFTRIGHT
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 /**
23 * Checks whether the given hostname is a domain.
24 *
25 * @param {string} hostname
26 * @returns {boolean}
27 */
22 function isDomain(hostname) 28 function isDomain(hostname)
23 { 29 {
24 // No hostname or IPv4 address, also considering hexadecimal octets. 30 // No hostname or IPv4 address, also considering hexadecimal octets.
25 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) 31 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname))
26 return false; 32 return false;
27 33
28 // IPv6 address. Since there can't be colons in domains, we can 34 // IPv6 address. Since there can't be colons in domains, we can
29 // just check whether there are any colons to exclude IPv6 addresses. 35 // just check whether there are any colons to exclude IPv6 addresses.
30 return hostname.indexOf(":") == -1; 36 return hostname.indexOf(":") == -1;
31 } 37 }
32 38
33 /** 39 /**
34 * Gets the base domain for the given hostname. 40 * Gets the base domain for the given hostname.
35 * 41 *
36 * @param {string} hostname 42 * @param {string} hostname
37 * @returns {string} 43 * @returns {string}
38 */ 44 */
39 function getDomain(hostname) 45 function getDomain(hostname)
Manish Jethani 2019/01/23 03:25:25 There's no need to export this now.
40 { 46 {
41 let bits = hostname.split("."); 47 let bits = hostname.split(".");
42 let cutoff = bits.length - 2; 48 let cutoff = bits.length - 2;
43 49
44 for (let i = 0; i < bits.length; i++) 50 for (let i = 0; i < bits.length; i++)
45 { 51 {
46 let offset = publicSuffixes[bits.slice(i).join(".")]; 52 let offset = publicSuffixes[bits.slice(i).join(".")];
47 53
48 if (typeof offset != "undefined") 54 if (typeof offset != "undefined")
49 { 55 {
50 cutoff = i - offset; 56 cutoff = i - offset;
51 break; 57 break;
52 } 58 }
53 } 59 }
54 60
55 if (cutoff <= 0) 61 if (cutoff <= 0)
56 return hostname; 62 return hostname;
57 63
58 return bits.slice(cutoff).join("."); 64 return bits.slice(cutoff).join(".");
59 } 65 }
60 66
61 /** 67 /**
62 * Checks whether the request's origin is different from the document's origin. 68 * Checks whether a request's origin is different from its document's origin.
63 * 69 *
64 * @param {URL} url The request URL 70 * @param {URL} url The request URL.
65 * @param {string} documentHost The IDN-decoded hostname of the document 71 * @param {string} documentHostname The IDNA-encoded hostname of the document.
66 * @return {Boolean} 72 *
73 * @returns {boolean}
67 */ 74 */
68 function isThirdParty(url, documentHost) 75 function isThirdParty(url, documentHostname)
69 { 76 {
70 let requestHost = url.hostname.replace(/\.+$/, ""); 77 let requestHostname = url.hostname.replace(/\.+$/, "");
71 documentHost = documentHost.replace(/\.+$/, ""); 78 documentHostname = documentHostname.replace(/\.+$/, "");
72 79
73 if (requestHost == documentHost) 80 if (requestHostname == documentHostname)
74 return false; 81 return false;
75 82
76 if (!isDomain(requestHost) || !isDomain(documentHost)) 83 if (!isDomain(requestHostname) || !isDomain(documentHostname))
77 return true; 84 return true;
78 85
79 return getDomain(requestHost) != getDomain(documentHost); 86 return getDomain(requestHostname) != getDomain(documentHostname);
80 } 87 }
81 88
82 exports.isThirdParty = isThirdParty; 89 exports.isThirdParty = isThirdParty;
LEFTRIGHT
« no previous file | test/domain.js » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld