-
Notifications
You must be signed in to change notification settings - Fork 151
fix(server): allow user-owned AI/WebDAV/RPC configs to use private IPs (fixes #244) #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ export interface ProxyRequestOptions { | |
| timeout?: number; | ||
| proxyConfig?: ProxyConfig | null; | ||
| preserveRawResponse?: boolean; | ||
| /** 放行回环/私有网段(仅用于用户自有配置来源的 URL)。 */ | ||
| allowPrivate?: boolean; | ||
| } | ||
|
|
||
| export interface ProxyResponse { | ||
|
|
@@ -30,40 +32,93 @@ export interface ProxyResponse { | |
| const BLOCKED_HOSTS = new Set(['localhost', '127.0.0.1', '::1', '0.0.0.0', '169.254.169.254']); | ||
| const PRIVATE_IP_PATTERNS = [/^10\./, /^172\.(1[6-9]|2\d|3[01])\./, /^192\.168\./]; | ||
|
|
||
| export function validateUrl(rawUrl: string): void { | ||
| export interface ValidateUrlOptions { | ||
| /** 宽松模式:放行回环地址与私有网段。仅用于「用户主动保存的自行配置」所来源的 URL | ||
| * (如 AI / WebDAV / 下载目标),不应作用于任意或半可信的输入。 */ | ||
| allowPrivate?: boolean; | ||
| } | ||
|
|
||
| /** 归一化 hostname:去掉 IPv6 方括号、去掉结尾点号(防 `localhost.`/`127.0.0.1.` 绕过), | ||
| * 并把 IPv4 映射型 IPv6(如 [::ffff:169.254.169.254])还原为对应的 IPv4 字符串, | ||
| * 使 SSRF 过滤看到真实的目标地址。 */ | ||
| function normalizeHostname(hostname: string): string { | ||
| // Strip trailing dot first to prevent SSRF bypasses (e.g. "localhost." or "127.0.0.1.") | ||
| const h = hostname.replace(/\.$/, '').replace(/^\[(.+)\]$/, '$1').toLowerCase(); | ||
| // Handle hybrid IPv4-mapped IPv6 written with dotted decimals (::ffff:169.254.169.254) | ||
| if (h.startsWith('::ffff:') && h.includes('.')) { | ||
| return h.slice(7); | ||
| } | ||
| // Handle IPv4-mapped IPv6 written in hex form (::ffff:a9fe:a9fe) | ||
| const mapped = h.match(/^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/); | ||
| if (mapped) { | ||
| const octets = [mapped[1], mapped[2]].flatMap((hex) => { | ||
| const v = parseInt(hex, 16); | ||
| return [(v >>> 8) & 0xff, v & 0xff]; | ||
| }); | ||
| return octets.join('.'); | ||
| } | ||
| return h; | ||
| } | ||
|
|
||
| /** IPv6 私有/本地网段前缀:唯一本地地址 fc00::/7、链路本地 fe80::/10、组播 ff00::/8 */ | ||
| function isPrivateOrLocalIpv6(h: string): boolean { | ||
| if (!h.includes(':')) return false; | ||
| return ( | ||
| h.startsWith('fc') || h.startsWith('fd') || | ||
| h.startsWith('fe8') || h.startsWith('fe9') || | ||
| h.startsWith('fea') || h.startsWith('feb') || | ||
| h.startsWith('ff') | ||
| ); | ||
| } | ||
|
|
||
| /** 判断 hostname 是否为回环地址或私有网段(含 IPv6 私有/本地网段)。 */ | ||
| export function isPrivateOrLoopback(hostname: string): boolean { | ||
| const h = normalizeHostname(hostname); | ||
| if (BLOCKED_HOSTS.has(h)) return true; | ||
| if (PRIVATE_IP_PATTERNS.some(p => p.test(h))) return true; | ||
| if (isPrivateOrLocalIpv6(h)) return true; | ||
| return false; | ||
| } | ||
|
Comment on lines
+75
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also block IPv6 private and local ranges (Unique Local Addresses export function isPrivateOrLoopback(hostname: string): boolean {
const h = normalizeHostname(hostname);
if (BLOCKED_HOSTS.has(h)) return true;
if (PRIVATE_IP_PATTERNS.some(p => p.test(h))) return true;
// Block IPv6 private/local ranges (ULA, Link-Local, Multicast)
if (
h.startsWith('fc') ||
h.startsWith('fd') ||
h.startsWith('fe8') ||
h.startsWith('fe9') ||
h.startsWith('fea') ||
h.startsWith('feb') ||
h.startsWith('ff')
) {
if (h.includes(':')) return true;
}
return false;
} |
||
|
|
||
| export function validateUrl(rawUrl: string, opts: ValidateUrlOptions = {}): void { | ||
| const parsed = new URL(rawUrl); | ||
| if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') { | ||
| throw new Error(`Blocked proxy request: unsupported protocol '${parsed.protocol}'`); | ||
| } | ||
| const hostname = parsed.hostname.toLowerCase(); | ||
|
|
||
| // 检查是否是IP地址 | ||
| const isIP = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(hostname); | ||
| if (isIP) { | ||
| // IP地址直接检查是否在阻止列表中 | ||
| if (BLOCKED_HOSTS.has(hostname)) { | ||
| throw new Error(`Blocked proxy request: IP '${hostname}' is not allowed`); | ||
|
|
||
| // 始终拦截 URL 内嵌的用户名/密码(防止凭证泄露到日志) | ||
| if (parsed.username || parsed.password) { | ||
| throw new Error(`Blocked proxy request: URL containing credentials is not allowed`); | ||
| } | ||
|
|
||
| // 归一化后可同时覆盖明文 169.254.169.254 与 [::ffff:a9fe:a9fe] 等 IPv4 映射型写法。 | ||
| const hostname = normalizeHostname(parsed.hostname); | ||
|
|
||
| if (opts.allowPrivate) { | ||
| // 宽松模式:仍死守云元数据 IMDS 地址——最高危的 SSRF 目标(云厂商密钥)。 | ||
| if (hostname === '169.254.169.254') { | ||
| throw new Error(`Blocked proxy request: IMDS address '169.254.169.254' is not allowed`); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // 严格模式(默认):拦截回环地址与私有网段(含 IPv6 私有/本地网段)。 | ||
| if (BLOCKED_HOSTS.has(hostname)) { | ||
| throw new Error(`Blocked proxy request: hostname '${hostname}' is not allowed`); | ||
| } | ||
| if (PRIVATE_IP_PATTERNS.some(p => p.test(hostname))) { | ||
| throw new Error(`Blocked proxy request: private IP '${hostname}' is not allowed`); | ||
| } | ||
|
|
||
| // 阻止URL中的用户名和密码(防止凭证泄露) | ||
| if (parsed.username || parsed.password) { | ||
| throw new Error(`Blocked proxy request: URL containing credentials is not allowed`); | ||
| if (isPrivateOrLocalIpv6(hostname)) { | ||
| throw new Error(`Blocked proxy request: private IPv6 address '${hostname}' is not allowed`); | ||
| } | ||
| } | ||
|
|
||
| export async function proxyRequest(options: ProxyRequestOptions): Promise<ProxyResponse> { | ||
| const { url, method, headers = {}, body, timeout = 30000, proxyConfig, preserveRawResponse = false } = options; | ||
|
|
||
| try { | ||
| validateUrl(url); | ||
| validateUrl(url, { allowPrivate: options.allowPrivate }); | ||
| logger.info('proxy.request', `${method} ${redactUrl(url)}`); | ||
|
|
||
| const axiosConfig: AxiosRequestConfig = { | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent SSRF bypasses, we should normalize hostnames by stripping trailing dots (e.g.,
localhost.or127.0.0.1.) and correctly handling hybrid IPv4-mapped IPv6 addresses (e.g.,::ffff:169.254.169.254). Without this, these patterns can bypass the blocked hosts and private IP checks.