@morgan9668/dsh-llm-gateway 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -20
- package/docs/DETAIL.md +21 -9
- package/index.js +23 -5
- package/package.json +1 -1
- package/tests/auth.mjs +11 -4
package/README.md
CHANGED
|
@@ -113,8 +113,9 @@ bundle 自带 `cordis.patch.yml`,安装时自动插入插件行,不需要手
|
|
|
113
113
|
|---|---|---|
|
|
114
114
|
| `host` | `127.0.0.1` | 绑定地址。默认只绑回环,局域网内其它机器访问不到 |
|
|
115
115
|
| `port` | `8392` | 监听端口;非法值静默回退到默认值,不会报错 |
|
|
116
|
-
| `apiKey` | 空 |
|
|
117
|
-
| `
|
|
116
|
+
| `apiKey` | 空 | 留空且 `requireAuth` 关闭时不校验 token;填了值则要求 `Authorization: Bearer <该值>` |
|
|
117
|
+
| `requireAuth` | `false` | 设为 `true` 强制校验 token;此时 `apiKey` 留空会自动生成并落盘 |
|
|
118
|
+
| `apiKeyFile` | `~/.dsh/dsh-llm-gateway.key` | `requireAuth` 打开时自动生成密钥的保存位置 |
|
|
118
119
|
| `allowedHosts` | 回环的几种写法(含端口) | 允许的 `Host` 头,挡 DNS rebinding;非白名单回 421 |
|
|
119
120
|
| `allowedOrigins` | 空(只允许不带 Origin 的请求) | 允许的 `Origin`,挡跨站页面调用;不匹配回 403 |
|
|
120
121
|
| `maxBodyBytes` | 256 MiB | 单个请求体上限。Codex 每轮重发整段历史,带图会话会很大;超过就回 413。缩小它是给暴露到公网的实例加一道闸 |
|
|
@@ -126,22 +127,32 @@ bundle 自带 `cordis.patch.yml`,安装时自动插入插件行,不需要手
|
|
|
126
127
|
|
|
127
128
|
### 安全边界
|
|
128
129
|
|
|
129
|
-
|
|
130
|
+
默认配置下,**不需要任何凭据**就能用——这一点是有意保持的,因为现成的客户端
|
|
131
|
+
(Codex、SDK、脚本)都是零配置直连,升级不该把它们打断。真正危险的两类场景由另外两道
|
|
132
|
+
校验挡住,它们默认就开着:
|
|
130
133
|
|
|
131
134
|
1. **绑定**只到 `127.0.0.1`,外部网络进不来。改 `host` 前先想清楚:这个端点会让调用方
|
|
132
135
|
以你的账号配额随意调用模型。
|
|
133
|
-
2.
|
|
134
|
-
|
|
136
|
+
2. **Host 必须白名单**。只接受回环的几种写法(`127.0.0.1` / `localhost` / `[::1]`,
|
|
137
|
+
带配置端口),否则回 421。这条挡的是 DNS rebinding:攻击者把自己的域名解析到
|
|
138
|
+
`127.0.0.1`,浏览器就会把请求当成同源发过来。
|
|
139
|
+
3. **Origin 存在时必须白名单**,否则回 403。这条挡的是浏览器里的跨站页面。命令行客户端
|
|
140
|
+
一般不带 `Origin`,不受影响。空列表表示「不接受任何跨站调用」,不是「全部放行」。
|
|
135
141
|
|
|
136
|
-
|
|
137
|
-
curl -H "Authorization: Bearer $(cat ~/.dsh/dsh-llm-gateway.key)" \
|
|
138
|
-
http://127.0.0.1:8392/v1/models
|
|
139
|
-
```
|
|
142
|
+
需要更强隔离(比如同机还有别的用户或不可信程序)时,打开认证:
|
|
140
143
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
144
|
+
```yaml
|
|
145
|
+
config:
|
|
146
|
+
requireAuth: true # 首次启动生成密钥到 ~/.dsh/dsh-llm-gateway.key(600)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
curl -H "Authorization: Bearer $(cat ~/.dsh/dsh-llm-gateway.key)" \
|
|
151
|
+
http://127.0.0.1:8392/v1/models
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
也可以在 `apiKey` 里直接填一个自己的值。密钥比较用 `timingSafeEqual`,不是会提前返回的
|
|
155
|
+
字符串比较。
|
|
145
156
|
|
|
146
157
|
跨源访问需要放行时,把来源写进 `allowedOrigins`,不要改成放行 `*`:
|
|
147
158
|
|
|
@@ -151,10 +162,7 @@ config:
|
|
|
151
162
|
- 'http://127.0.0.1:3000'
|
|
152
163
|
```
|
|
153
164
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
- 网关不落盘任何上游凭据;持久副作用只有日志与上面那个密钥文件。
|
|
157
|
-
- 换密钥:删掉 `~/.dsh/dsh-llm-gateway.key` 再重启,会生成新的;客户端记得同步更新。
|
|
165
|
+
其余说明:网关不落盘任何上游凭据;持久副作用只有日志(以及开启认证时的密钥文件)。
|
|
158
166
|
|
|
159
167
|
## 端点
|
|
160
168
|
|
|
@@ -395,9 +403,10 @@ node tests/auth.mjs
|
|
|
395
403
|
`image-budget.mjs` 四个用例覆盖路由图片预算:数量超限时省略最老的并保留最新的;
|
|
396
404
|
base64 字节超限时同样触发省略;未超限时一张不动;省略后确实落在预算内。
|
|
397
405
|
|
|
398
|
-
`auth.mjs`
|
|
399
|
-
|
|
400
|
-
|
|
406
|
+
`auth.mjs` 十一个用例覆盖认证与来源限制:默认不要求 token(升级不打断现有客户端);
|
|
407
|
+
`requireAuth` 打开后无 token / 错误 token 回 401;非白名单 Host 回 421;Origin 不在白名单
|
|
408
|
+
回 403;不带 Origin 的命令行客户端不受影响;`requireAuth` 下自动生成密钥并以 `600` 权限
|
|
409
|
+
落盘;显式配置的密钥优先。
|
|
401
410
|
|
|
402
411
|
未覆盖的部分(改代码时自己补测):请求转换与配对修复、槽位换算、路由解析、附件落地、状态码映射表、
|
|
403
412
|
认证。
|
package/docs/DETAIL.md
CHANGED
|
@@ -26,11 +26,22 @@ the whole history every turn, and image-heavy conversations get large quickly.
|
|
|
26
26
|
|
|
27
27
|
## Authentication and origin checks
|
|
28
28
|
|
|
29
|
-
Authentication is
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
Authentication is **off by default**, deliberately: existing clients (Codex,
|
|
30
|
+
SDKs, scripts) connect with no credentials, and upgrading a gateway should not
|
|
31
|
+
break them. The dangerous cases are covered by two checks that are on by default
|
|
32
|
+
instead — Host and Origin, below.
|
|
33
|
+
|
|
34
|
+
Turning it on takes one setting:
|
|
35
|
+
|
|
36
|
+
```yaml
|
|
37
|
+
config:
|
|
38
|
+
requireAuth: true
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
An empty `apiKey` then generates a random key into `apiKeyFile` (default
|
|
42
|
+
`~/.dsh/dsh-llm-gateway.key`, mode `600`) and logs the path; clients send
|
|
43
|
+
`Authorization: Bearer <that key>`. Setting `apiKey` directly pins your own value.
|
|
44
|
+
The comparison uses `timingSafeEqual`, not an early-returning string compare.
|
|
34
45
|
|
|
35
46
|
Two more checks run before routing:
|
|
36
47
|
|
|
@@ -227,10 +238,11 @@ and `retry-after: 0`; a missing `content-length` (chunked upload) is still count
|
|
|
227
238
|
occurrences are omitted first while the newest are kept; a byte overflow also triggers
|
|
228
239
|
omission; nothing is touched below the budget; and the result really fits.
|
|
229
240
|
|
|
230
|
-
`node tests/auth.mjs` runs
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
241
|
+
`node tests/auth.mjs` runs eleven cases for authentication and origin checks: the
|
|
242
|
+
default requires no token; with `requireAuth` a missing or wrong token returns 401;
|
|
243
|
+
a non-allowlisted Host returns 421; an origin outside `allowedOrigins` returns 403;
|
|
244
|
+
clients without an Origin header still work; `requireAuth` generates a key file with
|
|
245
|
+
mode 600; and an explicit key wins.
|
|
234
246
|
|
|
235
247
|
Conversion, pairing repair, slots, routing, attachments and the status map are not
|
|
236
248
|
covered.
|
package/index.js
CHANGED
|
@@ -22,11 +22,19 @@ export const inject = ['llm', 'attachments'];
|
|
|
22
22
|
const DEFAULTS = {
|
|
23
23
|
host: '127.0.0.1',
|
|
24
24
|
port: 8392,
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
//
|
|
25
|
+
// 认证默认**关闭**,保持向后兼容:空串就是「不校验 token」。
|
|
26
|
+
//
|
|
27
|
+
// 之所以不默认开启:现成的客户端(Codex、SDK、脚本)都是零配置直连的,
|
|
28
|
+
// 一旦默认要求 token,它们会在升级后立刻 401,而用户不一定知道去哪找密钥。
|
|
29
|
+
// 默认开启的安全收益有限——网关本来就只绑回环,外加下面 Host/Origin 两道
|
|
30
|
+
// 校验,已经挡住了真正危险的场景(浏览器里的跨站页面、DNS rebinding)。
|
|
31
|
+
//
|
|
32
|
+
// 需要更强隔离时:把 requireAuth 设为 true,密钥会自动生成到 apiKeyFile
|
|
33
|
+
// (600 权限),或者直接把 apiKey 填成想要的值。
|
|
28
34
|
apiKey: '',
|
|
29
|
-
//
|
|
35
|
+
// 为 true 时强制校验 bearer token;apiKey 为空则自动生成并落盘。
|
|
36
|
+
requireAuth: false,
|
|
37
|
+
// 自动生成的密钥落盘位置。requireAuth 打开时才使用。
|
|
30
38
|
apiKeyFile: '',
|
|
31
39
|
// 允许的 Host 头。默认只接受回环地址的几种写法,挡掉 DNS rebinding。
|
|
32
40
|
allowedHosts: [],
|
|
@@ -137,6 +145,7 @@ function normalizeConfig(raw) {
|
|
|
137
145
|
if (typeof raw.host === 'string' && raw.host !== '') config.host = raw.host;
|
|
138
146
|
if (Number.isInteger(raw.port) && raw.port > 0 && raw.port < 65536) config.port = raw.port;
|
|
139
147
|
if (typeof raw.apiKey === 'string') config.apiKey = raw.apiKey;
|
|
148
|
+
if (typeof raw.requireAuth === 'boolean') config.requireAuth = raw.requireAuth;
|
|
140
149
|
if (typeof raw.apiKeyFile === 'string') config.apiKeyFile = raw.apiKeyFile;
|
|
141
150
|
if (Array.isArray(raw.allowedHosts)) {
|
|
142
151
|
config.allowedHosts = raw.allowedHosts.filter((h) => typeof h === 'string' && h !== '');
|
|
@@ -177,6 +186,8 @@ function defaultAllowedHosts(host, port) {
|
|
|
177
186
|
*/
|
|
178
187
|
function resolveApiKey(config) {
|
|
179
188
|
if (config.apiKey !== '') return { key: config.apiKey, filePath: undefined, generated: false };
|
|
189
|
+
// 既没填 apiKey 也没要求认证:不校验。调用方据此跳过 token 比对。
|
|
190
|
+
if (config.requireAuth !== true) return { key: '', filePath: undefined, generated: false };
|
|
180
191
|
const filePath = config.apiKeyFile !== ''
|
|
181
192
|
? config.apiKeyFile
|
|
182
193
|
: join(homedir(), '.dsh', 'dsh-llm-gateway.key');
|
|
@@ -1686,7 +1697,12 @@ export function apply(ctx, rawConfig) {
|
|
|
1686
1697
|
if (apiKeyGenerated) {
|
|
1687
1698
|
log(`已生成新的 API 密钥并写入 ${apiKeyFile ?? '(仅本次进程)'};客户端请用 Authorization: Bearer <该文件内容>`);
|
|
1688
1699
|
}
|
|
1689
|
-
|
|
1700
|
+
if (apiKey === '') {
|
|
1701
|
+
log(`认证未启用(只绑回环 + Host/Origin 校验);需要时设 requireAuth: true 或填 apiKey`);
|
|
1702
|
+
} else {
|
|
1703
|
+
log('认证已启用');
|
|
1704
|
+
}
|
|
1705
|
+
log(`允许的 Host:${allowedHosts.join(', ')}`);
|
|
1690
1706
|
if (allowedOrigins.length > 0) log(`允许的 Origin:${allowedOrigins.join(', ')}`);
|
|
1691
1707
|
|
|
1692
1708
|
let cache = [];
|
|
@@ -1861,6 +1877,8 @@ export function apply(ctx, rawConfig) {
|
|
|
1861
1877
|
* 远程可达的端点上那是可测量的时间侧信道。
|
|
1862
1878
|
*/
|
|
1863
1879
|
function authorized(request) {
|
|
1880
|
+
// apiKey 为空 = 未启用认证(requireAuth 关闭且没填 key)。
|
|
1881
|
+
if (apiKey === '') return true;
|
|
1864
1882
|
const header = request.headers.get('authorization') ?? '';
|
|
1865
1883
|
const prefix = 'Bearer ';
|
|
1866
1884
|
if (!header.startsWith(prefix)) return false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@morgan9668/dsh-llm-gateway",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Local OpenAI-compatible gateway that exposes every model registered on the DSH llm service (DeepSeek, WorkBuddy, our-free-model) so external clients such as Codex can use them.",
|
|
6
6
|
"author": "chenzhao",
|
package/tests/auth.mjs
CHANGED
|
@@ -172,14 +172,21 @@ test('没有 Origin 头的命令行客户端不受影响', async () => {
|
|
|
172
172
|
});
|
|
173
173
|
});
|
|
174
174
|
|
|
175
|
-
test('
|
|
175
|
+
test('默认不要求 token:现有客户端升级后不会被打断', async () => {
|
|
176
|
+
await withGateway({}, async ({ port }) => {
|
|
177
|
+
const res = await request(port);
|
|
178
|
+
assert.equal(res.status, 200, '未开启 requireAuth 时必须免认证放行');
|
|
179
|
+
assert.match(res.text, /"ok":true/);
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test('requireAuth 打开后自动生成密钥并落盘(600 权限)', async () => {
|
|
176
184
|
const dir = mkdtempSync(join(tmpdir(), 'gateway-key-'));
|
|
177
185
|
const keyFile = join(dir, 'gateway.key');
|
|
178
186
|
try {
|
|
179
|
-
await withGateway({ apiKeyFile: keyFile }, async ({ port }) => {
|
|
180
|
-
// 未带认证 → 401,说明确实生成了密钥而不是放行
|
|
187
|
+
await withGateway({ requireAuth: true, apiKeyFile: keyFile }, async ({ port }) => {
|
|
181
188
|
const denied = await request(port);
|
|
182
|
-
assert.equal(denied.status, 401, '
|
|
189
|
+
assert.equal(denied.status, 401, 'requireAuth 打开后无 token 必须 401');
|
|
183
190
|
|
|
184
191
|
const key = readFileSync(keyFile, 'utf8').trim();
|
|
185
192
|
assert.ok(key.length >= 20, `生成的密钥太短:${key.length}`);
|