31 lines
1.9 KiB
JavaScript
31 lines
1.9 KiB
JavaScript
const fs = require('node:fs');
|
|
const vm = require('node:vm');
|
|
const assert = require('node:assert/strict');
|
|
const source = fs.readFileSync('vps/owrt-remote-hub.py', 'utf8');
|
|
const start = source.indexOf('function renderTachyon(router) {{');
|
|
const end = source.indexOf('\nfunction render(list)', start);
|
|
const code = source.slice(start, end).replaceAll('{{', '{').replaceAll('}}', '}');
|
|
const escape = value => String(value).replace(/[&<>"']/g, ch => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[ch]));
|
|
const context = {escapeHtml: escape, escapeAttr: escape};
|
|
vm.createContext(context); vm.runInContext(code, context);
|
|
assert.equal(context.renderTachyon({}), '');
|
|
assert.equal(context.renderTachyon({tachyon: {installed: false}}), '');
|
|
const router = {permissions: {admin: true}, tachyon: {installed: true, version: '2.0.0~beta.8-1', online: true, url: '/tachyon/a/'}};
|
|
let html = context.renderTachyon(router);
|
|
assert(html.includes('Открыть Tachyon'));
|
|
assert(html.includes('target="_blank"'));
|
|
assert(html.includes('Online'));
|
|
assert(html.includes('href="/tachyon/a/"'));
|
|
assert(html.includes('tachyonMeta'));
|
|
assert(html.includes('tachyonAction'));
|
|
const withoutVersion = context.renderTachyon({permissions:{admin:true}, tachyon:{installed:true, online:true, url:'/tachyon/a/'}});
|
|
assert.match(withoutVersion.replace(/<[^>]*>/g, ''), /Tachyon\s*Online/);
|
|
router.tachyon.online = false;
|
|
assert(context.renderTachyon(router).includes('Offline'));
|
|
router.permissions.admin = false;
|
|
assert(!context.renderTachyon(router).includes('<a '));
|
|
router.permissions.admin = true; router.tachyon.version = '<img onerror=alert(1)>';
|
|
assert(!context.renderTachyon(router).includes('<img '));
|
|
assert.equal(source.split(String.fromCharCode(36) + '{{renderTachyon(r)}}').length - 1, 2);
|
|
console.log('TACHYON_UI_OK: absent, installed, offline, access controls, escaping and both layouts');
|