This commit is contained in:
Your Name
2026-10-23 11:01:21 +08:00
parent a3d2d3170d
commit f78f46b28a
4 changed files with 183 additions and 10 deletions
@@ -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 {