Users can add any website as a dashboard quick link from Settings → Site Links. The link is stored in the integrations namespace as a `custom` type so it flows through the existing sites/integrations/settings pipeline, and appears in a new "Links" group. Its icon is discovered with the existing favicon pipeline; an uploaded icon overrides it and stays pinned until the user chooses "Use site's icon" again. resolveIcons and refreshIcon skip pinned sites (and re-check the live list before writing back) so an in-flight discovery can never clobber a freshly uploaded icon. URLs are normalized to http(s) only, with embedded credentials stripped, so a value can never reach an anchor's href as javascript:/data:. Uploads are validated, size-capped and downscaled to 192px (SVGs kept as-is), and only ever rendered through an <img>. Adds app/utils/site-url.js and app/utils/image-file.js, a custom-link form, a `custom` registry entry/group and adapter, and tests.
57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
// A value with a bare host and port (`example.com:8080`) parses as a URL with
|
|
// an odd scheme, so recognize it explicitly before deciding it is a scheme.
|
|
const HOST_PORT = /^[^/\s]+:\d+(?:[/?#]|$)/;
|
|
|
|
function parseUrl(value) {
|
|
try {
|
|
return new URL(value);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function isHttpUrl(url) {
|
|
return url.protocol === 'http:' || url.protocol === 'https:';
|
|
}
|
|
|
|
// Custom site links are typed by hand, so normalize them to an absolute
|
|
// http(s) URL before anything is stored or rendered. Only http(s) is allowed:
|
|
// a value like `javascript:` or `data:` must never reach an anchor's href.
|
|
export function normalizeSiteUrl(input) {
|
|
const value = (input ?? '').trim();
|
|
|
|
if (!value || /\s/.test(value)) {
|
|
return null;
|
|
}
|
|
|
|
let url = parseUrl(value);
|
|
|
|
if (!url || !isHttpUrl(url)) {
|
|
// A value with no scheme (`example.com`) or a bare host:port is assumed to
|
|
// be https. Any other explicit scheme (ftp:, javascript:, data:, …) is
|
|
// rejected by the checks below.
|
|
if (!url || HOST_PORT.test(value)) {
|
|
url = parseUrl(`https://${value}`);
|
|
}
|
|
}
|
|
|
|
if (!url || !isHttpUrl(url) || !url.hostname) {
|
|
return null;
|
|
}
|
|
|
|
// Never persist embedded credentials into a clickable link.
|
|
url.username = '';
|
|
url.password = '';
|
|
|
|
return url.href;
|
|
}
|
|
|
|
// The default label for a site, derived from its hostname.
|
|
export function labelForUrl(url) {
|
|
try {
|
|
return new URL(url).hostname;
|
|
} catch {
|
|
return url;
|
|
}
|
|
}
|