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 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 * <code>www.example.com</code>, this function yields | 57 * <code>www.example.com</code>, this function yields |
58 * <code>www.example.com</code>, <code>example.com</code>, and | 58 * <code>www.example.com</code>, <code>example.com</code>, and |
59 * <code>com</code>, in that order. | 59 * <code>com</code>, in that order. |
60 * | 60 * |
61 * @param {string} domain The domain. | 61 * @param {string} domain The domain. |
62 * @param {boolean} [includeBlank] Whether to include the blank suffix at the | 62 * @param {boolean} [includeBlank] Whether to include the blank suffix at the |
63 * end. | 63 * end. |
64 * | 64 * |
65 * @yields {string} The next suffix for the domain. | 65 * @yields {string} The next suffix for the domain. |
66 */ | 66 */ |
67 function* suffixes(domain, includeBlank = false) | 67 function* domainSuffixes(domain, includeBlank = false) |
68 { | 68 { |
69 while (domain != "") | 69 while (domain != "") |
70 { | 70 { |
71 yield domain; | 71 yield domain; |
72 | 72 |
73 let dotIndex = domain.indexOf("."); | 73 let dotIndex = domain.indexOf("."); |
74 domain = dotIndex == -1 ? "" : domain.substr(dotIndex + 1); | 74 domain = dotIndex == -1 ? "" : domain.substr(dotIndex + 1); |
75 } | 75 } |
76 | 76 |
77 if (includeBlank) | 77 if (includeBlank) |
78 yield ""; | 78 yield ""; |
79 } | 79 } |
80 | 80 |
81 exports.suffixes = suffixes; | 81 exports.domainSuffixes = domainSuffixes; |
82 | 82 |
83 /** | 83 /** |
84 * Checks whether the given hostname is a domain. | 84 * Checks whether the given hostname is a domain. |
85 * | 85 * |
86 * @param {string} hostname | 86 * @param {string} hostname |
87 * @returns {boolean} | 87 * @returns {boolean} |
88 */ | 88 */ |
89 function isDomain(hostname) | 89 function isDomain(hostname) |
90 { | 90 { |
91 // No hostname or IPv4 address, also considering hexadecimal octets. | 91 // No hostname or IPv4 address, also considering hexadecimal octets. |
92 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) | 92 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) |
93 return false; | 93 return false; |
94 | 94 |
95 // IPv6 address. Since there can't be colons in domains, we can | 95 // IPv6 address. Since there can't be colons in domains, we can |
96 // just check whether there are any colons to exclude IPv6 addresses. | 96 // just check whether there are any colons to exclude IPv6 addresses. |
97 return hostname.indexOf(":") == -1; | 97 return hostname.indexOf(":") == -1; |
98 } | 98 } |
99 | 99 |
100 /** | 100 /** |
101 * Gets the base domain for the given hostname. | 101 * Gets the base domain for the given hostname. |
102 * | 102 * |
103 * @param {string} hostname | 103 * @param {string} hostname |
104 * @returns {string} | 104 * @returns {string} |
105 */ | 105 */ |
106 function getDomain(hostname) | 106 function getBaseDomain(hostname) |
107 { | 107 { |
108 let slices = []; | 108 let slices = []; |
109 let cutoff = NaN; | 109 let cutoff = NaN; |
110 | 110 |
111 for (let suffix of suffixes(hostname)) | 111 for (let suffix of domainSuffixes(hostname)) |
112 { | 112 { |
113 slices.push(suffix); | 113 slices.push(suffix); |
114 | 114 |
115 let offset = publicSuffixMap.get(suffix); | 115 let offset = publicSuffixMap.get(suffix); |
116 | 116 |
117 if (typeof offset != "undefined") | 117 if (typeof offset != "undefined") |
118 { | 118 { |
119 cutoff = slices.length - 1 - offset; | 119 cutoff = slices.length - 1 - offset; |
120 break; | 120 break; |
121 } | 121 } |
122 } | 122 } |
123 | 123 |
124 if (isNaN(cutoff)) | 124 if (isNaN(cutoff)) |
125 return slices.length > 2 ? slices[slices.length - 2] : hostname; | 125 return slices.length > 2 ? slices[slices.length - 2] : hostname; |
126 | 126 |
127 if (cutoff <= 0) | 127 if (cutoff <= 0) |
128 return hostname; | 128 return hostname; |
129 | 129 |
130 return slices[cutoff]; | 130 return slices[cutoff]; |
131 } | 131 } |
132 | 132 |
133 exports.getDomain = getDomain; | 133 exports.getBaseDomain = getBaseDomain; |
134 | 134 |
135 /** | 135 /** |
136 * Checks whether a request's origin is different from its document's origin. | 136 * Checks whether a request's origin is different from its document's origin. |
137 * | 137 * |
138 * @param {URL} url The request URL. | 138 * @param {URL} url The request URL. |
139 * @param {string} documentHostname The IDNA-encoded hostname of the document. | 139 * @param {string} documentHostname The IDNA-encoded hostname of the document. |
140 * | 140 * |
141 * @returns {boolean} | 141 * @returns {boolean} |
142 */ | 142 */ |
143 function isThirdParty(url, documentHostname) | 143 function isThirdParty(url, documentHostname) |
144 { | 144 { |
145 let requestHostname = url.hostname; | 145 let requestHostname = url.hostname; |
146 | 146 |
147 if (requestHostname[requestHostname.length - 1] == ".") | 147 if (requestHostname[requestHostname.length - 1] == ".") |
148 requestHostname = requestHostname.replace(/\.+$/, ""); | 148 requestHostname = requestHostname.replace(/\.+$/, ""); |
149 | 149 |
150 if (documentHostname[documentHostname.length - 1] == ".") | 150 if (documentHostname[documentHostname.length - 1] == ".") |
151 documentHostname = documentHostname.replace(/\.+$/, ""); | 151 documentHostname = documentHostname.replace(/\.+$/, ""); |
152 | 152 |
153 if (requestHostname == documentHostname) | 153 if (requestHostname == documentHostname) |
154 return false; | 154 return false; |
155 | 155 |
156 if (!isDomain(requestHostname) || !isDomain(documentHostname)) | 156 if (!isDomain(requestHostname) || !isDomain(documentHostname)) |
157 return true; | 157 return true; |
158 | 158 |
159 return getDomain(requestHostname) != getDomain(documentHostname); | 159 return getBaseDomain(requestHostname) != getBaseDomain(documentHostname); |
160 } | 160 } |
161 | 161 |
162 exports.isThirdParty = isThirdParty; | 162 exports.isThirdParty = isThirdParty; |
OLD | NEW |