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

Delta Between Two Patch Sets: compiled/filter/Filter.cpp

Issue 29600641: Issue 5175 - Reject element hiding filter with empty domain names (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created Nov. 7, 2017, 11:17 p.m.
Right Patch Set: Make the new test more functional Created March 6, 2018, 7:47 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « compiled/filter/ElemHideFilter.cpp ('k') | compiled/filter/RegExpFilter.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include "Filter.h" 18 #include "Filter.h"
19 #include "CommentFilter.h" 19 #include "CommentFilter.h"
20 #include "InvalidFilter.h" 20 #include "InvalidFilter.h"
21 #include "RegExpFilter.h" 21 #include "RegExpFilter.h"
22 #include "BlockingFilter.h" 22 #include "BlockingFilter.h"
23 #include "WhitelistFilter.h" 23 #include "WhitelistFilter.h"
24 #include "ElemHideBase.h" 24 #include "ElemHideBase.h"
25 #include "ElemHideFilter.h" 25 #include "ElemHideFilter.h"
26 #include "ElemHideException.h" 26 #include "ElemHideException.h"
27 #include "ElemHideEmulationFilter.h" 27 #include "ElemHideEmulationFilter.h"
28 #include "../StringMap.h" 28 #include "../StringMap.h"
29
30 ABP_NS_USING
29 31
30 namespace 32 namespace
31 { 33 {
32 StringMap<Filter*> knownFilters(8192); 34 StringMap<Filter*> knownFilters(8192);
33 35
34 void NormalizeWhitespace(DependentString& text) 36 void NormalizeWhitespace(DependentString& text)
35 { 37 {
36 String::size_type start = 0; 38 String::size_type start = 0;
37 String::size_type end = text.length(); 39 String::size_type end = text.length();
38 40
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 if (text.empty()) 99 if (text.empty())
98 return nullptr; 100 return nullptr;
99 101
100 // Parsing also normalizes the filter text, so it has to be done before the 102 // Parsing also normalizes the filter text, so it has to be done before the
101 // lookup in knownFilters. 103 // lookup in knownFilters.
102 union 104 union
103 { 105 {
104 RegExpFilterData regexp; 106 RegExpFilterData regexp;
105 ElemHideData elemhide; 107 ElemHideData elemhide;
106 } data; 108 } data;
109 bool needConversion = false;
107 ParsedDomains parsedDomains; 110 ParsedDomains parsedDomains;
108 DependentString error; 111 DependentString error;
109 112
110 Filter::Type type = CommentFilter::Parse(text); 113 Filter::Type type = CommentFilter::Parse(text);
111 if (type == Filter::Type::UNKNOWN) 114 if (type == Filter::Type::UNKNOWN)
112 type = ElemHideBase::Parse(text, error, data.elemhide, parsedDomains); 115 type = ElemHideBase::Parse(text, error, data.elemhide, needConversion, parse dDomains);
113 if (type == Filter::Type::UNKNOWN) 116 if (type == Filter::Type::UNKNOWN)
114 type = RegExpFilter::Parse(text, error, data.regexp); 117 type = RegExpFilter::Parse(text, error, data.regexp);
118
119 if (needConversion)
120 text = ElemHideBase::ConvertFilter(text, data.elemhide.mSelectorStart);
121
122 // At that point we failed the conversion.
123 if (text.empty())
124 return nullptr;
115 125
116 auto knownFilter = knownFilters.find(text); 126 auto knownFilter = knownFilters.find(text);
117 if (knownFilter) 127 if (knownFilter)
118 { 128 {
119 knownFilter->second->AddRef(); 129 knownFilter->second->AddRef();
120 return knownFilter->second; 130 return knownFilter->second;
121 } 131 }
122 132
123 FilterPtr filter; 133 FilterPtr filter;
124 switch (type) 134 switch (type)
125 { 135 {
126 case CommentFilter::classType: 136 case CommentFilter::classType:
127 filter = FilterPtr(new CommentFilter(text), false); 137 filter = FilterPtr(new CommentFilter(text), false);
128 break; 138 break;
129 case InvalidFilter::classType: 139 case InvalidFilter::classType:
130 filter = FilterPtr(new InvalidFilter(text, error), false); 140 filter = FilterPtr(new InvalidFilter(text, error), false);
131 break; 141 break;
132 case BlockingFilter::classType: 142 case BlockingFilter::classType:
133 filter = FilterPtr(new BlockingFilter(text, data.regexp), false); 143 filter = FilterPtr(new BlockingFilter(text, data.regexp), false);
134 break; 144 break;
135 case WhitelistFilter::classType: 145 case WhitelistFilter::classType:
136 filter = FilterPtr(new WhitelistFilter(text, data.regexp), false); 146 filter = FilterPtr(new WhitelistFilter(text, data.regexp), false);
137 break; 147 break;
138 case ElemHideFilter::classType: 148 case ElemHideFilter::classType:
139 filter = FilterPtr(new ElemHideFilter(text, data.elemhide, 149 filter = FilterPtr(new ElemHideFilter(text, data.elemhide,
140 parsedDomains), false); 150 parsedDomains), false);
141 break; 151 break;
142 case ElemHideException::classType: 152 case ElemHideException::classType:
143 filter = FilterPtr(new ElemHideException(text, data.elemhide, 153 filter = FilterPtr(new ElemHideException(text, data.elemhide, parsedDomain s), false);
144 parsedDomains), false);
145 break; 154 break;
146 case ElemHideEmulationFilter::classType: 155 case ElemHideEmulationFilter::classType:
147 filter = FilterPtr(new ElemHideEmulationFilter(text, data.elemhide, 156 filter = FilterPtr(new ElemHideEmulationFilter(text, data.elemhide, parsed Domains), false);
148 parsedDomains), false);
149 if (static_cast<ElemHideEmulationFilter*>(filter.get())->IsGeneric()) 157 if (static_cast<ElemHideEmulationFilter*>(filter.get())->IsGeneric())
150 filter = FilterPtr(new InvalidFilter(text, u"filter_elemhideemulation_no domain"_str), false); 158 filter = FilterPtr(new InvalidFilter(text, u"filter_elemhideemulation_no domain"_str), false);
151 break; 159 break;
152 default: 160 default:
153 // This should never happen but just in case 161 // This should never happen but just in case
154 return nullptr; 162 return nullptr;
155 } 163 }
156 164
157 // This is a hack: we looked up the entry using text but create it using
158 // filter->mText. This works because both are equal at this point. However,
159 // text refers to a temporary buffer which will go away.
160 enter_context("Adding to known filters"); 165 enter_context("Adding to known filters");
161 knownFilter.assign(filter->mText, filter.get()); 166 if (text != filter->mText)
167 knownFilters[filter->mText] = filter.get();
168 else
169 // This is a hack: we looked up the entry using text but create it using
170 // filter->mText. This works because both are equal at this point. However,
171 // text refers to a temporary buffer which will go away.
172 knownFilter.assign(filter->mText, filter.get());
162 exit_context(); 173 exit_context();
163 174
164 return filter.release(); 175 return filter.release();
165 } 176 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld