| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 /** | 80 /** |
| 81 * Cache for known filters, maps string representation to filter objects. | 81 * Cache for known filters, maps string representation to filter objects. |
| 82 * @type {Map.<string,Filter>} | 82 * @type {Map.<string,Filter>} |
| 83 */ | 83 */ |
| 84 Filter.knownFilters = new Map(); | 84 Filter.knownFilters = new Map(); |
| 85 | 85 |
| 86 /** | 86 /** |
| 87 * Regular expression that element hiding filters should match | 87 * Regular expression that element hiding filters should match |
| 88 * @type {RegExp} | 88 * @type {RegExp} |
| 89 */ | 89 */ |
| 90 Filter.elemhideRegExp = /^([^/*|@"!]*?)(#([@?])?#)(.+)$/; | 90 Filter.elemhideRegExp = /^([^/*|@"!]*?)#([@?])?#(.+)$/; |
| 91 /** | 91 /** |
| 92 * Regular expression that RegExp filters specified as RegExps should match | 92 * Regular expression that RegExp filters specified as RegExps should match |
| 93 * @type {RegExp} | 93 * @type {RegExp} |
| 94 */ | 94 */ |
| 95 Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^, \s]+)?)*)?$/; | 95 Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^, \s]+)?)*)?$/; |
| 96 /** | 96 /** |
| 97 * Regular expression that options on a RegExp filter should match | 97 * Regular expression that options on a RegExp filter should match |
| 98 * @type {RegExp} | 98 * @type {RegExp} |
| 99 */ | 99 */ |
| 100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$/; | 100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$/; |
| 101 /** | |
| 102 * Regular expression that matches an invalid Content Security Policy | |
| 103 * @type {RegExp} | |
| 104 */ | |
| 105 Filter.invalidCSPRegExp = /(;|^) ?(base-uri|referrer|report-to|report-uri|upgrad e-insecure-requests)\b/i; | |
| 101 | 106 |
| 102 /** | 107 /** |
| 103 * Creates a filter of correct type from its text representation - does the | 108 * Creates a filter of correct type from its text representation - does the |
| 104 * basic normalisation and parsing where possible before deferring to the right | 109 * basic parsing and calls the right constructor then. |
| 105 * constructor. | 110 * |
| 106 * @param {string} text as in Filter() | 111 * @param {string} text as in Filter() |
| 107 * @return {Filter} | 112 * @return {Filter} |
| 108 */ | 113 */ |
| 109 Filter.fromText = function(text) | 114 Filter.fromText = function(text) |
| 110 { | 115 { |
| 111 let filter = Filter.knownFilters.get(text); | 116 let filter = Filter.knownFilters.get(text); |
| 112 if (filter) | 117 if (filter) |
| 113 return filter; | 118 return filter; |
| 114 | 119 |
| 115 let match = (text.includes("#") ? Filter.elemhideRegExp.exec(text) : null); | 120 let match = (text.includes("#") ? Filter.elemhideRegExp.exec(text) : null); |
| 116 if (match) | 121 if (match) |
| 117 { | 122 { |
| 118 let [, domain, seperator, type, selector] = match; | |
| 119 | |
| 120 domain = domain.replace(/\s/g, ""); | |
| 121 selector = selector.trim(); | |
| 122 | |
| 123 let propsMatch; | 123 let propsMatch; |
| 124 if (!type && | 124 if (!match[2] && |
| 125 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(selector))) | 125 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(match[3]))) |
| 126 { | 126 { |
| 127 // This is legacy CSS properties syntax, convert to current syntax | 127 // This is legacy CSS properties syntax, convert to current syntax |
| 128 let prefix = selector.substr(0, propsMatch.index); | 128 let prefix = match[3].substr(0, propsMatch.index); |
| 129 let expression = propsMatch[2]; | 129 let expression = propsMatch[2]; |
| 130 let suffix = selector.substr(propsMatch.index + propsMatch[0].length); | 130 let suffix = match[3].substr(propsMatch.index + propsMatch[0].length); |
| 131 return Filter.fromText(`${domain}#?#` + | 131 return Filter.fromText(`${match[1]}#?#` + |
| 132 `${prefix}:-abp-properties(${expression})${suffix}`); | 132 `${prefix}:-abp-properties(${expression})${suffix}`); |
| 133 } | 133 } |
| 134 | 134 |
| 135 filter = ElemHideBase.fromText( | 135 filter = ElemHideBase.fromText( |
| 136 domain + seperator + selector, domain, type, selector | 136 text, match[1], match[2], match[3] |
| 137 ); | 137 ); |
| 138 } | 138 } |
| 139 else if (text[0] == "!") | 139 else if (text[0] == "!") |
| 140 filter = new CommentFilter(text); | 140 filter = new CommentFilter(text); |
| 141 else | 141 else |
| 142 filter = RegExpFilter.fromText(text); | 142 filter = RegExpFilter.fromText(text); |
| 143 | 143 |
| 144 Filter.knownFilters.set(filter.text, filter); | 144 Filter.knownFilters.set(filter.text, filter); |
| 145 return filter; | 145 return filter; |
| 146 }; | 146 }; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 160 filter._disabled = (obj.disabled == "true"); | 160 filter._disabled = (obj.disabled == "true"); |
| 161 if ("hitCount" in obj) | 161 if ("hitCount" in obj) |
| 162 filter._hitCount = parseInt(obj.hitCount, 10) || 0; | 162 filter._hitCount = parseInt(obj.hitCount, 10) || 0; |
| 163 if ("lastHit" in obj) | 163 if ("lastHit" in obj) |
| 164 filter._lastHit = parseInt(obj.lastHit, 10) || 0; | 164 filter._lastHit = parseInt(obj.lastHit, 10) || 0; |
| 165 } | 165 } |
| 166 return filter; | 166 return filter; |
| 167 }; | 167 }; |
| 168 | 168 |
| 169 /** | 169 /** |
| 170 * Strip linebreaks etc and then trim whitespace from the filter text. | 170 * Removes unnecessary whitespaces from filter text, will only return null if |
| 171 * Will only return null if the input parameter is null. | 171 * the input parameter is null. |
| 172 * @param {string} text | 172 * @param {string} text |
| 173 * @return {string} | 173 * @return {string} |
| 174 */ | 174 */ |
| 175 Filter.stripJunk = function(text) | 175 Filter.normalize = function(text) |
| 176 { | 176 { |
| 177 if (!text) | 177 if (!text) |
| 178 return text; | 178 return text; |
| 179 return text.replace(/[^\S ]/g, "").trim(); | 179 |
| 180 // Remove line breaks, tabs etc | |
| 181 text = text.replace(/[^\S ]+/g, ""); | |
| 182 | |
| 183 // Don't remove spaces inside comments | |
| 184 if (/^ *!/.test(text)) | |
| 185 return text.trim(); | |
| 186 | |
| 187 // Special treatment for element hiding filters, right side is allowed to | |
| 188 // contain spaces | |
| 189 if (Filter.elemhideRegExp.test(text)) | |
| 190 { | |
| 191 let [, domain, separator, selector] = /^(.*?)(#@?#?)(.*)$/.exec(text); | |
| 192 return domain.replace(/ +/g, "") + separator + selector.trim(); | |
| 193 } | |
| 194 | |
| 195 // For most regexp filters we strip all spaces, but $csp filter options | |
| 196 // are allowed to contain single (non trailing) spaces. | |
| 197 let strippedText = text.replace(/ +/g, ""); | |
| 198 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) | |
| 199 return strippedText; | |
| 200 | |
| 201 let optionsMatch = Filter.optionsRegExp.exec(strippedText); | |
| 202 if (!optionsMatch) | |
| 203 return strippedText; | |
| 204 | |
| 205 // For $csp filters we must first separate out the options part of the | |
| 206 // text, being careful to preserve its spaces. | |
| 207 let beforeOptions = strippedText.substring(0, optionsMatch.index); | |
| 208 let strippedDollarIndex = -1; | |
| 209 let dollarIndex = -1; | |
| 210 do | |
| 211 { | |
| 212 strippedDollarIndex = beforeOptions.indexOf("$", strippedDollarIndex + 1); | |
| 213 dollarIndex = text.indexOf("$", dollarIndex + 1); | |
| 214 } | |
| 215 while (strippedDollarIndex != -1); | |
| 216 let optionsText = text.substr(dollarIndex + 1); | |
| 217 | |
| 218 // Then we can normalize spaces in the options part safely | |
| 219 let options = optionsText.split(","); | |
| 220 for (let i = 0; i < options.length; i++) | |
| 221 { | |
| 222 let option = options[i]; | |
| 223 let cspMatch = /^ *c *s *p *=/i.exec(option); | |
| 224 if (cspMatch) | |
| 225 { | |
| 226 options[i] = cspMatch[0].replace(/ +/g, "") + | |
| 227 option.substr(cspMatch[0].length).trim().replace(/ +/g, " "); | |
| 228 } | |
| 229 else | |
| 230 options[i] = option.replace(/ +/g, ""); | |
| 231 } | |
| 232 | |
| 233 return beforeOptions + "$" + options.join(); | |
| 180 }; | 234 }; |
| 181 | 235 |
| 182 /** | 236 /** |
| 183 * @see filterToRegExp | 237 * @see filterToRegExp |
| 184 */ | 238 */ |
| 185 Filter.toRegExp = filterToRegExp; | 239 Filter.toRegExp = filterToRegExp; |
| 186 | 240 |
| 187 /** | 241 /** |
| 188 * Class for invalid filters | 242 * Class for invalid filters |
| 189 * @param {string} text see Filter() | 243 * @param {string} text see Filter() |
| (...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 695 get() { return this; } | 749 get() { return this; } |
| 696 }); | 750 }); |
| 697 | 751 |
| 698 /** | 752 /** |
| 699 * Creates a RegExp filter from its text representation | 753 * Creates a RegExp filter from its text representation |
| 700 * @param {string} text same as in Filter() | 754 * @param {string} text same as in Filter() |
| 701 * @return {Filter} | 755 * @return {Filter} |
| 702 */ | 756 */ |
| 703 RegExpFilter.fromText = function(text) | 757 RegExpFilter.fromText = function(text) |
| 704 { | 758 { |
| 759 let blocking = true; | |
| 760 let origText = text; | |
| 761 if (text.indexOf("@@") == 0) | |
| 762 { | |
| 763 blocking = false; | |
| 764 text = text.substr(2); | |
| 765 } | |
| 766 | |
| 705 let contentType = null; | 767 let contentType = null; |
| 706 let csp = null; | |
| 707 let matchCase = null; | 768 let matchCase = null; |
| 708 let domains = null; | 769 let domains = null; |
| 709 let sitekeys = null; | 770 let sitekeys = null; |
| 710 let thirdParty = null; | 771 let thirdParty = null; |
| 711 let collapse = null; | 772 let collapse = null; |
| 712 let origText; | 773 let csp = null; |
| 713 | 774 let options; |
| 714 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); | 775 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); |
| 715 if (!match) | 776 if (match) |
| 716 { | 777 { |
| 717 origText = text = text.replace(/\s/g, ""); | 778 options = match[1].split(","); |
| 718 } | 779 text = match.input.substr(0, match.index); |
| 719 else | 780 for (let option of options) |
| 720 { | |
| 721 text = match.input.substring(0, match.index).replace(/\s/g, ""); | |
| 722 | |
| 723 // Strip all whitespace from the options string, except for CSP values which | |
| 724 // we only trim. | |
| 725 let rawOptions = match[1]; | |
| 726 let options = ""; | |
| 727 let offset = 0; | |
| 728 let cspMatch; | |
| 729 let cspRegexp = /((?:^|,)csp=)([^,]+)/gi; | |
| 730 while ((cspMatch = cspRegexp.exec(rawOptions))) | |
| 731 { | |
| 732 options += rawOptions.substring(offset, cspMatch.index).replace(/\s/g, "") ; | |
| 733 options += cspMatch[1] + cspMatch[2].trim(); | |
| 734 offset = cspRegexp.lastIndex; | |
| 735 } | |
| 736 options += rawOptions.substring(offset).replace(/\s/g, ""); | |
| 737 | |
| 738 origText = text + "$" + options; | |
| 739 | |
| 740 for (let option of options.toUpperCase().split(",")) | |
| 741 { | 781 { |
| 742 let value = null; | 782 let value = null; |
| 743 let separatorIndex = option.indexOf("="); | 783 let separatorIndex = option.indexOf("="); |
| 744 if (separatorIndex >= 0) | 784 if (separatorIndex >= 0) |
| 745 { | 785 { |
| 746 value = option.substr(separatorIndex + 1); | 786 value = option.substr(separatorIndex + 1); |
| 747 option = option.substring(0, separatorIndex); | 787 option = option.substr(0, separatorIndex); |
| 748 } | 788 } |
| 749 option = option.replace(/-/, "_"); | 789 option = option.replace(/-/, "_").toUpperCase(); |
| 750 if (option in RegExpFilter.typeMap) | 790 if (option in RegExpFilter.typeMap) |
| 751 { | 791 { |
| 752 if (contentType == null) | 792 if (contentType == null) |
| 753 contentType = 0; | 793 contentType = 0; |
| 754 contentType |= RegExpFilter.typeMap[option]; | 794 contentType |= RegExpFilter.typeMap[option]; |
| 755 | 795 |
| 756 if (option == "CSP" && typeof value == "string") | 796 if (option == "CSP" && typeof value != "undefined") |
| 757 { | 797 csp = value; |
| 758 csp = value.toLowerCase(); | |
|
Manish Jethani
2018/02/06 05:54:20
We could avoid uppercasing and then lowercasing th
kzar
2018/03/06 15:37:53
Done.
| |
| 759 // Quick check to prevent report-uri and report-to directives | |
| 760 if (csp.includes("report-")) | |
|
Manish Jethani
2018/02/06 05:54:20
Couldn't the value contain "report-" in a differen
kzar
2018/03/06 15:37:53
Done.
| |
| 761 return new InvalidFilter(origText, "filter_invalid_csp"); | |
| 762 } | |
| 763 } | 798 } |
| 764 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) | 799 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) |
| 765 { | 800 { |
| 766 if (contentType == null) | 801 if (contentType == null) |
| 767 ({contentType} = RegExpFilter.prototype); | 802 ({contentType} = RegExpFilter.prototype); |
| 768 contentType &= ~RegExpFilter.typeMap[option.substr(1)]; | 803 contentType &= ~RegExpFilter.typeMap[option.substr(1)]; |
| 769 } | 804 } |
| 770 else if (option == "MATCH_CASE") | 805 else if (option == "MATCH_CASE") |
| 771 matchCase = true; | 806 matchCase = true; |
| 772 else if (option == "~MATCH_CASE") | 807 else if (option == "~MATCH_CASE") |
| 773 matchCase = false; | 808 matchCase = false; |
| 774 else if (option == "DOMAIN" && typeof value != "undefined") | 809 else if (option == "DOMAIN" && typeof value != "undefined") |
| 775 domains = value; | 810 domains = value.toUpperCase(); |
| 776 else if (option == "THIRD_PARTY") | 811 else if (option == "THIRD_PARTY") |
| 777 thirdParty = true; | 812 thirdParty = true; |
| 778 else if (option == "~THIRD_PARTY") | 813 else if (option == "~THIRD_PARTY") |
| 779 thirdParty = false; | 814 thirdParty = false; |
| 780 else if (option == "COLLAPSE") | 815 else if (option == "COLLAPSE") |
| 781 collapse = true; | 816 collapse = true; |
| 782 else if (option == "~COLLAPSE") | 817 else if (option == "~COLLAPSE") |
| 783 collapse = false; | 818 collapse = false; |
| 784 else if (option == "SITEKEY" && typeof value != "undefined") | 819 else if (option == "SITEKEY" && typeof value != "undefined") |
| 785 sitekeys = value; | 820 sitekeys = value.toUpperCase(); |
| 786 else | 821 else |
| 787 return new InvalidFilter(origText, "filter_unknown_option"); | 822 return new InvalidFilter(origText, "filter_unknown_option"); |
| 788 } | 823 } |
| 789 } | 824 } |
| 790 | 825 |
| 791 try | 826 try |
| 792 { | 827 { |
| 793 if (text.indexOf("@@") == 0) | 828 if (blocking) |
| 794 { | 829 { |
| 795 return new WhitelistFilter(origText, text.substr(2), contentType, | 830 if (csp && Filter.invalidCSPRegExp.test(csp)) |
| 796 matchCase, domains, thirdParty, sitekeys); | 831 return new InvalidFilter(origText, "filter_invalid_csp"); |
| 797 } | 832 |
| 798 return new BlockingFilter(origText, text, contentType, matchCase, domains, | 833 return new BlockingFilter(origText, text, contentType, matchCase, domains, |
| 799 thirdParty, sitekeys, collapse, csp); | 834 thirdParty, sitekeys, collapse, csp); |
| 835 } | |
| 836 return new WhitelistFilter(origText, text, contentType, matchCase, domains, | |
| 837 thirdParty, sitekeys); | |
| 800 } | 838 } |
| 801 catch (e) | 839 catch (e) |
| 802 { | 840 { |
| 803 return new InvalidFilter(origText, "filter_invalid_regexp"); | 841 return new InvalidFilter(origText, "filter_invalid_regexp"); |
| 804 } | 842 } |
| 805 }; | 843 }; |
| 806 | 844 |
| 807 /** | 845 /** |
| 808 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks | 846 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks |
| 809 */ | 847 */ |
| 810 RegExpFilter.typeMap = { | 848 RegExpFilter.typeMap = { |
| 811 OTHER: 1, | 849 OTHER: 1, |
| 812 SCRIPT: 2, | 850 SCRIPT: 2, |
| 813 IMAGE: 4, | 851 IMAGE: 4, |
| 814 STYLESHEET: 8, | 852 STYLESHEET: 8, |
| 815 OBJECT: 16, | 853 OBJECT: 16, |
| 816 SUBDOCUMENT: 32, | 854 SUBDOCUMENT: 32, |
| 817 DOCUMENT: 64, | 855 DOCUMENT: 64, |
| 818 WEBSOCKET: 128, | 856 WEBSOCKET: 128, |
| 857 WEBRTC: 256, | |
| 819 CSP: 512, | 858 CSP: 512, |
|
Manish Jethani
2018/02/06 05:54:20
Perhaps a good idea to place CSP after WEBRTC?
kzar
2018/03/06 15:37:53
Done.
| |
| 820 WEBRTC: 256, | |
| 821 XBL: 1, | 859 XBL: 1, |
| 822 PING: 1024, | 860 PING: 1024, |
| 823 XMLHTTPREQUEST: 2048, | 861 XMLHTTPREQUEST: 2048, |
| 824 OBJECT_SUBREQUEST: 4096, | 862 OBJECT_SUBREQUEST: 4096, |
| 825 DTD: 1, | 863 DTD: 1, |
| 826 MEDIA: 16384, | 864 MEDIA: 16384, |
| 827 FONT: 32768, | 865 FONT: 32768, |
| 828 | 866 |
| 829 BACKGROUND: 4, // Backwards compat, same as IMAGE | 867 BACKGROUND: 4, // Backwards compat, same as IMAGE |
| 830 | 868 |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1042 */ | 1080 */ |
| 1043 function ElemHideEmulationFilter(text, domains, selector) | 1081 function ElemHideEmulationFilter(text, domains, selector) |
| 1044 { | 1082 { |
| 1045 ElemHideBase.call(this, text, domains, selector); | 1083 ElemHideBase.call(this, text, domains, selector); |
| 1046 } | 1084 } |
| 1047 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; | 1085 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; |
| 1048 | 1086 |
| 1049 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { | 1087 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { |
| 1050 type: "elemhideemulation" | 1088 type: "elemhideemulation" |
| 1051 }); | 1089 }); |
| LEFT | RIGHT |