'use strict'; /* * Theme settings backend (root/usr/libexec/rpcd/luci.carbon_update): custom * logo. The real shell functions run with a minimal jshn stand-in and * temporary paths. */ const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); const { execFileSync } = require('node:child_process'); const SCRIPT = fs.readFileSync(path.join(__dirname, '..', 'root/usr/libexec/rpcd/luci.carbon_update'), 'utf8'); const JSHN = path.join(__dirname, 'shell-stubs', 'jshn.sh'); function backend() { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'carbon-logo-')); const body = SCRIPT .replace('. /usr/share/libubox/jshn.sh', `. "${JSHN}"`) .replace(/\ncase "\$1" in\n\tlist\)[\s\S]*$/, '\n'); assert.notEqual(body, SCRIPT, 'dispatch block stripped'); const lib = path.join(dir, 'backend.sh'); fs.writeFileSync(lib, body + `\nlogo_dir="${dir}/www/custom"\nlogo_upload="${dir}/upload"\n`); const call = (action) => JSON.parse(execFileSync(process.env.CARBON_TEST_SH || 'sh', ['-c', `. "${lib}"; input='{"action":"${action}"}'; logo`], { encoding: 'utf8' })); const upload = (data) => fs.writeFileSync(path.join(dir, 'upload'), data); const files = () => fs.existsSync(path.join(dir, 'www/custom')) ? fs.readdirSync(path.join(dir, 'www/custom')).sort() : []; return { dir, call, upload, files, uploaded: () => fs.existsSync(path.join(dir, 'upload')) }; } const PNG = Buffer.concat([Buffer.from('89504e470d0a1a0a0000000d49484452', 'hex'), Buffer.alloc(64)]); const JPEG = Buffer.concat([Buffer.from('ffd8ffe000104a464946', 'hex'), Buffer.alloc(64)]); const WEBP = Buffer.concat([Buffer.from('RIFF'), Buffer.from('24000000', 'hex'), Buffer.from('WEBPVP8 '), Buffer.alloc(32)]); const SVG = '\n\n' + '' + 'Carbon one'; test('a PNG upload becomes the logo; a later JPEG replaces it', () => { const b = backend(); b.upload(PNG); const r = b.call('set'); assert.match(r.logo, /^\/luci-static\/carbon\/custom\/logo\.png(\?v=\d+)?$/); assert.deepEqual(b.files(), ['logo.png']); assert.equal(b.uploaded(), false, 'upload is consumed'); b.upload(JPEG); assert.match(b.call('set').logo, /logo\.jpg/); assert.deepEqual(b.files(), ['logo.jpg']); assert.equal(fs.statSync(path.join(b.dir, 'www/custom/logo.jpg')).mode & 0o777, 0o644); b.upload(WEBP); assert.match(b.call('set').logo, /logo\.webp/); }); test('the image type comes from the content, never the file name', () => { const b = backend(); b.upload('GIF89a' + '\0'.repeat(40)); assert.match(b.call('set').logo, /logo\.gif/); b.upload('not an image'); assert.deepEqual(b.call('set'), { error: 'logo_type' }); assert.deepEqual(b.files(), ['logo.gif'], 'a rejected upload keeps the current logo'); assert.equal(b.uploaded(), false); }); test('size limits: empty and over 256 KB are rejected', () => { const b = backend(); b.upload(''); assert.deepEqual(b.call('set'), { error: 'logo_size' }); b.upload(Buffer.concat([PNG, Buffer.alloc(262144)])); assert.deepEqual(b.call('set'), { error: 'logo_size' }); assert.deepEqual(b.call('set'), { error: 'logo_missing' }, 'nothing left to apply'); }); test('plain SVG logos are accepted, including DOCTYPE and raster data: images', () => { const b = backend(); b.upload(SVG); assert.match(b.call('set').logo, /logo\.svg/); b.upload(''); assert.match(b.call('set').logo, /logo\.svg/); }); test('SVG that could run script on the LuCI origin is rejected', () => { const unsafe = [ '', '', '\n', '