diff --git a/app/scripts/run_local_workstation.py b/app/scripts/run_local_workstation.py index 45fa1dfcf..77feb134a 100644 --- a/app/scripts/run_local_workstation.py +++ b/app/scripts/run_local_workstation.py @@ -49,6 +49,10 @@ def local_config_type(api: str): def main() -> int: + # Select the native backend before Qt creates network objects or scans TLS + # plugins. Selecting it later can load conflicting OpenSSL DLLs on Windows. + if sys.platform == "win32": + os.environ["QT_TLS_BACKEND"] = "schannel" root = Path(__file__).resolve().parents[1] api = os.environ.get("DOCTOR_LOCAL_API_URL", "https://css.zhenyangtang.com.cn") validate_local_url(api) diff --git a/server/app/common/service/DifyRequiredAttachmentUpload.php b/server/app/common/service/DifyRequiredAttachmentUpload.php index 7141fed6b..5a350ffe8 100644 --- a/server/app/common/service/DifyRequiredAttachmentUpload.php +++ b/server/app/common/service/DifyRequiredAttachmentUpload.php @@ -20,8 +20,9 @@ final class DifyRequiredAttachmentUpload /** * Options: local_origin/local_origins, storage_origins, public_root, temp_root, - * max_file_bytes, on_upload(array{index:int,total:int,status:string}). resolve_host - * is an optional server-owned DNS resolver (primarily for deterministic tests). + * max_file_bytes, on_upload(array{index:int,total:int,status:string}), resolved_hosts + * (exact hostname => nonempty public IP list). Pins take precedence over the optional + * server-owned resolve_host callable, then native DNS for unpinned hosts only. * No file ID cache survives this call, including between different Dify users. * * @return array{ok:bool,files?:array,error_code?:string,error?:string} @@ -138,7 +139,34 @@ final class DifyRequiredAttachmentUpload try { $configured = \config('prescription_ai.required_attachment_upload', []); } catch (\Throwable $ignored) { $configured = []; } } - return array_replace(is_array($configured) ? $configured : [], $options); + $options = array_replace(is_array($configured) ? $configured : [], $options); + $options['resolved_hosts'] = self::resolvedHosts(array_key_exists('resolved_hosts', $options) + ? $options['resolved_hosts'] : []); + return $options; + } + + /** Server-owned exact DNS pins never grant an origin access or permit private IPs. */ + private static function resolvedHosts($hosts): array + { + if (!is_array($hosts) || ($hosts !== [] && array_is_list($hosts))) self::fail('CONFIG_INVALID'); + $result = []; + foreach ($hosts as $host => $addresses) { + if (!is_string($host)) self::fail('CONFIG_INVALID'); + $host = strtolower($host); + // DNS names only, with bounded labels and an alphabetic final label. + // Reject URL/port/wildcard/IP spellings that cURL could interpret differently. + if (strlen($host) > 253 + || !preg_match('/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z](?:[a-z0-9-]{0,61}[a-z0-9])?$/D', $host) + || array_key_exists($host, $result) + || !is_array($addresses) || $addresses === [] || !array_is_list($addresses)) { + self::fail('CONFIG_INVALID'); + } + foreach ($addresses as $address) { + if (!is_string($address) || !self::publicIp($address)) self::fail('CONFIG_INVALID'); + } + $result[$host] = $addresses; + } + return $result; } private static function plan(array $files): array @@ -264,6 +292,10 @@ final class DifyRequiredAttachmentUpload $host = trim($parts['host'], '[]'); if (filter_var($host, FILTER_VALIDATE_IP) !== false) { $addresses = [$host]; + } elseif (array_key_exists($host, $options['resolved_hosts'])) { + // Validated server pins win over every resolver; an invalid configured + // map has already failed before any transfer, never fallen back to DNS. + $addresses = $options['resolved_hosts'][$host]; } elseif (is_callable($options['resolve_host'] ?? null)) { $addresses = ($options['resolve_host'])($host); } else { diff --git a/server/config/prescription_ai.php b/server/config/prescription_ai.php index f44954d45..a70ff8b73 100644 --- a/server/config/prescription_ai.php +++ b/server/config/prescription_ai.php @@ -29,15 +29,22 @@ return [ 'https://admin.zhenyangtang.com.cn,https://css.zhenyangtang.com.cn,http://admin.zhenyangtang.com.cn,http://css.zhenyangtang.com.cn' ))))), 'max_file_bytes' => (int) env('prescription_ai.REQUIRED_ATTACHMENT_MAX_BYTES', 20 * 1024 * 1024), + // Optional server-owned DNS pins for remote attachment storage only. Exact + // hostname => nonempty public-IP array; origins/TLS checks still apply. + // Keep invalid JSON/root types as null so the uploader fails closed. + 'resolved_hosts' => (static function () { + $decoded = json_decode((string) env('prescription_ai.REQUIRED_ATTACHMENT_RESOLVED_HOSTS', '{}')); + return $decoded instanceof \stdClass ? get_object_vars($decoded) : null; + })(), ], // Reuse only evidence for an identical, freshly authorized source; 0 disables. 'context_compaction_cache_ttl' => (int) env('prescription_ai.CONTEXT_COMPACTION_CACHE_TTL', 300), /** - * 单次请求可随附的附件总数上限。Dify 应用的 file_upload.number_limits 超限时 - * 直接返回 400 invalid_param 拒绝整单,患者纵向资料的附件数量又不可控, - * 因此这里必须与上游应用配置保持一致(默认 3),超出的附件改以清单形式送达。 - * 各应用的上限不同时用下面 models 里的 max_files 覆盖;核对方式是只读调用 - * 该应用的 /parameters,读取 file_upload.number_limits。 + * 仅供历史/非必需附件路径使用的单次附件数量上限(默认 3);这些路径可将 + * 超出附件改为清单。required_files 路径始终提交全部必需附件,上传、数量或 + * 类型被上游拒绝即整次失败,绝不靠减少附件或只发清单生成不完整报告。 + * 各应用可用 models 中的 max_files 覆盖非必需附件上限;上游实际数量限制 + * 应只读核对 /parameters 的 file_upload.number_limits。 */ 'max_files' => (int) env( 'prescription_ai.MAX_FILES', diff --git a/server/tests/DifyRequiredAttachmentUploadTest.php b/server/tests/DifyRequiredAttachmentUploadTest.php index 0a403c67c..072f7f03c 100644 --- a/server/tests/DifyRequiredAttachmentUploadTest.php +++ b/server/tests/DifyRequiredAttachmentUploadTest.php @@ -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 'fixture-private-path-url-key-body'; 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();