Bug 1388582 - Skip unsupported threat types on current platform while making v4 request. draft
authorHenry Chang <hchang@mozilla.com>
Wed, 16 Aug 2017 15:30:20 +0800
changeset 649716 677ab508030e3d0faf641e7dd6af2238a2191248
parent 649622 7dddbd85047c6dc73ddbe1e423cd643a217845b3
child 727153 29f6a5d9a261add706ed4a2c44b92a911dde9176
push id75113
push userhchang@mozilla.com
push dateMon, 21 Aug 2017 07:08:59 +0000
bugs1388582
milestone57.0a1
Bug 1388582 - Skip unsupported threat types on current platform while making v4 request. MozReview-Commit-ID: FTmOMQ339ZL
toolkit/components/url-classifier/nsUrlClassifierUtils.cpp
toolkit/components/url-classifier/tests/unit/test_bug1388582_pha_mobile_only.js
toolkit/components/url-classifier/tests/unit/xpcshell.ini
--- a/toolkit/components/url-classifier/nsUrlClassifierUtils.cpp
+++ b/toolkit/components/url-classifier/nsUrlClassifierUtils.cpp
@@ -139,16 +139,31 @@ CreateClientInfo()
     clientId = "Firefox"; // Use "Firefox" as fallback.
   }
 
   c->set_client_id(clientId.get());
 
   return c;
 }
 
+static bool
+IsAllowedOnCurrentPlatform(uint32_t aThreatType)
+{
+  PlatformType platform = GetPlatformType();
+
+  switch (aThreatType) {
+  case POTENTIALLY_HARMFUL_APPLICATION:
+    // Bug 1388582 - Google server would respond 404 error if the request
+    // contains PHA on non-mobile platform.
+    return ANDROID_PLATFORM == platform;
+  default:
+    return true;
+  }
+}
+
 } // end of namespace safebrowsing.
 } // end of namespace mozilla.
 
 nsUrlClassifierUtils::nsUrlClassifierUtils()
   : mProviderDictLock("nsUrlClassifierUtils.mProviderDictLock")
 {
 }
 
@@ -340,16 +355,23 @@ nsUrlClassifierUtils::MakeUpdateRequestV
 
   for (uint32_t i = 0; i < aCount; i++) {
     nsCString listName(aListNames[i]);
     uint32_t threatType;
     nsresult rv = ConvertListNameToThreatType(listName, &threatType);
     if (NS_FAILED(rv)) {
       continue; // Unknown list name.
     }
+    if (!IsAllowedOnCurrentPlatform(threatType)) {
+      NS_WARNING(nsPrintfCString("Threat type %d (%s) is unsupported on current platform: %d",
+                                 threatType,
+                                 aListNames[i],
+                                 GetPlatformType()).get());
+      continue; // Some threat types are not available on some platforms.
+    }
     auto lur = r.mutable_list_update_requests()->Add();
     InitListUpdateRequest(static_cast<ThreatType>(threatType), aStatesBase64[i], lur);
   }
 
   // Then serialize.
   std::string s;
   r.SerializeToString(&s);
 
