Bug 1188932 - Allow the User-Agent header to be explicitly set by requests, r=bkelly, r=jgraham
authorMichael Layzell <michael@thelayzells.com>
Fri, 28 Aug 2015 23:39:54 -0400
changeset 262176 86fd2ab7d9f62ea6a7cb85ac6f49e440d66ffe68
parent 262175 2cbbc360ec41d01937e41c5f264fea40032535e1
child 262177 10343dcbe03d370fd57a261c6833c9928956bb33
push id29362
push userphilringnalda@gmail.com
push dateSat, 12 Sep 2015 22:44:56 +0000
treeherdermozilla-central@0bdadeda2489 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly, jgraham
bugs1188932
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1188932 - Allow the User-Agent header to be explicitly set by requests, r=bkelly, r=jgraham
dom/base/nsContentUtils.cpp
dom/base/test/file_explicit_user_agent.sjs
dom/base/test/mochitest.ini
dom/base/test/test_explicit_user_agent.html
dom/base/test/test_xhr_forbidden_headers.html
dom/security/test/cors/test_CrossSiteXHR.html
dom/tests/mochitest/fetch/test_fetch_cors.js
dom/tests/mochitest/fetch/test_headers_mainthread.html
testing/web-platform/meta/XMLHttpRequest/preserve-ua-header-on-redirect.htm.ini
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -7183,17 +7183,17 @@ nsContentUtils::IsForbiddenRequestHeader
 bool
 nsContentUtils::IsForbiddenSystemRequestHeader(const nsACString& aHeader)
 {
   static const char *kInvalidHeaders[] = {
     "accept-charset", "accept-encoding", "access-control-request-headers",
     "access-control-request-method", "connection", "content-length",
     "cookie", "cookie2", "content-transfer-encoding", "date", "dnt",
     "expect", "host", "keep-alive", "origin", "referer", "te", "trailer",
-    "transfer-encoding", "upgrade", "user-agent", "via"
+    "transfer-encoding", "upgrade", "via"
   };
   for (uint32_t i = 0; i < ArrayLength(kInvalidHeaders); ++i) {
     if (aHeader.LowerCaseEqualsASCII(kInvalidHeaders[i])) {
       return true;
     }
   }
   return false;
 }
new file mode 100644
--- /dev/null
+++ b/dom/base/test/file_explicit_user_agent.sjs
@@ -0,0 +1,8 @@
+function handleRequest(request, response)
+{
+  if (request.hasHeader("User-Agent")) {
+    response.setHeader("Result-User-Agent",
+                       request.getHeader("User-Agent"));
+  }
+  response.write("");
+}
--- a/dom/base/test/mochitest.ini
+++ b/dom/base/test/mochitest.ini
@@ -248,16 +248,17 @@ support-files =
   noaudio.webm
   referrer_helper.js
   referrer_testserver.sjs
   script_postmessages_fileList.js
   iframe_postMessages.html
   test_performance_observer.js
   performance_observer.html
   test_anonymousContent_style_csp.html^headers^
+  file_explicit_user_agent.sjs
 
 [test_anonymousContent_api.html]
 [test_anonymousContent_append_after_reflow.html]
 [test_anonymousContent_insert.html]
 [test_anonymousContent_manipulate_content.html]
 [test_anonymousContent_style_csp.html]
 [test_applet_alternate_content.html]
 [test_appname_override.html]
@@ -838,8 +839,9 @@ skip-if = buildapp == 'mulet' || buildap
 [test_nonascii_blob_url.html]
 [test_window_element_enumeration.html]
 [test_referrer_redirect.html]
 [test_postMessages.html]
 support-files = worker_postMessages.js
 [test_window_proto.html]
 [test_frameLoader_switchProcess.html]
 skip-if = e10s || os != 'linux' || buildapp != 'browser'
