Files
Netcatty/electron/plugins/viewResourceService.test.cjs
T
陈大猫 f4a087e890 feat(plugins): add host-rendered IDE primitives, STARTTLS/SSH bindings, file/secret APIs, and pooled image rasterizer (#4437)
- Add host-rendered native IDE primitives (PluginCodeEditor/Monaco, PluginDataGrid/@tanstack/react-virtual, PluginSplitPane, PluginErCanvas) with Shadow DOM slot projection (zero iframes, zero plugin JS in renderer)
- Expand Remote DOM budgets, expose full 22 Netcatty semantic CSS tokens, add dblclick/contextmenu/scroll/selection events, cursor-anchored PluginPopover, database theme icon, and full-bleed chrome flag
- Add dynamic user-approved TCP/TLS byte streams (incl. localhost opt-in blocking internal control ports), in-band STARTTLS upgrade with cert fingerprint confirmation, durable revocable Vault SSH host bindings, plugin-owned encrypted secrets, native ui.openFile/saveFile streaming handles, 4 MiB clipboard, and prefix-filtered storage.keys
- Optimize plugin view image pipeline with pooled sandboxed rasterizer window (auto-destroyed on idle/error/dispose), LRU version+hash cache in PluginViewResources, transient failure eviction in HostDrawnViewHost, renderer-side session deduplication, and RemoteImageElement cache + retry
- Temporarily enable plugin host by default in hostBootstrap.cjs (marked TODO(revert), opt-out via NETCATTY_PLUGIN_DEV=0)
2026-11-26 17:52:27 +08:00

52 lines
3.2 KiB
JavaScript

'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs/promises');
const path = require('node:path');
const os = require('node:os');
const { PluginViewResources } = require('./viewResourceService.cjs');
test('view images reject traversal, symlink escapes, external SVGs and oversize data before decoding', async (t) => {
const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'netcatty-view-image-'));
t.after(() => fs.rm(directory, { recursive: true, force: true }));
const root = path.join(directory, 'package'); await fs.mkdir(root);
const resources = new PluginViewResources({ electron: {}, packageStore: { preparePackageRoot: async () => root } });
let decoded = 0; resources.rasterize = async () => { decoded++; throw new Error('Unexpected decoding'); };
for (const name of ['../secret', '/secret', 'https://example.invalid/image.png', 'a/../b.png', 'a%2fsecret', 'a\\b.png']) await assert.rejects(resources.image({}, name), /path/);
await fs.writeFile(path.join(directory, 'secret.png'), 'private'); await fs.symlink(path.join(directory, 'secret.png'), path.join(root, 'escape.png'));
await assert.rejects(resources.image({}, 'escape.png'), /symlink/);
await fs.writeFile(path.join(root, 'huge.png'), Buffer.alloc(512 * 1024 + 1));
await assert.rejects(resources.image({}, 'huge.png'), /size/);
for (const source of ['<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><image href="https://example.invalid/secret"/></svg>', '<svg xmlns="http://www.w3.org/2000/svg" width="999999" height="10"/>', '<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><script>attack()</script></svg>']) {
await fs.writeFile(path.join(root, 'bad.svg'), source); await assert.rejects(resources.image({}, 'bad.svg'));
}
assert.equal(decoded, 0);
});
test('view images cache decoded results by plugin version/hash and never cache transient failures', async (t) => {
const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'netcatty-view-image-cache-'));
t.after(() => fs.rm(directory, { recursive: true, force: true }));
const root = path.join(directory, 'package'); await fs.mkdir(root);
// Minimal valid 1x1 PNG header/bytes
const pngHeader = Buffer.from([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89,
]);
await fs.writeFile(path.join(root, 'icon.png'), pngHeader);
const resources = new PluginViewResources({ electron: {}, packageStore: { preparePackageRoot: async () => root } });
let attempts = 0;
resources.rasterize = async () => {
attempts += 1;
if (attempts === 1) throw new Error('transient timeout');
return pngHeader;
};
const plugin = { id: 'com.test.db', activeVersion: '0.2.0', archiveSha256: 'abc123' };
await assert.rejects(resources.image(plugin, 'icon.png'), /transient timeout/);
const first = await resources.image(plugin, 'icon.png');
const second = await resources.image(plugin, 'icon.png');
assert.equal(first.url, second.url);
assert.equal(attempts, 2);
resources.dispose();
});