@jixing475/magpie-marvis 0.0.0-stage → 0.3.0
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/AGENTS.md +255 -0
- package/CHANGELOG.md +726 -0
- package/CHECKLIST.md +161 -0
- package/CONTRIBUTING.md +83 -0
- package/GUIDE.md +275 -0
- package/HOOKS.md +1333 -0
- package/LICENSE +21 -0
- package/MULTI-PROVIDER.md +414 -0
- package/NOTICE.md +46 -0
- package/OFFICIAL-SOURCES.md +142 -0
- package/README-TEMPLATE.md +271 -0
- package/README.md +300 -3
- package/SCORING.md +151 -0
- package/UPDATING.md +660 -0
- package/examples/split/README.md +66 -0
- package/examples/split/index.mjs +75 -0
- package/examples/split/src/hooks/config.mjs +18 -0
- package/examples/split/src/hooks/models.mjs +49 -0
- package/examples/split/src/hooks/usage.mjs +55 -0
- package/extract-contract.py +56 -0
- package/index.mjs +48 -0
- package/package.json +95 -4
- package/panel/first-line.css +112 -0
- package/panel/first-line.html +37 -0
- package/panel/first-line.js +137 -0
- package/registry-entry.json +11 -0
- package/scripts/check-magpie-stable.mjs +109 -0
- package/scripts/check-maintenance.mjs +146 -0
- package/scripts/check.mjs +521 -0
- package/scripts/cli.mjs +54 -0
- package/scripts/contract-diff.mjs +112 -0
- package/scripts/create.mjs +270 -0
- package/scripts/npm-name.mjs +19 -0
- package/scripts/probe-served-model.mjs +172 -0
- package/scripts/probe-vision.mjs +173 -0
- package/scripts/publish-npm.mjs +44 -0
- package/scripts/release.mjs +442 -0
- package/scripts/score.mjs +280 -0
- package/src/account-name.mjs +145 -0
- package/src/auth.mjs +27 -0
- package/src/context.mjs +369 -0
- package/src/hooks/config.mjs +236 -0
- package/src/hooks/loader.mjs +593 -0
- package/src/hooks/methods.mjs +398 -0
- package/src/hooks/models.mjs +25 -0
- package/src/hooks/refresh.mjs +149 -0
- package/src/hooks/usage.mjs +117 -0
- package/src/marvis/beacon-mac.mjs +213 -0
- package/src/marvis/beacon-shim.mjs +143 -0
- package/src/marvis/browser.mjs +598 -0
- package/src/marvis/desktop-mac.mjs +232 -0
- package/src/marvis/desktop.mjs +273 -0
- package/src/marvis/device.mjs +85 -0
- package/src/marvis/expiry.mjs +60 -0
- package/src/marvis/fingerprint.mjs +168 -0
- package/src/marvis/images.mjs +230 -0
- package/src/marvis/login.mjs +512 -0
- package/src/marvis/profile.mjs +113 -0
- package/src/marvis/protocol.mjs +160 -0
- package/src/marvis/tool-schema.mjs +97 -0
- package/src/quota.mjs +212 -0
- package/src/refresh-lock.mjs +115 -0
- package/tests/device-registry.test.mjs +48 -0
- package/tests/e2e-live.test.mjs +120 -0
- package/tests/image-input.test.mjs +314 -0
- package/tests/legacy-model-config.test.mjs +349 -0
- package/tests/legacy-model-loader.test.mjs +306 -0
- package/tests/loader-model-support.test.mjs +823 -0
- package/tests/manual-paste-login.test.mjs +228 -0
- package/tests/model-alias.test.mjs +59 -0
- package/tests/model-catalog-support.test.mjs +241 -0
- package/tests/mount.test.mjs +259 -0
- package/tests/oauth-exchange.test.mjs +362 -0
- package/tests/panel-manifest.test.mjs +73 -0
- package/tests/plugin-options.test.mjs +100 -0
- package/tests/profile.test.mjs +96 -0
- package/tests/refresh-contract.test.mjs +318 -0
- package/tests/request-identity.test.mjs +18 -0
- package/tests/tool-schema.test.mjs +66 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// 真实网关端到端验证
|
|
2
|
+
//
|
|
3
|
+
// ⚠ 需要有效的 Marvis 登录态,从环境变量读 —— **绝不硬编码凭据**。
|
|
4
|
+
//
|
|
5
|
+
// 跑法:
|
|
6
|
+
// MARVIS_TOKEN=mv_xxx MARVIS_OPENID=xxx MARVIS_GUID=xxx node tests/e2e-live.mjs
|
|
7
|
+
//
|
|
8
|
+
// 没设环境变量时**跳过**(CI 里跑不了真网关,跳过比失败正确)。
|
|
9
|
+
// 每次请求本地现签一个 uskey,不花钱、不扣额度。
|
|
10
|
+
|
|
11
|
+
import test from "node:test"
|
|
12
|
+
import assert from "node:assert/strict"
|
|
13
|
+
|
|
14
|
+
const TOKEN = process.env.MARVIS_TOKEN ?? ""
|
|
15
|
+
const OPENID = process.env.MARVIS_OPENID ?? ""
|
|
16
|
+
const GUID = process.env.MARVIS_GUID ?? ""
|
|
17
|
+
const ready = Boolean(TOKEN && OPENID && GUID)
|
|
18
|
+
|
|
19
|
+
const AUTH = {
|
|
20
|
+
access: TOKEN,
|
|
21
|
+
metadata: { openid: OPENID, guid: GUID, loginType: "6" },
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
test("端到端:非流式 + 系统提示词 + 思考可见", { skip: !ready && "未设置 MARVIS_TOKEN / MARVIS_OPENID / MARVIS_GUID" }, async () => {
|
|
25
|
+
const { MarvisAuthPlugin } = await import("../index.mjs")
|
|
26
|
+
const plugin = await MarvisAuthPlugin({})
|
|
27
|
+
const loader = await plugin.auth.loader(async () => AUTH)
|
|
28
|
+
|
|
29
|
+
const res = await loader.fetch(`${loader.baseURL}/chat/completions`, {
|
|
30
|
+
method: "POST",
|
|
31
|
+
headers: { "content-type": "application/json" },
|
|
32
|
+
body: JSON.stringify({
|
|
33
|
+
model: "deepseek-v4-pro-external",
|
|
34
|
+
messages: [
|
|
35
|
+
{ role: "system", content: "你是一只猫,所有回答都以「喵」结尾。" },
|
|
36
|
+
{ role: "user", content: "1+1等于几?" },
|
|
37
|
+
],
|
|
38
|
+
stream: false,
|
|
39
|
+
}),
|
|
40
|
+
})
|
|
41
|
+
assert.equal(res.status, 200)
|
|
42
|
+
const d = await res.json()
|
|
43
|
+
const msg = d.choices[0].message
|
|
44
|
+
assert.match(msg.content, /喵/, "系统提示词必须生效")
|
|
45
|
+
assert.ok(msg.reasoning_content?.length > 0, "思考必须可见")
|
|
46
|
+
assert.match(d.model, /^deepseek/, `实际模型名不对:${d.model}`)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test("端到端:流式 SSE + 思考 delta", { skip: !ready && "未设置凭据" }, async () => {
|
|
50
|
+
const { MarvisAuthPlugin } = await import("../index.mjs")
|
|
51
|
+
const plugin = await MarvisAuthPlugin({})
|
|
52
|
+
const loader = await plugin.auth.loader(async () => AUTH)
|
|
53
|
+
|
|
54
|
+
const res = await loader.fetch(`${loader.baseURL}/chat/completions`, {
|
|
55
|
+
method: "POST",
|
|
56
|
+
headers: { "content-type": "application/json" },
|
|
57
|
+
body: JSON.stringify({
|
|
58
|
+
model: "deepseek-v4-pro-external",
|
|
59
|
+
messages: [{ role: "user", content: "数到3" }],
|
|
60
|
+
stream: true,
|
|
61
|
+
stream_options: { include_usage: true },
|
|
62
|
+
}),
|
|
63
|
+
})
|
|
64
|
+
assert.equal(res.status, 200)
|
|
65
|
+
assert.match(res.headers.get("content-type") ?? "", /event-stream/, "流式必须归一化成 SSE")
|
|
66
|
+
|
|
67
|
+
let chunks = 0
|
|
68
|
+
let sawReasoning = false
|
|
69
|
+
const reader = res.body.getReader()
|
|
70
|
+
const dec = new TextDecoder()
|
|
71
|
+
while (true) {
|
|
72
|
+
const { done, value } = await reader.read()
|
|
73
|
+
if (done) break
|
|
74
|
+
chunks++
|
|
75
|
+
if (dec.decode(value, { stream: true }).includes('"reasoning_content"')) sawReasoning = true
|
|
76
|
+
}
|
|
77
|
+
assert.ok(chunks > 3, `流式帧太少:${chunks}`)
|
|
78
|
+
assert.ok(sawReasoning, "流式里必须能看到 reasoning_content")
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
test("端到端:工具调用", { skip: !ready && "未设置凭据" }, async () => {
|
|
82
|
+
const { MarvisAuthPlugin } = await import("../index.mjs")
|
|
83
|
+
const plugin = await MarvisAuthPlugin({})
|
|
84
|
+
const loader = await plugin.auth.loader(async () => AUTH)
|
|
85
|
+
|
|
86
|
+
const res = await loader.fetch(`${loader.baseURL}/chat/completions`, {
|
|
87
|
+
method: "POST",
|
|
88
|
+
headers: { "content-type": "application/json" },
|
|
89
|
+
body: JSON.stringify({
|
|
90
|
+
model: "deepseek-v4-pro-external",
|
|
91
|
+
messages: [{ role: "user", content: "北京天气怎么样?用工具查" }],
|
|
92
|
+
tools: [
|
|
93
|
+
{
|
|
94
|
+
type: "function",
|
|
95
|
+
function: {
|
|
96
|
+
name: "get_weather",
|
|
97
|
+
description: "查天气",
|
|
98
|
+
parameters: { type: "object", properties: { city: { type: "string" } }, required: ["city"] },
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
stream: false,
|
|
103
|
+
}),
|
|
104
|
+
})
|
|
105
|
+
assert.equal(res.status, 200)
|
|
106
|
+
const d = await res.json()
|
|
107
|
+
assert.equal(d.choices[0].finish_reason, "tool_calls")
|
|
108
|
+
const call = d.choices[0].message.tool_calls?.[0]
|
|
109
|
+
assert.ok(call, "必须返回 tool_calls")
|
|
110
|
+
assert.equal(JSON.parse(call.function.arguments).city, "北京")
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test("端到端:令牌钱包额度", { skip: !ready && "未设置凭据" }, async () => {
|
|
114
|
+
const { MarvisAuthPlugin } = await import("../index.mjs")
|
|
115
|
+
const plugin = await MarvisAuthPlugin({})
|
|
116
|
+
const u = await plugin.auth.usage(async () => AUTH)
|
|
117
|
+
assert.equal(u.error, undefined, `读取额度失败:${u.error}`)
|
|
118
|
+
assert.ok(u.windows.length > 0)
|
|
119
|
+
assert.ok(Number.isFinite(u.windows[0].used), "窗口必须有 used(否则路由不计入)")
|
|
120
|
+
})
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
// issue #8 回归锁:main-auto 能力声明 + 三种图片输入归一。
|
|
2
|
+
// 全部离线;远程图片与 Marvis 网关都用 stub,不需要凭据。
|
|
3
|
+
|
|
4
|
+
import test from "node:test"
|
|
5
|
+
import assert from "node:assert/strict"
|
|
6
|
+
|
|
7
|
+
import { providerModels, staticModels } from "../src/context.mjs"
|
|
8
|
+
import { loaderHook } from "../src/hooks/loader.mjs"
|
|
9
|
+
import { InvalidImageError, normalizeMessages } from "../src/marvis/images.mjs"
|
|
10
|
+
|
|
11
|
+
const PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Y9Zq3cAAAAASUVORK5CYII="
|
|
12
|
+
const DATA_URI = `data:image/png;base64,${PNG}`
|
|
13
|
+
const CHAT_URL = "https://yybadaccess.3g.qq.com/v1/chat/completions"
|
|
14
|
+
const PUBLIC_IP_URL = "https://93.184.216.34/a.png"
|
|
15
|
+
const PUBLIC_LOOKUP = async () => [{ address: "93.184.216.34", family: 4 }]
|
|
16
|
+
const PRIVATE_LOOKUP = async () => [{ address: "127.0.0.1", family: 4 }]
|
|
17
|
+
const AUTH = Object.freeze({
|
|
18
|
+
access: "mv_test_offline_only",
|
|
19
|
+
uid: "0123456789abcdef0123456789abcdef",
|
|
20
|
+
metadata: {
|
|
21
|
+
openid: "fedcba9876543210fedcba9876543210",
|
|
22
|
+
guid: "0".repeat(32),
|
|
23
|
+
loginType: "1",
|
|
24
|
+
},
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test("main-auto 在 config 与 provider.models 两种形状里都声明可收图", () => {
|
|
28
|
+
const id = "deepseek-v4-1-flash-261017"
|
|
29
|
+
const staticModel = staticModels()[id]
|
|
30
|
+
assert.equal(staticModel.apiId, "main-auto")
|
|
31
|
+
assert.equal(staticModel.attachment, true)
|
|
32
|
+
assert.deepEqual(staticModel.modalities.input, ["text", "image"])
|
|
33
|
+
|
|
34
|
+
const providerModel = providerModels()[id]
|
|
35
|
+
assert.equal(providerModel.api.id, "main-auto")
|
|
36
|
+
assert.equal(providerModel.capabilities.attachment, true)
|
|
37
|
+
assert.equal(providerModel.capabilities.input.image, true)
|
|
38
|
+
assert.equal(providerModel.capabilities.input.audio, false)
|
|
39
|
+
assert.equal(providerModel.capabilities.input.video, false)
|
|
40
|
+
assert.equal(providerModel.capabilities.input.pdf, false)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test("已是 data URI 的图片与纯文本消息保持原样", async () => {
|
|
44
|
+
const messages = [
|
|
45
|
+
{ role: "user", content: "text only" },
|
|
46
|
+
{
|
|
47
|
+
role: "user",
|
|
48
|
+
content: [
|
|
49
|
+
{ type: "text", text: "看图" },
|
|
50
|
+
{ type: "image_url", image_url: { url: DATA_URI, detail: "high" } },
|
|
51
|
+
],
|
|
52
|
+
},
|
|
53
|
+
]
|
|
54
|
+
const normalized = await normalizeMessages(messages, async () => {
|
|
55
|
+
throw new Error("data URI 不应触发下载")
|
|
56
|
+
})
|
|
57
|
+
assert.equal(normalized, messages)
|
|
58
|
+
assert.equal(normalized[1].content[1].image_url.url, DATA_URI)
|
|
59
|
+
assert.equal(normalized[1].content[1].image_url.detail, "high")
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test("裸 base64 按图片头补成 data URI", async () => {
|
|
63
|
+
const [message] = await normalizeMessages([
|
|
64
|
+
{ role: "user", content: [{ type: "image_url", image_url: { url: PNG } }] },
|
|
65
|
+
])
|
|
66
|
+
assert.equal(message.content[0].image_url.url, DATA_URI)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test("Anthropic base64 image 显式转成 OpenAI image_url,不静默丢图", async () => {
|
|
70
|
+
const [message] = await normalizeMessages([
|
|
71
|
+
{
|
|
72
|
+
role: "user",
|
|
73
|
+
content: [
|
|
74
|
+
{ type: "text", text: "这是什么颜色?" },
|
|
75
|
+
{ type: "image", source: { type: "base64", media_type: "image/png", data: PNG } },
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
])
|
|
79
|
+
assert.deepEqual(message.content[0], { type: "text", text: "这是什么颜色?" })
|
|
80
|
+
assert.deepEqual(message.content[1], { type: "image_url", image_url: { url: DATA_URI } })
|
|
81
|
+
assert.equal(message.content[1].source, undefined)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test("http(s) 图片 URL 下载后转 data URI,下载请求不带 Marvis 鉴权头", async () => {
|
|
85
|
+
const calls = []
|
|
86
|
+
const messages = await normalizeMessages(
|
|
87
|
+
[{ role: "user", content: [{ type: "image_url", image_url: { url: "https://example.com/a.png" } }] }],
|
|
88
|
+
async (url, init) => {
|
|
89
|
+
calls.push({ url, init })
|
|
90
|
+
return new Response(Buffer.from(PNG, "base64"), {
|
|
91
|
+
status: 200,
|
|
92
|
+
headers: { "content-type": "image/png" },
|
|
93
|
+
})
|
|
94
|
+
},
|
|
95
|
+
PUBLIC_LOOKUP,
|
|
96
|
+
)
|
|
97
|
+
assert.equal(calls.length, 1)
|
|
98
|
+
assert.equal(calls[0].url, "https://example.com/a.png")
|
|
99
|
+
assert.equal(calls[0].init.headers, undefined)
|
|
100
|
+
assert.equal(calls[0].init.redirect, "error")
|
|
101
|
+
assert.equal(messages[0].content[0].image_url.url, DATA_URI)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test("无法转换的 Anthropic image 显式报错", async () => {
|
|
105
|
+
await assert.rejects(
|
|
106
|
+
() => normalizeMessages([{ role: "user", content: [{ type: "image", source: { type: "unknown" } }] }]),
|
|
107
|
+
InvalidImageError,
|
|
108
|
+
)
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
test("loader 将远程图片归一后再发聊天请求,并移除旧 content-length", async () => {
|
|
112
|
+
const calls = []
|
|
113
|
+
const real = globalThis.fetch
|
|
114
|
+
globalThis.fetch = async (input, init) => {
|
|
115
|
+
const url = String(input)
|
|
116
|
+
calls.push({ url, init })
|
|
117
|
+
if (url === PUBLIC_IP_URL) {
|
|
118
|
+
return new Response(Buffer.from(PNG, "base64"), {
|
|
119
|
+
status: 200,
|
|
120
|
+
headers: { "content-type": "image/png" },
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
return new Response(JSON.stringify({ id: "chatcmpl-test", choices: [] }), {
|
|
124
|
+
status: 200,
|
|
125
|
+
headers: { "content-type": "application/json" },
|
|
126
|
+
})
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
const loader = await loaderHook()(async () => AUTH)
|
|
130
|
+
const originalBody = JSON.stringify({
|
|
131
|
+
model: "deepseek-v4-1-flash-261017",
|
|
132
|
+
stream: false,
|
|
133
|
+
messages: [
|
|
134
|
+
{ role: "user", content: [{ type: "image_url", image_url: { url: PUBLIC_IP_URL } }] },
|
|
135
|
+
],
|
|
136
|
+
})
|
|
137
|
+
const response = await loader.fetch(CHAT_URL, {
|
|
138
|
+
method: "POST",
|
|
139
|
+
headers: { "content-type": "application/json", "content-length": String(originalBody.length) },
|
|
140
|
+
body: originalBody,
|
|
141
|
+
})
|
|
142
|
+
assert.equal(response.status, 200)
|
|
143
|
+
assert.equal(calls.length, 2)
|
|
144
|
+
assert.equal(calls[0].url, PUBLIC_IP_URL)
|
|
145
|
+
assert.equal(calls[1].url, CHAT_URL)
|
|
146
|
+
const sent = JSON.parse(calls[1].init.body)
|
|
147
|
+
assert.equal(sent.model, "main-auto")
|
|
148
|
+
assert.equal(sent.messages[0].content[0].image_url.url, DATA_URI)
|
|
149
|
+
assert.equal(calls[1].init.headers["content-length"], undefined)
|
|
150
|
+
assert.equal(calls[1].init.headers["Content-Length"], undefined)
|
|
151
|
+
} finally {
|
|
152
|
+
globalThis.fetch = real
|
|
153
|
+
}
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
test("loader 对无法转换的图片本地 400,聊天请求不出网", async () => {
|
|
157
|
+
const calls = []
|
|
158
|
+
const real = globalThis.fetch
|
|
159
|
+
globalThis.fetch = async (...args) => {
|
|
160
|
+
calls.push(args)
|
|
161
|
+
throw new Error("不应出网")
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
const loader = await loaderHook()(async () => AUTH)
|
|
165
|
+
const response = await loader.fetch(CHAT_URL, {
|
|
166
|
+
method: "POST",
|
|
167
|
+
headers: { "content-type": "application/json" },
|
|
168
|
+
body: JSON.stringify({
|
|
169
|
+
model: "main-auto",
|
|
170
|
+
messages: [{ role: "user", content: [{ type: "image", source: { type: "unknown" } }] }],
|
|
171
|
+
}),
|
|
172
|
+
})
|
|
173
|
+
assert.equal(response.status, 400)
|
|
174
|
+
const body = await response.json()
|
|
175
|
+
assert.equal(body.error.code, "invalid_image")
|
|
176
|
+
assert.match(body.error.message, /静默丢图|图片输入无效/)
|
|
177
|
+
assert.equal(calls.length, 0)
|
|
178
|
+
} finally {
|
|
179
|
+
globalThis.fetch = real
|
|
180
|
+
}
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
test("明确不可用模型不会先下载图片", async () => {
|
|
184
|
+
const calls = []
|
|
185
|
+
const real = globalThis.fetch
|
|
186
|
+
globalThis.fetch = async (...args) => {
|
|
187
|
+
calls.push(args)
|
|
188
|
+
throw new Error("不应出网")
|
|
189
|
+
}
|
|
190
|
+
try {
|
|
191
|
+
const loader = await loaderHook()(async () => AUTH)
|
|
192
|
+
const response = await loader.fetch(CHAT_URL, {
|
|
193
|
+
method: "POST",
|
|
194
|
+
headers: { "content-type": "application/json" },
|
|
195
|
+
body: JSON.stringify({
|
|
196
|
+
model: "kimi-k2.5",
|
|
197
|
+
messages: [
|
|
198
|
+
{ role: "user", content: [{ type: "image_url", image_url: { url: PUBLIC_IP_URL } }] },
|
|
199
|
+
],
|
|
200
|
+
}),
|
|
201
|
+
})
|
|
202
|
+
assert.equal(response.status, 404)
|
|
203
|
+
assert.equal((await response.json()).error.code, "model_unavailable")
|
|
204
|
+
assert.equal(calls.length, 0)
|
|
205
|
+
} finally {
|
|
206
|
+
globalThis.fetch = real
|
|
207
|
+
}
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
test("loopback / 私网 / 云元数据地址在出网前拦截", async () => {
|
|
211
|
+
for (const url of ["http://127.0.0.1/a.png", "http://169.254.169.254/latest/meta-data", "http://10.0.0.5/a.png", "http://192.168.1.1/a.png", "http://[::1]/a.png"]) {
|
|
212
|
+
const calls = []
|
|
213
|
+
await assert.rejects(
|
|
214
|
+
() => normalizeMessages(
|
|
215
|
+
[{ role: "user", content: [{ type: "image_url", image_url: { url } }] }],
|
|
216
|
+
async () => { calls.push(1); throw new Error("不应出网") },
|
|
217
|
+
),
|
|
218
|
+
InvalidImageError,
|
|
219
|
+
)
|
|
220
|
+
assert.equal(calls.length, 0, url)
|
|
221
|
+
}
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
test("域名解析到内网同样拦截(DNS 预检)", async () => {
|
|
225
|
+
const calls = []
|
|
226
|
+
await assert.rejects(
|
|
227
|
+
() => normalizeMessages(
|
|
228
|
+
[{ role: "user", content: [{ type: "image_url", image_url: { url: "https://example.com/a.png" } }] }],
|
|
229
|
+
async () => { calls.push(1); throw new Error("不应出网") },
|
|
230
|
+
PRIVATE_LOOKUP,
|
|
231
|
+
),
|
|
232
|
+
InvalidImageError,
|
|
233
|
+
)
|
|
234
|
+
assert.equal(calls.length, 0)
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
test("携带账号信息的图片 URL 拒绝", async () => {
|
|
238
|
+
await assert.rejects(
|
|
239
|
+
() => normalizeMessages([{ role: "user", content: [{ type: "image_url", image_url: { url: "https://user:pass@93.184.216.34/a.png" } }] }]),
|
|
240
|
+
InvalidImageError,
|
|
241
|
+
)
|
|
242
|
+
})
|
|
243
|
+
|
|
244
|
+
test("下载禁止重定向,重定向按失败处理", async () => {
|
|
245
|
+
let seen
|
|
246
|
+
await assert.rejects(
|
|
247
|
+
() => normalizeMessages(
|
|
248
|
+
[{ role: "user", content: [{ type: "image_url", image_url: { url: "https://example.com/a.png" } }] }],
|
|
249
|
+
async (url, init) => { seen = init; throw new TypeError("redirect mode is set to error") },
|
|
250
|
+
PUBLIC_LOOKUP,
|
|
251
|
+
),
|
|
252
|
+
InvalidImageError,
|
|
253
|
+
)
|
|
254
|
+
assert.equal(seen.redirect, "error")
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
test("合法 data URI 重编码后值不变;非 base64 data URI 拒绝", async () => {
|
|
258
|
+
const [message] = await normalizeMessages([
|
|
259
|
+
{ role: "user", content: [{ type: "image_url", image_url: { url: DATA_URI } }] },
|
|
260
|
+
])
|
|
261
|
+
assert.equal(message.content[0].image_url.url, DATA_URI)
|
|
262
|
+
await assert.rejects(
|
|
263
|
+
() => normalizeMessages([{ role: "user", content: [{ type: "image_url", image_url: { url: "data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>" } }] }]),
|
|
264
|
+
InvalidImageError,
|
|
265
|
+
)
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
test("超过 20 MiB 的 data URI 同样被上限拦截", async () => {
|
|
269
|
+
const huge = "A".repeat(Math.ceil((20 * 1024 * 1024) / 3) * 4 + 8)
|
|
270
|
+
await assert.rejects(
|
|
271
|
+
() => normalizeMessages([{ role: "user", content: [{ type: "image_url", image_url: { url: `data:image/png;base64,${huge}` } }] }]),
|
|
272
|
+
InvalidImageError,
|
|
273
|
+
)
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
// issue #34:DNS 过滤 / sinkhole 会把同一域名解析成「N 条公网 + 若干条 0.0.0.0」。
|
|
277
|
+
// 逐条否决会把完全正常的外链图一起拦掉,所以只在**一条公网记录都没有**时才拒。
|
|
278
|
+
|
|
279
|
+
test("解析结果混进 0.0.0.0(DNS sinkhole)时放行 —— 只要还有一条公网记录", async () => {
|
|
280
|
+
const mixedLookup = async () => [
|
|
281
|
+
{ address: "2606:4700::6810:84e5", family: 6 },
|
|
282
|
+
{ address: "0.0.0.0", family: 4 },
|
|
283
|
+
]
|
|
284
|
+
const calls = []
|
|
285
|
+
const messages = await normalizeMessages(
|
|
286
|
+
[{ role: "user", content: [{ type: "image_url", image_url: { url: "https://cdn.example.com/a.png" } }] }],
|
|
287
|
+
async (url) => {
|
|
288
|
+
calls.push(url)
|
|
289
|
+
return new Response(Buffer.from(PNG, "base64"), {
|
|
290
|
+
status: 200,
|
|
291
|
+
headers: { "content-type": "image/png" },
|
|
292
|
+
})
|
|
293
|
+
},
|
|
294
|
+
mixedLookup,
|
|
295
|
+
)
|
|
296
|
+
assert.equal(calls.length, 1, "有公网记录就该照常下载")
|
|
297
|
+
assert.equal(messages[0].content[0].image_url.url, DATA_URI)
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
test("解析结果全是保留地址时仍然拒(SSRF 面不变)", async () => {
|
|
301
|
+
const calls = []
|
|
302
|
+
await assert.rejects(
|
|
303
|
+
() => normalizeMessages(
|
|
304
|
+
[{ role: "user", content: [{ type: "image_url", image_url: { url: "https://example.com/a.png" } }] }],
|
|
305
|
+
async () => { calls.push(1); throw new Error("不应出网") },
|
|
306
|
+
async () => [
|
|
307
|
+
{ address: "0.0.0.0", family: 4 },
|
|
308
|
+
{ address: "127.0.0.1", family: 4 },
|
|
309
|
+
],
|
|
310
|
+
),
|
|
311
|
+
InvalidImageError,
|
|
312
|
+
)
|
|
313
|
+
assert.equal(calls.length, 0)
|
|
314
|
+
})
|