| Left: | ||
| Right: | 
| 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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^,\s]+)?)*)$/; | 100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$/; | 
| 101 | |
| 102 /** | |
| 103 * Forbidden Content Security Policy directives | |
| 104 * @type {Set<string>} | |
| 105 */ | |
| 106 Filter.cspDirectiveBlacklist = new Set([ | |
| 107 "base-uri", "referrer", "report-to", "report-uri", "upgrade-insecure-requests" | |
| 108 ]); | |
| 
 
Sebastian Noack
2018/03/21 16:23:13
I don't get why we create this Set here, just to t
 
kzar
2018/03/21 16:58:11
I created the Set since I figured being about to d
 
 | |
| 109 /** | |
| 110 * Regular expression that matches an invalid Content Security Policy | |
| 111 * @type {RegExp} | |
| 112 */ | |
| 113 Filter.invalidCSPRegExp = new RegExp( | |
| 114 "(;|^) ?(" + Array.from(Filter.cspDirectiveBlacklist).join("|") + ")\\b" | |
| 115 ); | |
| 101 | 116 | 
| 102 /** | 117 /** | 
| 103 * Creates a filter of correct type from its text representation - does the | 118 * Creates a filter of correct type from its text representation - does the | 
| 104 * basic parsing and calls the right constructor then. | 119 * basic parsing and calls the right constructor then. | 
| 105 * | 120 * | 
| 106 * @param {string} text as in Filter() | 121 * @param {string} text as in Filter() | 
| 107 * @return {Filter} | 122 * @return {Filter} | 
| 108 */ | 123 */ | 
| 109 Filter.fromText = function(text) | 124 Filter.fromText = function(text) | 
| 110 { | 125 { | 
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 * Removes unnecessary whitespaces from filter text, will only return null if | 180 * Removes unnecessary whitespaces from filter text, will only return null if | 
| 166 * the input parameter is null. | 181 * the input parameter is null. | 
| 167 * @param {string} text | 182 * @param {string} text | 
| 168 * @return {string} | 183 * @return {string} | 
| 169 */ | 184 */ | 
| 170 Filter.normalize = function(text) | 185 Filter.normalize = function(text) | 
| 171 { | 186 { | 
| 172 if (!text) | 187 if (!text) | 
| 173 return text; | 188 return text; | 
| 174 | 189 | 
| 175 // Remove line breaks and such | 190 // Remove line breaks, tabs etc | 
| 176 text = text.replace(/[^\S ]/g, ""); | 191 text = text.replace(/[^\S ]+/g, ""); | 
| 177 | 192 | 
| 178 if (/^\s*!/.test(text)) | 193 // Don't remove spaces inside comments | 
| 194 if (/^ *!/.test(text)) | |
| 195 return text.trim(); | |
| 196 | |
| 197 // Special treatment for element hiding filters, right side is allowed to | |
| 198 // contain spaces | |
| 199 if (Filter.elemhideRegExp.test(text)) | |
| 179 { | 200 { | 
| 180 // Don't remove spaces inside comments | 201 let [, domain, separator, selector] = /^(.*?)(#@?#?)(.*)$/.exec(text); | 
| 181 return text.trim(); | 202 return domain.replace(/ +/g, "") + separator + selector.trim(); | 
| 182 } | 203 } | 
| 183 else if (Filter.elemhideRegExp.test(text)) | 204 | 
| 205 // For most regexp filters we strip all spaces, but $csp filter options | |
| 206 // are allowed to contain single (non trailing) spaces. | |
| 207 let strippedText = text.replace(/ +/g, ""); | |
| 208 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) | |
| 209 return strippedText; | |
| 210 | |
| 211 let optionsMatch = Filter.optionsRegExp.exec(strippedText); | |
| 212 if (!optionsMatch) | |
| 213 return strippedText; | |
| 214 | |
| 215 // For $csp filters we must first separate out the options part of the | |
| 216 // text, being careful to preserve its spaces. | |
| 217 let beforeOptions = strippedText.substring(0, optionsMatch.index); | |
| 218 let strippedDollarIndex = -1; | |
| 219 let dollarIndex = -1; | |
| 220 do | |
| 184 { | 221 { | 
| 185 // Special treatment for element hiding filters, right side is allowed to | 222 strippedDollarIndex = beforeOptions.indexOf("$", strippedDollarIndex + 1); | 
| 186 // contain spaces | 223 dollarIndex = text.indexOf("$", dollarIndex + 1); | 
| 187 let [, domain, separator, selector] = /^(.*?)(#@?#?)(.*)$/.exec(text); | |
| 188 return domain.replace(/\s/g, "") + separator + selector.trim(); | |
| 189 } | 224 } | 
| 190 return text.replace(/\s/g, ""); | 225 while (strippedDollarIndex != -1); | 
| 226 let optionsText = text.substr(dollarIndex + 1); | |
| 227 | |
| 228 // Then we can normalize spaces in the options part safely | |
| 229 let options = optionsText.split(","); | |
| 230 for (let i = 0; i < options.length; i++) | |
| 231 { | |
| 232 let option = options[i]; | |
| 233 let cspMatch = /^ *c *s *p *=/i.exec(option); | |
| 234 if (cspMatch) | |
| 235 { | |
| 236 options[i] = cspMatch[0].replace(/ +/g, "") + | |
| 237 option.substr(cspMatch[0].length).trim().replace(/ +/g, " "); | |
| 238 } | |
| 239 else | |
| 240 options[i] = option.replace(/ +/g, ""); | |
| 241 } | |
| 242 | |
| 243 return beforeOptions + "$" + options.join(); | |
| 191 }; | 244 }; | 
| 192 | 245 | 
| 193 /** | 246 /** | 
| 194 * @see filterToRegExp | 247 * @see filterToRegExp | 
| 195 */ | 248 */ | 
| 196 Filter.toRegExp = filterToRegExp; | 249 Filter.toRegExp = filterToRegExp; | 
| 197 | 250 | 
| 198 /** | 251 /** | 
| 199 * Class for invalid filters | 252 * Class for invalid filters | 
| 200 * @param {string} text see Filter() | 253 * @param {string} text see Filter() | 
| (...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 720 blocking = false; | 773 blocking = false; | 
| 721 text = text.substr(2); | 774 text = text.substr(2); | 
| 722 } | 775 } | 
| 723 | 776 | 
| 724 let contentType = null; | 777 let contentType = null; | 
| 725 let matchCase = null; | 778 let matchCase = null; | 
| 726 let domains = null; | 779 let domains = null; | 
| 727 let sitekeys = null; | 780 let sitekeys = null; | 
| 728 let thirdParty = null; | 781 let thirdParty = null; | 
| 729 let collapse = null; | 782 let collapse = null; | 
| 783 let csp = null; | |
| 730 let options; | 784 let options; | 
| 731 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); | 785 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); | 
| 732 if (match) | 786 if (match) | 
| 733 { | 787 { | 
| 734 options = match[1].toUpperCase().split(","); | 788 options = match[1].split(","); | 
| 735 text = match.input.substr(0, match.index); | 789 text = match.input.substr(0, match.index); | 
| 736 for (let option of options) | 790 for (let option of options) | 
| 737 { | 791 { | 
| 738 let value = null; | 792 let value = null; | 
| 739 let separatorIndex = option.indexOf("="); | 793 let separatorIndex = option.indexOf("="); | 
| 740 if (separatorIndex >= 0) | 794 if (separatorIndex >= 0) | 
| 741 { | 795 { | 
| 742 value = option.substr(separatorIndex + 1); | 796 value = option.substr(separatorIndex + 1); | 
| 743 option = option.substr(0, separatorIndex); | 797 option = option.substr(0, separatorIndex); | 
| 744 } | 798 } | 
| 745 option = option.replace(/-/, "_"); | 799 option = option.replace(/-/, "_").toUpperCase(); | 
| 746 if (option in RegExpFilter.typeMap) | 800 if (option in RegExpFilter.typeMap) | 
| 747 { | 801 { | 
| 748 if (contentType == null) | 802 if (contentType == null) | 
| 749 contentType = 0; | 803 contentType = 0; | 
| 750 contentType |= RegExpFilter.typeMap[option]; | 804 contentType |= RegExpFilter.typeMap[option]; | 
| 805 | |
| 806 if (option == "CSP" && typeof value != "undefined") | |
| 807 { | |
| 808 if (csp) | |
| 809 csp.push(value); | |
| 
 
Sebastian Noack
2018/03/21 16:23:13
Does that mean that if multiple $csp options are g
 
kzar
2018/03/21 16:58:11
Right.
 
 | |
| 810 else | |
| 811 csp = [value]; | |
| 812 } | |
| 751 } | 813 } | 
| 752 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) | 814 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) | 
| 753 { | 815 { | 
| 754 if (contentType == null) | 816 if (contentType == null) | 
| 755 ({contentType} = RegExpFilter.prototype); | 817 ({contentType} = RegExpFilter.prototype); | 
| 756 contentType &= ~RegExpFilter.typeMap[option.substr(1)]; | 818 contentType &= ~RegExpFilter.typeMap[option.substr(1)]; | 
| 757 } | 819 } | 
| 758 else if (option == "MATCH_CASE") | 820 else if (option == "MATCH_CASE") | 
| 759 matchCase = true; | 821 matchCase = true; | 
| 760 else if (option == "~MATCH_CASE") | 822 else if (option == "~MATCH_CASE") | 
| 761 matchCase = false; | 823 matchCase = false; | 
| 762 else if (option == "DOMAIN" && typeof value != "undefined") | 824 else if (option == "DOMAIN" && typeof value != "undefined") | 
| 763 domains = value; | 825 domains = value.toUpperCase(); | 
| 764 else if (option == "THIRD_PARTY") | 826 else if (option == "THIRD_PARTY") | 
| 765 thirdParty = true; | 827 thirdParty = true; | 
| 766 else if (option == "~THIRD_PARTY") | 828 else if (option == "~THIRD_PARTY") | 
| 767 thirdParty = false; | 829 thirdParty = false; | 
| 768 else if (option == "COLLAPSE") | 830 else if (option == "COLLAPSE") | 
| 769 collapse = true; | 831 collapse = true; | 
| 770 else if (option == "~COLLAPSE") | 832 else if (option == "~COLLAPSE") | 
| 771 collapse = false; | 833 collapse = false; | 
| 772 else if (option == "SITEKEY" && typeof value != "undefined") | 834 else if (option == "SITEKEY" && typeof value != "undefined") | 
| 773 sitekeys = value; | 835 sitekeys = value.toUpperCase(); | 
| 774 else | 836 else | 
| 775 return new InvalidFilter(origText, "filter_unknown_option"); | 837 return new InvalidFilter(origText, "filter_unknown_option"); | 
| 776 } | 838 } | 
| 777 } | 839 } | 
| 778 | 840 | 
| 779 try | 841 try | 
| 780 { | 842 { | 
| 781 if (blocking) | 843 if (blocking) | 
| 782 { | 844 { | 
| 845 if (csp) | |
| 846 { | |
| 847 csp = csp.join("; ").toLowerCase(); | |
| 
 
Sebastian Noack
2018/03/21 16:23:13
If we convert the CSP to lower case, shouldn't thi
 
kzar
2018/03/21 16:58:11
Hmm good point, initially I converted the value to
 
kzar
2018/03/21 16:58:11
Yes and no.
While I see your logic of course we c
 
 | |
| 848 | |
| 849 if (Filter.invalidCSPRegExp.test(csp)) | |
| 850 return new InvalidFilter(origText, "filter_invalid_csp"); | |
| 851 } | |
| 852 | |
| 783 return new BlockingFilter(origText, text, contentType, matchCase, domains, | 853 return new BlockingFilter(origText, text, contentType, matchCase, domains, | 
| 784 thirdParty, sitekeys, collapse); | 854 thirdParty, sitekeys, collapse, csp); | 
| 785 } | 855 } | 
| 786 return new WhitelistFilter(origText, text, contentType, matchCase, domains, | 856 return new WhitelistFilter(origText, text, contentType, matchCase, domains, | 
| 787 thirdParty, sitekeys); | 857 thirdParty, sitekeys); | 
| 788 } | 858 } | 
| 789 catch (e) | 859 catch (e) | 
| 790 { | 860 { | 
| 791 return new InvalidFilter(origText, "filter_invalid_regexp"); | 861 return new InvalidFilter(origText, "filter_invalid_regexp"); | 
| 792 } | 862 } | 
| 793 }; | 863 }; | 
| 794 | 864 | 
| 795 /** | 865 /** | 
| 796 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks | 866 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks | 
| 797 */ | 867 */ | 
| 798 RegExpFilter.typeMap = { | 868 RegExpFilter.typeMap = { | 
| 799 OTHER: 1, | 869 OTHER: 1, | 
| 800 SCRIPT: 2, | 870 SCRIPT: 2, | 
| 801 IMAGE: 4, | 871 IMAGE: 4, | 
| 802 STYLESHEET: 8, | 872 STYLESHEET: 8, | 
| 803 OBJECT: 16, | 873 OBJECT: 16, | 
| 804 SUBDOCUMENT: 32, | 874 SUBDOCUMENT: 32, | 
| 805 DOCUMENT: 64, | 875 DOCUMENT: 64, | 
| 806 WEBSOCKET: 128, | 876 WEBSOCKET: 128, | 
| 807 WEBRTC: 256, | 877 WEBRTC: 256, | 
| 878 CSP: 512, | |
| 808 XBL: 1, | 879 XBL: 1, | 
| 809 PING: 1024, | 880 PING: 1024, | 
| 810 XMLHTTPREQUEST: 2048, | 881 XMLHTTPREQUEST: 2048, | 
| 811 OBJECT_SUBREQUEST: 4096, | 882 OBJECT_SUBREQUEST: 4096, | 
| 812 DTD: 1, | 883 DTD: 1, | 
| 813 MEDIA: 16384, | 884 MEDIA: 16384, | 
| 814 FONT: 32768, | 885 FONT: 32768, | 
| 815 | 886 | 
| 816 BACKGROUND: 4, // Backwards compat, same as IMAGE | 887 BACKGROUND: 4, // Backwards compat, same as IMAGE | 
| 817 | 888 | 
| 818 POPUP: 0x10000000, | 889 POPUP: 0x10000000, | 
| 819 GENERICBLOCK: 0x20000000, | 890 GENERICBLOCK: 0x20000000, | 
| 820 ELEMHIDE: 0x40000000, | 891 ELEMHIDE: 0x40000000, | 
| 821 GENERICHIDE: 0x80000000 | 892 GENERICHIDE: 0x80000000 | 
| 822 }; | 893 }; | 
| 823 | 894 | 
| 824 // DOCUMENT, ELEMHIDE, POPUP, GENERICHIDE and GENERICBLOCK options shouldn't | 895 // CSP, DOCUMENT, ELEMHIDE, POPUP, GENERICHIDE and GENERICBLOCK options | 
| 825 // be there by default | 896 // shouldn't be there by default | 
| 826 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.DOCUMENT | | 897 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP | | 
| 898 RegExpFilter.typeMap.DOCUMENT | | |
| 827 RegExpFilter.typeMap.ELEMHIDE | | 899 RegExpFilter.typeMap.ELEMHIDE | | 
| 828 RegExpFilter.typeMap.POPUP | | 900 RegExpFilter.typeMap.POPUP | | 
| 829 RegExpFilter.typeMap.GENERICHIDE | | 901 RegExpFilter.typeMap.GENERICHIDE | | 
| 830 RegExpFilter.typeMap.GENERICBLOCK); | 902 RegExpFilter.typeMap.GENERICBLOCK); | 
| 831 | 903 | 
| 832 /** | 904 /** | 
| 833 * Class for blocking filters | 905 * Class for blocking filters | 
| 834 * @param {string} text see Filter() | 906 * @param {string} text see Filter() | 
| 835 * @param {string} regexpSource see RegExpFilter() | 907 * @param {string} regexpSource see RegExpFilter() | 
| 836 * @param {number} contentType see RegExpFilter() | 908 * @param {number} contentType see RegExpFilter() | 
| 837 * @param {boolean} matchCase see RegExpFilter() | 909 * @param {boolean} matchCase see RegExpFilter() | 
| 838 * @param {string} domains see RegExpFilter() | 910 * @param {string} domains see RegExpFilter() | 
| 839 * @param {boolean} thirdParty see RegExpFilter() | 911 * @param {boolean} thirdParty see RegExpFilter() | 
| 840 * @param {string} sitekeys see RegExpFilter() | 912 * @param {string} sitekeys see RegExpFilter() | 
| 841 * @param {boolean} collapse | 913 * @param {boolean} collapse | 
| 842 * defines whether the filter should collapse blocked content, can be null | 914 * defines whether the filter should collapse blocked content, can be null | 
| 915 * @param {string} [csp] | |
| 916 * Content Security Policy to inject when the filter matches | |
| 843 * @constructor | 917 * @constructor | 
| 844 * @augments RegExpFilter | 918 * @augments RegExpFilter | 
| 845 */ | 919 */ | 
| 846 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, | 920 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, | 
| 847 thirdParty, sitekeys, collapse) | 921 thirdParty, sitekeys, collapse, csp) | 
| 848 { | 922 { | 
| 849 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, | 923 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, | 
| 850 thirdParty, sitekeys); | 924 thirdParty, sitekeys); | 
| 851 | 925 | 
| 852 this.collapse = collapse; | 926 this.collapse = collapse; | 
| 927 this.csp = csp; | |
| 853 } | 928 } | 
| 854 exports.BlockingFilter = BlockingFilter; | 929 exports.BlockingFilter = BlockingFilter; | 
| 855 | 930 | 
| 856 BlockingFilter.prototype = extend(RegExpFilter, { | 931 BlockingFilter.prototype = extend(RegExpFilter, { | 
| 857 type: "blocking", | 932 type: "blocking", | 
| 858 | 933 | 
| 859 /** | 934 /** | 
| 860 * Defines whether the filter should collapse blocked content. | 935 * Defines whether the filter should collapse blocked content. | 
| 861 * Can be null (use the global preference). | 936 * Can be null (use the global preference). | 
| 862 * @type {boolean} | 937 * @type {boolean} | 
| 863 */ | 938 */ | 
| 864 collapse: null | 939 collapse: null, | 
| 940 | |
| 941 /** | |
| 942 * Content Security Policy to inject for matching requests. | |
| 943 * @type {string} | |
| 944 */ | |
| 945 csp: null | |
| 865 }); | 946 }); | 
| 866 | 947 | 
| 867 /** | 948 /** | 
| 868 * Class for whitelist filters | 949 * Class for whitelist filters | 
| 869 * @param {string} text see Filter() | 950 * @param {string} text see Filter() | 
| 870 * @param {string} regexpSource see RegExpFilter() | 951 * @param {string} regexpSource see RegExpFilter() | 
| 871 * @param {number} contentType see RegExpFilter() | 952 * @param {number} contentType see RegExpFilter() | 
| 872 * @param {boolean} matchCase see RegExpFilter() | 953 * @param {boolean} matchCase see RegExpFilter() | 
| 873 * @param {string} domains see RegExpFilter() | 954 * @param {string} domains see RegExpFilter() | 
| 874 * @param {boolean} thirdParty see RegExpFilter() | 955 * @param {boolean} thirdParty see RegExpFilter() | 
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1019 */ | 1100 */ | 
| 1020 function ElemHideEmulationFilter(text, domains, selector) | 1101 function ElemHideEmulationFilter(text, domains, selector) | 
| 1021 { | 1102 { | 
| 1022 ElemHideBase.call(this, text, domains, selector); | 1103 ElemHideBase.call(this, text, domains, selector); | 
| 1023 } | 1104 } | 
| 1024 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; | 1105 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; | 
| 1025 | 1106 | 
| 1026 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { | 1107 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { | 
| 1027 type: "elemhideemulation" | 1108 type: "elemhideemulation" | 
| 1028 }); | 1109 }); | 
| OLD | NEW |