Files
wekan/models/lib/browserRootUrl.js
T
Lauri Ojansivu 0f2e81e01b Copy links use the address the browser reached WeKan by when ROOT_URL is loopback.
The snap's default ROOT_URL is 127.0.0.1, and every link built in the
browser - Copy link of a card, list or swimlane, comment permalinks -
goes through Meteor.absoluteUrl(), which reads ROOT_URL, so a browser
that opened WeKan by the machine's name copied a 127.0.0.1 link nobody
else could follow. When ROOT_URL is loopback and the page is not, the
client now builds absolute links from the page's own origin, keeping
ROOT_URL's path. A real ROOT_URL still wins, and the server keeps
ROOT_URL for emails: `snap set wekan root-url=...` remains the setting
for those.

Thanks to n8willis and xet7 !

Fixes #6752.
2026-10-08 14:04:48 +03:00

44 lines
1.9 KiB
JavaScript

'use strict';
// #6752: the snap's default ROOT_URL is the loopback address 127.0.0.1, and
// every link WeKan builds in the browser - Copy link of a card, list or
// swimlane, a comment permalink - goes through Meteor.absoluteUrl(), which
// reads ROOT_URL. So a browser that opened WeKan by the machine's name copied
// a link to 127.0.0.1, which no other machine can follow.
//
// A loopback ROOT_URL seen from a browser on a NON-loopback address can only
// mean ROOT_URL was left at that default: the browser demonstrably reached the
// server some other way. Then the browser's own origin is the address that
// works, and the client's absoluteUrl uses it, keeping ROOT_URL's path (a
// sub-URL install). Anything else is left alone:
// - a real ROOT_URL is the administrator's canonical address and wins, even
// when this browser came in through another name or an IP;
// - a browser on loopback itself is the case ROOT_URL already describes.
// The server keeps ROOT_URL as it is - emails have no browser to ask - so
// `snap set wekan root-url=...` is still what fixes the links in emails.
function isLoopbackHost(hostname) {
const host = String(hostname || '').toLowerCase().replace(/^\[|\]$/g, '');
return host === 'localhost' || host.endsWith('.localhost') || host === '::1'
|| host === '0.0.0.0' || /^127(?:\.\d{1,3}){3}$/.test(host);
}
// The root URL the browser should build absolute links from, or null to keep
// Meteor's default (ROOT_URL).
function browserRootUrl(rootUrl, browserLocation) {
let root;
let page;
try {
root = new URL(rootUrl);
page = new URL(browserLocation);
} catch (e) {
return null;
}
if (!/^https?:$/.test(page.protocol)) return null;
if (!isLoopbackHost(root.hostname) || isLoopbackHost(page.hostname)) return null;
const rootPath = root.pathname.replace(/\/+$/, '');
return `${page.origin}${rootPath}`;
}
module.exports = { isLoopbackHost, browserRootUrl };