fix: make clone URL respect public URL detection setting (#37615)
Fix #37614
This commit is contained in:
@@ -5,7 +5,6 @@ import {showErrorToast} from '../modules/toast.ts';
|
|||||||
import {sleep} from '../utils.ts';
|
import {sleep} from '../utils.ts';
|
||||||
import RepoActivityTopAuthors from '../components/RepoActivityTopAuthors.vue';
|
import RepoActivityTopAuthors from '../components/RepoActivityTopAuthors.vue';
|
||||||
import {createApp} from 'vue';
|
import {createApp} from 'vue';
|
||||||
import {toOriginUrl} from '../utils/url.ts';
|
|
||||||
import {createTippy} from '../modules/tippy.ts';
|
import {createTippy} from '../modules/tippy.ts';
|
||||||
import {localUserSettings} from '../modules/user-settings.ts';
|
import {localUserSettings} from '../modules/user-settings.ts';
|
||||||
|
|
||||||
@@ -79,7 +78,8 @@ function initCloneSchemeUrlSelection(parent: Element) {
|
|||||||
const isTea = scheme === 'tea';
|
const isTea = scheme === 'tea';
|
||||||
|
|
||||||
if (tabHttps) {
|
if (tabHttps) {
|
||||||
tabHttps.textContent = window.origin.split(':')[0].toUpperCase(); // show "HTTP" or "HTTPS"
|
const link = tabHttps.getAttribute('data-link')!;
|
||||||
|
tabHttps.textContent = link.split(':')[0].toUpperCase(); // show "HTTP" or "HTTPS"
|
||||||
tabHttps.classList.toggle('active', isHttps);
|
tabHttps.classList.toggle('active', isHttps);
|
||||||
}
|
}
|
||||||
if (tabSsh) {
|
if (tabSsh) {
|
||||||
@@ -99,7 +99,7 @@ function initCloneSchemeUrlSelection(parent: Element) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!tab) return;
|
if (!tab) return;
|
||||||
const link = toOriginUrl(tab.getAttribute('data-link')!);
|
const link = tab.getAttribute('data-link')!;
|
||||||
|
|
||||||
for (const el of document.querySelectorAll('.js-clone-url')) {
|
for (const el of document.querySelectorAll('.js-clone-url')) {
|
||||||
if (el.nodeName === 'INPUT') {
|
if (el.nodeName === 'INPUT') {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {linkifyURLs, pathEscape, pathEscapeSegments, toOriginUrl, urlQueryEscape} from './url.ts';
|
import {linkifyURLs, pathEscape, pathEscapeSegments, urlQueryEscape} from './url.ts';
|
||||||
|
|
||||||
describe('escape', () => {
|
describe('escape', () => {
|
||||||
const queryNonAscii = " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
|
const queryNonAscii = " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
|
||||||
@@ -45,19 +45,3 @@ test('linkifyURLs', () => {
|
|||||||
expect(linkifyURLs('https://evil.com/\nonclick=alert(1)')).toEqual(`${link('https://evil.com/')}\nonclick=alert(1)`);
|
expect(linkifyURLs('https://evil.com/\nonclick=alert(1)')).toEqual(`${link('https://evil.com/')}\nonclick=alert(1)`);
|
||||||
expect(linkifyURLs('https://evil.com/"onmouseover=alert(1)')).toEqual(`${link('https://evil.com/"onmouseover=alert')}(1)`);
|
expect(linkifyURLs('https://evil.com/"onmouseover=alert(1)')).toEqual(`${link('https://evil.com/"onmouseover=alert')}(1)`);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('toOriginUrl', () => {
|
|
||||||
const oldLocation = String(window.location);
|
|
||||||
for (const origin of ['https://example.com', 'https://example.com:3000']) {
|
|
||||||
window.location.assign(`${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.assign(oldLocation);
|
|
||||||
});
|
|
||||||
|
|||||||
@@ -57,19 +57,3 @@ export function linkifyURLs(html: string): string {
|
|||||||
return `<a href="${cleanUrl}" target="_blank">${cleanUrl}</a>${trailing}`; // eslint-disable-line github/unescaped-html-literal
|
return `<a href="${cleanUrl}" target="_blank">${cleanUrl}</a>${trailing}`; // eslint-disable-line github/unescaped-html-literal
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Convert an absolute or relative URL to an absolute URL with the current origin. It only
|
|
||||||
* processes absolute HTTP/HTTPS URLs or relative URLs like '/xxx' or '//host/xxx'. */
|
|
||||||
export function toOriginUrl(urlStr: string) {
|
|
||||||
try {
|
|
||||||
if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) {
|
|
||||||
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');
|
|
||||||
return url.toString();
|
|
||||||
}
|
|
||||||
} catch {}
|
|
||||||
return urlStr;
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user