Skip to content

Commit

Permalink
Bug 1273587 - Change the User-Agent header sent to Sync to include OS…
Browse files Browse the repository at this point in the history
… information. r=kitcambridge

The custom UA string sent by Sync now includes the "oscpu" portion of the
default UA string. Eg, with this patch my UA string looks like
"Firefox/49.0a1 (Windows NT 6.1; WOW64) FxSync/1.51.0.20160524173017.desktop"
where the value in parenthesis is added by this patch.

MozReview-Commit-ID: 1gfqQoVbO6V
  • Loading branch information
mhammond committed May 30, 2016
1 parent 2018c03 commit 7b0d14a
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 34 deletions.
17 changes: 1 addition & 16 deletions services/sync/modules/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,6 @@ AsyncResource.prototype = {
*/
authenticator: null,

// The string to use as the base User-Agent in Sync requests.
// These strings will look something like
//
// Firefox/4.0 FxSync/1.8.0.20100101.mobile
//
// or
//
// Firefox Aurora/5.0a1 FxSync/1.9.0.20110409.desktop
//
_userAgent:
Services.appinfo.name + "/" + Services.appinfo.version + // Product.
" FxSync/" + WEAVE_VERSION + "." + // Sync.
Services.appinfo.appBuildID + ".", // Build.

// Wait 5 minutes before killing a request.
ABORT_TIMEOUT: 300000,

Expand Down Expand Up @@ -161,8 +147,7 @@ AsyncResource.prototype = {

// Compose a UA string fragment from the various available identifiers.
if (Svc.Prefs.get("sendVersionInfo", true)) {
let ua = this._userAgent + Svc.Prefs.get("client.type", "desktop");
channel.setRequestHeader("user-agent", ua, false);
channel.setRequestHeader("user-agent", Utils.userAgent, false);
}

let headers = this.headers;
Expand Down
18 changes: 1 addition & 17 deletions services/sync/modules/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,6 @@ SyncStorageRequest.prototype = {

_logName: "Sync.StorageRequest",

/**
* The string to use as the base User-Agent in Sync requests.
* These strings will look something like
*
* Firefox/4.0 FxSync/1.8.0.20100101.mobile
*
* or
*
* Firefox Aurora/5.0a1 FxSync/1.9.0.20110409.desktop
*/
userAgent:
Services.appinfo.name + "/" + Services.appinfo.version + // Product.
" FxSync/" + WEAVE_VERSION + "." + // Sync.
Services.appinfo.appBuildID + ".", // Build.

/**
* Wait 5 minutes before killing a request.
*/
Expand All @@ -50,8 +35,7 @@ SyncStorageRequest.prototype = {
dispatch: function dispatch(method, data, onComplete, onProgress) {
// Compose a UA string fragment from the various available identifiers.
if (Svc.Prefs.get("sendVersionInfo", true)) {
let ua = this.userAgent + Svc.Prefs.get("client.type", "desktop");
this.setHeader("user-agent", ua);
this.setHeader("user-agent", Utils.userAgent);
}

if (this.authenticator) {
Expand Down
2 changes: 1 addition & 1 deletion services/sync/modules/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ Sync11Service.prototype = {

sync: function sync(engineNamesToSync) {
let dateStr = new Date().toLocaleFormat(LOG_DATE_FORMAT);
this._log.debug("User-Agent: " + SyncStorageRequest.prototype.userAgent);
this._log.debug("User-Agent: " + Utils.userAgent);
this._log.info("Starting sync at " + dateStr);
this._catch(function () {
// Make sure we're logged in.
Expand Down
19 changes: 19 additions & 0 deletions services/sync/modules/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ this.Utils = {
deriveKeyFromPassphrase: CryptoUtils.deriveKeyFromPassphrase,
getHTTPMACSHA1Header: CryptoUtils.getHTTPMACSHA1Header,

/**
* The string to use as the base User-Agent in Sync requests.
* This string will look something like
*
* Firefox/49.0a1 (Windows NT 6.1; WOW64; rv:46.0) FxSync/1.51.0.20160516142357.desktop
*/
_userAgent: null,
get userAgent() {
if (!this._userAgent) {
let hph = Cc["@mozilla.org/network/protocol;1?name=http"].getService(Ci.nsIHttpProtocolHandler);
this._userAgent =
Services.appinfo.name + "/" + Services.appinfo.version + // Product.
" (" + hph.oscpu + ")" + // (oscpu)
" FxSync/" + WEAVE_VERSION + "." + // Sync.
Services.appinfo.appBuildID + "."; // Build.
}
return this._userAgent + Svc.Prefs.get("client.type", "desktop");
},

/**
* Wrap a function to catch all exceptions and log them
*
Expand Down
6 changes: 6 additions & 0 deletions services/sync/tests/unit/test_resource_ua.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Cu.import("resource://services-sync/service.js");
Cu.import("resource://services-sync/util.js");
Cu.import("resource://testing-common/services/sync/utils.js");

var httpProtocolHandler = Cc["@mozilla.org/network/protocol;1?name=http"]
.getService(Ci.nsIHttpProtocolHandler);

// Tracking info/collections.
var collectionsHelper = track_collections_helper();
var collections = collectionsHelper.collections;
Expand Down Expand Up @@ -37,7 +40,10 @@ function run_test() {
Service.clusterURL = server.baseURI + "/";
_("Server URL: " + server.baseURI);

// Note this string is missing the trailing ".destkop" as the test
// adjusts the "client.type" pref where that portion comes from.
expectedUA = Services.appinfo.name + "/" + Services.appinfo.version +
" (" + httpProtocolHandler.oscpu + ")" +
" FxSync/" + WEAVE_VERSION + "." +
Services.appinfo.appBuildID;

Expand Down
4 changes: 4 additions & 0 deletions services/sync/tests/unit/test_service_getStorageInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Cu.import("resource://services-sync/service.js");
Cu.import("resource://services-sync/util.js");
Cu.import("resource://testing-common/services/sync/utils.js");

var httpProtocolHandler = Cc["@mozilla.org/network/protocol;1?name=http"]
.getService(Ci.nsIHttpProtocolHandler);

var collections = {steam: 65.11328,
petrol: 82.488281,
diesel: 2.25488281};
Expand Down Expand Up @@ -37,6 +40,7 @@ add_test(function test_success() {
Service.identity.username,
Service.identity.basicPassword));
let expectedUA = Services.appinfo.name + "/" + Services.appinfo.version +
" (" + httpProtocolHandler.oscpu + ")" +
" FxSync/" + WEAVE_VERSION + "." +
Services.appinfo.appBuildID + ".desktop";
do_check_eq(handler.request.getHeader("User-Agent"), expectedUA);
Expand Down
5 changes: 5 additions & 0 deletions services/sync/tests/unit/test_syncstoragerequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Cu.import("resource://services-sync/service.js");
Cu.import("resource://services-sync/util.js");
Cu.import("resource://testing-common/services/sync/utils.js");

var httpProtocolHandler = Cc["@mozilla.org/network/protocol;1?name=http"]
.getService(Ci.nsIHttpProtocolHandler);

function run_test() {
Log.repository.getLogger("Sync.RESTRequest").level = Log.Level.Trace;
initTestLogging();
Expand All @@ -22,6 +25,7 @@ add_test(function test_user_agent_desktop() {
let server = httpd_setup({"/resource": handler});

let expectedUA = Services.appinfo.name + "/" + Services.appinfo.version +
" (" + httpProtocolHandler.oscpu + ")" +
" FxSync/" + WEAVE_VERSION + "." +
Services.appinfo.appBuildID + ".desktop";

Expand All @@ -41,6 +45,7 @@ add_test(function test_user_agent_mobile() {

Svc.Prefs.set("client.type", "mobile");
let expectedUA = Services.appinfo.name + "/" + Services.appinfo.version +
" (" + httpProtocolHandler.oscpu + ")" +
" FxSync/" + WEAVE_VERSION + "." +
Services.appinfo.appBuildID + ".mobile";

Expand Down

0 comments on commit 7b0d14a

Please sign in to comment.