更新
This commit is contained in:
@@ -3,6 +3,14 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service {
|
||||
/** Never query real DNS: reproduce the unsafe proxy answer deterministically. */
|
||||
function dns_get_record(string $host, int $type): array
|
||||
{
|
||||
$GLOBALS['attachmentNativeDnsCalls'][] = $host;
|
||||
if ($host !== 'storage.synthetic.test') throw new \RuntimeException('Unexpected synthetic DNS host');
|
||||
return [['ip' => '198.18.0.1']];
|
||||
}
|
||||
|
||||
/** Route synthetic public storage to loopback AFTER recording real security options. */
|
||||
function curl_setopt_array($handle, array $options): bool
|
||||
{
|
||||
@@ -20,6 +28,12 @@ namespace app\common\service {
|
||||
}
|
||||
|
||||
namespace {
|
||||
/** Config-file regression tests read only this synthetic environment map. */
|
||||
function env(string $name, $default = null)
|
||||
{
|
||||
return $GLOBALS['attachmentSyntheticEnv'][$name] ?? $default;
|
||||
}
|
||||
|
||||
// This same file is a loopback-only fixture router. It never loads application config.
|
||||
if (PHP_SAPI === 'cli-server') {
|
||||
$root = (string) getenv('DIFY_ATTACHMENT_FIXTURE_ROOT');
|
||||
@@ -52,6 +66,7 @@ namespace {
|
||||
return;
|
||||
}
|
||||
$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 === '/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; }
|
||||
@@ -111,6 +126,7 @@ namespace {
|
||||
$port = (int) substr($address, strrpos($address, ':') + 1);
|
||||
$attachmentFixturePort = $port;
|
||||
$attachmentCurlOptions = [];
|
||||
$attachmentNativeDnsCalls = [];
|
||||
$process = proc_open([PHP_BINARY, '-S', '127.0.0.1:' . $port, __FILE__],
|
||||
[['pipe', 'r'], ['file', $root . '/fixture-output.log', 'ab'], ['file', $root . '/fixture-error.log', 'ab']],
|
||||
$pipes, $root, ['DIFY_ATTACHMENT_FIXTURE_ROOT' => $root,
|
||||
@@ -139,8 +155,8 @@ namespace {
|
||||
if (!is_file($root . '/requests.jsonl')) return [];
|
||||
return array_map(static fn (string $line): array => json_decode($line, true), file($root . '/requests.jsonl', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
|
||||
};
|
||||
$clear = static function () use ($root, &$attachmentCurlOptions): void {
|
||||
file_put_contents($root . '/requests.jsonl', ''); $attachmentCurlOptions = [];
|
||||
$clear = static function () use ($root, &$attachmentCurlOptions, &$attachmentNativeDnsCalls): void {
|
||||
file_put_contents($root . '/requests.jsonl', ''); $attachmentCurlOptions = []; $attachmentNativeDnsCalls = [];
|
||||
};
|
||||
$clean = static function () use ($root): void {
|
||||
attachmentExpect((glob($root . '/private/uploads/required-*') ?: []) === [], 'temporary files are removed after every terminal outcome');
|
||||
@@ -231,6 +247,120 @@ namespace {
|
||||
&& $attachmentCurlOptions[0][CURLOPT_RESOLVE] === ['storage.synthetic.test:443:8.8.8.8'], 'a rebound DNS answer cannot replace the pinned validated address');
|
||||
$clean();
|
||||
|
||||
$clear(); $dnsCalls = 0;
|
||||
$pinned = $run($remote, ['resolved_hosts' => ['Storage.Synthetic.Test' => ['8.8.8.8', '2606:4700:4700::1111']],
|
||||
'resolve_host' => static function () use (&$dnsCalls): array { ++$dnsCalls; return ['198.18.0.1']; }]);
|
||||
attachmentExpect(!empty($pinned['ok']) && $dnsCalls === 0 && $attachmentNativeDnsCalls === []
|
||||
&& count($requests()) === 2, 'exact server pin bypasses both unsafe callable and native DNS');
|
||||
attachmentExpect($attachmentCurlOptions[0][CURLOPT_RESOLVE] === ['storage.synthetic.test:443:8.8.8.8']
|
||||
&& $attachmentCurlOptions[0][CURLOPT_URL] === $remote[0]['url']
|
||||
&& $attachmentCurlOptions[0][CURLOPT_SSL_VERIFYPEER] === true
|
||||
&& $attachmentCurlOptions[0][CURLOPT_SSL_VERIFYHOST] === 2
|
||||
&& $attachmentCurlOptions[0][CURLOPT_PROXY] === ''
|
||||
&& $attachmentCurlOptions[0][CURLOPT_FOLLOWLOCATION] === false,
|
||||
'pins normalize exact hostname and preserve original URL/TLS hostname, no proxies or redirects');
|
||||
attachmentExpect(!isset($attachmentCurlOptions[1][CURLOPT_RESOLVE]), 'storage pin never alters the upload endpoint');
|
||||
$clean();
|
||||
|
||||
$clear();
|
||||
$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(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');
|
||||
attachmentExpect($attachmentNativeDnsCalls === [], 'pinned 404 never retries through native DNS');
|
||||
$clean();
|
||||
|
||||
$clear();
|
||||
$pinnedV6 = $run($remote, ['resolved_hosts' => ['storage.synthetic.test' => ['2606:4700:4700::1111']], 'resolve_host' => null]);
|
||||
attachmentExpect(!empty($pinnedV6['ok']) && $attachmentNativeDnsCalls === []
|
||||
&& $attachmentCurlOptions[0][CURLOPT_RESOLVE] === ['storage.synthetic.test:443:[2606:4700:4700::1111]'],
|
||||
'validated global IPv6 pin uses bracketed CURLOPT_RESOLVE without DNS');
|
||||
$clean();
|
||||
|
||||
$clear(); $dnsCalls = 0;
|
||||
$unpinned = $run($remote, ['resolved_hosts' => ['other.synthetic.test' => ['1.1.1.1']],
|
||||
'resolve_host' => static function () use (&$dnsCalls): array { ++$dnsCalls; return ['8.8.8.8']; }]);
|
||||
attachmentExpect(!empty($unpinned['ok']) && $dnsCalls === 1 && $attachmentNativeDnsCalls === [],
|
||||
'pin for another hostname does not replace the supported unpinned resolver');
|
||||
$clean();
|
||||
|
||||
$clear();
|
||||
attachmentError($run($remote, ['resolve_host' => null]), 'REQUIRED_FILES_SOURCE_UNTRUSTED',
|
||||
'without a pin the unsafe native proxy-DNS range remains forbidden');
|
||||
attachmentExpect($attachmentNativeDnsCalls === ['storage.synthetic.test'] && $requests() === []
|
||||
&& $attachmentCurlOptions === [], 'unsafe native DNS opens no connection');
|
||||
$clear();
|
||||
attachmentError($run($remote, ['storage_origins' => [],
|
||||
'resolved_hosts' => ['storage.synthetic.test' => ['8.8.8.8']]]), 'REQUIRED_FILES_SOURCE_UNTRUSTED',
|
||||
'a DNS pin cannot grant access to an untrusted storage origin');
|
||||
attachmentExpect($requests() === [] && $attachmentNativeDnsCalls === [], 'origin check precedes pinned download');
|
||||
$clean();
|
||||
|
||||
foreach ([
|
||||
null, false, 'not-a-map', [['8.8.8.8']],
|
||||
['storage.synthetic.test' => []],
|
||||
['storage.synthetic.test' => '8.8.8.8'],
|
||||
['storage.synthetic.test' => ['address' => '8.8.8.8']],
|
||||
['storage.synthetic.test' => [8]],
|
||||
['storage.synthetic.test' => ['127.0.0.1']],
|
||||
['storage.synthetic.test' => ['198.18.0.1']],
|
||||
['storage.synthetic.test' => ['8.8.8.8', '169.254.169.254']],
|
||||
['storage.synthetic.test' => ['2606:4700:4700::1111', '::1']],
|
||||
['storage.synthetic.test' => [' 8.8.8.8']],
|
||||
['storage.synthetic.test' => ['not-an-address']],
|
||||
['https://storage.synthetic.test' => ['8.8.8.8']],
|
||||
['storage.synthetic.test:443' => ['8.8.8.8']],
|
||||
['storage.synthetic.test/path' => ['8.8.8.8']],
|
||||
['*.synthetic.test' => ['8.8.8.8']],
|
||||
['storage.synthetic.test.' => ['8.8.8.8']],
|
||||
[' storage.synthetic.test' => ['8.8.8.8']],
|
||||
['storage..synthetic.test' => ['8.8.8.8']],
|
||||
['-storage.synthetic.test' => ['8.8.8.8']],
|
||||
[str_repeat('a', 64) . '.synthetic.test' => ['8.8.8.8']],
|
||||
['8.8.8.8' => ['1.1.1.1']],
|
||||
['0x7f000001' => ['8.8.8.8']],
|
||||
['storage.synthetic.test' => ['8.8.8.8'], 'Storage.Synthetic.Test' => ['1.1.1.1']],
|
||||
] as $pins) {
|
||||
$clear(); $dnsCalls = 0;
|
||||
$invalidPin = $run($remote, ['resolved_hosts' => $pins,
|
||||
'resolve_host' => static function () use (&$dnsCalls): array { ++$dnsCalls; return ['8.8.8.8']; }]);
|
||||
attachmentError($invalidPin, 'CONFIG_INVALID', 'malformed/private/mixed/empty pins fail closed');
|
||||
attachmentExpect($dnsCalls === 0 && $attachmentNativeDnsCalls === [] && $requests() === []
|
||||
&& $attachmentCurlOptions === [], 'invalid pins never fall back to either DNS resolver or open transport');
|
||||
attachmentExpect(!str_contains(json_encode($invalidPin), 'storage.synthetic.test')
|
||||
&& !str_contains(json_encode($invalidPin), '8.8.8.8'), 'invalid pin response never discloses configured host/IP');
|
||||
$clean();
|
||||
}
|
||||
|
||||
// Exercise the real config decoder with synthetic INI text, never a .env file.
|
||||
$attachmentSyntheticEnv = [];
|
||||
$defaultConfig = require dirname(__DIR__) . '/config/prescription_ai.php';
|
||||
attachmentExpect($defaultConfig['required_attachment_upload']['resolved_hosts'] === [], 'DNS pins default to an empty map');
|
||||
$syntheticIni = "[PRESCRIPTION_AI]\nREQUIRED_ATTACHMENT_RESOLVED_HOSTS = {\"storage.synthetic.test\":[\"8.8.8.8\"]}\n";
|
||||
$parsedIni = parse_ini_string($syntheticIni, true, INI_SCANNER_RAW);
|
||||
$attachmentSyntheticEnv = ['prescription_ai.REQUIRED_ATTACHMENT_RESOLVED_HOSTS'
|
||||
=> $parsedIni['PRESCRIPTION_AI']['REQUIRED_ATTACHMENT_RESOLVED_HOSTS']];
|
||||
$pinConfig = require dirname(__DIR__) . '/config/prescription_ai.php';
|
||||
$clear();
|
||||
$configuredPin = $run($remote, ['resolved_hosts' => $pinConfig['required_attachment_upload']['resolved_hosts'], 'resolve_host' => null]);
|
||||
attachmentExpect(!empty($configuredPin['ok']) && $attachmentNativeDnsCalls === []
|
||||
&& $attachmentCurlOptions[0][CURLOPT_RESOLVE] === ['storage.synthetic.test:443:8.8.8.8'],
|
||||
'documented unquoted JSON INI syntax decodes to a working exact pin');
|
||||
$clean();
|
||||
foreach (['[', 'null', '[]', 'true', '"not-a-map"', '{"storage.synthetic.test":{}}', '{"storage.synthetic.test":[]}'] as $json) {
|
||||
$attachmentSyntheticEnv = ['prescription_ai.REQUIRED_ATTACHMENT_RESOLVED_HOSTS' => $json];
|
||||
$invalidConfig = require dirname(__DIR__) . '/config/prescription_ai.php';
|
||||
$clear();
|
||||
attachmentError($run($remote, ['resolved_hosts' => $invalidConfig['required_attachment_upload']['resolved_hosts'],
|
||||
'resolve_host' => null]), 'CONFIG_INVALID', 'invalid JSON/map environment configuration cannot disappear into default DNS');
|
||||
attachmentExpect($attachmentNativeDnsCalls === [] && $requests() === [], 'invalid environment pin config never performs DNS/network fallback');
|
||||
$clean();
|
||||
}
|
||||
$attachmentSyntheticEnv = [];
|
||||
|
||||
foreach (['127.0.0.1', '10.0.0.1', '172.16.0.1', '192.168.1.1', '169.254.169.254', '100.100.100.200',
|
||||
'0.0.0.0', '224.0.0.1', '::1', 'fc00::1', 'fe80::1', '::ffff:127.0.0.1', '64:ff9b::7f00:1', '2002:7f00:1::', '2001:db8::1'] as $ip) {
|
||||
$clear();
|
||||
|
||||
Reference in New Issue
Block a user