@@ -373,34 +395,41 @@ nsUrlClassifierUtils::MakeFindFullHashRe
                                                 uint32_t aPrefixCount,
                                                 nsACString &aRequest)
 {
   FindFullHashesRequest r;
   r.set_allocated_client(CreateClientInfo());
 
   nsresult rv;
 
-  // Set up FindFullHashesRequest.client_states.
-  for (uint32_t i = 0; i < aListCount; i++) {
-    nsCString stateBinary;
-    rv = Base64Decode(nsDependentCString(aListStatesBase64[i]), stateBinary);
-    NS_ENSURE_SUCCESS(rv, rv);
-    r.add_client_states(stateBinary.get(), stateBinary.Length());
-  }
-
   //-------------------------------------------------------------------
   // Set up FindFullHashesRequest.threat_info.
   auto threatInfo = r.mutable_threat_info();
 
   // 1) Set threat types.
   for (uint32_t i = 0; i < aListCount; i++) {
+    // Add threat types.
     uint32_t threatType;
     rv = ConvertListNameToThreatType(nsDependentCString(aListNames[i]), &threatType);
     NS_ENSURE_SUCCESS(rv, rv);
+    if (!IsAllowedOnCurrentPlatform(threatType)) {
+      NS_WARNING(nsPrintfCString("Threat type %d (%s) is unsupported on current platform: %d",
+                                 threatType,
+                                 aListNames[i],
+                                 GetPlatformType()).get());
+      continue;
+    }
     threatInfo->add_threat_types((ThreatType)threatType);
+
+    // Add client states for index 'i' only when the threat type is available
+    // on current platform.
+    nsCString stateBinary;
+    rv = Base64Decode(nsDependentCString(aListStatesBase64[i]), stateBinary);
+    NS_ENSURE_SUCCESS(rv, rv);
+    r.add_client_states(stateBinary.get(), stateBinary.Length());
   }
 
   // 2) Set platform type.
   threatInfo->add_platform_types(GetPlatformType());
 
   // 3) Set threat entry type.
   threatInfo->add_threat_entry_types(URL);
 
new file mode 100644
--- /dev/null
+++ b/toolkit/components/url-classifier/tests/unit/test_bug1388582_pha_mobile_only.js
@@ -0,0 +1,48 @@
+Cu.import("resource://gre/modules/AppConstants.jsm");
+
+let urlUtils = Cc["@mozilla.org/url-classifier/utils;1"]
+                 .getService(Ci.nsIUrlClassifierUtils);
+
+function testUpdateRequest() {
+  let requestWithPHA =
+    urlUtils.makeUpdateRequestV4(["goog-phish-proto", "goog-harmful-proto"],
+                                 ["AAAAAA", "AAAAAA"], 2);
+
+  let requestNoPHA =
+    urlUtils.makeUpdateRequestV4(["goog-phish-proto"], ["AAAAAA"], 1);
+
+  if (AppConstants.platform === "android") {
+    notEqual(requestWithPHA, requestNoPHA,
+             "PHA (i.e. goog-harmful-proto) shouldn't be filtered on mobile platform.");
+  } else {
+    equal(requestWithPHA, requestNoPHA,
+          "PHA (i.e. goog-harmful-proto) should be filtered on non-mobile platform.");
+  }
+}
+
+function testFullHashRequest() {
+  let requestWithPHA =
+    urlUtils.makeFindFullHashRequestV4(["goog-phish-proto", "goog-harmful-proto"],
+                                       ["", ""],       // state.
+                                       [btoa("0123")], // prefix.
+                                       2, 1);
+
+  let requestNoPHA =
+    urlUtils.makeFindFullHashRequestV4(["goog-phish-proto"],
+                                       [""],           // state.
+                                       [btoa("0123")], // prefix.
+                                       1, 1);
+
+  if (AppConstants.platform === "android") {
+    notEqual(requestWithPHA, requestNoPHA,
+             "PHA (i.e. goog-harmful-proto) shouldn't be filtered on mobile platform.");
+  } else {
+    equal(requestWithPHA, requestNoPHA,
+          "PHA (i.e. goog-harmful-proto) should be filtered on non-mobile platform.");
+  }
+}
+
+function run_test() {
+  testUpdateRequest();
+  testFullHashRequest();
+}
--- a/toolkit/components/url-classifier/tests/unit/xpcshell.ini
+++ b/toolkit/components/url-classifier/tests/unit/xpcshell.ini
@@ -17,8 +17,9 @@ support-files =
 [test_prefixset.js]
 [test_threat_type_conversion.js]
 [test_provider_url.js]
 [test_streamupdater.js]
 [test_digest256.js]
 [test_listmanager.js]
 [test_pref.js]
 [test_safebrowsing_protobuf.js]
+[test_bug1388582_pha_mobile_only.js]
\ No newline at end of file