Fix user-agent handling for faked UAs from connector
This commit is contained in:
parent
6960b7f86e
commit
4acd178819
|
@ -2606,29 +2606,51 @@ Zotero.VersionHeader = {
|
|||
|
||||
observe: function (subject, topic, data) {
|
||||
try {
|
||||
// Add "Firefox/[version]" to the user agent before "Zotero/[version]"
|
||||
let channel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
|
||||
let ua = channel.getRequestHeader('User-Agent');
|
||||
let info = Services.appinfo;
|
||||
let pos = ua.indexOf(info.name + '/');
|
||||
ua = ua.slice(0, pos) + `Firefox/${info.platformVersion.match(/^\d+/)[0]}.0 `
|
||||
+ ZOTERO_CONFIG.CLIENT_NAME + '/';
|
||||
// Send full Zotero version to zotero.org
|
||||
if (channel.URI.host.endsWith(ZOTERO_CONFIG.DOMAIN_NAME)) {
|
||||
ua += Zotero.version;
|
||||
let domain = channel.URI.host;
|
||||
if (domain.endsWith(ZOTERO_CONFIG.DOMAIN_NAME)) {
|
||||
channel.setRequestHeader("X-Zotero-Version", Zotero.version, false);
|
||||
}
|
||||
// Otherwise only send major.minor version
|
||||
else {
|
||||
ua += Zotero.version.replace(/(\d+\.\d+).*/, '$1');
|
||||
let ua = channel.getRequestHeader('User-Agent');
|
||||
ua = this.update(domain, ua);
|
||||
channel.setRequestHeader('User-Agent', ua, false);
|
||||
}
|
||||
channel.setRequestHeader('User-Agent', ua, false);
|
||||
}
|
||||
catch (e) {
|
||||
Zotero.debug(e, 1);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Add Firefox/[version] to the default user agent and replace Zotero/[version] with
|
||||
* Zotero/[major.minor] (except for requests to zotero.org, where we include the full version)
|
||||
*
|
||||
* @param {String} domain
|
||||
* @param {String} ua - User Agent
|
||||
* @param {String} [testAppName] - App name to look for (necessary in tests, which are
|
||||
* currently run in Firefox)
|
||||
*/
|
||||
update: function (domain, ua, testAppName) {
|
||||
var info = Services.appinfo;
|
||||
var appName = testAppName || info.name;
|
||||
|
||||
var pos = ua.indexOf(appName + '/');
|
||||
|
||||
// Default UA
|
||||
if (pos != -1) {
|
||||
ua = ua.slice(0, pos) + `Firefox/${info.platformVersion.match(/^\d+/)[0]}.0 `
|
||||
+ appName + '/';
|
||||
}
|
||||
// Fake UA from connector
|
||||
else {
|
||||
ua += ' ' + appName + '/';
|
||||
}
|
||||
ua += Zotero.version.replace(/(\d+\.\d+).*/, '$1');
|
||||
|
||||
return ua;
|
||||
},
|
||||
|
||||
unregister: function () {
|
||||
Services.obs.removeObserver(Zotero.VersionHeader, "http-on-modify-request");
|
||||
}
|
||||
|
|
|
@ -27,4 +27,24 @@ describe("Zotero", function() {
|
|||
assert.equal(Zotero.localeCompare("_Abcd", "Abcd"), -1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("VersionHeader", function () {
|
||||
describe("#update()", function () {
|
||||
var majorMinorVersion;
|
||||
|
||||
before(function () {
|
||||
majorMinorVersion = Zotero.version.replace(/(\d+\.\d+).*/, '$1');
|
||||
});
|
||||
|
||||
it("should add Zotero/[major.minor] to Chrome user agent", function () {
|
||||
var ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36';
|
||||
assert.equal(Zotero.VersionHeader.update('example.com', ua, ZOTERO_CONFIG.CLIENT_NAME), ua + ` Zotero/${majorMinorVersion}`);
|
||||
});
|
||||
|
||||
it("should add Zotero/[major.minor] to Firefox user agent", function () {
|
||||
var ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:60.0) Gecko/20100101 Firefox/60.0';
|
||||
assert.equal(Zotero.VersionHeader.update('example.com', ua, ZOTERO_CONFIG.CLIENT_NAME), ua + ` Zotero/${majorMinorVersion}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user