FIX: Do not strip // from the middle of URLs in discourse-url (#28210)

Protocol/domain should only be stripped when they occur at the beginning of a URL
This commit is contained in:
David Taylor 2024-08-02 16:23:54 +01:00 committed by GitHub
parent 624dc87321
commit f70a65ea02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 1 deletions

View File

@ -214,7 +214,7 @@ class DiscourseURL extends EmberObject {
return this.redirectTo(path); return this.redirectTo(path);
} }
const pathname = path.replace(/(https?\:)?\/\/[^\/]+/, ""); const pathname = path.replace(/^(https?\:)?\/\/[^\/]+/, "");
if (!this.isInternal(path)) { if (!this.isInternal(path)) {
return this.redirectTo(path); return this.redirectTo(path);

View File

@ -114,6 +114,40 @@ module("Unit | Utility | url", function (hooks) {
); );
}); });
test("routeTo protocol/domain stripping", async function (assert) {
sinon.stub(DiscourseURL, "origin").get(() => "http://example.com");
sinon.stub(DiscourseURL, "handleURL");
sinon.stub(DiscourseURL, "router").get(() => {
return {
currentURL: "/bar",
};
});
DiscourseURL.routeTo("http://example.com/foo1");
assert.ok(
DiscourseURL.handleURL.calledWith(`/foo1`),
"it strips the protocol and domain when http"
);
DiscourseURL.routeTo("https://example.com/foo2");
assert.ok(
DiscourseURL.handleURL.calledWith(`/foo2`),
"it strips the protocol and domain when https"
);
DiscourseURL.routeTo("//example.com/foo3");
assert.ok(
DiscourseURL.handleURL.calledWith(`/foo3`),
"it strips the protocol and domain when protocol-less"
);
DiscourseURL.routeTo("https://example.com//foo4");
assert.ok(
DiscourseURL.handleURL.calledWith(`//foo4`),
"it does not strip double-slash in the middle of urls"
);
});
test("routeTo does not rewrite routes started with /my", async function (assert) { test("routeTo does not rewrite routes started with /my", async function (assert) {
logIn(); logIn();
sinon.stub(DiscourseURL, "router").get(() => { sinon.stub(DiscourseURL, "router").get(() => {