Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(615)

Side by Side Diff: chrome/common/content_settings_pattern.cc

Issue 8676020: Detect invalid content settings pattern that were not detected yet. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/common/content_settings_pattern.h" 5 #include "chrome/common/content_settings_pattern.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/string_split.h" 10 #include "base/string_split.h"
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 } 134 }
135 135
136 BuilderInterface* ContentSettingsPattern::Builder::Invalid() { 136 BuilderInterface* ContentSettingsPattern::Builder::Invalid() {
137 is_valid_ = false; 137 is_valid_ = false;
138 return this; 138 return this;
139 } 139 }
140 140
141 ContentSettingsPattern ContentSettingsPattern::Builder::Build() { 141 ContentSettingsPattern ContentSettingsPattern::Builder::Build() {
142 if (!is_valid_) 142 if (!is_valid_)
143 return ContentSettingsPattern(); 143 return ContentSettingsPattern();
144 Canonicalize(&parts_); 144 if (!Canonicalize(&parts_))
145 return ContentSettingsPattern();
145 if (use_legacy_validate_) { 146 if (use_legacy_validate_) {
146 is_valid_ = LegacyValidate(parts_); 147 is_valid_ = LegacyValidate(parts_);
147 } else { 148 } else {
148 is_valid_ = Validate(parts_); 149 is_valid_ = Validate(parts_);
149 } 150 }
150 return ContentSettingsPattern(parts_, is_valid_); 151 return ContentSettingsPattern(parts_, is_valid_);
151 } 152 }
152 153
153 // static 154 // static
154 void ContentSettingsPattern::Builder::Canonicalize(PatternParts* parts) { 155 bool ContentSettingsPattern::Builder::Canonicalize(PatternParts* parts) {
155 // Canonicalize the scheme part. 156 // Canonicalize the scheme part.
156 const std::string scheme(StringToLowerASCII(parts->scheme)); 157 const std::string scheme(StringToLowerASCII(parts->scheme));
157 parts->scheme = scheme; 158 parts->scheme = scheme;
158 159
159 if (parts->scheme == std::string(chrome::kFileScheme)) { 160 if (parts->scheme == std::string(chrome::kFileScheme)) {
160 GURL url(std::string(chrome::kFileScheme) + 161 GURL url(std::string(chrome::kFileScheme) +
161 std::string(chrome::kStandardSchemeSeparator) + parts->path); 162 std::string(chrome::kStandardSchemeSeparator) + parts->path);
162 parts->path = url.path(); 163 parts->path = url.path();
163 } 164 }
164 165
165 // Canonicalize the host part. 166 // Canonicalize the host part.
166 const std::string host(parts->host); 167 const std::string host(parts->host);
167 url_canon::CanonHostInfo host_info; 168 url_canon::CanonHostInfo host_info;
168 std::string canonicalized_host(net::CanonicalizeHost(host, &host_info)); 169 std::string canonicalized_host(net::CanonicalizeHost(host, &host_info));
170 if (host_info.IsIPAddress() && parts->has_domain_wildcard)
171 return false;
169 canonicalized_host = net::TrimEndingDot(canonicalized_host); 172 canonicalized_host = net::TrimEndingDot(canonicalized_host);
170 173
171 parts->host = ""; 174 parts->host = "";
172 if ((host.find('*') == std::string::npos) && 175 if ((host.find('*') == std::string::npos) &&
173 !canonicalized_host.empty()) { 176 !canonicalized_host.empty()) {
174 // Valid host. 177 // Valid host.
175 parts->host += canonicalized_host; 178 parts->host += canonicalized_host;
176 } 179 }
180 return true;
177 } 181 }
178 182
179 // static 183 // static
180 bool ContentSettingsPattern::Builder::Validate(const PatternParts& parts) { 184 bool ContentSettingsPattern::Builder::Validate(const PatternParts& parts) {
181 // If the pattern is for a "file-pattern" test if it is valid. 185 // Sanity checks first: {scheme, port} wildcards imply empty {scheme, port}.
182 if (parts.scheme == std::string(chrome::kFileScheme) && 186 if ((parts.is_scheme_wildcard && !parts.scheme.empty()) ||
183 !parts.is_scheme_wildcard && 187 (parts.is_port_wildcard && !parts.port.empty())) {
184 parts.host.empty() && 188 NOTREACHED();
185 parts.port.empty()) 189 return false;
186 return true; 190 }
191
192 // file:// URL patterns have an empty host and port.
193 if (parts.scheme == std::string(chrome::kFileScheme))
194 return parts.host.empty() &&
195 parts.port.empty() &&
196 !parts.path.empty() &&
197 parts.path != std::string("/") &&
198 parts.path.find("*") == std::string::npos;
187 199
188 // If the pattern is for an extension URL test if it is valid. 200 // If the pattern is for an extension URL test if it is valid.
189 if (parts.scheme == std::string(chrome::kExtensionScheme) && 201 if (parts.scheme == std::string(chrome::kExtensionScheme) &&
190 !parts.is_scheme_wildcard &&
191 !parts.host.empty() && 202 !parts.host.empty() &&
192 !parts.has_domain_wildcard && 203 !parts.has_domain_wildcard &&
193 parts.port.empty() && 204 parts.port.empty() &&
194 !parts.is_port_wildcard) 205 !parts.is_port_wildcard) {
195 return true; 206 return true;
207 }
196 208
197 // Non-file patterns are invalid if either the scheme, host or port part is 209 // Non-file patterns are invalid if either the scheme, host or port part is
198 // empty. 210 // empty.
199 if ((parts.scheme.empty() && !parts.is_scheme_wildcard) || 211 if ((parts.scheme.empty() && !parts.is_scheme_wildcard) ||
200 (parts.host.empty() && !parts.has_domain_wildcard) || 212 (parts.host.empty() && !parts.has_domain_wildcard) ||
201 (parts.port.empty() && !parts.is_port_wildcard)) 213 (parts.port.empty() && !parts.is_port_wildcard)) {
214 return false;
215 }
216
217 if (parts.host.find("*") != std::string::npos)
202 return false; 218 return false;
203 219
204 // Test if the scheme is supported or a wildcard. 220 // Test if the scheme is supported or a wildcard.
205 if (!parts.is_scheme_wildcard && 221 if (!parts.is_scheme_wildcard &&
206 parts.scheme != std::string(chrome::kHttpScheme) && 222 parts.scheme != std::string(chrome::kHttpScheme) &&
207 parts.scheme != std::string(chrome::kHttpsScheme)) { 223 parts.scheme != std::string(chrome::kHttpsScheme)) {
208 return false; 224 return false;
209 } 225 }
210 return true; 226 return true;
211 } 227 }
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
614 if (!parts.is_port_wildcard && other_parts.is_port_wildcard) 630 if (!parts.is_port_wildcard && other_parts.is_port_wildcard)
615 return ContentSettingsPattern::PREDECESSOR; 631 return ContentSettingsPattern::PREDECESSOR;
616 632
617 int result = parts.port.compare(other_parts.port); 633 int result = parts.port.compare(other_parts.port);
618 if (result == 0) 634 if (result == 0)
619 return ContentSettingsPattern::IDENTITY; 635 return ContentSettingsPattern::IDENTITY;
620 if (result > 0) 636 if (result > 0)
621 return ContentSettingsPattern::DISJOINT_ORDER_PRE; 637 return ContentSettingsPattern::DISJOINT_ORDER_PRE;
622 return ContentSettingsPattern::DISJOINT_ORDER_POST; 638 return ContentSettingsPattern::DISJOINT_ORDER_POST;
623 } 639 }
OLDNEW
« no previous file with comments | « chrome/common/content_settings_pattern.h ('k') | chrome/common/content_settings_pattern_parser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698