Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/filterClasses.js

Issue 29680689: [$csp2 adblockpluscore] Issue 6329 - Add the CSP filter type (Closed)
Patch Set: Rebased, brought back Filter.normalize, other changes Created March 6, 2018, 2:44 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/filterClasses.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 101
102 /** 102 /**
103 * Creates a filter of correct type from its text representation - does the 103 * Creates a filter of correct type from its text representation - does the
104 * basic parsing and calls the right constructor then. 104 * basic parsing and calls the right constructor then.
105 * 105 *
106 * @param {string} text as in Filter() 106 * @param {string} text as in Filter()
107 * @return {Filter} 107 * @return {Filter}
108 */ 108 */
109 Filter.fromText = function(text) 109 Filter.fromText = function(text)
110 { 110 {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 // Don't remove spaces inside comments 180 // Don't remove spaces inside comments
181 return text.trim(); 181 return text.trim();
182 } 182 }
183 else if (Filter.elemhideRegExp.test(text)) 183 else if (Filter.elemhideRegExp.test(text))
184 { 184 {
185 // Special treatment for element hiding filters, right side is allowed to 185 // Special treatment for element hiding filters, right side is allowed to
186 // contain spaces 186 // contain spaces
187 let [, domain, separator, selector] = /^(.*?)(#@?#?)(.*)$/.exec(text); 187 let [, domain, separator, selector] = /^(.*?)(#@?#?)(.*)$/.exec(text);
188 return domain.replace(/\s/g, "") + separator + selector.trim(); 188 return domain.replace(/\s/g, "") + separator + selector.trim();
189 } 189 }
190 return text.replace(/\s/g, ""); 190 return text.trim().replace(/\s+/g, " ");
191 }; 191 };
192 192
193 /** 193 /**
194 * @see filterToRegExp 194 * @see filterToRegExp
195 */ 195 */
196 Filter.toRegExp = filterToRegExp; 196 Filter.toRegExp = filterToRegExp;
197 197
198 /** 198 /**
199 * Class for invalid filters 199 * Class for invalid filters
200 * @param {string} text see Filter() 200 * @param {string} text see Filter()
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 blocking = false; 720 blocking = false;
721 text = text.substr(2); 721 text = text.substr(2);
722 } 722 }
723 723
724 let contentType = null; 724 let contentType = null;
725 let matchCase = null; 725 let matchCase = null;
726 let domains = null; 726 let domains = null;
727 let sitekeys = null; 727 let sitekeys = null;
728 let thirdParty = null; 728 let thirdParty = null;
729 let collapse = null; 729 let collapse = null;
730 let csp = null;
730 let options; 731 let options;
731 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); 732 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null);
732 if (match) 733 if (match)
733 { 734 {
734 options = match[1].toUpperCase().split(","); 735 options = match[1].split(",");
735 text = match.input.substr(0, match.index); 736 text = match.input.substr(0, match.index);
736 for (let option of options) 737 for (let option of options)
737 { 738 {
738 let value = null; 739 let value = null;
739 let separatorIndex = option.indexOf("="); 740 let separatorIndex = option.indexOf("=");
740 if (separatorIndex >= 0) 741 if (separatorIndex >= 0)
741 { 742 {
742 value = option.substr(separatorIndex + 1); 743 value = option.substr(separatorIndex + 1);
743 option = option.substr(0, separatorIndex); 744 option = option.substr(0, separatorIndex).toUpperCase();
745
746 if (option == "CSP")
747 value = value.trim();
748 else
749 value = value.replace(/\s/g, "");
744 } 750 }
751 else
752 option = option.toUpperCase();
753
745 option = option.replace(/-/, "_"); 754 option = option.replace(/-/, "_");
755
746 if (option in RegExpFilter.typeMap) 756 if (option in RegExpFilter.typeMap)
747 { 757 {
748 if (contentType == null) 758 if (contentType == null)
749 contentType = 0; 759 contentType = 0;
750 contentType |= RegExpFilter.typeMap[option]; 760 contentType |= RegExpFilter.typeMap[option];
761
762 if (option == "CSP" && typeof value != "undefined")
763 {
764 if (csp === null)
765 csp = [value];
766 else
767 csp.push(value);
768 }
751 } 769 }
752 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) 770 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap)
753 { 771 {
754 if (contentType == null) 772 if (contentType == null)
755 ({contentType} = RegExpFilter.prototype); 773 ({contentType} = RegExpFilter.prototype);
756 contentType &= ~RegExpFilter.typeMap[option.substr(1)]; 774 contentType &= ~RegExpFilter.typeMap[option.substr(1)];
757 } 775 }
758 else if (option == "MATCH_CASE") 776 else if (option == "MATCH_CASE")
759 matchCase = true; 777 matchCase = true;
760 else if (option == "~MATCH_CASE") 778 else if (option == "~MATCH_CASE")
761 matchCase = false; 779 matchCase = false;
762 else if (option == "DOMAIN" && typeof value != "undefined") 780 else if (option == "DOMAIN" && typeof value != "undefined")
763 domains = value; 781 domains = value.toUpperCase();
764 else if (option == "THIRD_PARTY") 782 else if (option == "THIRD_PARTY")
765 thirdParty = true; 783 thirdParty = true;
766 else if (option == "~THIRD_PARTY") 784 else if (option == "~THIRD_PARTY")
767 thirdParty = false; 785 thirdParty = false;
768 else if (option == "COLLAPSE") 786 else if (option == "COLLAPSE")
769 collapse = true; 787 collapse = true;
770 else if (option == "~COLLAPSE") 788 else if (option == "~COLLAPSE")
771 collapse = false; 789 collapse = false;
772 else if (option == "SITEKEY" && typeof value != "undefined") 790 else if (option == "SITEKEY" && typeof value != "undefined")
773 sitekeys = value; 791 sitekeys = value.toUpperCase();
774 else 792 else
775 return new InvalidFilter(origText, "filter_unknown_option"); 793 return new InvalidFilter(origText, "filter_unknown_option");
776 } 794 }
777 } 795 }
796 text = text.replace(/\s/g, "");
778 797
779 try 798 try
780 { 799 {
781 if (blocking) 800 if (blocking)
782 { 801 {
802 if (csp !== null)
803 {
804 csp = csp.join("; ").toLowerCase();
805
806 // Quick check to prevent report-uri and report-to directives
807 if (csp.includes("report-"))
808 return new InvalidFilter(origText, "filter_invalid_csp");
809 }
810
783 return new BlockingFilter(origText, text, contentType, matchCase, domains, 811 return new BlockingFilter(origText, text, contentType, matchCase, domains,
784 thirdParty, sitekeys, collapse); 812 thirdParty, sitekeys, collapse, csp);
785 } 813 }
786 return new WhitelistFilter(origText, text, contentType, matchCase, domains, 814 return new WhitelistFilter(origText, text, contentType, matchCase, domains,
787 thirdParty, sitekeys); 815 thirdParty, sitekeys);
788 } 816 }
789 catch (e) 817 catch (e)
790 { 818 {
791 return new InvalidFilter(origText, "filter_invalid_regexp"); 819 return new InvalidFilter(origText, "filter_invalid_regexp");
792 } 820 }
793 }; 821 };
794 822
795 /** 823 /**
796 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks 824 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks
797 */ 825 */
798 RegExpFilter.typeMap = { 826 RegExpFilter.typeMap = {
799 OTHER: 1, 827 OTHER: 1,
800 SCRIPT: 2, 828 SCRIPT: 2,
801 IMAGE: 4, 829 IMAGE: 4,
802 STYLESHEET: 8, 830 STYLESHEET: 8,
803 OBJECT: 16, 831 OBJECT: 16,
804 SUBDOCUMENT: 32, 832 SUBDOCUMENT: 32,
805 DOCUMENT: 64, 833 DOCUMENT: 64,
806 WEBSOCKET: 128, 834 WEBSOCKET: 128,
807 WEBRTC: 256, 835 WEBRTC: 256,
836 CSP: 512,
808 XBL: 1, 837 XBL: 1,
809 PING: 1024, 838 PING: 1024,
810 XMLHTTPREQUEST: 2048, 839 XMLHTTPREQUEST: 2048,
811 OBJECT_SUBREQUEST: 4096, 840 OBJECT_SUBREQUEST: 4096,
812 DTD: 1, 841 DTD: 1,
813 MEDIA: 16384, 842 MEDIA: 16384,
814 FONT: 32768, 843 FONT: 32768,
815 844
816 BACKGROUND: 4, // Backwards compat, same as IMAGE 845 BACKGROUND: 4, // Backwards compat, same as IMAGE
817 846
818 POPUP: 0x10000000, 847 POPUP: 0x10000000,
819 GENERICBLOCK: 0x20000000, 848 GENERICBLOCK: 0x20000000,
820 ELEMHIDE: 0x40000000, 849 ELEMHIDE: 0x40000000,
821 GENERICHIDE: 0x80000000 850 GENERICHIDE: 0x80000000
822 }; 851 };
823 852
824 // DOCUMENT, ELEMHIDE, POPUP, GENERICHIDE and GENERICBLOCK options shouldn't 853 // CSP, DOCUMENT, ELEMHIDE, POPUP, GENERICHIDE and GENERICBLOCK options
825 // be there by default 854 // shouldn't be there by default
826 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.DOCUMENT | 855 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP |
856 RegExpFilter.typeMap.DOCUMENT |
827 RegExpFilter.typeMap.ELEMHIDE | 857 RegExpFilter.typeMap.ELEMHIDE |
828 RegExpFilter.typeMap.POPUP | 858 RegExpFilter.typeMap.POPUP |
829 RegExpFilter.typeMap.GENERICHIDE | 859 RegExpFilter.typeMap.GENERICHIDE |
830 RegExpFilter.typeMap.GENERICBLOCK); 860 RegExpFilter.typeMap.GENERICBLOCK);
831 861
832 /** 862 /**
833 * Class for blocking filters 863 * Class for blocking filters
834 * @param {string} text see Filter() 864 * @param {string} text see Filter()
835 * @param {string} regexpSource see RegExpFilter() 865 * @param {string} regexpSource see RegExpFilter()
836 * @param {number} contentType see RegExpFilter() 866 * @param {number} contentType see RegExpFilter()
837 * @param {boolean} matchCase see RegExpFilter() 867 * @param {boolean} matchCase see RegExpFilter()
838 * @param {string} domains see RegExpFilter() 868 * @param {string} domains see RegExpFilter()
839 * @param {boolean} thirdParty see RegExpFilter() 869 * @param {boolean} thirdParty see RegExpFilter()
840 * @param {string} sitekeys see RegExpFilter() 870 * @param {string} sitekeys see RegExpFilter()
841 * @param {boolean} collapse 871 * @param {boolean} collapse
842 * defines whether the filter should collapse blocked content, can be null 872 * defines whether the filter should collapse blocked content, can be null
873 * @param {string} [csp]
874 * Content Security Policy to inject when the filter matches
843 * @constructor 875 * @constructor
844 * @augments RegExpFilter 876 * @augments RegExpFilter
845 */ 877 */
846 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, 878 function BlockingFilter(text, regexpSource, contentType, matchCase, domains,
847 thirdParty, sitekeys, collapse) 879 thirdParty, sitekeys, collapse, csp)
848 { 880 {
849 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, 881 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains,
850 thirdParty, sitekeys); 882 thirdParty, sitekeys);
851 883
852 this.collapse = collapse; 884 this.collapse = collapse;
885 this.csp = csp;
853 } 886 }
854 exports.BlockingFilter = BlockingFilter; 887 exports.BlockingFilter = BlockingFilter;
855 888
856 BlockingFilter.prototype = extend(RegExpFilter, { 889 BlockingFilter.prototype = extend(RegExpFilter, {
857 type: "blocking", 890 type: "blocking",
858 891
859 /** 892 /**
860 * Defines whether the filter should collapse blocked content. 893 * Defines whether the filter should collapse blocked content.
861 * Can be null (use the global preference). 894 * Can be null (use the global preference).
862 * @type {boolean} 895 * @type {boolean}
863 */ 896 */
864 collapse: null 897 collapse: null,
898
899 /**
900 * Content Security Policy to inject for matching requests.
901 * @type {string}
902 */
903 csp: null
865 }); 904 });
866 905
867 /** 906 /**
868 * Class for whitelist filters 907 * Class for whitelist filters
869 * @param {string} text see Filter() 908 * @param {string} text see Filter()
870 * @param {string} regexpSource see RegExpFilter() 909 * @param {string} regexpSource see RegExpFilter()
871 * @param {number} contentType see RegExpFilter() 910 * @param {number} contentType see RegExpFilter()
872 * @param {boolean} matchCase see RegExpFilter() 911 * @param {boolean} matchCase see RegExpFilter()
873 * @param {string} domains see RegExpFilter() 912 * @param {string} domains see RegExpFilter()
874 * @param {boolean} thirdParty see RegExpFilter() 913 * @param {boolean} thirdParty see RegExpFilter()
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1019 */ 1058 */
1020 function ElemHideEmulationFilter(text, domains, selector) 1059 function ElemHideEmulationFilter(text, domains, selector)
1021 { 1060 {
1022 ElemHideBase.call(this, text, domains, selector); 1061 ElemHideBase.call(this, text, domains, selector);
1023 } 1062 }
1024 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; 1063 exports.ElemHideEmulationFilter = ElemHideEmulationFilter;
1025 1064
1026 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { 1065 ElemHideEmulationFilter.prototype = extend(ElemHideBase, {
1027 type: "elemhideemulation" 1066 type: "elemhideemulation"
1028 }); 1067 });
OLDNEW
« no previous file with comments | « no previous file | test/filterClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld