mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-11-30 16:47:16 +08:00
* 新增按实例对外地址校验回调的 Web OAuth 客户端 * 支持为 Web OAuth 单独登记本机联调回调地址 * 让授权码客户端按已登记本机来源使用任意回调路径
162 lines
5.9 KiB
JavaScript
162 lines
5.9 KiB
JavaScript
import assert from "node:assert/strict"
|
|
import test from "node:test"
|
|
import { isSafeClientLaunchURL } from "../src/lib/client-callback.ts"
|
|
|
|
test("授权链接仅允许当前客户端已登记的回调地址", () => {
|
|
for (const [client, redirect] of [
|
|
["monkeyai-desktop", "monkeyai-desktop://oauth/callback"],
|
|
["monkeyai-mobile", "monkeyai-mobile://oauth/callback"],
|
|
["monkeyai-desktop", "http://127.0.0.1:54321/oauth/callback"],
|
|
["monkeyai-desktop", "http://[::1]:54321/oauth/callback"],
|
|
["monkeyai-desktop", "http://127.0.0.1:80/oauth/callback"],
|
|
["monkeyai-desktop", "http://localhost:54321/oauth/callback"],
|
|
["monkeyai-desktop", "http://localhost:80/oauth/callback"],
|
|
]) {
|
|
assert.equal(
|
|
isSafeClientLaunchURL(
|
|
client,
|
|
redirect,
|
|
`${redirect}?code=one-time-code&state=client-state`
|
|
),
|
|
true
|
|
)
|
|
for (const launch of [
|
|
"https://oauth-audit.invalid/callback?code=code&state=state",
|
|
"javascript:alert(1)//?code=code&state=state",
|
|
"data:text/html,test?code=code&state=state",
|
|
"//attacker.example/callback?code=code&state=state",
|
|
"monkeyai-desktop://other/callback?code=code&state=state",
|
|
`${redirect}?code=code&state=state#fragment`,
|
|
`${redirect}?code=code&state=state&next=https://attacker.example`,
|
|
`${redirect}?code=first&code=second&state=state`,
|
|
`${redirect}?code=code&state=first&state=second`,
|
|
`${redirect}?code=&state=state`,
|
|
`${redirect}?code=code&state=`,
|
|
redirect,
|
|
])
|
|
assert.equal(
|
|
isSafeClientLaunchURL(client, redirect, launch),
|
|
false,
|
|
`${redirect}: ${launch}`
|
|
)
|
|
}
|
|
})
|
|
|
|
test("Web 授权只跳转到当前实例的客户端回调", () => {
|
|
const page = "https://ai.example.com:18443/client-login?request_id=test"
|
|
const callback = "https://ai.example.com:18443/client/oauth/callback"
|
|
const query = "?code=one-time-code&state=client-state"
|
|
const safe = (redirect, launch) =>
|
|
isSafeClientLaunchURL("monkeyai-web", redirect, launch, page)
|
|
assert.equal(safe(callback, callback + query), true)
|
|
for (const other of [
|
|
"https://ai.example.com/client/oauth/callback",
|
|
"https://control.example.com:18443/client/oauth/callback",
|
|
"https://ai.example.com:18443/oauth/callback",
|
|
"monkeyai-desktop://oauth/callback",
|
|
"http://localhost:54321/oauth/callback",
|
|
]) {
|
|
assert.equal(safe(other, other + query), false)
|
|
assert.equal(safe(callback, other + query), false)
|
|
}
|
|
assert.equal(safe(callback, callback + "?state=client-state"), false)
|
|
})
|
|
|
|
test("授权码客户端共用本机来源,跳转仍绑定原路径", () => {
|
|
const page = "https://ai.example.com/client-login"
|
|
const origin = "http://localhost:5173"
|
|
const local = origin + "/custom/done"
|
|
const query = "?code=one-time-code&state=client-state"
|
|
for (const client of ["monkeyai-web", "monkeyai-desktop", "monkeyai-mobile"]) {
|
|
const safe = (uri, target, allowed) =>
|
|
isSafeClientLaunchURL(client, uri, target + query, page, allowed)
|
|
assert.equal(safe(local, local, []), false)
|
|
assert.equal(safe(local, local, [origin]), true)
|
|
assert.equal(safe(local, origin + "/other", [origin]), false)
|
|
for (const other of [local.replace("5173", "5174"), local.replace("http:", "https:"), local + "#"]) {
|
|
assert.equal(safe(other, other, [origin]), false)
|
|
}
|
|
}
|
|
for (const client of ["unknown", "monkeyai-runtime"]) {
|
|
assert.equal(isSafeClientLaunchURL(client, local, local + query, page, [origin]), false)
|
|
}
|
|
})
|
|
|
|
test("unregistered destinations and clients cannot authorize a manual launch link", () => {
|
|
for (const redirect of [
|
|
"https://oauth-audit.invalid/callback",
|
|
"http://localhost.attacker.example:54321/oauth/callback",
|
|
"http://localhost@attacker.example:54321/oauth/callback",
|
|
"http://attacker@localhost:54321/oauth/callback",
|
|
"http://localhost/oauth/callback",
|
|
"http://localhost:0/oauth/callback",
|
|
"http://localhost:65536/oauth/callback",
|
|
"http://localhost:05432/oauth/callback",
|
|
"https://localhost:54321/oauth/callback",
|
|
"http://localhost:54321/other",
|
|
"http://localhost:54321/oauth/%63allback",
|
|
"http://localhost:54321/oauth/callback?next=external",
|
|
"http://localhost:54321/oauth/callback#fragment",
|
|
"http://127.0.0.1.attacker.example:54321/oauth/callback",
|
|
"http://127.0.0.1@attacker.example:54321/oauth/callback",
|
|
"http://attacker@127.0.0.1:54321/oauth/callback",
|
|
"http://127.0.0.1/oauth/callback",
|
|
"http://127.0.0.1:0/oauth/callback",
|
|
"http://127.0.0.1:65536/oauth/callback",
|
|
"http://127.0.0.1:05432/oauth/callback",
|
|
"http://127.0.0.1:54321/other",
|
|
"http://127.0.0.1:54321/oauth/%63allback",
|
|
"http://127.0.0.1:54321/oauth/callback?",
|
|
"http://127.0.0.1:54321/oauth/callback#",
|
|
])
|
|
assert.equal(
|
|
isSafeClientLaunchURL(
|
|
"monkeyai-desktop",
|
|
redirect,
|
|
`${redirect}?code=code&state=state`
|
|
),
|
|
false,
|
|
redirect
|
|
)
|
|
assert.equal(
|
|
isSafeClientLaunchURL(
|
|
"monkeyai-mobile",
|
|
"http://127.0.0.1:54321/oauth/callback",
|
|
"http://127.0.0.1:54321/oauth/callback?code=code&state=state"
|
|
),
|
|
false
|
|
)
|
|
assert.equal(
|
|
isSafeClientLaunchURL(
|
|
"monkeyai-mobile",
|
|
"http://localhost:54321/oauth/callback",
|
|
"http://localhost:54321/oauth/callback?code=code&state=state"
|
|
),
|
|
false
|
|
)
|
|
assert.equal(
|
|
isSafeClientLaunchURL(
|
|
"monkeyai-desktop",
|
|
"http://localhost:54321/oauth/callback",
|
|
"http://127.0.0.1:54321/oauth/callback?code=code&state=state"
|
|
),
|
|
false
|
|
)
|
|
assert.equal(
|
|
isSafeClientLaunchURL(
|
|
"unknown",
|
|
"monkeyai-desktop://oauth/callback",
|
|
"monkeyai-desktop://oauth/callback?code=code&state=state"
|
|
),
|
|
false
|
|
)
|
|
assert.equal(
|
|
isSafeClientLaunchURL(
|
|
"monkeyai-desktop",
|
|
"http://127.0.0.1:54321/oauth/callback",
|
|
"http://127.0.0.1:54322/oauth/callback?code=code&state=state"
|
|
),
|
|
false
|
|
)
|
|
})
|