Fix handling of redirecting OpenURL base URLs with url query parameter

For a URL like
`https://ezproxy.school.edu/login?url=http://resolver.ebscohost.com/openurl`,
without a trailing `?`, we were just adding a `%` to the end, making the
URL invalid after the redirection (`/openurl&url_ver=Z39.88-2004`
instead of `/openurl?url_ver=Z39.88-2004`).

(And apparently no one who sent in these URLs actually tested them
in-app?)
This commit is contained in:
Dan Stillman 2024-12-20 02:43:14 -05:00
parent 244d182293
commit 20e28d3f9e
2 changed files with 50 additions and 3 deletions

View file

@ -3039,9 +3039,18 @@ Zotero.Utilities.Internal.OpenURL = {
);
if (co) {
let base = Zotero.Prefs.get("openURL.resolver");
// Add & if there's already a ?
let splice = base.indexOf("?") == -1 ? "?" : "&";
return base + splice + co;
if (!base.endsWith('?')) {
if (base.includes('login?url=')) {
base += '?';
}
else if (base.includes('?')) {
base += '&';
}
else {
base += '?';
}
}
return base + co;
}
return false;
},

View file

@ -738,4 +738,42 @@ describe("Zotero.Utilities.Internal", function () {
assert.equal(html, 'yes1yes2 yes3');
});
});
describe("OpenURL", function () {
var item;
before(async function () {
item = await createDataObject('item', { title: 'Foo Bar', date: '2024-12-19' });
})
after(function () {
Zotero.Prefs.clear('openURL.resolver');
});
describe("#resolve()", function () {
it("should add trailing '?' if no query string", async function () {
Zotero.Prefs.set("openURL.resolver", "https://resolver.ebsco.com/c/abcdef/result");
var url = Zotero.Utilities.Internal.OpenURL.resolve(item);
assert.include(url, 'result?url_ver=Z39.88-2004');
});
it("should add trailing '&' if already a query string", async function () {
Zotero.Prefs.set("openURL.resolver", "https://resolver.ebscohost.com/openurl?custid=abcdef&groupid=main&profile=ftf&authtype=ip,uid");
var url = Zotero.Utilities.Internal.OpenURL.resolve(item);
assert.include(url, 'authtype=ip,uid&url_ver=Z39.88-2004');
});
it("should add trailing '?' after /login?url=", async function () {
Zotero.Prefs.set("openURL.resolver", "https://proxy.school.edu/login?url=https://resolver.ebscohost.com/openurl");
var url = Zotero.Utilities.Internal.OpenURL.resolve(item);
assert.include(url, 'openurl?url_ver=Z39.88-2004');
});
it("shouldn't add trailing '?' after /login?url= if URL already ends in '?'", async function () {
Zotero.Prefs.set("openURL.resolver", "https://proxy.school.edu/login?url=https://resolver.ebscohost.com/openurl?");
var url = Zotero.Utilities.Internal.OpenURL.resolve(item);
assert.include(url, 'openurl?url_ver=Z39.88-2004');
});
});
});
});