import fs from 'fs-extra' import os from 'node:os' import path from 'node:path' import { afterEach, describe, expect, it } from 'vitest' import { createExtensionPaths } from '../../src/main/extensions/extension-paths' import { buildExtensionPrivateMediaPreviewUrl, resolveExtensionProtocolFile } from '../../src/main/extensions/extension-protocol' const tempDirs: string[] = [] function makeTempDir(): string { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'tm-ext-protocol-')) tempDirs.push(dir) return dir } afterEach(() => { while (tempDirs.length > 0) { const dir = tempDirs.pop() if (dir) fs.removeSync(dir) } }) describe('extension-protocol 受控私有媒体预览', () => { it('仅放行本扩展 rendered/ 与 media-cache/ 下的白名单媒体文件,拒绝越界与敏感文件', async () => { const root = makeTempDir() const paths = createExtensionPaths(root) const extId = 'com.example.preview' const otherId = 'com.example.other' const extInstallDir = paths.installDir(extId) const extDataDir = paths.extensionDataDir(extId) const otherDataDir = paths.extensionDataDir(otherId) fs.ensureDirSync(path.join(extInstallDir, 'ui')) fs.writeFileSync(path.join(extInstallDir, 'ui', 'index.html'), '') fs.ensureDirSync(path.join(extDataDir, 'rendered')) fs.ensureDirSync(path.join(extDataDir, 'media-cache', 'acct1234', 'images')) fs.ensureDirSync(path.join(otherDataDir, 'rendered')) const pngPath = path.join(extDataDir, 'rendered', 'digest.png') const cachedJpgPath = path.join(extDataDir, 'media-cache', 'acct1234', 'images', 'a.jpg') const storagePath = path.join(extDataDir, 'storage.json') const htmlInRendered = path.join(extDataDir, 'rendered', 'evil.html') const outsideSecret = path.join(root, 'secret.png') const symlinkInRendered = path.join(extDataDir, 'rendered', 'link.png') const otherPngPath = path.join(otherDataDir, 'rendered', 'other.png') fs.writeFileSync(pngPath, Buffer.from([0x89, 0x50, 0x4e, 0x47, 1, 2, 3, 4])) fs.writeFileSync(cachedJpgPath, Buffer.from([0xff, 0xd8, 0xff, 0xe0])) fs.writeFileSync(storagePath, '{"token":"secret"}') fs.writeFileSync(htmlInRendered, '') fs.writeFileSync(outsideSecret, Buffer.from([0x89, 0x50, 0x4e, 0x47, 9, 9])) fs.writeFileSync(otherPngPath, Buffer.from([0x89, 0x50, 0x4e, 0x47, 7, 7])) fs.symlinkSync(outsideSecret, symlinkInRendered) const deps = { paths, resolveRoot: (id: string) => (id === extId ? extInstallDir : null) } const previewUrl = buildExtensionPrivateMediaPreviewUrl(extId, extDataDir, pngPath) expect(previewUrl).toBe(`tm-extension://${extId}/_data/rendered/digest.png`) const resolvedPng = await resolveExtensionProtocolFile(previewUrl!, deps) expect(resolvedPng).not.toBeNull() expect(resolvedPng?.contentType).toBe('image/png') expect(resolvedPng?.isPrivateMedia).toBe(true) expect(resolvedPng?.csp).toContain("default-src 'none'") const cachedPreviewUrl = buildExtensionPrivateMediaPreviewUrl(extId, extDataDir, cachedJpgPath) expect(cachedPreviewUrl).toBe(`tm-extension://${extId}/_data/media-cache/acct1234/images/a.jpg`) const resolvedJpg = await resolveExtensionProtocolFile(cachedPreviewUrl!, deps) expect(resolvedJpg?.contentType).toBe('image/jpeg') // 拒绝 storage.json 与非白名单后缀(如 html)。 expect(buildExtensionPrivateMediaPreviewUrl(extId, extDataDir, storagePath)).toBeUndefined() expect( await resolveExtensionProtocolFile(`tm-extension://${extId}/_data/storage.json`, deps) ).toBeNull() expect( await resolveExtensionProtocolFile(`tm-extension://${extId}/_data/rendered/evil.html`, deps) ).toBeNull() // 拒绝路径穿越、符号链接逃逸和未安装扩展访问。 expect( await resolveExtensionProtocolFile( `tm-extension://${extId}/_data/rendered/../../secret.png`, deps ) ).toBeNull() expect( await resolveExtensionProtocolFile(`tm-extension://${extId}/_data/rendered/link.png`, deps) ).toBeNull() expect( await resolveExtensionProtocolFile(`tm-extension://${otherId}/_data/rendered/other.png`, deps) ).toBeNull() }) })