Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(browser): 媒体转换仅使用宿主批准的字节

Dash9a93d74597f7
diff --git a/packages/lizi-mcps/src/browser/opencli-recipes.test.ts b/packages/lizi-mcps/src/browser/opencli-recipes.test.ts
index 9646a9daa8..dd0e7cfb73 100644
--- a/packages/lizi-mcps/src/browser/opencli-recipes.test.ts
+++ b/packages/lizi-mcps/src/browser/opencli-recipes.test.ts
@@ -29,12 +29,20 @@ import { readUtf8File } from '../../../opencli-vendor/clis/_atlassian/shared.js'
import { resolvePayload } from '../../../opencli-vendor/clis/zhihu/write-shared.js';
// @ts-expect-error Pinned upstream JavaScript helpers have no declarations.
import { prepareChatGPTFilePaths } from '../../../opencli-vendor/clis/chatgpt/utils.js';
-import { readFileSync, openSync, fstatSync } from 'node:fs';
+import { readFileSync, writeFileSync, openSync, fstatSync } from 'node:fs';
+import * as childProcess from 'node:child_process';
import { AsyncResource } from 'node:async_hooks';
// @ts-expect-error Upstream JavaScript helpers have no declarations.
import { validateLocalReferences } from '../../../opencli-vendor/clis/midjourney/utils.js';
// @ts-expect-error Pinned upstream JavaScript uploader has no declarations.
import { tosUpload } from '../../../opencli-vendor/clis/douyin/_shared/tos-upload.js';
+// @ts-expect-error Pinned upstream JavaScript media helpers have no declarations.
+import * as instagramMedia from '../../../opencli-vendor/clis/instagram/_shared/private-publish.js';
+
+// Wrap the native namespace so tests can replace only the subprocess boundary.
+vi.mock('node:child_process', async importOriginal => ({
+ ...await importOriginal<typeof import('node:child_process')>(),
+}));
afterEach(() => {
vi.useRealTimers();
@@ -43,6 +51,63 @@ afterEach(() => {
});
describe("bundled OpenCLI recipes", () => {
+ // #6826 file boundary: actual media preparation / story entry, unit tier.
+ // Prior upload tests never exercised converters reopening a checked path.
+ // Replace the original immediately after broker approval; subprocess fakes
+ // must still see approved bytes. Native macOS capability stays platform-owned.
+ it.each(['feed', 'story', 'video', 'video-story', 'conversion-failure'])('isolates %s conversion from source replacement and cleans staged bytes', async kind => {
+ const dir = await mkdtemp(path.join(tmpdir(), 'cindy-recipe-convert-test-'));
+ const video = kind.startsWith('video');
+ const file = path.join(dir, video ? 'clip.mp4' : 'image.png');
+ const png = Buffer.alloc(24);
+ Buffer.from('89504e470d0a1a0a', 'hex').copy(png);
+ png.write('IHDR', 12);
+ png.writeUInt32BE(4, 16);
+ png.writeUInt32BE(40, 20);
+ const approved = video ? Buffer.from('approved-video') : Buffer.concat([png, Buffer.from('approved')]);
+ const replaced = video ? Buffer.from('unapproved-video') : Buffer.concat([png, Buffer.from('unapproved')]);
+ await writeFile(file, approved);
+ const inputs: string[] = [];
+ const spawn = vi.spyOn(childProcess, 'spawnSync').mockImplementation(((command: string, args: string[]) => {
+ const input = args[command === 'sips' ? 5 : 1];
+ inputs.push(input);
+ expect(input).not.toBe(file);
+ const bytes = readFileSync(input);
+ expect([approved.toString('hex'), Buffer.from('trimmed-video').toString('hex')]).toContain(bytes.toString('hex'));
+ if (kind === 'conversion-failure') return { status: 1, stderr: 'fixture conversion failed', stdout: '' };
+ if (command === 'sips') writeFileSync(args[7], approved);
+ else if (args.length === 2) return { status: 0, stdout: JSON.stringify({ width: 100, height: 100,
+ durationMs: bytes.equals(approved) ? 20000 : 10000 }), stderr: '' };
+ else writeFileSync(args[2], args.length === 4 ? Buffer.from('trimmed-video')
+ : Buffer.from('ffd8ffc0000b08000100010101110000', 'hex'));
+ return { status: 0, stdout: '{}', stderr: '' };
+ }) as typeof childProcess.spawnSync);
+ const run = () => kind === 'video-story' ? instagramMedia.publishStoryViaPrivateApi({
+ page: {}, mediaItem: { type: 'video', filePath: file }, currentUserId: 'fixture-user', apiContext: {},
+ fetcher: async () => new Response(JSON.stringify({ status: 'ok', media: { pk: 'fixture-pk' } })),
+ }) : Promise.resolve(video ? instagramMedia.readVideoAsset(file)
+ : kind === 'story' ? instagramMedia.prepareImageAssetForPrivateStoryUpload(file)
+ : instagramMedia.prepareImageAssetForPrivateUpload(file));
+ try {
+ const result = withRecipeHost(undefined, run, {
+ directory: dir, temporaryRoot: dir, open: candidate => openSync(candidate, 'r'),
+ read: candidate => { const bytes = readFileSync(candidate); writeFileSync(candidate, replaced); return bytes; },
+ });
+ if (process.platform !== 'darwin' || kind === 'conversion-failure') {
+ await expect(result).rejects.toThrow(process.platform !== 'darwin' ? 'does not support' : 'failed to normalize');
+ } else {
+ const asset = await result;
+ if (kind === 'video-story') expect(asset).toMatchObject({ mediaPk: 'fixture-pk' });
+ else expect(asset).toMatchObject({ bytes: approved });
+ expect(inputs.length).toBeGreaterThan(0);
+ }
+ if (process.platform !== 'darwin') expect(spawn).not.toHaveBeenCalled();
+ for (const input of inputs) await expect(access(input)).rejects.toThrow();
+ expect(await readFile(file)).toEqual(replaced);
+ expect(await readdir(dir)).toEqual([path.basename(file)]);
+ } finally { await rm(dir, { recursive: true, force: true }); }
+ });
+
// #6826 file-state recurrence, unit tier. Real uploader and broker-approved
// descriptors; old tests never reached multipart init with task-only files.
// The old resume write fails after init, before any part can be transferred.
diff --git a/packages/opencli-vendor/README.md b/packages/opencli-vendor/README.md
index 765d73aabf..5f1d47092e 100644
--- a/packages/opencli-vendor/README.md
+++ b/packages/opencli-vendor/README.md
@@ -56,6 +56,8 @@
成功和失败均沿用调用 finally 清理,源文件保留;外部 Chrome 与内嵌浏览器使用相同的暂存入口。
- 图片、音频和下载成果的默认路径与目录参数元数据均使用任务内相对路径;
诊断截图及跟踪文件按调用隔离,结束后清理,兼容 Windows。
+- Instagram 图片转换、视频元数据、封面及 story 裁剪只读取批准字节的调用内副本,
+ 不重新打开原始源路径;临时输入及转换结果沿用同一调用目录,在成功或失败后清理。
- 抖音分片上传先批准并打开源文件,再创建远端上传;上传 ID 和进度只存于本次调用,
不读写跨调用断点文件。初始化、分片、完成失败及成功都关闭源描述符,不自动重试。
- Midjourney 保留当前额度和批次估算,不读写历史监控快照;历史趋势字段返回空值,
diff --git a/packages/opencli-vendor/UPSTREAM.json b/packages/opencli-vendor/UPSTREAM.json
index 783f4ff6a5..e1f73430d5 100644
--- a/packages/opencli-vendor/UPSTREAM.json
+++ b/packages/opencli-vendor/UPSTREAM.json
@@ -715,7 +715,7 @@
"clis/indeed/utils.js": "432ee24c15fdd18df6d9e9a54fbfcdd75edf86708eae88b64dcd4bec06822118",
"clis/instagram/_shared/navigation.js": "acb71a106f5086de5e2053d8fda4bfd1ecc67ce96fb3c0722b118d1501ca7725",
"clis/instagram/_shared/post-like.js": "e1acb51a3f00823df80f18c89f529f2613e62b75a3525e1a872861049e39dbda",
- "clis/instagram/_shared/private-publish.js": "bf5c86f273a658ce4d1d13285f3e6c7c9ac88c28c3a9465a613a7e0eaab2699a",
+ "clis/instagram/_shared/private-publish.js": "a7767d1992cdf5ffb945b20a4de84dd14cedd3adde7375d9cedc006eea307b9f",
"clis/instagram/_shared/protocol-capture.js": "1d6d92e962d76cfe8ddd30b7df914b1a3f8c3a3a76c6433808249aef64a7db20",
"clis/instagram/_shared/read-response.js": "7805d4fc63e6ecb767136a6c7ae5415a000c41768b047f2ee539eb23f76bb513",
"clis/instagram/_shared/runtime-info.js": "40ecefbf47ee65685048d9b8f61ba12dc9666d5e1ba8c048828b9e7eff74592c",
diff --git a/packages/opencli-vendor/cindy/patches.json b/packages/opencli-vendor/cindy/patches.json
index 77ce394063..1ea3c26328 100644
--- a/packages/opencli-vendor/cindy/patches.json
+++ b/packages/opencli-vendor/cindy/patches.json
@@ -3219,5 +3219,37 @@
"file": "cli-manifest.json",
"before": "Show Midjourney quota, conservative batch estimates, and account-consumption trend",
"after": "Show Midjourney current quota and conservative batch estimates (historical trend unavailable)"
+ },
+ {
+ "file": "clis/instagram/_shared/private-publish.js",
+ "before": "export function prepareImageAssetForPrivateUpload(filePath) {",
+ "after": "// Converters must never reopen a source path after the host approved its bytes.\n// The existing invocation directory owns and cleans every staged copy.\nfunction stagePrivateMediaBytes(filePath, bytes) {\n const stagedPath = path.join(recipeTemporaryDirectory(), `input-${crypto.randomUUID()}${path.extname(filePath)}`);\n fs.writeFileSync(recipeOutputPath(stagedPath), bytes, { mode: 0o600 });\n return stagedPath;\n}\nexport function prepareImageAssetForPrivateUpload(filePath) {"
+ },
+ {
+ "file": "clis/instagram/_shared/private-publish.js",
+ "before": " const outputPath = buildPrivateNormalizedImagePath(filePath);\n const result = spawnSync('sips', [",
+ "after": " const inputPath = stagePrivateMediaBytes(filePath, asset.bytes);\n const outputPath = buildPrivateNormalizedImagePath(filePath);\n const result = spawnSync('sips', [",
+ "count": 2
+ },
+ {
+ "file": "clis/instagram/_shared/private-publish.js",
+ "before": " INSTAGRAM_PRIVATE_PAD_COLOR,\n filePath,",
+ "after": " INSTAGRAM_PRIVATE_PAD_COLOR,\n inputPath,",
+ "count": 2
+ },
+ {
+ "file": "clis/instagram/_shared/private-publish.js",
+ "before": " const metadata = readVideoMetadata(filePath);\n const coverImage = generateVideoCoverImage(filePath);",
+ "after": " const stagedPath = stagePrivateMediaBytes(filePath, bytes);\n const metadata = readVideoMetadata(stagedPath);\n const coverImage = generateVideoCoverImage(stagedPath);"
+ },
+ {
+ "file": "clis/instagram/_shared/private-publish.js",
+ "before": " bytes,\n coverImage,",
+ "after": " bytes,\n stagedPath,\n coverImage,"
+ },
+ {
+ "file": "clis/instagram/_shared/private-publish.js",
+ "before": " const trimmedPath = trimVideoForInstagramStory(filePath, INSTAGRAM_MAX_STORY_VIDEO_DURATION_MS);",
+ "after": " const trimmedPath = trimVideoForInstagramStory(asset.stagedPath, INSTAGRAM_MAX_STORY_VIDEO_DURATION_MS);"
}
]
diff --git a/packages/opencli-vendor/clis/instagram/_shared/private-publish.js b/packages/opencli-vendor/clis/instagram/_shared/private-publish.js
index fca1bbe146..42caf5b21f 100644
--- a/packages/opencli-vendor/clis/instagram/_shared/private-publish.js
+++ b/packages/opencli-vendor/clis/instagram/_shared/private-publish.js
@@ -331,6 +331,13 @@ function buildPrivateNormalizedImagePath(filePath) {
const parsed = path.parse(filePath);
return path.join(recipeTemporaryDirectory(), `opencli-instagram-private-${parsed.name}-${crypto.randomUUID()}${parsed.ext || '.png'}`);
}
+// Converters must never reopen a source path after the host approved its bytes.
+// The existing invocation directory owns and cleans every staged copy.
+function stagePrivateMediaBytes(filePath, bytes) {
+ const stagedPath = path.join(recipeTemporaryDirectory(), `input-${crypto.randomUUID()}${path.extname(filePath)}`);
+ fs.writeFileSync(recipeOutputPath(stagedPath), bytes, { mode: 0o600 });
+ return stagedPath;
+}
export function prepareImageAssetForPrivateUpload(filePath) {
const asset = readImageAsset(filePath);
const normalizedDimensions = getInstagramFeedNormalizedDimensions(asset.width, asset.height);
@@ -340,6 +347,7 @@ export function prepareImageAssetForPrivateUpload(filePath) {
if (process.platform !== 'darwin') {
throw new CommandExecutionError(`Instagram private publish does not support auto-normalizing ${asset.fileName} on ${process.platform}`, `Use images within ${INSTAGRAM_MIN_FEED_ASPECT_RATIO.toFixed(2)}-${INSTAGRAM_MAX_FEED_ASPECT_RATIO.toFixed(2)} aspect ratio, or use the UI route`);
}
+ const inputPath = stagePrivateMediaBytes(filePath, asset.bytes);
const outputPath = buildPrivateNormalizedImagePath(filePath);
const result = spawnSync('sips', [
'--padToHeightWidth',
@@ -347,7 +355,7 @@ export function prepareImageAssetForPrivateUpload(filePath) {
String(normalizedDimensions.width),
'--padColor',
INSTAGRAM_PRIVATE_PAD_COLOR,
- filePath,
+ inputPath,
'--out',
outputPath,
], {
@@ -374,6 +382,7 @@ export function prepareImageAssetForPrivateStoryUpload(filePath) {
if (process.platform !== 'darwin') {
throw new CommandExecutionError(`Instagram private story publish does not support auto-normalizing ${asset.fileName} on ${process.platform}`, `Use images within ${INSTAGRAM_MIN_STORY_ASPECT_RATIO.toFixed(2)}-${INSTAGRAM_MAX_STORY_ASPECT_RATIO.toFixed(2)} aspect ratio, or use the UI route`);
}
+ const inputPath = stagePrivateMediaBytes(filePath, asset.bytes);
const outputPath = buildPrivateNormalizedImagePath(filePath);
const result = spawnSync('sips', [
'--padToHeightWidth',
@@ -381,7 +390,7 @@ export function prepareImageAssetForPrivateStoryUpload(filePath) {
String(normalizedDimensions.width),
'--padColor',
INSTAGRAM_PRIVATE_PAD_COLOR,
- filePath,
+ inputPath,
'--out',
outputPath,
], {
@@ -501,8 +510,9 @@ FileHandle.standardOutput.write(json)
}
export function readVideoAsset(filePath) {
const bytes = readRecipeFileSync(filePath);
- const metadata = readVideoMetadata(filePath);
- const coverImage = generateVideoCoverImage(filePath);
+ const stagedPath = stagePrivateMediaBytes(filePath, bytes);
+ const metadata = readVideoMetadata(stagedPath);
+ const coverImage = generateVideoCoverImage(stagedPath);
return {
filePath,
fileName: path.basename(filePath),
@@ -512,6 +522,7 @@ export function readVideoAsset(filePath) {
durationMs: metadata.durationMs,
byteLength: bytes.length,
bytes,
+ stagedPath,
coverImage,
cleanupPaths: coverImage.cleanupPath ? [coverImage.cleanupPath] : [],
};
@@ -561,7 +572,7 @@ function prepareVideoAssetForPrivateStoryUpload(filePath) {
if (asset.durationMs <= INSTAGRAM_MAX_STORY_VIDEO_DURATION_MS) {
return asset;
}
- const trimmedPath = trimVideoForInstagramStory(filePath, INSTAGRAM_MAX_STORY_VIDEO_DURATION_MS);
+ const trimmedPath = trimVideoForInstagramStory(asset.stagedPath, INSTAGRAM_MAX_STORY_VIDEO_DURATION_MS);
const trimmedAsset = readVideoAsset(trimmedPath);
return {
...trimmedAsset,