Bug 1192666 - Emit '[]' around origin strings for ipv6 origins. r=ehsan, a=sledru
authorMichael Layzell <michael@thelayzells.com>
Sun, 09 Aug 2015 18:47:20 -0400
changeset 288761 41bd412c39647a041ad1a9c4584ec63f4402419c
parent 288760 5ae2ab4052b8d27ed04bcc8a6764d77882ffbb99
child 288762 947b74a5bef4ee5ac6f4bf61f6dfbb69825b069c
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan, sledru
bugs1192666
milestone42.0a2
Bug 1192666 - Emit '[]' around origin strings for ipv6 origins. r=ehsan, a=sledru
caps/nsPrincipal.cpp
caps/tests/unit/test_origin.js
--- a/caps/nsPrincipal.cpp
+++ b/caps/nsPrincipal.cpp
@@ -106,29 +106,29 @@ nsPrincipal::GetOriginForURI(nsIURI* aUR
     return NS_ERROR_FAILURE;
   }
 
   nsCOMPtr<nsIURI> origin = NS_GetInnermostURI(aURI);
   if (!origin) {
     return NS_ERROR_FAILURE;
   }
 
-  nsAutoCString hostPort;
+  nsAutoCString host;
 
   // chrome: URLs don't have a meaningful origin, so make
   // sure we just get the full spec for them.
   // XXX this should be removed in favor of the solution in
   // bug 160042.
   bool isChrome;
   nsresult rv = origin->SchemeIs("chrome", &isChrome);
   if (NS_SUCCEEDED(rv) && !isChrome) {
-    rv = origin->GetAsciiHost(hostPort);
+    rv = origin->GetAsciiHost(host);
     // Some implementations return an empty string, treat it as no support
     // for asciiHost by that implementation.
-    if (hostPort.IsEmpty()) {
+    if (host.IsEmpty()) {
       rv = NS_ERROR_FAILURE;
     }
   }
 
   // We want the invariant that prinA.origin == prinB.origin i.f.f.
   // prinA.equals(prinB). However, this requires that we impose certain constraints
   // on the behavior and origin semantics of principals, and in particular, forbid
   // creating origin strings for principals whose equality constraints are not
@@ -154,16 +154,25 @@ nsPrincipal::GetOriginForURI(nsIURI* aUR
   }
 
   int32_t port;
   if (NS_SUCCEEDED(rv) && !isChrome) {
     rv = origin->GetPort(&port);
   }
 
   if (NS_SUCCEEDED(rv) && !isChrome) {
+    nsAutoCString hostPort;
+    if (host.FindChar(':') != -1) {
+      hostPort.Assign("[");
+      hostPort.Append(host);
+      hostPort.Append("]");
+    } else {
+      hostPort.Assign(host);
+    }
+
     if (port != -1) {
       hostPort.Append(':');
       hostPort.AppendInt(port, 10);
     }
 
     rv = origin->GetScheme(aOrigin);
     NS_ENSURE_SUCCESS(rv, rv);
     aOrigin.AppendLiteral("://");
--- a/caps/tests/unit/test_origin.js
+++ b/caps/tests/unit/test_origin.js
@@ -42,16 +42,22 @@ function run_test() {
   do_check_eq(exampleOrg.origin, 'http://example.org');
   checkOriginAttributes(exampleOrg);
   var exampleCom = ssm.createCodebasePrincipal(makeURI('https://www.example.com:123'), {});
   do_check_eq(exampleCom.origin, 'https://www.example.com:123');
   checkOriginAttributes(exampleCom);
   var nullPrin = Cu.getObjectPrincipal(new Cu.Sandbox(null));
   do_check_true(/^moz-nullprincipal:\{([0-9]|[a-z]|\-){36}\}$/.test(nullPrin.origin));
   checkOriginAttributes(nullPrin);
+  var ipv6Prin = ssm.createCodebasePrincipal(makeURI('https://[2001:db8::ff00:42:8329]:123'), {});
+  do_check_eq(ipv6Prin.origin, 'https://[2001:db8::ff00:42:8329]:123');
+  checkOriginAttributes(ipv6Prin);
+  var ipv6NPPrin = ssm.createCodebasePrincipal(makeURI('https://[2001:db8::ff00:42:8329]'), {});
+  do_check_eq(ipv6NPPrin.origin, 'https://[2001:db8::ff00:42:8329]');
+  checkOriginAttributes(ipv6NPPrin);
   var ep = ssm.createExpandedPrincipal([exampleCom, nullPrin, exampleOrg]);
   checkOriginAttributes(ep);
   checkCrossOrigin(exampleCom, exampleOrg);
   checkCrossOrigin(exampleOrg, nullPrin);
 
   // nsEP origins should be in lexical order.
   do_check_eq(ep.origin, `[Expanded Principal [${exampleOrg.origin}, ${exampleCom.origin}, ${nullPrin.origin}]]`);