This commit is contained in:
Your Name
2026-10-23 11:17:58 +08:00
parent f78f46b28a
commit 86476920cf
10 changed files with 709 additions and 94 deletions
@@ -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);