From 280f2fa31e1f27549cbc1cd8f44c14aa38145c66 Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 7 Feb 2024 20:33:54 +0100 Subject: [PATCH 1/3] Fix gitea-origin-url with default ports When setting `url.host` on a URL object with no port specified (like is the case of default port), the resulting URL's port will not change. Workaround this quirk in the URL standard by explicitely setting port for the http and https protocols. Extracted the logic to a function for the purpose of testing. Initially I wanted to have the function in utils.js, but it turns out esbuild can not treeshake the unused functions which would result in the webcomponents chunk having all 2kB utils.js inlined, so it seemed not worth. Fixes: https://github.com/go-gitea/gitea/issues/29084 --- .eslintrc.yaml | 2 +- web_src/js/webcomponents/GiteaOriginUrl.js | 28 +++++++++++-------- .../js/webcomponents/GiteaOriginUrl.test.js | 17 +++++++++++ 3 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 web_src/js/webcomponents/GiteaOriginUrl.test.js diff --git a/.eslintrc.yaml b/.eslintrc.yaml index 78042a7598101..fc6f38ec531d4 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -811,7 +811,7 @@ rules: wc/no-constructor-params: [2] wc/no-constructor: [2] wc/no-customized-built-in-elements: [2] - wc/no-exports-with-element: [2] + wc/no-exports-with-element: [0] wc/no-invalid-element-name: [2] wc/no-invalid-extends: [2] wc/no-method-prefixed-with-on: [2] diff --git a/web_src/js/webcomponents/GiteaOriginUrl.js b/web_src/js/webcomponents/GiteaOriginUrl.js index fca736064c0c9..edc8be85f7607 100644 --- a/web_src/js/webcomponents/GiteaOriginUrl.js +++ b/web_src/js/webcomponents/GiteaOriginUrl.js @@ -1,17 +1,21 @@ // Convert an absolute or relative URL to an absolute URL with the current origin +export function toOriginUrl(urlStr) { + try { + // only process absolute HTTP/HTTPS URL or relative URLs ('/xxx' or '//host/xxx') + if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) { + const url = new URL(urlStr, window.origin); + const {protocol, hostname, port} = window.location; + url.protocol = protocol; + url.hostname = hostname; + url.port = port || (protocol === 'https:' ? '443' : '80'); + return url.toString(); + } + } catch {} + return urlStr; +} + window.customElements.define('gitea-origin-url', class extends HTMLElement { connectedCallback() { - const urlStr = this.getAttribute('data-url'); - try { - // only process absolute HTTP/HTTPS URL or relative URLs ('/xxx' or '//host/xxx') - if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) { - const url = new URL(urlStr, window.origin); - url.protocol = window.location.protocol; - url.host = window.location.host; - this.textContent = url.toString(); - return; - } - } catch {} - this.textContent = urlStr; + this.textContent = toOriginUrl(this.getAttribute('data-url')); } }); diff --git a/web_src/js/webcomponents/GiteaOriginUrl.test.js b/web_src/js/webcomponents/GiteaOriginUrl.test.js new file mode 100644 index 0000000000000..f0629842b85a4 --- /dev/null +++ b/web_src/js/webcomponents/GiteaOriginUrl.test.js @@ -0,0 +1,17 @@ +import {toOriginUrl} from './GiteaOriginUrl.js'; + +test('toOriginUrl', () => { + const oldLocation = window.location; + for (const origin of ['https://example.com', 'https://example.com:3000']) { + window.location = new URL(`${origin}/`); + expect(toOriginUrl('/')).toEqual(`${origin}/`); + expect(toOriginUrl('/org/repo.git')).toEqual(`${origin}/org/repo.git`); + expect(toOriginUrl('https://another.com')).toEqual(`${origin}/`); + expect(toOriginUrl('https://another.com/')).toEqual(`${origin}/`); + expect(toOriginUrl('https://another.com/org/repo.git')).toEqual(`${origin}/org/repo.git`); + expect(toOriginUrl('https://another.com:4000')).toEqual(`${origin}/`); + expect(toOriginUrl('https://another.com:4000/')).toEqual(`${origin}/`); + expect(toOriginUrl('https://another.com:4000/org/repo.git')).toEqual(`${origin}/org/repo.git`); + } + window.location = oldLocation; +}); From 77d0e8b0a6251909a1e5834ade1996912687bba7 Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 7 Feb 2024 21:27:45 +0100 Subject: [PATCH 2/3] use window.location.origin - see https://stackoverflow.com/questions/55451493 --- web_src/js/webcomponents/GiteaOriginUrl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/webcomponents/GiteaOriginUrl.js b/web_src/js/webcomponents/GiteaOriginUrl.js index edc8be85f7607..31752f00f909e 100644 --- a/web_src/js/webcomponents/GiteaOriginUrl.js +++ b/web_src/js/webcomponents/GiteaOriginUrl.js @@ -3,7 +3,7 @@ export function toOriginUrl(urlStr) { try { // only process absolute HTTP/HTTPS URL or relative URLs ('/xxx' or '//host/xxx') if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) { - const url = new URL(urlStr, window.origin); + const url = new URL(urlStr, window.location.origin); const {protocol, hostname, port} = window.location; url.protocol = protocol; url.hostname = hostname; From 00b8b304fa764f266efb6548fc4706c3879cd0da Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 7 Feb 2024 21:29:40 +0100 Subject: [PATCH 3/3] use destructuring --- web_src/js/webcomponents/GiteaOriginUrl.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web_src/js/webcomponents/GiteaOriginUrl.js b/web_src/js/webcomponents/GiteaOriginUrl.js index 31752f00f909e..5d71d95c604c1 100644 --- a/web_src/js/webcomponents/GiteaOriginUrl.js +++ b/web_src/js/webcomponents/GiteaOriginUrl.js @@ -3,8 +3,8 @@ export function toOriginUrl(urlStr) { try { // only process absolute HTTP/HTTPS URL or relative URLs ('/xxx' or '//host/xxx') if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) { - const url = new URL(urlStr, window.location.origin); - const {protocol, hostname, port} = window.location; + const {origin, protocol, hostname, port} = window.location; + const url = new URL(urlStr, origin); url.protocol = protocol; url.hostname = hostname; url.port = port || (protocol === 'https:' ? '443' : '80');