Skip to content

Text to vertical video in 100+ styles: AI codes each scene (HTML/SVG/WebGL), renders a 9:16 MP4 with music and English/Vietnamese voice. MCP, CLI and REST for AI agents.

4.6 KB
Raw Blame
// T2 (THIET-KE gói mẫu §3.3): 12 gói độc mẫu đều bị từ chối, không file nào được ghi vào HOME/packs.
//   node packs/test/t2-bad-packs.mjs
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { check, info, done } from './helpers.mjs';

const HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'lumreels-t2-'));
process.env.LUMREELS_HOME = HOME;
const { exportPack, readPack, installPack, contentHash, MAX_ITEMS } = await import('../pack.mjs');
const { packsDir } = await import('../registry.mjs');

// gói gốc hợp lệ: 1 mẫu dựng + 1 cách dựng (lấy từ repo), id không dành riêng
const base = JSON.parse(exportPack({ id: 'thu.goi-tot', name: 'Gói tốt', author: 'Người thử', items: ['template:009', 'style:thong-tin-do-hoa'] }).buffer.toString('utf8'));
const tpl = () => base.items.find((x) => x.kind === 'template');
const sty = () => base.items.find((x) => x.kind === 'edit-style');
/** Sửa bản sao của gói tốt, tính lại contentHash (để lỗi đúng là do luật đang thử, không phải do hash). */
function variant(mut) {
  const p = JSON.parse(JSON.stringify(base));
  mut(p);
  p.contentHash = contentHash(p.items.filter((x) => typeof x.text === 'string' && typeof x.slug === 'string'));
  return Buffer.from(JSON.stringify(p));
}
const withText = (p, kind, f) => { const it = p.items.find((x) => x.kind === kind); it.text = f(it.text); };
const svg = Buffer.from('<svg xmlns="http://www.w3.org/2000/svg" onload="alert(1)"><script>alert(1)</script></svg>').toString('base64');

const cases = [
  ['slug "../x"', variant((p) => { p.items[0].slug = '../x'; }), /slug/],
  ['slug "con" (tên thiết bị Windows)', variant((p) => { p.items[0].slug = 'con'; }), /slug/],
  ['tác giả "local"', variant((p) => { p.id = 'local.thu'; }), /local/],
  ['tác giả "lumreels" không chữ ký', variant((p) => { p.id = 'lumreels.gia-mao'; }), /chữ ký/],
  ['text có <script>', variant((p) => withText(p, 'template', (t) => t.replace('## 11. Đừng làm', '<script>alert(1)</script>\n\n## 11. Đừng làm'))), /thẻ HTML/],
  ['text có URL', variant((p) => withText(p, 'template', (t) => t.replace('## 11. Đừng làm', 'Xem thêm https://example.com/x\n\n## 11. Đừng làm'))), /URL/],
  ['text có U+202E', variant((p) => withText(p, 'template', (t) => t.replace('## 11. Đừng làm', 'abc‮def\n\n## 11. Đừng làm'))), /ẩn\/đảo chiều/],
  ['preset "accent":"red;} body{display:none"', variant((p) => withText(p, 'edit-style', (t) => t.replace(/"accent":\s*"#[0-9A-Fa-f]{6}"/, '"accent": "red;} body{display:none"'))), /accent: màu/],
  ['preset có __proto__', variant((p) => withText(p, 'edit-style', (t) => t.replace('"version": 1,', '"version": 1, "__proto__": { "polluted": true },'))), /khoá cấm/],
  ['ảnh là SVG đổi đuôi', variant((p) => { p.items[0].thumb = svg; }), /WebP/],
  ['file 13 MB', Buffer.concat([Buffer.from(JSON.stringify(base).slice(0, -1) + ',"pad":"'), Buffer.alloc(13 * 1024 * 1024, 97), Buffer.from('"}')]), /MB/],
  ['201 mục', variant((p) => { const t = p.items[0]; p.items = Array.from({ length: MAX_ITEMS + 1 }, (_, i) => ({ ...t, slug: `mau-${String(i).padStart(3, '0')}` })); }), /tối đa 200/],
];

console.log(`== T2: ${cases.length} gói độc (LUMREELS_HOME=${HOME})`);
const okBase = readPack(Buffer.from(JSON.stringify(base)));
check(okBase.report.errors.length === 0, 'gói tốt (mốc so) qua kiểm', okBase.report.errors[0] || '');
const before = fs.existsSync(packsDir()) ? fs.readdirSync(packsDir()) : [];
for (const [name, buf, re] of cases) {
  // đi cả đường file trên đĩa (kiểm cỡ trước khi đọc) lẫn buffer
  const f = path.join(HOME, 'case.lumpack');
  fs.writeFileSync(f, buf);
  const r = readPack(f);
  let threw = null;
  try { installPack(r); } catch (e) { threw = e.message; }
  const hit = r.report.errors.find((e) => re.test(e));
  check(r.report.errors.length > 0 && !!threw && !!hit, `${name}: bị từ chối`, (hit || r.report.errors[0] || 'KHÔNG CÓ LỖI').slice(0, 120));
  fs.rmSync(f, { force: true });
}
const after = fs.existsSync(packsDir()) ? fs.readdirSync(packsDir()) : [];
check(after.length === before.length && after.length === 0, `không có gì được ghi vào ${packsDir()}`, `${after.length} mục: ${after.join(', ')}`);
info('gói tốt cài được (mốc so cho installPack):');
const m = installPack(okBase);
check(fs.existsSync(path.join(packsDir(), m.id, 'templates', 'mau-009.md')), `cài gói tốt: có ${m.id}/templates/mau-009.md`);
fs.rmSync(HOME, { recursive: true, force: true });
done('T2');