OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/installer/util/google_update_util.h" | 5 #include "chrome/installer/util/google_update_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 return !(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || | 125 return !(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || |
126 c >= '0' && c <= '9' || c == '-' || c == '_' || c == '$'); | 126 c >= '0' && c <= '9' || c == '-' || c == '_' || c == '$'); |
127 } | 127 } |
128 | 128 |
129 // Returns true if |key| from untrusted data is valid. | 129 // Returns true if |key| from untrusted data is valid. |
130 bool IsUntrustedDataKeyValid(const std::string& key) { | 130 bool IsUntrustedDataKeyValid(const std::string& key) { |
131 return std::find_if(key.begin(), key.end(), IsIllegalUntrustedDataKeyChar) | 131 return std::find_if(key.begin(), key.end(), IsIllegalUntrustedDataKeyChar) |
132 == key.end(); | 132 == key.end(); |
133 } | 133 } |
134 | 134 |
135 // Reads and parses untrusted data passed from Google Update as key-value | 135 bool ParseUntrustedData( |
grt (UTC plus 2)
2014/03/03 15:18:26
add doc comment
jackhou1
2014/03/04 00:56:28
Done.
| |
136 // pairs, then overwrites |untrusted_data_map| with the result. | 136 const std::string& data_string, |
137 // Returns true if data are successfully read. | |
138 bool GetGoogleUpdateUntrustedData( | |
139 std::map<std::string, std::string>* untrusted_data) { | 137 std::map<std::string, std::string>* untrusted_data) { |
140 DCHECK(untrusted_data); | 138 if (!IsStringPrintable(data_string)) { |
141 scoped_ptr<base::Environment> env(base::Environment::Create()); | 139 LOG(ERROR) << "Invalid value in untrusted data string."; |
142 std::string data_string; | |
143 if (env == NULL || !env->GetVar(kEnvVariableUntrustedData, &data_string)) | |
144 return false; | |
145 | |
146 if (data_string.length() > kEnvVariableUntrustedDataMaxLength || | |
147 !IsStringPrintable(data_string)) { | |
148 LOG(ERROR) << "Invalid value in " << kEnvVariableUntrustedData; | |
149 return false; | 140 return false; |
150 } | 141 } |
151 | |
152 VLOG(1) << kEnvVariableUntrustedData << ": " << data_string; | |
153 | |
154 std::vector<std::pair<std::string, std::string> > kv_pairs; | 142 std::vector<std::pair<std::string, std::string> > kv_pairs; |
155 if (!base::SplitStringIntoKeyValuePairs(data_string, '=', '&', &kv_pairs)) { | 143 if (!base::SplitStringIntoKeyValuePairs(data_string, '=', '&', &kv_pairs)) { |
156 LOG(ERROR) << "Failed to parse untrusted data: " << data_string; | 144 LOG(ERROR) << "Failed to parse untrusted data: " << data_string; |
157 return false; | 145 return false; |
158 } | 146 } |
159 | 147 |
160 untrusted_data->clear(); | 148 untrusted_data->clear(); |
161 std::vector<std::pair<std::string, std::string> >::const_iterator it; | 149 std::vector<std::pair<std::string, std::string> >::const_iterator it; |
162 for (it = kv_pairs.begin(); it != kv_pairs.end(); ++it) { | 150 for (it = kv_pairs.begin(); it != kv_pairs.end(); ++it) { |
163 const std::string& key(it->first); | 151 const std::string& key(it->first); |
164 // TODO(huangs): URL unescape |value|. | 152 // TODO(huangs): URL unescape |value|. |
165 const std::string& value(it->second); | 153 const std::string& value(it->second); |
166 if (IsUntrustedDataKeyValid(key) && IsStringPrintable(value)) | 154 if (IsUntrustedDataKeyValid(key) && IsStringPrintable(value)) |
167 (*untrusted_data)[key] = value; | 155 (*untrusted_data)[key] = value; |
168 else | 156 else |
169 LOG(ERROR) << "Illegal character found in untrusted data."; | 157 LOG(ERROR) << "Illegal character found in untrusted data."; |
170 } | 158 } |
171 return true; | 159 return true; |
172 } | 160 } |
173 | 161 |
162 // Reads and parses untrusted data passed from Google Update as key-value | |
163 // pairs, then overwrites |untrusted_data_map| with the result. | |
164 // Returns true if data are successfully read. | |
165 bool GetGoogleUpdateUntrustedData( | |
166 std::map<std::string, std::string>* untrusted_data) { | |
167 DCHECK(untrusted_data); | |
168 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
169 std::string data_string; | |
170 if (env == NULL || !env->GetVar(kEnvVariableUntrustedData, &data_string)) | |
grt (UTC plus 2)
2014/03/03 15:18:26
i think "!env" is more idomatic for testing that a
jackhou1
2014/03/04 00:56:28
Done.
| |
171 return false; | |
172 | |
173 if (data_string.length() > kEnvVariableUntrustedDataMaxLength) { | |
174 LOG(ERROR) << kEnvVariableUntrustedData << " is too long."; | |
175 return false; | |
176 } | |
177 | |
178 VLOG(1) << kEnvVariableUntrustedData << ": " << data_string; | |
grt (UTC plus 2)
2014/03/03 15:18:26
don't log data_string here since IsStringPrintable
jackhou1
2014/03/04 00:56:28
Done.
| |
179 | |
180 return ParseUntrustedData(data_string, untrusted_data); | |
181 } | |
182 | |
174 } // namespace | 183 } // namespace |
175 | 184 |
176 bool EnsureUserLevelGoogleUpdatePresent() { | 185 bool EnsureUserLevelGoogleUpdatePresent() { |
177 VLOG(0) << "Ensuring Google Update is present at user-level."; | 186 VLOG(0) << "Ensuring Google Update is present at user-level."; |
178 | 187 |
179 bool success = false; | 188 bool success = false; |
180 if (IsGoogleUpdatePresent(false)) { | 189 if (IsGoogleUpdatePresent(false)) { |
181 success = true; | 190 success = true; |
182 } else { | 191 } else { |
183 base::string16 cmd_string; | 192 base::string16 cmd_string; |
(...skipping 30 matching lines...) Expand all Loading... | |
214 if (GetGoogleUpdateUntrustedData(&untrusted_data)) { | 223 if (GetGoogleUpdateUntrustedData(&untrusted_data)) { |
215 std::map<std::string, std::string>::const_iterator data_it( | 224 std::map<std::string, std::string>::const_iterator data_it( |
216 untrusted_data.find(key)); | 225 untrusted_data.find(key)); |
217 if (data_it != untrusted_data.end()) | 226 if (data_it != untrusted_data.end()) |
218 return data_it->second; | 227 return data_it->second; |
219 } | 228 } |
220 | 229 |
221 return std::string(); | 230 return std::string(); |
222 } | 231 } |
223 | 232 |
233 std::string GetUntrustedDataValueFromTag(const std::string& tag, | |
grt (UTC plus 2)
2014/03/03 15:18:26
should the size of |tag| be restricted in the same
jackhou1
2014/03/04 00:56:28
Done.
I'm don't know if there is a specific reaso
| |
234 const std::string& key) { | |
235 std::map<std::string, std::string> untrusted_data; | |
236 if (ParseUntrustedData(tag, &untrusted_data)) { | |
grt (UTC plus 2)
2014/03/03 15:18:26
without the logging calls (see next comment), i be
jackhou1
2014/03/04 00:56:28
Done.
| |
237 std::map<std::string, std::string>::const_iterator data_it( | |
238 untrusted_data.find(key)); | |
239 if (data_it != untrusted_data.end()) { | |
240 VLOG(1) << "Key " << key << " is " << data_it->second; | |
grt (UTC plus 2)
2014/03/03 15:18:26
coding style says "Remove most logging calls befor
jackhou1
2014/03/04 00:56:28
Done.
| |
241 return data_it->second; | |
242 } else { | |
243 VLOG(1) << "Key not found: " << key; | |
244 } | |
245 } | |
246 | |
247 return std::string(); | |
248 } | |
249 | |
224 } // namespace google_update | 250 } // namespace google_update |
OLD | NEW |