A platform for community discussion. Free, open, simple.
FIX: Plus encoding (+) was not handled correctly in onebox and UI (#36822)
Sam62f797c3de8a
diff --git a/frontend/discourse/app/components/modal/upsert-hyperlink.gjs b/frontend/discourse/app/components/modal/upsert-hyperlink.gjsindex 5f534f5f1d2..4b84d2d7a80 100644--- a/frontend/discourse/app/components/modal/upsert-hyperlink.gjs+++ b/frontend/discourse/app/components/modal/upsert-hyperlink.gjs@@ -149,18 +149,22 @@ export default class UpsertHyperlink extends Component {if (typeof linkUrl === "string") {try {- linkUrl = encodeURI(decodeURI(linkUrl));- } catch {- try {- let withPercentEncoded = linkUrl.replace(- /%(?![0-9A-Fa-f]{2})/g,- "%25"- );- linkUrl = encodeURI(decodeURI(withPercentEncoded));- } catch {- // give up and do something- linkUrl = encodeURI(linkUrl);+ // Ensure any loose % characters are encoded before passing to URL constructor+ // this is technically an invalid URL, but err ont the relaxed side+ const safeLinkUrl = linkUrl.replace(/%(?![0-9A-Fa-f]{2})/g, "%25");+ const parsed = new URL(safeLinkUrl, window.location.origin);+ linkUrl = parsed.toString();++ // new URL("https://example.com").toString() -> https://example.com/+ if (+ linkUrl.endsWith("/") &&+ !safeLinkUrl.endsWith("/") &&+ parsed.pathname === "/"+ ) {+ linkUrl = linkUrl.slice(0, -1);}+ } catch {+ linkUrl = encodeURI(linkUrl);}}diff --git a/frontend/discourse/tests/acceptance/composer-hyperlink-test.js b/frontend/discourse/tests/acceptance/composer-hyperlink-test.jsindex 79e58a9bb0b..30ad674a694 100644--- a/frontend/discourse/tests/acceptance/composer-hyperlink-test.js+++ b/frontend/discourse/tests/acceptance/composer-hyperlink-test.js@@ -1,4 +1,10 @@-import { click, fillIn, triggerKeyEvent, visit } from "@ember/test-helpers";+import {+ click,+ fillIn,+ triggerKeyEvent,+ visit,+ waitFor,+} from "@ember/test-helpers";import { test } from "qunit";import { acceptance } from "discourse/tests/helpers/qunit-helpers";@@ -103,4 +109,24 @@ acceptance(`Composer - Hyperlink`, function (needs) {"focus stays on composer after dismissing modal using Esc key");});++ test("handles percent-encoded reserved characters without double-encoding", async function (assert) {+ const testUrl = "https://example.com/?q=%3D+test";++ await visit("/t/internationalization-localization/280");+ await click(".topic-post[data-post-number='1'] button.reply");++ await waitFor(".d-editor-input:not([disabled])");+ await fillIn(".d-editor-input", "");+ await click(".d-editor-button-bar .link");+ await waitFor(".upsert-hyperlink-modal");++ await fillIn(".link-url", testUrl);+ await fillIn(".link-text", "Link");+ await click(".d-modal__footer button.btn-primary");++ assert+ .dom(".d-editor-input")+ .hasValue(`[Link](${testUrl})`, "should preserve the encoded '=' (%3D)");+ });});diff --git a/lib/onebox/helpers.rb b/lib/onebox/helpers.rbindex 7d23d776473..7c57c0411e8 100644--- a/lib/onebox/helpers.rb+++ b/lib/onebox/helpers.rb@@ -251,7 +251,7 @@ module Oneboxquery:Addressable::URI.encode_component(uri.query,- "a-zA-Z0-9\\-\\.\\_\\~\\$\\&\\*\\,\\=\\:\\@\\?\\%",+ "a-zA-Z0-9\\-\\.\\_\\~\\$\\&\\*\\,\\=\\:\\@\\?\\%\\+",),fragment:Addressable::URI.encode_component(diff --git a/spec/lib/onebox/helpers_spec.rb b/spec/lib/onebox/helpers_spec.rbindex cdb5c0b3190..762071abb5f 100644--- a/spec/lib/onebox/helpers_spec.rb+++ b/spec/lib/onebox/helpers_spec.rb@@ -350,8 +350,8 @@ RSpec.describe Onebox::Helpers do)endit do- expect(described_class.uri_encode("http://example.com/<pa'th>(foo)?b+a+r")).to eq(- "http://example.com/%3Cpa'th%3E(foo)?b%2Ba%2Br",+ expect(described_class.uri_encode("http://example.com/<+pa'th>(foo)?b+a+r")).to eq(+ "http://example.com/%3C+pa'th%3E(foo)?b+a+r",)endit do@@ -408,5 +408,10 @@ RSpec.describe Onebox::Helpers dodescribed_class.uri_encode("https://gitpod.io/#https://github.com/eclipse-theia/theia"),).to eq("https://gitpod.io/#https://github.com/eclipse-theia/theia")end++ it "does not encode '+' in query parameters because it is a valid way to represent a space" do+ url = "https://example.com/search?q=ruby+on+rails"+ expect(described_class.uri_encode(url)).to eq(url)+ endendenddiff --git a/spec/system/composer/prosemirror_links_spec.rb b/spec/system/composer/prosemirror_links_spec.rbindex f379b4eea7a..1f46c5fdcb1 100644--- a/spec/system/composer/prosemirror_links_spec.rb+++ b/spec/system/composer/prosemirror_links_spec.rb@@ -46,7 +46,7 @@ describe "Composer - ProseMirror - Links", type: :system doupsert_hyperlink_modal.fill_in_link_url("https://updated-example.com?query=with space")upsert_hyperlink_modal.click_primary_buttonexpect(rich).to have_css(- "a[href='https://updated-example.com?query=with%20space']",+ "a[href='https://updated-example.com/?query=with%20space']",text: "Example",)end