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(media): 补齐地址刷新重试与签名参数脱敏

liugaoweie2754445c91c
diff --git a/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts b/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts
index a15d14466c..3812b32440 100644
--- a/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts
+++ b/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts
@@ -1549,7 +1549,8 @@ describe('Cindy Core media invocation state and security boundary', () => {
});
});
- it('下载地址失效后客户端自动刷新原任务地址,不重复生成', async () => {
+ it.each(['none', 'network', 'timeout', 'server', 'exhausted', 'rejected'])(
+ '下载地址失效后客户端刷新原任务地址(%s),不重复生成', async (failure) => {
const asyncOperation = {
...operation(
{
@@ -1590,7 +1591,18 @@ describe('Cindy Core media invocation state and security boundary', () => {
{ status: 200 },
),
)
- .mockResolvedValueOnce(new Response(null, { status: 403 }))
+ .mockResolvedValueOnce(new Response(null, { status: 403 }));
+ const failures = failure === 'none' ? 0 : failure === 'exhausted' ? 3 : failure === 'rejected' ? 1 : 2;
+ for (let index = 0; index < failures; index += 1) {
+ if (failure === 'server' || failure === 'rejected') {
+ mocks.outboundFetch.mockResolvedValueOnce(new Response('{}', { status: failure === 'server' ? 503 : 401 }));
+ } else {
+ const error = new Error('temporary refresh failure');
+ if (failure === 'timeout') error.name = 'AbortError';
+ mocks.outboundFetch.mockRejectedValueOnce(error);
+ }
+ }
+ mocks.outboundFetch
.mockResolvedValueOnce(new Response(JSON.stringify({ status: 'succeeded', video: 'https://cdn.example.com/refreshed.mp4' })))
.mockResolvedValueOnce(new Response(MP4, { headers: { 'content-type': 'video/mp4' } }));
@@ -1602,12 +1614,22 @@ describe('Cindy Core media invocation state and security boundary', () => {
const invocationId = prepared.invocation_id as string;
await callCindyMedia({ action: 'request', invocationId, body: { content: [] } });
- await expect(callCindyMedia({ action: 'poll', invocationId })).resolves.toMatchObject({
- ok: true,
- status: 'complete',
- });
- expect(mocks.rows.get(invocationId)?.state).toBe('complete');
- expect(mocks.outboundFetch).toHaveBeenCalledTimes(5);
+ const result = await callCindyMedia({ action: 'poll', invocationId });
+ if (failure === 'exhausted' || failure === 'rejected') {
+ expect(result).toMatchObject({
+ ok: false, retryable: false,
+ errorCode: failure === 'rejected' ? 'UPSTREAM_REJECTED' : 'POLL_UNAVAILABLE',
+ });
+ expect(mocks.rows.get(invocationId)).toMatchObject({
+ state: 'pending',
+ responseJson: JSON.stringify({ status: 'succeeded', video: 'https://cdn.example.com/expired.mp4' }),
+ });
+ expect(mocks.outboundFetch).toHaveBeenCalledTimes(3 + failures);
+ } else {
+ expect(result).toMatchObject({ ok: true, status: 'complete' });
+ expect(mocks.rows.get(invocationId)?.state).toBe('complete');
+ expect(mocks.outboundFetch).toHaveBeenCalledTimes(5 + failures);
+ }
expect(mocks.confirm).not.toHaveBeenCalled();
expect(mocks.outboundFetch.mock.calls.filter(([, init]) => init.method === 'POST')).toHaveLength(1);
});
diff --git a/apps/desktop/src/main/cindy-media/__tests__/mediaDownload.test.ts b/apps/desktop/src/main/cindy-media/__tests__/mediaDownload.test.ts
index 7229cbd842..e75ffd5f6b 100644
--- a/apps/desktop/src/main/cindy-media/__tests__/mediaDownload.test.ts
+++ b/apps/desktop/src/main/cindy-media/__tests__/mediaDownload.test.ts
@@ -158,15 +158,17 @@ describe('media download approval and network recovery', () => {
it('shows the private endpoint and ordinary parameters without login or signature values', async () => {
const ctx = context();
mocks.fetch.mockRejectedValueOnce(new SsrFBlockedError('private'));
- await download(ctx, 'https://test-user:[email protected]/media/view?operation=read&signature=test-signature#hidden-fragment');
+ await download(ctx, 'https://test-user:[email protected]/media/view?operation=read&signature=test-signature&sig=test-sig&OSSAccessKeyId=test-key-id#hidden-fragment');
const request = ctx.confirm.mock.calls.at(-1)![0];
expect(request.reasons).toEqual(['network']);
const shown = new URL(request.source);
expect(shown.pathname).toBe('/media/view');
expect(shown.searchParams.get('operation')).toBe('read');
- expect(shown.searchParams.get('signature')).toBe('[REDACTED]');
+ for (const key of ['signature', 'sig', 'OSSAccessKeyId']) {
+ expect(shown.searchParams.get(key)).toBe('[REDACTED]');
+ }
expect(shown.hash).toBe('');
- expect(JSON.stringify(ctx.confirm.mock.calls)).not.toMatch(/test-user|test-password|test-signature|hidden-fragment/);
+ expect(JSON.stringify(ctx.confirm.mock.calls)).not.toMatch(/test-user|test-password|test-signature|test-sig|test-key-id|hidden-fragment/);
});
it('a refreshed signed URL on the same origin reuses the operation approval', async () => {
diff --git a/apps/desktop/src/main/cindy-media/__tests__/mediaRequestLog.test.ts b/apps/desktop/src/main/cindy-media/__tests__/mediaRequestLog.test.ts
index 69d6e5a0da..92980a7473 100644
--- a/apps/desktop/src/main/cindy-media/__tests__/mediaRequestLog.test.ts
+++ b/apps/desktop/src/main/cindy-media/__tests__/mediaRequestLog.test.ts
@@ -13,6 +13,14 @@ describe('media request log redaction', () => {
);
});
+ it.each(['sig', 'OSSAccessKeyId'])('隐藏媒体签名参数 %s,同时保留普通参数', (key) => {
+ const shown = new URL(mediaRequestUrlForLog(
+ `https://example.test/media?operation=read&${key}=test-credential`,
+ ));
+ expect(shown.searchParams.get(key)).toBe('[REDACTED]');
+ expect(shown.searchParams.get('operation')).toBe('read');
+ });
+
it('保留参数结构并收敛凭证和媒体正文', () => {
expect(
mediaRequestParamsForLog({
diff --git a/apps/desktop/src/main/cindy-media/invocationService.ts b/apps/desktop/src/main/cindy-media/invocationService.ts
index 326cbbaf7f..a92225d136 100644
--- a/apps/desktop/src/main/cindy-media/invocationService.ts
+++ b/apps/desktop/src/main/cindy-media/invocationService.ts
@@ -1575,20 +1575,34 @@ async function pollInvocation(
refreshExpiredUrl = true;
}
try {
- const response = await dispatchRequest({
- invocationId: invocation.id,
- providerId: invocation.guide.connection.providerId,
- modelId: invocation.modelId,
- capability: invocation.capability,
- connection: resolveConnection(invocation.guide.connection.providerId),
- method: guide.method,
- path: pollPath(guide.path, invocation.taskId),
- headers: guide.headers,
- body: pollBody(guide, invocation.taskId),
- timeoutMs: guide.timeoutMs,
- maxResponseBytes: guide.maxResponseBytes,
- operation: 'poll',
- });
+ let response: unknown;
+ for (let attempt = 0; ; attempt += 1) {
+ assertAuthScope(scope, invocation.owner);
+ try {
+ response = await dispatchRequest({
+ invocationId: invocation.id,
+ providerId: invocation.guide.connection.providerId,
+ modelId: invocation.modelId,
+ capability: invocation.capability,
+ connection: resolveConnection(invocation.guide.connection.providerId),
+ method: guide.method,
+ path: pollPath(guide.path, invocation.taskId),
+ headers: guide.headers,
+ body: pollBody(guide, invocation.taskId),
+ timeoutMs: guide.timeoutMs,
+ maxResponseBytes: guide.maxResponseBytes,
+ operation: 'poll',
+ });
+ break;
+ } catch (error) {
+ assertAuthScope(scope, invocation.owner);
+ if (!refreshExpiredUrl || attempt >= 2 ||
+ !(error instanceof MediaInvocationError) || error.code !== 'POLL_UNAVAILABLE') {
+ throw error;
+ }
+ await delay(250 * (attempt + 1), undefined, { signal: context?.signal });
+ }
+ }
assertAuthScope(scope, invocation.owner);
const rawStatus = valuesAtPath(response, guide.statusPath)[0];
const status = typeof rawStatus === 'string' ? rawStatus : '';
diff --git a/apps/desktop/src/main/cindy-media/mediaRequestLog.ts b/apps/desktop/src/main/cindy-media/mediaRequestLog.ts
index 0a68c057d4..baeb4bbd29 100644
--- a/apps/desktop/src/main/cindy-media/mediaRequestLog.ts
+++ b/apps/desktop/src/main/cindy-media/mediaRequestLog.ts
@@ -3,7 +3,7 @@ import { redactSensitiveText } from '@cindy/maker-shared/error-redaction';
const MAX_LOG_STRING_CHARS = 20_000;
const MAX_LOG_DEPTH = 24;
const SENSITIVE_PARAM_NAME =
- /(?:^|[-_.])(authorization|proxy[-_]?authorization|api[-_]?key|access[-_]?key(?:[-_]?id)?|private[-_]?key|key|access[-_]?token|refresh[-_]?token|id[-_]?token|token|secret|password|passwd|signature|credential|cookie|session)(?:$|[-_.])/i;
+ /(?:^|[-_.])(authorization|proxy[-_]?authorization|api[-_]?key|access[-_]?key(?:[-_]?id)?|private[-_]?key|key|access[-_]?token|refresh[-_]?token|id[-_]?token|token|secret|password|passwd|sig|signature|ossaccesskeyid|credential|cookie|session)(?:$|[-_.])/i;
function boundedText(value: string): string {
const redacted = redactSensitiveText(value);