Files
wekan/tests/browserRootUrl.test.cjs
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

76 lines
3.8 KiB
JavaScript

'use strict';
// #6752: with the snap's default ROOT_URL (http://127.0.0.1), Copy link gave
// http://127.0.0.1/... even when the browser opened WeKan by its real name.
// models/lib/browserRootUrl.js decides when the browser's own address is used
// instead; client/00-startup.js applies it to Meteor.absoluteUrl, which every
// copied link goes through.
// Run: node --test tests/browserRootUrl.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { test } = require('node:test');
const { browserRootUrl, isLoopbackHost } = require('../models/lib/browserRootUrl');
const read = rel => fs.readFileSync(path.join(__dirname, '..', rel), 'utf8');
test('the reported case: a loopback ROOT_URL, a browser on a real name', () => {
assert.strictEqual(browserRootUrl('http://127.0.0.1', 'http://wekan.example.lan/b/abc/board'), 'http://wekan.example.lan');
assert.strictEqual(browserRootUrl('http://127.0.0.1:3001/', 'https://wekan.example.com:8443/b/x'), 'https://wekan.example.com:8443');
assert.strictEqual(browserRootUrl('http://localhost', 'http://192.168.1.20:3001/'), 'http://192.168.1.20:3001');
assert.strictEqual(browserRootUrl('http://[::1]:3000', 'http://kanban/'), 'http://kanban');
});
test('a sub-URL install keeps its path', () => {
assert.strictEqual(browserRootUrl('http://127.0.0.1/wekan', 'http://wekan.example.lan/wekan/b/abc'), 'http://wekan.example.lan/wekan');
assert.strictEqual(browserRootUrl('http://127.0.0.1/wekan/', 'http://host/wekan/'), 'http://host/wekan');
});
test('negative: a real ROOT_URL always wins, even from another address', () => {
assert.strictEqual(browserRootUrl('https://wekan.example.com', 'http://10.0.0.5:3000/'), null);
assert.strictEqual(browserRootUrl('https://wekan.example.com/wekan', 'http://localhost:3000/'), null);
});
test('negative: a browser on loopback itself keeps ROOT_URL', () => {
assert.strictEqual(browserRootUrl('http://127.0.0.1', 'http://localhost:3000/'), null);
assert.strictEqual(browserRootUrl('http://localhost', 'http://127.0.0.1/'), null);
});
test('negative: junk and non-web pages change nothing', () => {
assert.strictEqual(browserRootUrl('', 'http://host/'), null);
assert.strictEqual(browserRootUrl('http://127.0.0.1', 'not a url'), null);
assert.strictEqual(browserRootUrl('http://127.0.0.1', 'file:///index.html'), null);
});
test('loopback detection', () => {
for (const h of ['localhost', 'LOCALHOST', 'app.localhost', '127.0.0.1', '127.1.2.3', '::1', '[::1]', '0.0.0.0']) {
assert.ok(isLoopbackHost(h), h);
}
for (const h of ['127.example.com', 'wekan.example.com', '192.168.1.1', 'localhost.example.com', '']) {
assert.ok(!isLoopbackHost(h), h);
}
});
test('the client applies it to Meteor.absoluteUrl at startup, before anything builds a link', () => {
const startup = read('client/00-startup.js');
const at = startup.indexOf("require('/models/lib/browserRootUrl')");
assert.ok(at !== -1, 'client/00-startup.js loads the rule');
assert.match(startup, /browserRootUrl\(Meteor\.absoluteUrl\.defaultOptions\.rootUrl, window\.location\.href\)/);
assert.match(startup, /if \(rootUrl\) Meteor\.absoluteUrl\.defaultOptions\.rootUrl = rootUrl;/);
assert.ok(at < startup.indexOf('installAccountsCookiePaths('), 'before the first absoluteUrl use');
});
test('negative: the server never rewrites ROOT_URL from a request', () => {
const offenders = [];
const walk = dir => {
for (const e of fs.readdirSync(path.join(__dirname, '..', dir), { withFileTypes: true })) {
const rel = `${dir}/${e.name}`;
if (e.isDirectory()) walk(rel);
else if (/\.js$/.test(e.name) && /browserRootUrl/.test(read(rel))) offenders.push(rel);
}
};
walk('server');
assert.deepStrictEqual(offenders, [], 'emails keep the configured ROOT_URL');
});