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: Created Jan. 26, 2018, 5:41 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 685 matching lines...) Expand 10 before | Expand all | Expand 10 after
696 }); 696 });
697 697
698 /** 698 /**
699 * Creates a RegExp filter from its text representation 699 * Creates a RegExp filter from its text representation
700 * @param {string} text same as in Filter() 700 * @param {string} text same as in Filter()
701 * @return {Filter} 701 * @return {Filter}
702 */ 702 */
703 RegExpFilter.fromText = function(text) 703 RegExpFilter.fromText = function(text)
704 { 704 {
705 let contentType = null; 705 let contentType = null;
706 let csp = null;
706 let matchCase = null; 707 let matchCase = null;
707 let domains = null; 708 let domains = null;
708 let sitekeys = null; 709 let sitekeys = null;
709 let thirdParty = null; 710 let thirdParty = null;
710 let collapse = null; 711 let collapse = null;
711 let origText; 712 let origText;
712 713
713 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); 714 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null);
714 if (!match) 715 if (!match)
715 { 716 {
716 origText = text = text.replace(/\s/g, ""); 717 origText = text = text.replace(/\s/g, "");
717 } 718 }
718 else 719 else
719 { 720 {
720 text = match.input.substring(0, match.index).replace(/\s/g, ""); 721 text = match.input.substring(0, match.index).replace(/\s/g, "");
721 let options = match[1].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
722 origText = text + "$" + options; 738 origText = text + "$" + options;
723 739
724 for (let option of options.toUpperCase().split(",")) 740 for (let option of options.toUpperCase().split(","))
725 { 741 {
726 let value = null; 742 let value = null;
727 let separatorIndex = option.indexOf("="); 743 let separatorIndex = option.indexOf("=");
728 if (separatorIndex >= 0) 744 if (separatorIndex >= 0)
729 { 745 {
730 value = option.substr(separatorIndex + 1); 746 value = option.substr(separatorIndex + 1);
731 option = option.substring(0, separatorIndex); 747 option = option.substring(0, separatorIndex);
732 } 748 }
733 option = option.replace(/-/, "_"); 749 option = option.replace(/-/, "_");
734 if (option in RegExpFilter.typeMap) 750 if (option in RegExpFilter.typeMap)
735 { 751 {
736 if (contentType == null) 752 if (contentType == null)
737 contentType = 0; 753 contentType = 0;
738 contentType |= RegExpFilter.typeMap[option]; 754 contentType |= RegExpFilter.typeMap[option];
755
756 if (option == "CSP" && typeof value == "string")
757 {
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 }
739 } 763 }
740 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) 764 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap)
741 { 765 {
742 if (contentType == null) 766 if (contentType == null)
743 ({contentType} = RegExpFilter.prototype); 767 ({contentType} = RegExpFilter.prototype);
744 contentType &= ~RegExpFilter.typeMap[option.substr(1)]; 768 contentType &= ~RegExpFilter.typeMap[option.substr(1)];
745 } 769 }
746 else if (option == "MATCH_CASE") 770 else if (option == "MATCH_CASE")
747 matchCase = true; 771 matchCase = true;
748 else if (option == "~MATCH_CASE") 772 else if (option == "~MATCH_CASE")
(...skipping 16 matching lines...) Expand all
765 } 789 }
766 790
767 try 791 try
768 { 792 {
769 if (text.indexOf("@@") == 0) 793 if (text.indexOf("@@") == 0)
770 { 794 {
771 return new WhitelistFilter(origText, text.substr(2), contentType, 795 return new WhitelistFilter(origText, text.substr(2), contentType,
772 matchCase, domains, thirdParty, sitekeys); 796 matchCase, domains, thirdParty, sitekeys);
773 } 797 }
774 return new BlockingFilter(origText, text, contentType, matchCase, domains, 798 return new BlockingFilter(origText, text, contentType, matchCase, domains,
775 thirdParty, sitekeys, collapse); 799 thirdParty, sitekeys, collapse, csp);
776 } 800 }
777 catch (e) 801 catch (e)
778 { 802 {
779 return new InvalidFilter(origText, "filter_invalid_regexp"); 803 return new InvalidFilter(origText, "filter_invalid_regexp");
780 } 804 }
781 }; 805 };
782 806
783 /** 807 /**
784 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks 808 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks
785 */ 809 */
786 RegExpFilter.typeMap = { 810 RegExpFilter.typeMap = {
787 OTHER: 1, 811 OTHER: 1,
788 SCRIPT: 2, 812 SCRIPT: 2,
789 IMAGE: 4, 813 IMAGE: 4,
790 STYLESHEET: 8, 814 STYLESHEET: 8,
791 OBJECT: 16, 815 OBJECT: 16,
792 SUBDOCUMENT: 32, 816 SUBDOCUMENT: 32,
793 DOCUMENT: 64, 817 DOCUMENT: 64,
794 WEBSOCKET: 128, 818 WEBSOCKET: 128,
819 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.
795 WEBRTC: 256, 820 WEBRTC: 256,
796 XBL: 1, 821 XBL: 1,
797 PING: 1024, 822 PING: 1024,
798 XMLHTTPREQUEST: 2048, 823 XMLHTTPREQUEST: 2048,
799 OBJECT_SUBREQUEST: 4096, 824 OBJECT_SUBREQUEST: 4096,
800 DTD: 1, 825 DTD: 1,
801 MEDIA: 16384, 826 MEDIA: 16384,
802 FONT: 32768, 827 FONT: 32768,
803 828
804 BACKGROUND: 4, // Backwards compat, same as IMAGE 829 BACKGROUND: 4, // Backwards compat, same as IMAGE
805 830
806 POPUP: 0x10000000, 831 POPUP: 0x10000000,
807 GENERICBLOCK: 0x20000000, 832 GENERICBLOCK: 0x20000000,
808 ELEMHIDE: 0x40000000, 833 ELEMHIDE: 0x40000000,
809 GENERICHIDE: 0x80000000 834 GENERICHIDE: 0x80000000
810 }; 835 };
811 836
812 // DOCUMENT, ELEMHIDE, POPUP, GENERICHIDE and GENERICBLOCK options shouldn't 837 // CSP, DOCUMENT, ELEMHIDE, POPUP, GENERICHIDE and GENERICBLOCK options
813 // be there by default 838 // shouldn't be there by default
814 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.DOCUMENT | 839 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP |
840 RegExpFilter.typeMap.DOCUMENT |
815 RegExpFilter.typeMap.ELEMHIDE | 841 RegExpFilter.typeMap.ELEMHIDE |
816 RegExpFilter.typeMap.POPUP | 842 RegExpFilter.typeMap.POPUP |
817 RegExpFilter.typeMap.GENERICHIDE | 843 RegExpFilter.typeMap.GENERICHIDE |
818 RegExpFilter.typeMap.GENERICBLOCK); 844 RegExpFilter.typeMap.GENERICBLOCK);
819 845
820 /** 846 /**
821 * Class for blocking filters 847 * Class for blocking filters
822 * @param {string} text see Filter() 848 * @param {string} text see Filter()
823 * @param {string} regexpSource see RegExpFilter() 849 * @param {string} regexpSource see RegExpFilter()
824 * @param {number} contentType see RegExpFilter() 850 * @param {number} contentType see RegExpFilter()
825 * @param {boolean} matchCase see RegExpFilter() 851 * @param {boolean} matchCase see RegExpFilter()
826 * @param {string} domains see RegExpFilter() 852 * @param {string} domains see RegExpFilter()
827 * @param {boolean} thirdParty see RegExpFilter() 853 * @param {boolean} thirdParty see RegExpFilter()
828 * @param {string} sitekeys see RegExpFilter() 854 * @param {string} sitekeys see RegExpFilter()
829 * @param {boolean} collapse 855 * @param {boolean} collapse
830 * defines whether the filter should collapse blocked content, can be null 856 * defines whether the filter should collapse blocked content, can be null
857 * @param {string} [csp]
858 * Content Security Policy to inject when the filter matches
831 * @constructor 859 * @constructor
832 * @augments RegExpFilter 860 * @augments RegExpFilter
833 */ 861 */
834 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, 862 function BlockingFilter(text, regexpSource, contentType, matchCase, domains,
835 thirdParty, sitekeys, collapse) 863 thirdParty, sitekeys, collapse, csp)
836 { 864 {
837 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, 865 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains,
838 thirdParty, sitekeys); 866 thirdParty, sitekeys);
839 867
840 this.collapse = collapse; 868 this.collapse = collapse;
869 this.csp = csp;
841 } 870 }
842 exports.BlockingFilter = BlockingFilter; 871 exports.BlockingFilter = BlockingFilter;
843 872
844 BlockingFilter.prototype = extend(RegExpFilter, { 873 BlockingFilter.prototype = extend(RegExpFilter, {
845 type: "blocking", 874 type: "blocking",
846 875
847 /** 876 /**
848 * Defines whether the filter should collapse blocked content. 877 * Defines whether the filter should collapse blocked content.
849 * Can be null (use the global preference). 878 * Can be null (use the global preference).
850 * @type {boolean} 879 * @type {boolean}
851 */ 880 */
852 collapse: null 881 collapse: null,
882
883 /**
884 * Content Security Policy to inject for matching requests.
885 * @type {string}
886 */
887 csp: null
853 }); 888 });
854 889
855 /** 890 /**
856 * Class for whitelist filters 891 * Class for whitelist filters
857 * @param {string} text see Filter() 892 * @param {string} text see Filter()
858 * @param {string} regexpSource see RegExpFilter() 893 * @param {string} regexpSource see RegExpFilter()
859 * @param {number} contentType see RegExpFilter() 894 * @param {number} contentType see RegExpFilter()
860 * @param {boolean} matchCase see RegExpFilter() 895 * @param {boolean} matchCase see RegExpFilter()
861 * @param {string} domains see RegExpFilter() 896 * @param {string} domains see RegExpFilter()
862 * @param {boolean} thirdParty see RegExpFilter() 897 * @param {boolean} thirdParty see RegExpFilter()
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1007 */ 1042 */
1008 function ElemHideEmulationFilter(text, domains, selector) 1043 function ElemHideEmulationFilter(text, domains, selector)
1009 { 1044 {
1010 ElemHideBase.call(this, text, domains, selector); 1045 ElemHideBase.call(this, text, domains, selector);
1011 } 1046 }
1012 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; 1047 exports.ElemHideEmulationFilter = ElemHideEmulationFilter;
1013 1048
1014 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { 1049 ElemHideEmulationFilter.prototype = extend(ElemHideBase, {
1015 type: "elemhideemulation" 1050 type: "elemhideemulation"
1016 }); 1051 });
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