Bug 1049583 - Allow sub-domain wildcard on ScreenSharing domain whitelist patterns. r=ethanhugg, r=mt, a=sledru
authorPaul Kerr [:pkerr] <pkerr@mozilla.com>
Thu, 07 Aug 2014 15:12:26 -0700
changeset 217732 104c671afe110c6c4200b4fc7cba0c8d63824b13
parent 217731 b529a7e31a988a8ab53665c51e95efe9c4ffcf84
child 217733 ee3f447df950925334ee031d15fbe135acb7002f
push id515
push userraliiev@mozilla.com
push dateMon, 06 Oct 2014 12:51:51 +0000
treeherdermozilla-release@267c7a481bef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersethanhugg, mt, sledru
bugs1049583
milestone33.0a2
Bug 1049583 - Allow sub-domain wildcard on ScreenSharing domain whitelist patterns. r=ethanhugg, r=mt, a=sledru
dom/media/MediaManager.cpp
--- a/dom/media/MediaManager.cpp
+++ b/dom/media/MediaManager.cpp
@@ -103,16 +103,38 @@ using dom::MediaStreamConstraints;      
 using dom::MediaTrackConstraintSet;        // Mandatory or optional constraints
 using dom::MediaTrackConstraints;          // Raw mMandatory (as JSObject)
 using dom::GetUserMediaRequest;
 using dom::Sequence;
 using dom::OwningBooleanOrMediaTrackConstraints;
 using dom::SupportedAudioConstraints;
 using dom::SupportedVideoConstraints;
 
+static bool
+HostInDomain(const nsCString &aHost, const nsCString &aPattern)
+{
+  PRInt32 patternOffset = 0;
+  PRInt32 hostOffset = 0;
+
+  // Act on '*.' wildcard in the left-most position in a domain pattern.
+  if (aPattern.Length() > 2 && aPattern[0] == '*' && aPattern[1] == '.') {
+    patternOffset = 2;
+
+    // Ignore the lowest level sub-domain for the hostname.
+    hostOffset = aHost.FindChar('.') + 1;
+
+    if (hostOffset <= 1) {
+      // Reject a match between a wildcard and a TLD or '.foo' form.
+      return false;
+    }
+  }
+
+  nsDependentCString hostRoot(aHost, hostOffset);
+  return hostRoot.EqualsIgnoreCase(aPattern.BeginReading() + patternOffset);
+}
 
 static bool
 HostHasPermission(nsIURI &docURI)
 {
   nsAdoptingCString hostName;
   docURI.GetAsciiHost(hostName); //normalize UTF8 to ASCII equivalent
   nsAdoptingCString domainWhiteList =
     Preferences::GetCString("media.getusermedia.screensharing.allowed_domains");
@@ -143,17 +165,17 @@ HostHasPermission(nsIURI &docURI)
     if (end == (PRUint32)-1) {
       // Last or only domain name in the comma separated list
       end = domainWhiteList.Length();
     }
 
     rv = idnService->ConvertUTF8toACE(Substring(domainWhiteList, begin, end - begin),
                                       domainName);
     if (NS_SUCCEEDED(rv)) {
-      if (hostName.EqualsIgnoreCase(domainName.Data(), domainName.Length())) {
+      if (HostInDomain(hostName, domainName)) {
         return true;
       }
     } else {
       NS_WARNING("Failed to convert UTF-8 host to ASCII");
     }
 
     begin = end + 1;
   } while (end < domainWhiteList.Length());