Skip to content

Commit 7175145

Browse files
coulsontlcoulsonpl
andauthored
💄 style: allow users to disable SSRF or set a whitelist (#4633)
* feat: allow users to disable SSRF or set a whitelist * chore: add document description * chore: refactor code * chore: modify code specification --------- Co-authored-by: coulsonpl <[email protected]>
1 parent 444a911 commit 7175145

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

docs/self-hosting/environment-variables/basic.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ For specific content, please refer to the [Feature Flags](/docs/self-hosting/adv
9393
try using `host.docker.internal` instead of `localhost`.
9494
</Callout>
9595

96+
### `SSRF_ALLOW_PRIVATE_IP_ADDRESS`
97+
98+
- Type: Optional
99+
- Description: Allow to connect private IP address. In a trusted environment, it can be set to true to turn off SSRF protection.
100+
- Default: `0`
101+
- Example: `1` or `0`
102+
103+
### `SSRF_ALLOW_IP_ADDRESS_LIST`
104+
105+
- Type: Optional
106+
- Description: Allow private IP address list, multiple IP addresses are separated by commas. Only when `SSRF_ALLOW_PRIVATE_IP_ADDRESS` is `0`, it takes effect.
107+
- Default: -
108+
- Example: `198.18.1.62,224.0.0.3`
109+
96110
## Plugin Service
97111

98112
### `PLUGINS_INDEX_URL`

docs/self-hosting/environment-variables/basic.zh-CN.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ LobeChat 在部署时提供了一些额外的配置项,你可以使用环境
8888
是走到自身容器的 `localhost`,此时请尝试用 `host.docker.internal` 替代 `localhost`
8989
</Callout>
9090

91+
### `SSRF_ALLOW_PRIVATE_IP_ADDRESS`
92+
93+
- 类型:可选
94+
- 描述:是否允许连接私有IP地址。在可信环境中可以设置为true来关闭SSRF防护。
95+
- 默认值:`0`
96+
- 示例:`1` or `0`
97+
98+
### `SSRF_ALLOW_IP_ADDRESS_LIST`
99+
100+
- 类型:可选
101+
- 描述:允许连接的私有IP地址列表,多个IP地址时使用逗号分隔。当 `SSRF_ALLOW_PRIVATE_IP_ADDRESS``0` 时才会生效。
102+
- 默认值:-
103+
- 示例:`198.18.1.62,224.0.0.3`
104+
91105
## 插件服务
92106

93107
### `PLUGINS_INDEX_URL`

src/app/(backend)/webapi/proxy/route.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { NextResponse } from 'next/server';
22
import fetch from 'node-fetch';
3-
import { useAgent as ssrfAgent } from 'request-filtering-agent';
3+
import { RequestFilteringAgentOptions, useAgent as ssrfAgent } from 'request-filtering-agent';
4+
5+
import { appEnv } from '@/config/app';
46

57
/**
68
* just for a proxy
@@ -9,7 +11,14 @@ export const POST = async (req: Request) => {
911
const url = await req.text();
1012

1113
try {
12-
const res = await fetch(url, { agent: ssrfAgent(url) });
14+
// https://www.npmjs.com/package/request-filtering-agent
15+
const options: RequestFilteringAgentOptions = {
16+
allowIPAddressList: appEnv.SSRF_ALLOW_IP_ADDRESS_LIST?.split(',') || [],
17+
allowMetaIPAddress: appEnv.SSRF_ALLOW_PRIVATE_IP_ADDRESS,
18+
allowPrivateIPAddress: appEnv.SSRF_ALLOW_PRIVATE_IP_ADDRESS,
19+
denyIPAddressList: [],
20+
};
21+
const res = await fetch(url, { agent: ssrfAgent(url, options) });
1322

1423
return new Response(await res.arrayBuffer(), { headers: { ...res.headers } });
1524
} catch (err) {

src/config/app.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ export const getAppConfig = () => {
4646
CDN_USE_GLOBAL: z.boolean().optional(),
4747
CUSTOM_FONT_FAMILY: z.string().optional(),
4848
CUSTOM_FONT_URL: z.string().optional(),
49+
50+
SSRF_ALLOW_PRIVATE_IP_ADDRESS: z.boolean().optional(),
51+
SSRF_ALLOW_IP_ADDRESS_LIST: z.string().optional(),
4952
},
5053
runtimeEnv: {
5154
NEXT_PUBLIC_BASE_PATH: process.env.NEXT_PUBLIC_BASE_PATH || '',
@@ -72,6 +75,9 @@ export const getAppConfig = () => {
7275
CUSTOM_FONT_FAMILY: process.env.CUSTOM_FONT_FAMILY,
7376
CUSTOM_FONT_URL: process.env.CUSTOM_FONT_URL,
7477
CDN_USE_GLOBAL: process.env.CDN_USE_GLOBAL === '1',
78+
79+
SSRF_ALLOW_PRIVATE_IP_ADDRESS: process.env.SSRF_ALLOW_PRIVATE_IP_ADDRESS === '1',
80+
SSRF_ALLOW_IP_ADDRESS_LIST: process.env.SSRF_ALLOW_IP_ADDRESS_LIST,
7581
},
7682
});
7783
};

0 commit comments

Comments
 (0)