This commit is contained in:
gr
2026-10-23 11:18:10 +08:00
10 changed files with 709 additions and 94 deletions
@@ -46,4 +46,27 @@ errorReceiptExpect(!str_contains(end($frames), 'required_sections') && str_conta
'cancel terminal does not reuse unrelated failed receipt');
$format->invoke(null, [], ['ok' => true, 'content' => '合成回复']);
errorReceiptExpect(DiagnosisAiLogic::getAssistantErrorMeta() === [], 'success clears stale error metadata');
$skippedResult = $result;
$skippedResult['items'] = [
['kind' => 'image', 'status' => 'skipped', 'reason' => 'not_found', 'url' => 'https://private.invalid/missing'],
['kind' => 'image', 'status' => 'failed', 'reason' => 'private-error-body'],
];
$skippedResult['attachment_count'] = 2;
$skippedResult['submitted_attachment_count'] = 0;
$skippedResult['skipped_attachment_count'] = 1;
$format->invoke(null, [], $skippedResult);
$safeSkipped = DiagnosisAiLogic::getAssistantErrorMeta();
errorReceiptExpect($safeSkipped['items'][0]['status'] === 'skipped' && $safeSkipped['items'][0]['reason'] === 'not_found'
&& $safeSkipped['items'][1]['status'] === 'failed' && !isset($safeSkipped['items'][1]['reason'])
&& $safeSkipped['submitted_attachment_count'] === 0 && $safeSkipped['skipped_attachment_count'] === 1,
'final errors preserve safe missing-source facts and separate counters');
errorReceiptExpect(!str_contains(json_encode($safeSkipped), 'private.invalid')
&& !str_contains(json_encode($safeSkipped), 'private-error-body'), 'skipped error metadata never includes resource URLs or private reasons');
$success = $format->invoke(null, [], ['ok' => true, 'content' => '合成回复', 'transfer_manifest_final' => true,
'required_sections' => AiRequiredSource::manifest([]), 'items' => [['kind' => 'image', 'status' => 'skipped', 'reason' => 'not_found']],
'attachment_count' => 1, 'submitted_attachment_count' => 0, 'skipped_attachment_count' => 1,
'attachment_transfer' => 'uploaded_files']);
errorReceiptExpect($success['answer'] === '合成回复' && $success['items'][0]['status'] === 'skipped'
&& $success['submitted_attachment_count'] === 0 && $success['skipped_attachment_count'] === 1,
'a text answer with all attachments missing keeps its successful final receipt');
echo "Assistant final error receipt / real controller SSE / private metadata filter / cancellation isolation: OK\n";
@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
namespace think\facade {
class Log { public static function warning(string $message): void {} public static function write(string $message, string $level): void {} }
}
namespace app\common\service {
require __DIR__ . '/fixtures/CancellableCurlMultiStub.php';
function config(string $name): array { return $GLOBALS['missingFallbackConfig']; }
final class DifyRequiredAttachmentUpload {
public static function prepare(string $endpoint, string $key, string $user, array $files, int $timeout,
?callable $abort = null, array $options = []): array {
$skips = []; $wire = []; $indices = [];
foreach ($files as $position => $file) {
$missing = $GLOBALS['missingFallbackScenario'] === 'all-missing' || $position === 0;
if ($missing) {
$skips[] = ['index' => $position + 1, 'kind' => $file['type'], 'reason' => 'not_found'];
($options['on_upload'])(['index' => $position + 1, 'status' => 'skipped', 'reason' => 'not_found']);
} else {
$wire[] = ['type' => $file['type'], 'transfer_method' => 'local_file', 'upload_file_id' => 'synthetic-id'];
$indices[] = $position + 1;
($options['on_upload'])(['index' => $position + 1, 'status' => 'uploaded']);
}
}
return ['ok' => true, 'files' => $wire, 'uploaded_indices' => $indices, 'skipped' => $skips];
}
}
function curl_init(): \stdClass { return new \stdClass(); }
function curl_setopt_array(\stdClass $handle, array $options): bool {
if (!str_starts_with($options[CURLOPT_URL], 'https://fixture.invalid/v1/')) throw new \RuntimeException('Fixture destinations only');
$handle->options = $options; return true;
}
function curl_exec(\stdClass $handle): string|bool {
$payload = json_decode($handle->options[CURLOPT_POSTFIELDS], true);
$GLOBALS['missingFallbackRequests'][] = $payload;
$dify = str_ends_with($handle->options[CURLOPT_URL], '/chat-messages');
$handle->code = $dify ? 404 : 200;
($handle->options[CURLOPT_HEADERFUNCTION])($handle, 'HTTP/1.1 ' . $handle->code . " Synthetic\r\n");
if (isset($handle->options[CURLOPT_WRITEFUNCTION])) {
$write = $handle->options[CURLOPT_WRITEFUNCTION];
if ($dify) $write($handle, '{"code":"not_found"}');
else {
$write($handle, 'data: ' . json_encode(['choices' => [['delta' => ['content' => 'synthetic answer']]]]) . "\n\n");
$write($handle, "data: [DONE]\n\n");
}
return true;
}
return $dify ? '{"code":"not_found"}' : '{"choices":[{"message":{"content":"synthetic answer"}}]}';
}
function curl_errno(\stdClass $handle): int { return 0; }
function curl_getinfo(\stdClass $handle, int $option): int { return $handle->code; }
function curl_close(\stdClass $handle): void {}
}
namespace {
require dirname(__DIR__) . '/vendor/autoload.php';
use app\common\service\DifyChatService;
function missingFallbackExpect(bool $ok, string $message): void { if (!$ok) throw new RuntimeException($message); }
$missingFallbackConfig = ['enable' => true, 'base_url' => 'https://fixture.invalid/v1',
'timeout' => 5, 'required_attachment_transport' => 'upload',
'models' => ['qwen' => ['name' => 'synthetic', 'api_key' => 'fixture-only']]];
foreach ([false, true] as $stream) foreach (['all-missing', 'missing-image-available-document',
'missing-image-available-image', 'missing-document-available-image'] as $missingFallbackScenario) {
$missingFallbackRequests = []; $events = [];
$files = $missingFallbackScenario === 'all-missing'
? [['type' => 'document', 'url' => 'https://fixture.invalid/missing.pdf'], ['type' => 'audio', 'url' => 'https://fixture.invalid/missing.wav']]
: [['type' => $missingFallbackScenario === 'missing-document-available-image' ? 'document' : 'image', 'url' => 'https://fixture.invalid/missing'],
['type' => $missingFallbackScenario === 'missing-image-available-document' ? 'document' : 'image', 'url' => 'https://fixture.invalid/available']];
$options = ['required_files' => true, 'knowledge_mode' => 'evidence',
'on_progress' => static function (array $event) use (&$events): void { $events[] = $event; }];
$result = $stream ? DifyChatService::streamChat('qwen', [], 'complete synthetic clinical text', 'synthetic-user',
static fn (string $text): bool => true, null, $files, $options)
: DifyChatService::chat('qwen', [], 'complete synthetic clinical text', 'synthetic-user', $files, $options);
$expectedMissing = $missingFallbackScenario === 'all-missing' ? 2 : 1;
missingFallbackExpect($result['skipped_attachment_count'] === $expectedMissing && $result['attachment_count'] === 2,
'protocol fallback keeps original source and confirmed missing counts');
missingFallbackExpect($result['items'][0]['status'] === 'skipped' && $result['items'][0]['reason'] === 'not_found',
'known missing item survives every fallback outcome');
if ($missingFallbackScenario === 'missing-image-available-document') {
missingFallbackExpect(empty($result['ok']) && $result['error_code'] === 'REQUIRED_FILES_UNSUPPORTED'
&& count($missingFallbackRequests) === 1 && $result['submitted_attachment_count'] === 0
&& $result['attachment_transfer'] === 'uploaded_files', 'unsupported fallback retains the prior staged receipt');
continue;
}
missingFallbackExpect(!empty($result['ok']) && count($missingFallbackRequests) === 2
&& $result['submitted_attachment_count'] === 2 - $expectedMissing, 'text or available images complete through protocol fallback');
$wire = json_encode($missingFallbackRequests[1], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
missingFallbackExpect(!str_contains($wire, 'https://fixture.invalid/missing')
&& !str_contains($wire, 'ATTACHMENTS_NOT_INLINE') && str_contains($wire, 'complete synthetic clinical text')
&& substr_count($wire, '【附件可用性说明】') === 1, 'fallback builds from available sources without resurrecting dead references');
missingFallbackExpect(($missingFallbackScenario === 'all-missing') === !str_contains($wire, 'https://fixture.invalid/available'),
'available images are preserved while all-missing requests send only text');
}
echo "Missing attachment protocol fallback / truthful receipts / no revived references / blocking and stream: OK\n";
}
@@ -3,6 +3,13 @@
declare(strict_types=1);
namespace app\common\service {
/** Simulate access denied deterministically on Windows without changing ACLs. */
function scandir(string $directory)
{
if (($GLOBALS['attachmentDeniedDirectory'] ?? null) === $directory) return false;
return \scandir($directory);
}
/** Never query real DNS: reproduce the unsafe proxy answer deterministically. */
function dns_get_record(string $host, int $type): array
{
@@ -53,6 +60,7 @@ namespace {
if (str_starts_with($path, '/budget/')) usleep(650000);
header('Content-Type: application/json');
if (str_starts_with($path, '/fail/')) { http_response_code(422); echo '{"error":"fixture-private-path-url-key-body"}'; return; }
if (str_starts_with($path, '/missing-upload/')) { http_response_code(404); echo '{"error":"fixture-private-path-url-key-body"}'; return; }
if (str_starts_with($path, '/fail-second/')) {
$entries = array_map(static fn (string $line): array => json_decode($line, true), file($root . '/requests.jsonl', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
$uploads = array_filter($entries, static fn (array $entry): bool => ($entry['kind'] ?? '') === 'upload');
@@ -67,6 +75,10 @@ namespace {
}
$append(['kind' => 'download', 'path' => $path, 'authorization' => $_SERVER['HTTP_AUTHORIZATION'] ?? '']);
if ($path === '/missing-object') { http_response_code(404); echo '<Error>fixture-private-path-url-key-body</Error>'; return; }
if ($path === '/gone-object') { http_response_code(410); return; }
if ($path === '/denied-object') { http_response_code(403); echo '<Error>fixture-private-path-url-key-body</Error>'; return; }
if ($path === '/broken-object') { http_response_code(500); echo '<Error>fixture-private-path-url-key-body</Error>'; return; }
if ($path === '/stalled-missing-object') { http_response_code(404); echo '<Error>'; flush(); usleep(1200000); echo '</Error>'; return; }
if ($path === '/redirect') { header('Location: http://169.254.169.254/latest/meta-data/'); http_response_code(302); echo 'redirect'; return; }
if ($path === '/oversize-header') { header('Content-Length: 2000000'); echo 'x'; return; }
if ($path === '/oversize-stream') { echo str_repeat('x', 30000); return; }
@@ -174,11 +186,12 @@ namespace {
attachmentExpect(!empty($result['ok']) && count($result['files']) === 3 && $result['attachment_transport'] === 'uploaded_files', 'two images and PDF all become local_file references');
attachmentExpect(array_column($result['files'], 'type') === ['image', 'image', 'document']
&& array_unique(array_column($result['files'], 'transfer_method')) === ['local_file'], 'types and attachment order preserved');
attachmentExpect($result['uploaded_indices'] === [1, 2, 3] && $result['skipped'] === [], 'unchanged uploads report original indices and no skipped attachments');
attachmentExpect(count($requests()) === 3 && array_unique(array_column($requests(), 'user')) === ['synthetic-user'], 'every multipart upload has the exact chat user');
attachmentExpect(array_column($requests(), 'hash') === [hash_file('sha256', $root . '/public/uploads/first.png'),
hash_file('sha256', $root . '/public/uploads/nested/second.png'), hash_file('sha256', $root . '/public/uploads/report.pdf')], 'multipart contains complete binary bytes');
attachmentExpect(count($events) === 6 && array_column($events, 'status') === ['uploading', 'uploaded', 'uploading', 'uploaded', 'uploading', 'uploaded'], 'safe one-based progress follows each actual upload');
foreach ($events as $event) attachmentExpect(array_keys($event) === ['index', 'total', 'status'] && $event['total'] === 3, 'progress contains safe counts/status only');
foreach ($events as $event) attachmentExpect(array_keys($event) === ['index', 'total', 'status'] && $event['total'] === 6, 'progress contains safe original counts/status only');
foreach ($attachmentCurlOptions as $curlOptions) {
$temporary = $curlOptions[CURLOPT_POSTFIELDS]['file']->getFilename();
attachmentExpect(str_starts_with(str_replace('\\', '/', $temporary), str_replace('\\', '/', $root) . '/private/uploads/') && !is_file($temporary), 'CURLFile uses cleaned nonpublic snapshot');
@@ -220,6 +233,11 @@ namespace {
'https://site.synthetic.test/uploads//first.png',
'https://site.synthetic.test/uploads/first.png::$DATA',
'https://site.synthetic.test/uploads/first.png.',
'https://site.synthetic.test/uploads/nested/missing/../../private.txt',
'https://site.synthetic.test/uploads/nested/missing/%2e%2e/private.txt',
'https://site.synthetic.test/uploads/nested/missing/con.txt',
'https://site.synthetic.test/uploads/nested/missing/%3Cinvalid%3E.png',
'https://site.synthetic.test/uploads/nested/missing/file%2A.png',
'https://site.synthetic.test/uploads-evil/escape.txt',
'https://site.synthetic.test/private/do-not-read.txt',
'https://site.synthetic.test@evil.invalid/uploads/first.png',
@@ -227,15 +245,81 @@ namespace {
'file:///private/do-not-read.txt',
'/uploads/first.png',
] as $url) attachmentError($run($source($url)), 'REQUIRED_FILES_INVALID', 'local path/URL bypass rejected before network');
attachmentError($run($source('https://site.synthetic.test/uploads/missing.png')), 'REQUIRED_FILES_SOURCE_UNAVAILABLE', 'missing local files do not trigger network fallback');
$localMissingEvents = [];
$localMissing = $run($source('https://site.synthetic.test/uploads/missing.png'),
['on_upload' => static function (array $event) use (&$localMissingEvents): void { $localMissingEvents[] = $event; }]);
attachmentExpect(!empty($localMissing['ok']) && $localMissing['files'] === [] && $localMissing['uploaded_indices'] === []
&& $localMissing['skipped'] === [['index' => 1, 'kind' => 'document', 'reason' => 'not_found']], 'confirmed missing local file is skipped for a text-only response');
attachmentExpect($localMissingEvents === [['index' => 1, 'total' => 1, 'status' => 'skipped', 'reason' => 'not_found']], 'local preflight absence emits only safe skipped progress');
$missingParent = $run($source('https://site.synthetic.test/uploads/missing-parent/deeper/file.png'));
attachmentExpect(!empty($missingParent['ok']) && count($missingParent['skipped']) === 1, 'a safely absent parent establishes local absence without a download fallback');
attachmentError($run($source('https://site.synthetic.test/uploads/missing.png'), ['skip_missing' => false]),
'REQUIRED_FILES_SOURCE_UNAVAILABLE', 'server can explicitly require every local source');
attachmentError($run($source('https://site.synthetic.test/uploads/nested')), 'REQUIRED_FILES_SOURCE_UNAVAILABLE', 'existing directories remain unreadable attachments rather than missing');
attachmentError($run($source('https://site.synthetic.test/uploads/first.png/missing.png')),
'REQUIRED_FILES_SOURCE_UNAVAILABLE', 'non-directory parent cannot establish missing-file status');
$attachmentDeniedDirectory = realpath($root . '/public/uploads');
attachmentError($run($source('https://site.synthetic.test/uploads/inaccessible.png')),
'REQUIRED_FILES_SOURCE_UNAVAILABLE', 'inaccessible directory listing cannot prove a local source is missing');
$attachmentDeniedDirectory = null;
attachmentExpect($requests() === [], 'origin/path failures never upload or download');
if (@symlink($root . '/private/do-not-read.txt', $root . '/public/uploads/link.txt')) {
attachmentError($run($source('https://site.synthetic.test/uploads/link.txt')), 'REQUIRED_FILES_SOURCE_UNAVAILABLE', 'realpath rejects symlink escape');
}
if (@symlink($root . '/private', $root . '/public/uploads/outside-parent')) {
attachmentError($run($source('https://site.synthetic.test/uploads/outside-parent/missing.png')),
'REQUIRED_FILES_SOURCE_UNAVAILABLE', 'missing file below an escaping symlink remains unsafe');
}
if (@symlink($root . '/private/nonexistent.txt', $root . '/public/uploads/broken-link.txt')) {
attachmentError($run($source('https://site.synthetic.test/uploads/broken-link.txt')),
'REQUIRED_FILES_SOURCE_UNAVAILABLE', 'broken symlink is not a confirmed absent local file');
}
$clean();
$clear();
$localMixed = $run([$source('https://site.synthetic.test/uploads/missing.png')[0], $files[0],
$source('https://site.synthetic.test/uploads/nested/missing.png')[0], $files[2]]);
attachmentExpect(!empty($localMixed['ok']) && $localMixed['uploaded_indices'] === [2, 4]
&& array_column($localMixed['skipped'], 'index') === [1, 3]
&& array_column($requests(), 'kind') === ['upload', 'upload'], 'local missing-first and missing-middle sources preserve every remaining source without downloads');
$clean();
$clear();
$allLocalMissing = $run([$source('https://site.synthetic.test/uploads/missing.png')[0],
$source('https://site.synthetic.test/uploads/nested/missing.png')[0]]);
attachmentExpect(!empty($allLocalMissing['ok']) && $allLocalMissing['files'] === []
&& $allLocalMissing['uploaded_indices'] === [] && array_column($allLocalMissing['skipped'], 'index') === [1, 2]
&& $requests() === [], 'all missing local files return a safe empty upload list for text-only chat');
$videoEvents = [];
$videoMixed = $run([['type' => 'video', 'url' => 'https://untrusted.invalid/record.mp4'],
$source('https://site.synthetic.test/uploads/missing.png')[0], $files[0]],
['on_upload' => static function (array $event) use (&$videoEvents): void { $videoEvents[] = $event; }]);
attachmentExpect(!empty($videoMixed['ok']) && $videoMixed['uploaded_indices'] === [3]
&& array_column($videoMixed['skipped'], 'index') === [2]
&& array_column($videoEvents, 'index') === [2, 3, 3] && array_unique(array_column($videoEvents, 'total')) === [3],
'original input positions and total remain accurate when videos are excluded');
$clean();
$clear();
foreach ([['max_file_bytes' => 0], ['temp_root' => $root . '/public/unsafe-temp'], ['skip_missing' => 'true'],
['ca_file' => []], ['ca_file' => $root . '/missing-ca.pem'],
['resolved_hosts' => ['storage.synthetic.test' => ['127.0.0.1']]]] as $invalidOptions) {
attachmentError($run($source('https://site.synthetic.test/uploads/missing.png'), $invalidOptions),
'CONFIG_INVALID', 'all-missing local input cannot bypass invalid server configuration');
}
attachmentError($run([$source('https://site.synthetic.test/uploads/missing.png')[0],
$source('https://site.synthetic.test/uploads/missing-parent/../private.txt')[0]]),
'REQUIRED_FILES_INVALID', 'earlier confirmed absence cannot bypass unsafe later path validation');
attachmentExpect($requests() === [], 'invalid configuration and unsafe paths open no transport even with missing sources');
$clean();
$clear();
$remote = [['type' => 'image', 'url' => 'https://storage.synthetic.test/download.png']];
$withCa = $run($remote, ['ca_file' => $root . '/public/uploads/report.pdf']);
attachmentExpect(!empty($withCa['ok']) && count($attachmentCurlOptions) === 2
&& $attachmentCurlOptions[0][CURLOPT_CAINFO] === $root . '/public/uploads/report.pdf'
&& $attachmentCurlOptions[1][CURLOPT_CAINFO] === $root . '/public/uploads/report.pdf'
&& $attachmentCurlOptions[0][CURLOPT_SSL_VERIFYPEER] === true
&& $attachmentCurlOptions[1][CURLOPT_SSL_VERIFYHOST] === 2,
'explicit server CA bundle applies to download and upload while certificate verification stays enabled');
$clear(); $clean();
$downloaded = $run($remote);
attachmentExpect(!empty($downloaded['ok']) && count($requests()) === 2 && $requests()[0]['kind'] === 'download'
&& $requests()[0]['authorization'] === '' && $requests()[1]['authorization'] === 'Bearer fixture-only-key', 'remote bytes upload without sharing Dify key with storage');
@@ -266,13 +350,60 @@ namespace {
$missingPinnedSource = $run(array_merge($remote, [['type' => 'image',
'url' => 'https://storage.synthetic.test/missing-object']]),
['resolved_hosts' => ['storage.synthetic.test' => ['8.8.8.8']], 'resolve_host' => null]);
attachmentError($missingPinnedSource, 'REQUIRED_FILES_DOWNLOAD_FAILED', 'pinned missing source fails rather than dropping a mandatory file');
attachmentExpect(!empty($missingPinnedSource['ok']) && count($missingPinnedSource['files']) === 1
&& $missingPinnedSource['uploaded_indices'] === [1]
&& $missingPinnedSource['skipped'] === [['index' => 2, 'kind' => 'image', 'reason' => 'not_found']],
'pinned confirmed missing source preserves the first existing upload');
attachmentExpect(count($requests()) === 3 && array_column($requests(), 'kind') === ['download', 'upload', 'download']
&& $missingPinnedSource['failed_index'] === 2 && $missingPinnedSource['attachment_count'] === 2,
'a second-file 404 returns no partial files after the first upload');
&& count($missingPinnedSource['files']) === 1, 'a second-file 404 makes no upload for the missing bytes');
attachmentExpect($attachmentNativeDnsCalls === [], 'pinned 404 never retries through native DNS');
$clean();
$missingRemote = ['type' => 'image', 'url' => 'https://storage.synthetic.test/missing-object'];
$goneRemote = ['type' => 'document', 'url' => 'https://storage.synthetic.test/gone-object'];
foreach ([[[$missingRemote, $files[0], $files[2]], [2, 3], [1]],
[[$files[0], $missingRemote, $files[2]], [1, 3], [2]],
[[$missingRemote, $goneRemote], [], [1, 2]]] as [$input, $uploadedIndices, $skippedIndices]) {
$clear(); $missingEvents = [];
$mixedMissing = $run($input, ['on_upload' => static function (array $event) use (&$missingEvents): void { $missingEvents[] = $event; }]);
attachmentExpect(!empty($mixedMissing['ok']) && $mixedMissing['uploaded_indices'] === $uploadedIndices
&& count($mixedMissing['files']) === count($uploadedIndices)
&& array_column($mixedMissing['skipped'], 'index') === $skippedIndices,
'missing-first, missing-middle and all-missing preserve original upload mapping');
foreach ($mixedMissing['skipped'] as $skip) attachmentExpect(array_keys($skip) === ['index', 'kind', 'reason']
&& $skip['kind'] === $input[$skip['index'] - 1]['type'] && $skip['reason'] === 'not_found', 'skip metadata contains safe index/kind/reason only');
foreach ($missingEvents as $event) attachmentExpect($event['total'] === count($input)
&& array_keys($event) === ($event['status'] === 'skipped' ? ['index', 'total', 'status', 'reason'] : ['index', 'total', 'status']),
'mixed missing progress preserves manifest total and exposes no source details');
$uploadedRequests = array_values(array_filter($requests(), static fn (array $entry): bool => $entry['kind'] === 'upload'));
foreach ($uploadedIndices as $position => $originalIndex) {
attachmentExpect($mixedMissing['files'][$position]['upload_file_id'] === $uploadedRequests[$position]['id']
&& $mixedMissing['files'][$position]['type'] === $input[$originalIndex - 1]['type'], 'remaining references match the correct original attachment bytes and type');
}
$clean();
}
$clear();
$duplicatesWithMissing = $run([$missingRemote, $files[0], $missingRemote, $files[0], $goneRemote]);
attachmentExpect(!empty($duplicatesWithMissing['ok']) && $duplicatesWithMissing['uploaded_indices'] === [2, 4]
&& array_column($duplicatesWithMissing['skipped'], 'index') === [1, 3, 5]
&& $duplicatesWithMissing['files'][0]['upload_file_id'] === $duplicatesWithMissing['files'][1]['upload_file_id']
&& array_column($requests(), 'kind') === ['download', 'upload', 'download'], 'duplicate missing sources are fetched once and every original position is retained deterministically');
$clean();
$clear();
attachmentError($run([$missingRemote], ['skip_missing' => false]), 'REQUIRED_FILES_DOWNLOAD_FAILED', 'server can explicitly retain strict remote missing-file policy');
foreach (['/denied-object', '/broken-object'] as $path) {
$clear();
attachmentError($run([['type' => 'image', 'url' => 'https://storage.synthetic.test' . $path]]),
'REQUIRED_FILES_DOWNLOAD_FAILED', '403 and 500 cannot be skipped as absent attachments');
attachmentExpect(array_column($requests(), 'kind') === ['download'], 'non-missing remote error is never uploaded');
$clean();
}
$clear(); $start = microtime(true);
attachmentError($run([['type' => 'image', 'url' => 'https://storage.synthetic.test/stalled-missing-object']], [], null,
'synthetic-user', null, 1), 'REQUIRED_FILES_TIMEOUT', '404 headers followed by a stalled transfer remain a timeout');
attachmentExpect(microtime(true) - $start < 1.4 && array_column($requests(), 'kind') === ['download'], 'incomplete 404 response is never accepted as confirmed missing');
$clean(); usleep(300000);
$clear();
$pinnedV6 = $run($remote, ['resolved_hosts' => ['storage.synthetic.test' => ['2606:4700:4700::1111']], 'resolve_host' => null]);
attachmentExpect(!empty($pinnedV6['ok']) && $attachmentNativeDnsCalls === []
@@ -381,7 +512,7 @@ namespace {
attachmentExpect($requests() === [], 'oversize local file never uploads');
$clean();
foreach (['fail', 'bad-id', 'large-response'] as $mode) {
foreach (['fail', 'missing-upload', 'bad-id', 'large-response'] as $mode) {
$clear(); $failedEvents = [];
$failed = $run($files, ['on_upload' => static function (array $event) use (&$failedEvents): void { $failedEvents[] = $event; }], null, 'synthetic-user',
'http://127.0.0.1:' . $port . '/' . $mode . '/v1/chat-messages');
@@ -395,6 +526,14 @@ namespace {
attachmentExpect(count($requests()) === 2 && $midway['failed_index'] === 2 && $midway['attachment_count'] === 3, 'failure identifies safe index and never skips ahead to remaining attachment');
$clean();
$clear();
$failedAfterSkip = $run([$missingRemote, $files[0], $files[2]], [], null,
'synthetic-user', 'http://127.0.0.1:' . $port . '/fail/v1/chat-messages');
attachmentError($failedAfterSkip, 'REQUIRED_FILES_UPLOAD_FAILED', 'upload error after a confirmed absence still fails without partial files');
attachmentExpect($failedAfterSkip['skipped'] === [['index' => 1, 'kind' => 'image', 'reason' => 'not_found']]
&& $failedAfterSkip['failed_index'] === 2 && $failedAfterSkip['attachment_count'] === 3,
'fatal response preserves safe skipped metadata and the actual failed index');
$clean();
$clear();
attachmentError($run($files, [], static fn (): bool => true), 'AI_ASSISTANT_CANCELLED', 'cancellation before preparation opens no transport');
attachmentExpect($requests() === [], 'early cancellation has no network side effects');
$cancelDuringCopy = false;
@@ -402,6 +541,17 @@ namespace {
static function () use (&$cancelDuringCopy): bool { return $cancelDuringCopy; }), 'AI_ASSISTANT_CANCELLED', 'cancellation during local copy cleans its new temporary file');
attachmentExpect($requests() === [], 'cancelled local preparation never uploads');
$clean();
$clear(); $cancelAfterSkip = false; $cancelledSkipEvents = [];
$cancelledAfterSkip = $run([$missingRemote, $files[0]], ['on_upload' => static function (array $event) use (&$cancelAfterSkip, &$cancelledSkipEvents): void {
$cancelledSkipEvents[] = $event;
if ($event['status'] === 'skipped') $cancelAfterSkip = true;
}], static function () use (&$cancelAfterSkip): bool { return $cancelAfterSkip; });
attachmentError($cancelledAfterSkip, 'AI_ASSISTANT_CANCELLED', 'cancellation immediately after skipped progress cannot continue into remaining uploads');
attachmentExpect(array_column($requests(), 'kind') === ['download']
&& $cancelledAfterSkip['skipped'] === [['index' => 1, 'kind' => 'image', 'reason' => 'not_found']]
&& array_column($cancelledSkipEvents, 'status') === ['uploading', 'skipped'],
'cancelled request retains known skipped source and opens no subsequent upload');
$clean();
foreach (['slow-headers', 'slow-body'] as $mode) {
$clear(); $start = microtime(true);
$cancelled = $run([$files[0]], [], static fn (): bool => microtime(true) - $start > 0.15, 'synthetic-user',
@@ -431,7 +581,7 @@ namespace {
attachmentExpect($requests() === [], 'unsafe temp configuration opens no connection');
attachmentError($run([['type' => 'image', 'transfer_method' => 'local_file', 'url' => $files[0]['url']]]), 'REQUIRED_FILES_INVALID', 'callers cannot inject upload IDs from other requests');
$clean();
echo "Dify required attachment upload: binary multipart, all sources, origins/paths/DNS/pinning/redirects/limits, isolation, cancellation/deadline, cleanup: OK\n";
echo "Dify required attachment upload: binary multipart, confirmed-missing skips and original indices, origins/paths/DNS/pinning/redirects/limits, isolation, cancellation/deadline, cleanup: OK\n";
} finally {
if (is_resource($process)) { proc_terminate($process); proc_close($process); }
attachmentRemoveTree($root, $root);
@@ -14,13 +14,26 @@ namespace app\common\service {
if ($GLOBALS['uploadTransportScenario'] === 'cancel') return ['ok' => false, 'error_code' => 'AI_ASSISTANT_CANCELLED', 'error' => '合成取消'];
if ($GLOBALS['uploadTransportScenario'] === 'upload-failed') return ['ok' => false, 'error_code' => 'REQUIRED_FILES_UPLOAD_FAILED', 'error' => '合成上传失败'];
$result = [];
$indices = []; $skipped = [];
foreach ($files as $index => $file) {
$scenario = $GLOBALS['uploadTransportScenario'];
$missing = $scenario === 'all-missing' || (in_array($scenario, ['missing-first', 'missing-inputs', 'failed-after-skip', 'invalid-skip-receipt'], true) && $index === 0)
|| ($scenario === 'missing-middle' && $index === 1);
if ($missing) {
$skipped[] = ['index' => $index + 1, 'kind' => $file['type'], 'reason' => 'not_found'];
if (is_callable($options['on_upload'] ?? null)) ($options['on_upload'])(['index' => $index + 1,
'total' => count($files), 'status' => 'skipped', 'reason' => 'not_found']);
continue;
}
if ($scenario === 'failed-after-skip') return ['ok' => false, 'error_code' => 'REQUIRED_FILES_UPLOAD_FAILED', 'error' => '合成上传失败', 'skipped' => $skipped];
foreach (['uploading', 'uploaded'] as $status) {
if (is_callable($options['on_upload'] ?? null)) ($options['on_upload'])(['index' => $index + 1, 'total' => count($files), 'status' => $status]);
}
$result[] = ['type' => $file['type'], 'transfer_method' => 'local_file', 'upload_file_id' => 'fixture-upload-' . ($index + 1)];
$indices[] = $index + 1;
}
return ['ok' => true, 'files' => $result, 'attachment_transport' => 'uploaded_files'];
if ($GLOBALS['uploadTransportScenario'] === 'invalid-skip-receipt') $indices[0] = 1;
return ['ok' => true, 'files' => $result, 'uploaded_indices' => $indices, 'skipped' => $skipped, 'attachment_transport' => 'uploaded_files'];
}
}
function curl_init(): \stdClass { return new \stdClass(); }
@@ -29,7 +42,7 @@ namespace app\common\service {
$payload = json_decode($handle->options[CURLOPT_POSTFIELDS], true);
$GLOBALS['uploadTransportRequests'][] = $payload;
$stream = ($payload['response_mode'] ?? '') === 'streaming';
$handle->code = $GLOBALS['uploadTransportScenario'] === 'inputs' && !empty($payload['inputs']) ? 400 : 200;
$handle->code = in_array($GLOBALS['uploadTransportScenario'], ['inputs', 'missing-inputs'], true) && !empty($payload['inputs']) ? 400 : 200;
($handle->options[CURLOPT_HEADERFUNCTION])($handle, 'HTTP/1.1 ' . $handle->code . " Synthetic\r\n");
if ($handle->code !== 200) {
$body = json_encode(['code' => 'invalid_param', 'message' => 'synthetic_undeclared_input input variable invalid']);
@@ -58,7 +71,8 @@ namespace {
['type' => 'image', 'url' => 'https://synthetic.invalid/two.png'],
['type' => 'document', 'url' => 'https://synthetic.invalid/three.pdf']];
$options = ['required_files' => true, 'knowledge_mode' => 'evidence', 'required_sections' => AiRequiredSource::manifest([])];
foreach ([false, true] as $stream) foreach (['success', 'inputs', 'upload-failed', 'cancel'] as $scenario) {
foreach ([false, true] as $stream) foreach (['success', 'inputs', 'upload-failed', 'cancel',
'missing-first', 'missing-middle', 'all-missing', 'missing-inputs', 'failed-after-skip', 'invalid-skip-receipt'] as $scenario) {
$uploadTransportScenario = $scenario; $uploadTransportStages = []; $uploadTransportRequests = []; $events = [];
$opts = $options + ['on_progress' => static function (array $event) use (&$events): void { $events[] = $event; }];
$result = $stream ? DifyChatService::streamChat('qwen', ['synthetic_undeclared_input' => 'value'], '合成资料', 'fixture-same-user',
@@ -67,18 +81,38 @@ namespace {
uploadTransportExpect(count($uploadTransportStages) === 1, 'all required files are staged once per request');
uploadTransportExpect($uploadTransportStages[0]['user'] === 'fixture-same-user' && count($uploadTransportStages[0]['files']) === 3,
'upload and chat share end user and complete source set');
if (in_array($scenario, ['upload-failed', 'cancel'], true)) {
if (in_array($scenario, ['upload-failed', 'cancel', 'failed-after-skip', 'invalid-skip-receipt'], true)) {
uploadTransportExpect(empty($result['ok']) && $uploadTransportRequests === [], 'failed or cancelled upload never starts model generation');
uploadTransportExpect($result['error_code'] === ($scenario === 'cancel' ? 'AI_ASSISTANT_CANCELLED' : 'REQUIRED_FILES_UPLOAD_FAILED'),
'upload failure retains actual cause');
if ($scenario === 'failed-after-skip') uploadTransportExpect($result['items'][0]['status'] === 'skipped'
&& $result['items'][0]['reason'] === 'not_found' && $result['items'][1]['status'] === 'failed',
'a later real failure does not relabel a known nonexistent source');
continue;
}
uploadTransportExpect(!empty($result['ok']) && $result['attachment_transfer'] === 'uploaded_files', 'final receipt identifies actual uploads');
uploadTransportExpect(count($uploadTransportRequests) === ($scenario === 'inputs' ? 2 : 1), 'bounded input compatibility remains intact');
$missingCount = $scenario === 'all-missing' ? 3 : (in_array($scenario, ['missing-first', 'missing-middle', 'missing-inputs'], true) ? 1 : 0);
uploadTransportExpect(count($uploadTransportRequests) === (in_array($scenario, ['inputs', 'missing-inputs'], true) ? 2 : 1), 'bounded input compatibility remains intact');
uploadTransportExpect($result['submitted_attachment_count'] === 3 - $missingCount && $result['skipped_attachment_count'] === $missingCount
&& $result['attachment_count'] === 3 && count($result['items']) === 3
&& $result['transmitted_file_count'] === 3 - $missingCount, 'final manifest distinguishes original, submitted and skipped counts');
uploadTransportExpect(count($result['required_sections']) === 13 && array_unique(array_column($result['required_sections'], 'status')) === ['submitted'],
'missing attachments do not block any of the thirteen clinical text sections');
foreach ($result['items'] as $item) {
uploadTransportExpect(in_array($item['status'], ['submitted', 'skipped'], true), 'no false failed item on a completed response');
if ($item['status'] === 'skipped') uploadTransportExpect($item['reason'] === 'not_found', 'skip reason is explicit');
}
foreach ($uploadTransportRequests as $payload) {
uploadTransportExpect($payload['user'] === 'fixture-same-user' && count($payload['files']) === 3, 'every generation attempt retains all uploaded files');
uploadTransportExpect(array_column($payload['files'], 'transfer_method') === ['local_file', 'local_file', 'local_file'], 'Dify gets upload IDs, not resource URLs');
uploadTransportExpect(!array_key_exists('url', $payload['files'][0]) && $payload['files'][0]['upload_file_id'] === 'fixture-upload-1', 'original URL never leaks into uploaded wire files');
uploadTransportExpect($payload['user'] === 'fixture-same-user' && count($payload['files']) === 3 - $missingCount, 'every generation attempt retains all available uploaded files');
uploadTransportExpect(array_column($payload['files'], 'transfer_method') === array_fill(0, 3 - $missingCount, 'local_file'), 'Dify gets upload IDs, not resource URLs');
foreach ($payload['files'] as $file) uploadTransportExpect(!array_key_exists('url', $file), 'original URLs never leak into uploaded wire files');
if ($missingCount > 0) uploadTransportExpect(substr_count($payload['query'], '【附件可用性说明】') === 1
&& str_contains($payload['query'], '源文件不存在') && str_contains($payload['query'], '继续依据')
&& !str_contains($payload['query'], 'synthetic.invalid/'), 'model receives one safe missing-source notice and can continue');
}
foreach ($events as $event) if (in_array($event['status'] ?? '', ['submitting', 'submitted'], true)) {
uploadTransportExpect(count(array_filter($event['items'], static fn (array $item): bool => $item['status'] === 'skipped')) === $missingCount,
'transport notifications preserve every missing item across input retries');
}
uploadTransportExpect(count(array_filter($events, static fn (array $event): bool => ($event['status'] ?? '') === 'uploading')) >= 3,
'safe per-file upload progress reaches the desktop');