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: Check for report-to and report-uri directives more carefully Created March 6, 2018, 3:34 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")
Manish Jethani 2018/03/07 00:22:13 I've left a comment on the other patch, I think we
kzar 2018/03/12 13:35:58 Done.
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)
Manish Jethani 2018/03/07 00:22:13 As far as I can tell the strict equality is unnece
kzar 2018/03/12 13:35:58 Done.
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 // Prevent filters from injecting report-uri or report-to directives
807 // since they are a privacy concern. Regexp based upon reBadCSP[1].
808 // [1] - https://github.com/gorhill/uBlock/blob/67e06f53b4d73df6179f6d32 0553a55da4ead40e/src/js/static-net-filtering.js#L1362
809 if (/(;|^)\s*report-(to|uri)\b/.test(csp))
810 return new InvalidFilter(origText, "filter_invalid_csp");
811 }
812
783 return new BlockingFilter(origText, text, contentType, matchCase, domains, 813 return new BlockingFilter(origText, text, contentType, matchCase, domains,
784 thirdParty, sitekeys, collapse); 814 thirdParty, sitekeys, collapse, csp);
785 } 815 }
786 return new WhitelistFilter(origText, text, contentType, matchCase, domains, 816 return new WhitelistFilter(origText, text, contentType, matchCase, domains,
787 thirdParty, sitekeys); 817 thirdParty, sitekeys);
788 } 818 }
789 catch (e) 819 catch (e)
790 { 820 {
791 return new InvalidFilter(origText, "filter_invalid_regexp"); 821 return new InvalidFilter(origText, "filter_invalid_regexp");
792 } 822 }
793 }; 823 };
794 824
795 /** 825 /**
796 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks 826 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks
797 */ 827 */
798 RegExpFilter.typeMap = { 828 RegExpFilter.typeMap = {
799 OTHER: 1, 829 OTHER: 1,
800 SCRIPT: 2, 830 SCRIPT: 2,
801 IMAGE: 4, 831 IMAGE: 4,
802 STYLESHEET: 8, 832 STYLESHEET: 8,
803 OBJECT: 16, 833 OBJECT: 16,
804 SUBDOCUMENT: 32, 834 SUBDOCUMENT: 32,
805 DOCUMENT: 64, 835 DOCUMENT: 64,
806 WEBSOCKET: 128, 836 WEBSOCKET: 128,
807 WEBRTC: 256, 837 WEBRTC: 256,
838 CSP: 512,
808 XBL: 1, 839 XBL: 1,
809 PING: 1024, 840 PING: 1024,
810 XMLHTTPREQUEST: 2048, 841 XMLHTTPREQUEST: 2048,
811 OBJECT_SUBREQUEST: 4096, 842 OBJECT_SUBREQUEST: 4096,
812 DTD: 1, 843 DTD: 1,
813 MEDIA: 16384, 844 MEDIA: 16384,
814 FONT: 32768, 845 FONT: 32768,
815 846
816 BACKGROUND: 4, // Backwards compat, same as IMAGE 847 BACKGROUND: 4, // Backwards compat, same as IMAGE
817 848
818 POPUP: 0x10000000, 849 POPUP: 0x10000000,
819 GENERICBLOCK: 0x20000000, 850 GENERICBLOCK: 0x20000000,
820 ELEMHIDE: 0x40000000, 851 ELEMHIDE: 0x40000000,
821 GENERICHIDE: 0x80000000 852 GENERICHIDE: 0x80000000
822 }; 853 };
823 854
824 // DOCUMENT, ELEMHIDE, POPUP, GENERICHIDE and GENERICBLOCK options shouldn't 855 // CSP, DOCUMENT, ELEMHIDE, POPUP, GENERICHIDE and GENERICBLOCK options
825 // be there by default 856 // shouldn't be there by default
826 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.DOCUMENT | 857 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP |
858 RegExpFilter.typeMap.DOCUMENT |
827 RegExpFilter.typeMap.ELEMHIDE | 859 RegExpFilter.typeMap.ELEMHIDE |
828 RegExpFilter.typeMap.POPUP | 860 RegExpFilter.typeMap.POPUP |
829 RegExpFilter.typeMap.GENERICHIDE | 861 RegExpFilter.typeMap.GENERICHIDE |
830 RegExpFilter.typeMap.GENERICBLOCK); 862 RegExpFilter.typeMap.GENERICBLOCK);
831 863
832 /** 864 /**
833 * Class for blocking filters 865 * Class for blocking filters
834 * @param {string} text see Filter() 866 * @param {string} text see Filter()
835 * @param {string} regexpSource see RegExpFilter() 867 * @param {string} regexpSource see RegExpFilter()
836 * @param {number} contentType see RegExpFilter() 868 * @param {number} contentType see RegExpFilter()
837 * @param {boolean} matchCase see RegExpFilter() 869 * @param {boolean} matchCase see RegExpFilter()
838 * @param {string} domains see RegExpFilter() 870 * @param {string} domains see RegExpFilter()
839 * @param {boolean} thirdParty see RegExpFilter() 871 * @param {boolean} thirdParty see RegExpFilter()
840 * @param {string} sitekeys see RegExpFilter() 872 * @param {string} sitekeys see RegExpFilter()
841 * @param {boolean} collapse 873 * @param {boolean} collapse
842 * defines whether the filter should collapse blocked content, can be null 874 * defines whether the filter should collapse blocked content, can be null
875 * @param {string} [csp]
876 * Content Security Policy to inject when the filter matches
843 * @constructor 877 * @constructor
844 * @augments RegExpFilter 878 * @augments RegExpFilter
845 */ 879 */
846 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, 880 function BlockingFilter(text, regexpSource, contentType, matchCase, domains,
847 thirdParty, sitekeys, collapse) 881 thirdParty, sitekeys, collapse, csp)
848 { 882 {
849 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, 883 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains,
850 thirdParty, sitekeys); 884 thirdParty, sitekeys);
851 885
852 this.collapse = collapse; 886 this.collapse = collapse;
887 this.csp = csp;
853 } 888 }
854 exports.BlockingFilter = BlockingFilter; 889 exports.BlockingFilter = BlockingFilter;
855 890
856 BlockingFilter.prototype = extend(RegExpFilter, { 891 BlockingFilter.prototype = extend(RegExpFilter, {
857 type: "blocking", 892 type: "blocking",
858 893
859 /** 894 /**
860 * Defines whether the filter should collapse blocked content. 895 * Defines whether the filter should collapse blocked content.
861 * Can be null (use the global preference). 896 * Can be null (use the global preference).
862 * @type {boolean} 897 * @type {boolean}
863 */ 898 */
864 collapse: null 899 collapse: null,
900
901 /**
902 * Content Security Policy to inject for matching requests.
903 * @type {string}
904 */
905 csp: null
865 }); 906 });
866 907
867 /** 908 /**
868 * Class for whitelist filters 909 * Class for whitelist filters
869 * @param {string} text see Filter() 910 * @param {string} text see Filter()
870 * @param {string} regexpSource see RegExpFilter() 911 * @param {string} regexpSource see RegExpFilter()
871 * @param {number} contentType see RegExpFilter() 912 * @param {number} contentType see RegExpFilter()
872 * @param {boolean} matchCase see RegExpFilter() 913 * @param {boolean} matchCase see RegExpFilter()
873 * @param {string} domains see RegExpFilter() 914 * @param {string} domains see RegExpFilter()
874 * @param {boolean} thirdParty see RegExpFilter() 915 * @param {boolean} thirdParty see RegExpFilter()
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1019 */ 1060 */
1020 function ElemHideEmulationFilter(text, domains, selector) 1061 function ElemHideEmulationFilter(text, domains, selector)
1021 { 1062 {
1022 ElemHideBase.call(this, text, domains, selector); 1063 ElemHideBase.call(this, text, domains, selector);
1023 } 1064 }
1024 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; 1065 exports.ElemHideEmulationFilter = ElemHideEmulationFilter;
1025 1066
1026 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { 1067 ElemHideEmulationFilter.prototype = extend(ElemHideBase, {
1027 type: "elemhideemulation" 1068 type: "elemhideemulation"
1028 }); 1069 });
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