@mindscraft/branch-video-agent-cli 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/dist/commands/workbench.js +13 -14
- package/dist/lib/http.d.ts +7 -1
- package/dist/lib/http.js +1 -0
- package/dist/lib/uploadEncryption.d.ts +10 -0
- package/dist/lib/uploadEncryption.js +40 -0
- package/dist/lib/uploadTemp.d.ts +1 -0
- package/dist/lib/uploadTemp.js +29 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -267,7 +267,11 @@ branch-video-agent workbench submit --file .\artifact-external.json --raw
|
|
|
267
267
|
branch-video-agent workbench annotation put --project wb_project_id --file .\annotation.json --raw
|
|
268
268
|
```
|
|
269
269
|
|
|
270
|
-
同一产物用稳定的 `artifactKey` 和由流程侧持久维护的递增 `sourceRevision`。`idempotencyKey` 由流程侧保存并在重试时复用。已有持久媒体使用 `content.type: "external"`;本地文档、脱敏 session 或其他需要长期保存的文件使用 `content.type: "local"` 与 `sourcePath`。CLI
|
|
270
|
+
同一产物用稳定的 `artifactKey` 和由流程侧持久维护的递增 `sourceRevision`。`idempotencyKey` 由流程侧保存并在重试时复用。已有持久媒体使用 `content.type: "external"`;本地文档、脱敏 session 或其他需要长期保存的文件使用 `content.type: "local"` 与 `sourcePath`。CLI 先计算原文件 SHA-256、声明 blob。0.5.2 对尚未 ready 的 blob 要求服务端返回 `uploadTransport` 公钥配置;缺少配置时失败,不自动回退。文件先 gzip,再使用每次随机 AES-256-GCM 密钥加密,密钥以服务端 RSA3072 OAEP-SHA256 公钥封装。传输使用 `application/octet-stream`、`x-workbench-upload-format: gzip-aes256gcm-v1`,以及受限的 key-id/wrapped-key/nonce/auth-tag headers,不使用 Content-Encoding。服务端完整认证密文后才解压,校验原始大小和 SHA-256;下载返回原字节,版本和幂等键不变。相同字节复用首次声明的 MIME,CLI 使用该权威 MIME 构造 AAD 和提交描述;不会更改 blob 的 purpose 或脱敏证明。
|
|
271
|
+
|
|
272
|
+
原文件上限 64 MiB,密文/压缩流上限 65 MiB。两端使用私有临时文件,成功、失败和取消时清理;下次上传只清理确定已退出 PID 的历史目录,保留活跃、无权探测及 PID 复用目录,因此不是总磁盘硬上限。TMPDIR 不得跨 PID namespace 共享(服务端 pod 私有 /tmp;CLI 宿主同 namespace)。异常强杀后恢复清理依赖下一次上传;仍需现有磁盘容量监控。
|
|
273
|
+
|
|
274
|
+
公钥附加在已授权 blob 声明响应;私钥仅存服务端独立 Mongo 集合,所有 pod 读取同一权威记录。私钥不进入 Blob DTO、CLI、日志或镜像。此机制保护上传传输,不保护数据库泄露或能替换公钥的可信代理;网关不能检查加密后的文件正文,服务端仍执行鉴权、大小及完整性验证。部署先服务端后 CLI。服务端保留旧 raw/gzip 兼容;ready blob 直接复用,无需公钥或再传输。CLI 不发送本地文件路径。
|
|
271
275
|
|
|
272
276
|
```json
|
|
273
277
|
{
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { pipeline } from 'node:stream/promises';
|
|
1
|
+
import { encryptUpload } from '../lib/uploadEncryption.js';
|
|
3
2
|
import { createHash } from 'node:crypto';
|
|
4
3
|
import { createReadStream } from 'node:fs';
|
|
5
4
|
import { stat, writeFile } from 'node:fs/promises';
|
|
@@ -70,19 +69,20 @@ async function uploadLocalContent(context, sourcePath, content) {
|
|
|
70
69
|
timeoutMs: context.flags.timeoutMs,
|
|
71
70
|
});
|
|
72
71
|
const blobId = getRequiredString(declared.data, 'id');
|
|
72
|
+
const declaredMime = declared.data.mime;
|
|
73
|
+
if (declared.data.sha256 !== sha256 || declared.data.size !== fileStat.size || typeof declaredMime !== 'string' || !/^[a-zA-Z0-9!#$&^_.+-]+\/[a-zA-Z0-9!#$&^_.+-]+$/.test(declaredMime) || declaredMime.length >= 200)
|
|
74
|
+
throw new CliCommandError('Blob declaration does not match the local file', 'BLOB_METADATA_MISMATCH');
|
|
73
75
|
if (declared.data.state !== 'ready') {
|
|
74
76
|
let uploadError;
|
|
75
|
-
const
|
|
76
|
-
const
|
|
77
|
-
// Fetch consumes the compressed readable; prevent an unhandled rejection
|
|
78
|
-
// until its stream error reaches requestStream. Always close both below.
|
|
79
|
-
void compressing.catch(() => undefined);
|
|
77
|
+
const encrypted = await encryptUpload(sourcePath, projectId(context.payload, context.flags.project), declared.data);
|
|
78
|
+
const stream = createReadStream(encrypted.path);
|
|
80
79
|
try {
|
|
81
80
|
await context.client.requestStream(`${workbenchPath}/projects/${encodeURIComponent(projectId(context.payload, context.flags.project))}/blobs/${encodeURIComponent(blobId)}/content`, {
|
|
82
81
|
method: 'PUT',
|
|
83
|
-
body: Readable.toWeb(
|
|
84
|
-
contentType: 'application/
|
|
85
|
-
uploadFormat: 'gzip',
|
|
82
|
+
body: Readable.toWeb(stream),
|
|
83
|
+
contentType: 'application/octet-stream',
|
|
84
|
+
uploadFormat: 'gzip-aes256gcm-v1',
|
|
85
|
+
encryption: encrypted.headers,
|
|
86
86
|
timeoutMs: context.flags.timeoutMs,
|
|
87
87
|
});
|
|
88
88
|
}
|
|
@@ -90,9 +90,8 @@ async function uploadLocalContent(context, sourcePath, content) {
|
|
|
90
90
|
uploadError = error;
|
|
91
91
|
}
|
|
92
92
|
finally {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
await compressing.catch(() => undefined);
|
|
93
|
+
stream.destroy();
|
|
94
|
+
await encrypted.cleanup();
|
|
96
95
|
}
|
|
97
96
|
if (uploadError) {
|
|
98
97
|
const status = await context.client.request(`${workbenchPath}/projects/${encodeURIComponent(projectId(context.payload, context.flags.project))}/blobs/${encodeURIComponent(blobId)}`, {
|
|
@@ -103,7 +102,7 @@ async function uploadLocalContent(context, sourcePath, content) {
|
|
|
103
102
|
throw uploadError;
|
|
104
103
|
}
|
|
105
104
|
}
|
|
106
|
-
return { type: 'blob', blobId, sha256, size: fileStat.size, mime };
|
|
105
|
+
return { type: 'blob', blobId, sha256, size: fileStat.size, mime: declaredMime };
|
|
107
106
|
}
|
|
108
107
|
function validateSubmission(payload) {
|
|
109
108
|
if (payload.schemaVersion !== 'workbench.artifact/1') {
|
package/dist/lib/http.d.ts
CHANGED
|
@@ -13,7 +13,13 @@ type StreamRequestOptions = {
|
|
|
13
13
|
method: string;
|
|
14
14
|
body: ReadableStream<Uint8Array>;
|
|
15
15
|
contentType: string;
|
|
16
|
-
uploadFormat?: 'gzip';
|
|
16
|
+
uploadFormat?: 'gzip' | 'gzip-aes256gcm-v1';
|
|
17
|
+
encryption?: {
|
|
18
|
+
kid: string;
|
|
19
|
+
wrappedKey: string;
|
|
20
|
+
nonce: string;
|
|
21
|
+
tag: string;
|
|
22
|
+
};
|
|
17
23
|
timeoutMs?: number;
|
|
18
24
|
};
|
|
19
25
|
type BinaryRequestResult = {
|
package/dist/lib/http.js
CHANGED
|
@@ -93,6 +93,7 @@ export class CliHttpClient {
|
|
|
93
93
|
headers: {
|
|
94
94
|
Authorization: `Bearer ${this.token}`,
|
|
95
95
|
'content-type': options.contentType,
|
|
96
|
+
...(options.encryption ? { 'x-workbench-key-id': options.encryption.kid, 'x-workbench-wrapped-key': options.encryption.wrappedKey, 'x-workbench-nonce': options.encryption.nonce, 'x-workbench-auth-tag': options.encryption.tag } : {}),
|
|
96
97
|
...(options.uploadFormat ? { 'x-workbench-upload-format': options.uploadFormat } : {}),
|
|
97
98
|
},
|
|
98
99
|
body: options.body,
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { uploadTempDirectory } from './uploadTemp.js';
|
|
2
|
+
import { constants, createCipheriv, createHash, createPublicKey, publicEncrypt, randomBytes } from 'node:crypto';
|
|
3
|
+
import { createReadStream, createWriteStream } from 'node:fs';
|
|
4
|
+
import { rm } from 'node:fs/promises';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { Transform } from 'node:stream';
|
|
7
|
+
import { pipeline } from 'node:stream/promises';
|
|
8
|
+
import { createGzip } from 'node:zlib';
|
|
9
|
+
import { CliCommandError } from './errors.js';
|
|
10
|
+
export async function encryptUpload(sourcePath, projectId, blob) {
|
|
11
|
+
const config = blob.uploadTransport;
|
|
12
|
+
if (!config || config.format !== 'gzip-aes256gcm-v1' || config.keyAlgorithm !== 'RSA-OAEP-256' || config.contentAlgorithm !== 'A256GCM' || typeof config.kid !== 'string' || !/^[a-f0-9]{64}$/.test(config.kid) || typeof config.publicKeySpki !== 'string' || config.publicKeySpki.length > 1024)
|
|
13
|
+
throw new CliCommandError('Server does not offer the required encrypted upload protocol', 'UPLOAD_ENCRYPTION_REQUIRED');
|
|
14
|
+
let publicKey;
|
|
15
|
+
try {
|
|
16
|
+
const der = Buffer.from(config.publicKeySpki, 'base64');
|
|
17
|
+
publicKey = createPublicKey({ key: der, type: 'spki', format: 'der' });
|
|
18
|
+
if (publicKey.asymmetricKeyType !== 'rsa' || publicKey.asymmetricKeyDetails?.modulusLength !== 3072 || der.toString('base64') !== config.publicKeySpki || createHash('sha256').update(der).digest('hex') !== config.kid)
|
|
19
|
+
throw new Error();
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
throw new CliCommandError('Invalid upload public key', 'UPLOAD_ENCRYPTION_REQUIRED');
|
|
23
|
+
}
|
|
24
|
+
const key = randomBytes(32), nonce = randomBytes(12);
|
|
25
|
+
const cipher = createCipheriv('aes-256-gcm', key, nonce, { authTagLength: 16 });
|
|
26
|
+
cipher.setAAD(Buffer.from(JSON.stringify([config.format, config.kid, projectId, blob.id, blob.sha256, blob.size, blob.mime])));
|
|
27
|
+
const wrappedKey = publicEncrypt({ key: publicKey, padding: constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' }, key);
|
|
28
|
+
key.fill(0);
|
|
29
|
+
const directory = await uploadTempDirectory();
|
|
30
|
+
const path = join(directory, 'payload.enc');
|
|
31
|
+
const limit = (max) => { let size = 0; return new Transform({ transform(chunk, _encoding, done) { size += chunk.length; done(size > max ? new CliCommandError('Upload exceeds file size limit', 'FILE_TOO_LARGE') : null, chunk); } }); };
|
|
32
|
+
try {
|
|
33
|
+
await pipeline(createReadStream(sourcePath), limit(64 * 1024 * 1024), createGzip(), cipher, limit(65 * 1024 * 1024), createWriteStream(path, { flags: 'wx', mode: 0o600 }));
|
|
34
|
+
return { path, headers: { kid: config.kid, wrappedKey: wrappedKey.toString('base64url'), nonce: nonce.toString('base64url'), tag: cipher.getAuthTag().toString('base64url') }, cleanup: () => rm(directory, { recursive: true, force: true }) };
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
await rm(directory, { recursive: true, force: true });
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function uploadTempDirectory(): Promise<string>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { lstat, mkdir, mkdtemp, readdir, rm } from 'node:fs/promises';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
// This directory must belong to one host/container PID namespace. Never
|
|
5
|
+
// share TMPDIR across PID namespaces. Cleanup runs only on the next upload;
|
|
6
|
+
// it is not a hard total-disk cap, and reused PIDs are preserved.
|
|
7
|
+
export async function uploadTempDirectory() {
|
|
8
|
+
const base = join(tmpdir(), 'workbench-upload-client');
|
|
9
|
+
await mkdir(base, { recursive: true, mode: 0o700 });
|
|
10
|
+
const info = await lstat(base);
|
|
11
|
+
if (!info.isDirectory() || info.isSymbolicLink() || (process.platform !== 'win32' && ((info.mode & 0o077) !== 0 || info.uid !== process.getuid?.())))
|
|
12
|
+
throw new Error('Unsafe upload temporary directory');
|
|
13
|
+
for (const entry of await readdir(base, { withFileTypes: true })) {
|
|
14
|
+
const match = /^([1-9][0-9]*)-[A-Za-z0-9]+$/.exec(entry.name);
|
|
15
|
+
if (!match || !entry.isDirectory() || entry.isSymbolicLink())
|
|
16
|
+
continue;
|
|
17
|
+
let dead = false;
|
|
18
|
+
try {
|
|
19
|
+
process.kill(Number(match[1]), 0);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
dead = error.code === 'ESRCH';
|
|
23
|
+
}
|
|
24
|
+
// EPERM and reused/live PIDs are conservatively preserved. No age heuristic.
|
|
25
|
+
if (dead)
|
|
26
|
+
await rm(join(base, entry.name), { recursive: true, force: true });
|
|
27
|
+
}
|
|
28
|
+
return mkdtemp(join(base, `${process.pid}-`));
|
|
29
|
+
}
|