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 752 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
763 text = text.substr(2); | 763 text = text.substr(2); |
764 } | 764 } |
765 | 765 |
766 let contentType = null; | 766 let contentType = null; |
767 let matchCase = null; | 767 let matchCase = null; |
768 let domains = null; | 768 let domains = null; |
769 let sitekeys = null; | 769 let sitekeys = null; |
770 let thirdParty = null; | 770 let thirdParty = null; |
771 let collapse = null; | 771 let collapse = null; |
772 let csp = null; | 772 let csp = null; |
773 let rewrite = null; | |
773 let options; | 774 let options; |
774 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); | 775 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); |
775 if (match) | 776 if (match) |
776 { | 777 { |
777 options = match[1].split(","); | 778 options = match[1].split(","); |
778 text = match.input.substr(0, match.index); | 779 text = match.input.substr(0, match.index); |
779 for (let option of options) | 780 for (let option of options) |
780 { | 781 { |
781 let value = null; | 782 let value = null; |
782 let separatorIndex = option.indexOf("="); | 783 let separatorIndex = option.indexOf("="); |
(...skipping 27 matching lines...) Expand all Loading... | |
810 else if (option == "THIRD_PARTY") | 811 else if (option == "THIRD_PARTY") |
811 thirdParty = true; | 812 thirdParty = true; |
812 else if (option == "~THIRD_PARTY") | 813 else if (option == "~THIRD_PARTY") |
813 thirdParty = false; | 814 thirdParty = false; |
814 else if (option == "COLLAPSE") | 815 else if (option == "COLLAPSE") |
815 collapse = true; | 816 collapse = true; |
816 else if (option == "~COLLAPSE") | 817 else if (option == "~COLLAPSE") |
817 collapse = false; | 818 collapse = false; |
818 else if (option == "SITEKEY" && value) | 819 else if (option == "SITEKEY" && value) |
819 sitekeys = value.toUpperCase(); | 820 sitekeys = value.toUpperCase(); |
821 else if (option == "REWRITE" && value) | |
822 rewrite = value; | |
820 else | 823 else |
821 return new InvalidFilter(origText, "filter_unknown_option"); | 824 return new InvalidFilter(origText, "filter_unknown_option"); |
822 } | 825 } |
823 } | 826 } |
824 | 827 |
825 try | 828 try |
826 { | 829 { |
827 if (blocking) | 830 if (blocking) |
828 { | 831 { |
829 if (csp && Filter.invalidCSPRegExp.test(csp)) | 832 if (csp && Filter.invalidCSPRegExp.test(csp)) |
830 return new InvalidFilter(origText, "filter_invalid_csp"); | 833 return new InvalidFilter(origText, "filter_invalid_csp"); |
831 | 834 |
832 return new BlockingFilter(origText, text, contentType, matchCase, domains, | 835 return new BlockingFilter(origText, text, contentType, matchCase, domains, |
833 thirdParty, sitekeys, collapse, csp); | 836 thirdParty, sitekeys, collapse, csp, rewrite); |
834 } | 837 } |
835 return new WhitelistFilter(origText, text, contentType, matchCase, domains, | 838 return new WhitelistFilter(origText, text, contentType, matchCase, domains, |
836 thirdParty, sitekeys); | 839 thirdParty, sitekeys); |
837 } | 840 } |
838 catch (e) | 841 catch (e) |
839 { | 842 { |
840 return new InvalidFilter(origText, "filter_invalid_regexp"); | 843 return new InvalidFilter(origText, "filter_invalid_regexp"); |
841 } | 844 } |
842 }; | 845 }; |
843 | 846 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
886 * @param {string} regexpSource see RegExpFilter() | 889 * @param {string} regexpSource see RegExpFilter() |
887 * @param {number} [contentType] see RegExpFilter() | 890 * @param {number} [contentType] see RegExpFilter() |
888 * @param {boolean} [matchCase] see RegExpFilter() | 891 * @param {boolean} [matchCase] see RegExpFilter() |
889 * @param {string} [domains] see RegExpFilter() | 892 * @param {string} [domains] see RegExpFilter() |
890 * @param {boolean} [thirdParty] see RegExpFilter() | 893 * @param {boolean} [thirdParty] see RegExpFilter() |
891 * @param {string} [sitekeys] see RegExpFilter() | 894 * @param {string} [sitekeys] see RegExpFilter() |
892 * @param {boolean} [collapse] | 895 * @param {boolean} [collapse] |
893 * defines whether the filter should collapse blocked content, can be null | 896 * defines whether the filter should collapse blocked content, can be null |
894 * @param {string} [csp] | 897 * @param {string} [csp] |
895 * Content Security Policy to inject when the filter matches | 898 * Content Security Policy to inject when the filter matches |
899 * @param {string} [rewrite] | |
900 * The rewrite expression | |
kzar
2018/05/15 14:08:58
Mind making this comment useful? (Same below.)
hub
2018/05/15 16:16:31
Done.
kzar
2018/05/16 09:22:40
Thanks, looking way better now.
| |
896 * @constructor | 901 * @constructor |
897 * @augments RegExpFilter | 902 * @augments RegExpFilter |
898 */ | 903 */ |
899 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, | 904 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, |
900 thirdParty, sitekeys, collapse, csp) | 905 thirdParty, sitekeys, collapse, csp, rewrite) |
901 { | 906 { |
902 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, | 907 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, |
903 thirdParty, sitekeys); | 908 thirdParty, sitekeys); |
904 | 909 |
905 this.collapse = collapse; | 910 this.collapse = collapse; |
906 this.csp = csp; | 911 this.csp = csp; |
912 this.rewrite = rewrite; | |
907 } | 913 } |
908 exports.BlockingFilter = BlockingFilter; | 914 exports.BlockingFilter = BlockingFilter; |
909 | 915 |
910 BlockingFilter.prototype = extend(RegExpFilter, { | 916 BlockingFilter.prototype = extend(RegExpFilter, { |
911 type: "blocking", | 917 type: "blocking", |
912 | 918 |
913 /** | 919 /** |
914 * Defines whether the filter should collapse blocked content. | 920 * Defines whether the filter should collapse blocked content. |
915 * Can be null (use the global preference). | 921 * Can be null (use the global preference). |
916 * @type {?boolean} | 922 * @type {?boolean} |
917 */ | 923 */ |
918 collapse: null, | 924 collapse: null, |
919 | 925 |
920 /** | 926 /** |
921 * Content Security Policy to inject for matching requests. | 927 * Content Security Policy to inject for matching requests. |
922 * @type {?string} | 928 * @type {?string} |
923 */ | 929 */ |
924 csp: null | 930 csp: null, |
931 | |
932 /** | |
933 * The rewrite expression | |
934 * @type {?string} | |
935 */ | |
936 rewrite: null, | |
937 | |
938 /** | |
939 * Rewrites an URL. | |
940 * @param {string} url the URL to rewrite | |
941 * @return {string} the rewritten URL, or the original in case of failure | |
942 */ | |
943 rewriteUrl(url) | |
944 { | |
945 try | |
946 { | |
947 let rewrittenUrl = new URL(url.replace(this.regexp, this.rewrite), url); | |
948 if (rewrittenUrl.origin == new URL(url).origin) | |
949 return rewrittenUrl.href; | |
950 } | |
951 catch (e) | |
952 { | |
953 } | |
954 | |
955 return url; | |
kzar
2018/05/15 14:24:57
Why do we return `url` on failure? Shouldn't we re
Sebastian Noack
2018/05/15 14:40:34
Even if no error is thrown and no origin change wa
kzar
2018/05/15 14:44:23
Not if we tweak the above logic, so we don't retur
Sebastian Noack
2018/05/15 14:51:02
Sure, we could add another check here, in order to
kzar
2018/05/15 14:53:58
Well it seems pointless to return the existing URL
Manish Jethani
2018/05/15 15:18:12
These are not the only cases in which the rewrite
hub
2018/05/15 16:16:31
The original idea is to not block the request in c
| |
956 } | |
925 }); | 957 }); |
926 | 958 |
927 /** | 959 /** |
928 * Class for whitelist filters | 960 * Class for whitelist filters |
929 * @param {string} text see Filter() | 961 * @param {string} text see Filter() |
930 * @param {string} regexpSource see RegExpFilter() | 962 * @param {string} regexpSource see RegExpFilter() |
931 * @param {number} [contentType] see RegExpFilter() | 963 * @param {number} [contentType] see RegExpFilter() |
932 * @param {boolean} [matchCase] see RegExpFilter() | 964 * @param {boolean} [matchCase] see RegExpFilter() |
933 * @param {string} [domains] see RegExpFilter() | 965 * @param {string} [domains] see RegExpFilter() |
934 * @param {boolean} [thirdParty] see RegExpFilter() | 966 * @param {boolean} [thirdParty] see RegExpFilter() |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1079 */ | 1111 */ |
1080 function ElemHideEmulationFilter(text, domains, selector) | 1112 function ElemHideEmulationFilter(text, domains, selector) |
1081 { | 1113 { |
1082 ElemHideBase.call(this, text, domains, selector); | 1114 ElemHideBase.call(this, text, domains, selector); |
1083 } | 1115 } |
1084 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; | 1116 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; |
1085 | 1117 |
1086 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { | 1118 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { |
1087 type: "elemhideemulation" | 1119 type: "elemhideemulation" |
1088 }); | 1120 }); |
OLD | NEW |