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