| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/browser/safe_browsing/safe_browsing_api_handler_util.h" | 5 #include "components/safe_browsing_db/safe_browsing_api_handler_util.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/browser/safe_browsing/metadata.pb.h" | 16 #include "components/safe_browsing_db/metadata.pb.h" |
| 17 #include "chrome/browser/safe_browsing/safe_browsing_util.h" | 17 #include "components/safe_browsing_db/util.h" |
| 18 | |
| 19 | 18 |
| 20 namespace safe_browsing { | 19 namespace safe_browsing { |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 23 // JSON metatdata keys. These are are fixed in the Java-side API. | 22 // JSON metatdata keys. These are are fixed in the Java-side API. |
| 24 const char kJsonKeyMatches[] = "matches"; | 23 const char kJsonKeyMatches[] = "matches"; |
| 25 const char kJsonKeyThreatType[] = "threat_type"; | 24 const char kJsonKeyThreatType[] = "threat_type"; |
| 26 | 25 |
| 27 // Do not reorder or delete. Make sure changes are reflected in | 26 // Do not reorder or delete. Make sure changes are reflected in |
| 28 // SB2RemoteCallThreatSubType. | 27 // SB2RemoteCallThreatSubType. |
| 29 enum UmaThreatSubType { | 28 enum UmaThreatSubType { |
| 30 UMA_THREAT_SUB_TYPE_NOT_SET = 0, | 29 UMA_THREAT_SUB_TYPE_NOT_SET = 0, |
| 31 UMA_THREAT_SUB_TYPE_LANDING = 1, | 30 UMA_THREAT_SUB_TYPE_LANDING = 1, |
| 32 UMA_THREAT_SUB_TYPE_DISTRIBUTION = 2, | 31 UMA_THREAT_SUB_TYPE_DISTRIBUTION = 2, |
| 33 UMA_THREAT_SUB_TYPE_UNKNOWN = 3, | 32 UMA_THREAT_SUB_TYPE_UNKNOWN = 3, |
| 34 UMA_THREAT_SUB_TYPE_MAX_VALUE | 33 UMA_THREAT_SUB_TYPE_MAX_VALUE |
| 35 }; | 34 }; |
| 36 | 35 |
| 37 void ReportUmaThreatSubType(SBThreatType threat_type, | 36 void ReportUmaThreatSubType(SBThreatType threat_type, |
| 38 UmaThreatSubType sub_type) { | 37 UmaThreatSubType sub_type) { |
| 39 if (threat_type == SB_THREAT_TYPE_URL_MALWARE) { | 38 if (threat_type == SB_THREAT_TYPE_URL_MALWARE) { |
| 40 UMA_HISTOGRAM_ENUMERATION( | 39 UMA_HISTOGRAM_ENUMERATION( |
| 41 "SB2.RemoteCall.ThreatSubType.PotentiallyHarmfulApp", sub_type, | 40 "SB2.RemoteCall.ThreatSubType.PotentiallyHarmfulApp", sub_type, |
| 42 UMA_THREAT_SUB_TYPE_MAX_VALUE); | 41 UMA_THREAT_SUB_TYPE_MAX_VALUE); |
| 43 } else { | 42 } else { |
| 44 UMA_HISTOGRAM_ENUMERATION( | 43 UMA_HISTOGRAM_ENUMERATION("SB2.RemoteCall.ThreatSubType.SocialEngineering", |
| 45 "SB2.RemoteCall.ThreatSubType.SocialEngineering", sub_type, | 44 sub_type, UMA_THREAT_SUB_TYPE_MAX_VALUE); |
| 46 UMA_THREAT_SUB_TYPE_MAX_VALUE); | |
| 47 } | 45 } |
| 48 } | 46 } |
| 49 | 47 |
| 50 // Parse the extra key/value pair(s) added by Safe Browsing backend. To make | 48 // Parse the extra key/value pair(s) added by Safe Browsing backend. To make |
| 51 // it binary compatible with the Pver3 metadata that UIManager expects to | 49 // it binary compatible with the Pver3 metadata that UIManager expects to |
| 52 // deserialize, we convert it to a MalwarePatternType. | 50 // deserialize, we convert it to a MalwarePatternType. |
| 53 // | 51 // |
| 54 // TODO(nparker): When chrome desktop is converted to use Pver4, convert this | 52 // TODO(nparker): When chrome desktop is converted to use Pver4, convert this |
| 55 // to the new proto instead. | 53 // to the new proto instead. |
| 56 const std::string ParseExtraMetadataToPB(const base::DictionaryValue* match, | 54 const std::string ParseExtraMetadataToPB(const base::DictionaryValue* match, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 case JAVA_THREAT_TYPE_SOCIAL_ENGINEERING: | 102 case JAVA_THREAT_TYPE_SOCIAL_ENGINEERING: |
| 105 return SB_THREAT_TYPE_URL_PHISHING; | 103 return SB_THREAT_TYPE_URL_PHISHING; |
| 106 default: | 104 default: |
| 107 // Unknown threat type | 105 // Unknown threat type |
| 108 return SB_THREAT_TYPE_SAFE; | 106 return SB_THREAT_TYPE_SAFE; |
| 109 } | 107 } |
| 110 } | 108 } |
| 111 | 109 |
| 112 } // namespace | 110 } // namespace |
| 113 | 111 |
| 114 | |
| 115 // Valid examples: | 112 // Valid examples: |
| 116 // {"matches":[{"threat_type":"5"}]} | 113 // {"matches":[{"threat_type":"5"}]} |
| 117 // or | 114 // or |
| 118 // {"matches":[{"threat_type":"4"}, | 115 // {"matches":[{"threat_type":"4"}, |
| 119 // {"threat_type":"5", "se_pattern_type":"LANDING"}]} | 116 // {"threat_type":"5", "se_pattern_type":"LANDING"}]} |
| 120 UmaRemoteCallResult ParseJsonToThreatAndPB(const std::string& metadata_str, | 117 UmaRemoteCallResult ParseJsonToThreatAndPB(const std::string& metadata_str, |
| 121 SBThreatType* worst_threat, | 118 SBThreatType* worst_threat, |
| 122 std::string* metadata_pb_str) { | 119 std::string* metadata_pb_str) { |
| 123 *worst_threat = SB_THREAT_TYPE_SAFE; // Default to safe. | 120 *worst_threat = SB_THREAT_TYPE_SAFE; // Default to safe. |
| 124 *metadata_pb_str = std::string(); | 121 *metadata_pb_str = std::string(); |
| 125 | 122 |
| 126 if (metadata_str.empty()) | 123 if (metadata_str.empty()) |
| 127 return UMA_STATUS_JSON_EMPTY; | 124 return UMA_STATUS_JSON_EMPTY; |
| 128 | 125 |
| 129 // Pick out the "matches" list. | 126 // Pick out the "matches" list. |
| 130 scoped_ptr<base::Value> value = base::JSONReader::Read(metadata_str); | 127 scoped_ptr<base::Value> value = base::JSONReader::Read(metadata_str); |
| 131 const base::ListValue* matches; | 128 const base::ListValue* matches = nullptr; |
| 132 if (!value.get() || !value->IsType(base::Value::TYPE_DICTIONARY) || | 129 if (!value.get() || !value->IsType(base::Value::TYPE_DICTIONARY) || |
| 133 !(static_cast<base::DictionaryValue*>(value.get())) | 130 !(static_cast<base::DictionaryValue*>(value.get())) |
| 134 ->GetList(kJsonKeyMatches, &matches)) { | 131 ->GetList(kJsonKeyMatches, &matches) || |
| 132 !matches) { |
| 135 return UMA_STATUS_JSON_FAILED_TO_PARSE; | 133 return UMA_STATUS_JSON_FAILED_TO_PARSE; |
| 136 } | 134 } |
| 137 | 135 |
| 138 // Go through each matched threat type and pick the most severe. | 136 // Go through each matched threat type and pick the most severe. |
| 139 int worst_threat_num = -1; | 137 int worst_threat_num = -1; |
| 140 const base::DictionaryValue* worst_match = nullptr; | 138 const base::DictionaryValue* worst_match = nullptr; |
| 141 for (size_t i = 0; i < matches->GetSize(); i++) { | 139 for (size_t i = 0; i < matches->GetSize(); i++) { |
| 142 // Get the threat number | 140 // Get the threat number |
| 143 const base::DictionaryValue* match; | 141 const base::DictionaryValue* match; |
| 144 std::string threat_num_str; | 142 std::string threat_num_str; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 158 | 156 |
| 159 *worst_threat = JavaToSBThreatType(worst_threat_num); | 157 *worst_threat = JavaToSBThreatType(worst_threat_num); |
| 160 if (*worst_threat == SB_THREAT_TYPE_SAFE || !worst_match) | 158 if (*worst_threat == SB_THREAT_TYPE_SAFE || !worst_match) |
| 161 return UMA_STATUS_JSON_UNKNOWN_THREAT; | 159 return UMA_STATUS_JSON_UNKNOWN_THREAT; |
| 162 *metadata_pb_str = ParseExtraMetadataToPB(worst_match, *worst_threat); | 160 *metadata_pb_str = ParseExtraMetadataToPB(worst_match, *worst_threat); |
| 163 | 161 |
| 164 return UMA_STATUS_UNSAFE; // success | 162 return UMA_STATUS_UNSAFE; // success |
| 165 } | 163 } |
| 166 | 164 |
| 167 } // namespace safe_browsing | 165 } // namespace safe_browsing |
| OLD | NEW |