Files
wekan/tests/jsWebLabels.test.cjs
T
Lauri Ojansivu 587efab82b Fix GitHub CodeQL alerts #584 and #585.
#584 (bad HTML filtering regexp): tests/accountEmailHtml.test.cjs checked
that the e-mail layout escapes markup with /<img|<script|<b>/, which a
<SCRIPT> passes. It now lists every tag in the output, in any letter case,
and allows only the layout's own div, p, a and br; an upper- and mixed-case
input is tried as well. The renderer was already escaping them.

#585 (incomplete regular expression for hostnames): the same test made the
reset address into a RegExp; it compares the text instead.

Both are pinned as shapes in tests/securityAlerts20261012.test.cjs, which
found more of them: a case-sensitive <script> pattern in jsWebLabels.test.cjs,
a path spliced into generated code in singleFile.test.cjs (now passed through
the child's environment), and twelve test patterns with a host name's dot
unescaped. No regex literal in the tree leaves one unescaped now.

These are in tests, so there is no attempt at runtime to record in Admin
Panel -> Problems.

Thanks to GitHub CodeQL and xet7 !
2026-10-14 16:43:00 +03:00

55 lines
3.3 KiB
JavaScript

'use strict';
// #1040: LibreJS license information for the browser bundle - a JS Web Labels
// page at /jslicense.html, linked from every page with rel="jslicense".
// Run: node tests/jsWebLabels.test.cjs
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { LICENSES, bundleScripts, renderJsWebLabels, jsLicenseLink } = require('../models/lib/jsWebLabels.js');
const ROOT = path.join(__dirname, '..');
const read = f => fs.readFileSync(path.join(ROOT, f), 'utf8');
const manifest = [
{ type: 'js', where: 'client', url: '/abc123.js?meteor_js_resource=true' },
{ type: 'css', where: 'client', url: '/def.css?meteor_css_resource=true' },
{ type: 'js', where: 'internal', url: '/internal.js' },
{ type: 'asset', url: '/x.png' },
];
// Positive.
assert.deepEqual(bundleScripts(manifest), ['/abc123.js?meteor_js_resource=true'], 'scripts only, never css, assets or internal');
assert.deepEqual(bundleScripts(manifest, '/wekan/'), ['/wekan/abc123.js?meteor_js_resource=true'], 'under ROOT_URL\'s path');
const page = renderJsWebLabels({ scripts: bundleScripts(manifest), version: '12.43' });
assert.match(page, /<table id="jslicense-labels1">/, 'the id LibreJS looks for');
assert.match(page, /<tr><td><a href="\/abc123\.js\?meteor_js_resource=true">abc123\.js<\/a><\/td><td>/);
assert.match(page, /<a href="http:\/\/www\.jclark\.com\/xml\/copying\.txt">Expat<\/a>/, 'MIT under LibreJS\'s own name for it');
assert.match(page, /<td><a href="https:\/\/github\.com\/wekan\/wekan\/tree\/v12\.43">/, 'the source at the running version');
assert.equal(jsLicenseLink('/wekan/'), '<link rel="jslicense" href="/wekan/jslicense.html">');
assert.equal(jsLicenseLink(), '<link rel="jslicense" href="/jslicense.html">');
// Every license the bundle's packages use is a free one LibreJS knows.
assert.deepEqual(LICENSES.map(l => l.name), ['Expat', 'Apache-2.0', 'Modified BSD', 'FreeBSD', 'ISC']);
console.log(' ok - a labels table of the served scripts, linked from the page');
// Negative.
assert.doesNotMatch(renderJsWebLabels({ scripts: ['/a.js?x="><script>alert(1)</script>'] }), /<script\b/i, 'escaped: no script tag, in any letter case');
assert.match(renderJsWebLabels({ scripts: [] }), /<table id="jslicense-labels1">\n\n<\/table>/, 'no scripts, no rows');
assert.deepEqual(bundleScripts(null), []);
assert.match(renderJsWebLabels({ scripts: ['/a.js'] }), /href="https:\/\/github\.com\/wekan\/wekan"/, 'no version: the repository');
console.log(' ok - escaped, and empty or unknown input yields no rows');
// Wiring.
const server = read('server/jsWebLabels.js');
assert.match(server, /WebApp\.handlers\.get\('\/jslicense\.html'/);
assert.match(server, /\['web\.browser', 'web\.browser\.legacy'\]/);
assert.match(read('server/imports.js'), /import '\/server\/jsWebLabels';/);
assert.match(read('server/lib/customHeadRender.js'), /if \(!\/rel=\["'\]\?jslicense\/i\.test\(data\.head\)\) \{[\s\S]{0,300}jsLicenseLink\(/,
'every page links it, before the custom-head early return');
const head = read('server/lib/customHeadRender.js');
assert.ok(head.indexOf('jsLicenseLink(') < head.indexOf('if (!setting || !setting.customHeadEnabled)'), 'also when custom head is off');
console.log(' ok - served, imported and linked from every page');
console.log('\njsWebLabels: all tests passed');