+[test_explicit_user_agent.html]
new file mode 100644
--- /dev/null
+++ b/dom/base/test/test_explicit_user_agent.html
@@ -0,0 +1,65 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Test for XMLHttpRequest.GetResponseHeader(foo) byte-inflates the output</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+  <meta charset="utf-8">
+</head>
+<body>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="application/javascript;version=1.7">
+  "use strict";
+
+  add_task(function*() {
+    yield new Promise((r) => {
+      let xhr = new XMLHttpRequest();
+      xhr.open('GET', 'file_explicit_user_agent.sjs', true);
+      xhr.onreadystatechange = function() {
+        if (xhr.readyState == 4) {
+          is(xhr.getResponseHeader("Result-User-Agent"), navigator.userAgent,
+             "The resulting user-agent is the navigator's UA");
+          r();
+        }
+      }
+      xhr.send(null);
+    });
+
+    yield new Promise((r) => {
+      let xhr = new XMLHttpRequest();
+      xhr.open('GET', 'file_explicit_user_agent.sjs', true);
+      xhr.setRequestHeader('User-Agent', 'custom-ua/10.0');
+      xhr.onreadystatechange = function() {
+        if (xhr.readyState == 4) {
+          is(xhr.getResponseHeader("Result-User-Agent"), 'custom-ua/10.0',
+             "The resulting user-agent is the custom UA");
+          r();
+        }
+      }
+      xhr.send(null);
+    });
+
+    var response = yield fetch('file_explicit_user_agent.sjs', {
+      method: 'GET'
+    });
+    is(response.headers.get("Result-User-Agent"), navigator.userAgent,
+       "The user-agent is the navigator's UA");
+
+    var headers = new Headers();
+    headers.set('User-Agent', 'custom-ua/20.0');
+    var response2 = yield fetch('file_explicit_user_agent.sjs', {
+      method: 'GET',
+      headers: headers,
+    });
+    is(response2.headers.get("Result-User-Agent"), 'custom-ua/20.0',
+       "The user-agent is the custom UA");
+  });
+</script>
+</pre>
+</body>
+</html>
--- a/dom/base/test/test_xhr_forbidden_headers.html
+++ b/dom/base/test/test_xhr_forbidden_headers.html
@@ -35,17 +35,16 @@ var headers = [
   "hOSt",
   "keep-alive",
   "oRiGiN",
   "reFERer",
   "te",
   "trAiLer",
   "trANsfEr-eNcoDiNg",
   "uPGraDe",
-  "user-AGENT",
   "viA",
   "pRoxy-",
   "sEc-",
   "proxy-fOobar",
   "sec-bAZbOx"
 ];
 var i, request;
 
--- a/dom/security/test/cors/test_CrossSiteXHR.html
+++ b/dom/security/test/cors/test_CrossSiteXHR.html
@@ -160,16 +160,25 @@ function runTest() {
                  headers: { "x-my-header": "myValue" },
                  allowHeaders: "x-my-header, y-my-he(ader",
                },
                { pass: 0,
                  method: "GET",
                  headers: { "myheader": "" },
                  allowMethods: "myheader",
                },
+               { pass: 1,
+                 method: "GET",
+                 headers: { "User-Agent": "myValue" },
+                 allowHeaders: "User-Agent",
+               },
+               { pass: 0,
+                 method: "GET",
+                 headers: { "User-Agent": "myValue" },
+               },
 
                // Multiple custom headers
                { pass: 1,
                  method: "GET",
                  headers: { "x-my-header": "myValue",
                             "second-header": "secondValue",
                             "third-header": "thirdValue" },
                  allowHeaders: "x-my-header, second-header, third-header",
--- a/dom/tests/mochitest/fetch/test_fetch_cors.js
+++ b/dom/tests/mochitest/fetch/test_fetch_cors.js
@@ -259,16 +259,25 @@ function testModeCors() {
                  headers: { "x-my-header": "myValue" },
                  allowHeaders: "x-my-header, y-my-he(ader",
                },
                { pass: 0,
                  method: "GET",
                  headers: { "myheader": "" },
                  allowMethods: "myheader",
                },
+               { pass: 1,
+                 method: "GET",
+                 headers: { "User-Agent": "myValue" },
+                 allowHeaders: "User-Agent",
+               },
+               { pass: 0,
+                 method: "GET",
+                 headers: { "User-Agent": "myValue" },
+               },
 
                // Multiple custom headers
                { pass: 1,
                  method: "GET",
                  headers: { "x-my-header": "myValue",
                             "second-header": "secondValue",
                             "third-header": "thirdValue" },
                  allowHeaders: "x-my-header, second-header, third-header",
--- a/dom/tests/mochitest/fetch/test_headers_mainthread.html
+++ b/dom/tests/mochitest/fetch/test_headers_mainthread.html
@@ -34,17 +34,16 @@ function TestRequestHeaders() {
     "Host",
     "Keep-Alive",
     "Origin",
     "Referer",
     "TE",
     "Trailer",
     "Transfer-Encoding",
     "Upgrade",
-    "User-Agent",
     "Via",
     "Proxy-Authorization",
     "Proxy-blarg",
     "Proxy-",
     "Sec-foo",
     "Sec-"
   ];
 
deleted file mode 100644
--- a/testing/web-platform/meta/XMLHttpRequest/preserve-ua-header-on-redirect.htm.ini
+++ /dev/null
@@ -1,5 +0,0 @@
-[preserve-ua-header-on-redirect.htm]
-  type: testharness
-  [XMLHttpRequest: User-Agent header is preserved on redirect 1]
-    expected: FAIL
-