OLD | NEW |
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 function isDomain(hostname) |
| 23 { |
| 24 // No hostname or IPv4 address, also considering hexadecimal octets. |
| 25 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) |
| 26 return false; |
| 27 |
| 28 // IPv6 address. Since there can't be colons in domains, we can |
| 29 // just check whether there are any colons to exclude IPv6 addresses. |
| 30 return hostname.indexOf(":") == -1; |
| 31 } |
| 32 |
22 /** | 33 /** |
23 * Gets the base domain for the given hostname. | 34 * Gets the base domain for the given hostname. |
24 * | 35 * |
25 * @param {string} hostname | 36 * @param {string} hostname |
26 * @returns {string} | 37 * @returns {string} |
27 */ | 38 */ |
28 exports.getDomain = hostname => | 39 function getDomain(hostname) |
29 { | 40 { |
30 let bits = hostname.split("."); | 41 let bits = hostname.split("."); |
31 let cutoff = bits.length - 2; | 42 let cutoff = bits.length - 2; |
32 | 43 |
33 for (let i = 0; i < bits.length; i++) | 44 for (let i = 0; i < bits.length; i++) |
34 { | 45 { |
35 let offset = publicSuffixes[bits.slice(i).join(".")]; | 46 let offset = publicSuffixes[bits.slice(i).join(".")]; |
36 | 47 |
37 if (typeof offset != "undefined") | 48 if (typeof offset != "undefined") |
38 { | 49 { |
39 cutoff = i - offset; | 50 cutoff = i - offset; |
40 break; | 51 break; |
41 } | 52 } |
42 } | 53 } |
43 | 54 |
44 if (cutoff <= 0) | 55 if (cutoff <= 0) |
45 return hostname; | 56 return hostname; |
46 | 57 |
47 return bits.slice(cutoff).join("."); | 58 return bits.slice(cutoff).join("."); |
48 }; | 59 } |
| 60 |
| 61 /** |
| 62 * Checks whether the request's origin is different from the document's origin. |
| 63 * |
| 64 * @param {URL} url The request URL |
| 65 * @param {string} documentHost The IDN-decoded hostname of the document |
| 66 * @return {Boolean} |
| 67 */ |
| 68 function isThirdParty(url, documentHost) |
| 69 { |
| 70 let requestHost = url.hostname.replace(/\.+$/, ""); |
| 71 documentHost = documentHost.replace(/\.+$/, ""); |
| 72 |
| 73 if (requestHost == documentHost) |
| 74 return false; |
| 75 |
| 76 if (!isDomain(requestHost) || !isDomain(documentHost)) |
| 77 return true; |
| 78 |
| 79 return getDomain(requestHost) != getDomain(documentHost); |
| 80 } |
| 81 |
| 82 exports.isThirdParty = isThirdParty; |
OLD | NEW |