Files
yihheng 04f3608d49 sensenova 0.1.0: SenseNova Token Plan provider plugin (#577, yihheng)
A new provider plugin, @magpie-community/opencode-sensenova-auth 0.1.0, for the SenseNova (SenseTime 日日新) Token Plan, provider id `sensenova-token-plan`.

- Sign-in: a console sign-in (OIDC + PKCE). The password is sealed to JWE under the platform's JWKS key and is not stored.
- Quota: the account's credit pools (5h/7d windows, expiring grants) from pool-usage.
- Chat: with an optional pasted API key, it serves the plan's chat models at token.sensenova.cn/v1. The list is filtered per account to models the plan covers.
- Also adds a registry.json entry and a root README row.

Hosts:
- platform.sensenova.cn gets the access JWT.
- iam.sensecoreapi.cn gets the sealed password.
- signin.sensecore.cn gets the refresh token and the code.
- token.sensenova.cn gets the API key.

There are no dependencies and no install scripts.

Run at head 3804ab46ae6c6c16d393e5dc08d6ae81c63da668 on mote (macOS 27.2, Bun 1.4.2), with a fresh HOME:
- `bun test packages/sensenova`: 43 pass, 0 fail.
- `bun scripts/check.mjs sensenova` passed.
- Full `bun test`: 4455 pass, 9 skip, 0 fail.
- Broken by hand: I removed each of the two `redirect: "manual"` lines that the last review found untested (IAM login, code exchange). Each time, its new 307 test went red (42/1). The earlier reviews' reverts went red at 0aa00b5 and 10984cf.

Not tried: a real sign-in, quota read or chat. There is no SenseNova account. Unauthenticated probes of the real hosts (in the last review) match the walk's shape.

This is the first version, not on npm yet, so the dist branch serves it until it is published.
2026-11-02 03:45:35 +08:00

96 lines
4.1 KiB
JavaScript

// The walk's guards, tested as the pure helpers they are. The fake gives every
// host a single origin, so it cannot show a cookie crossing from one host to
// another, a downgrade of a *production* endpoint, or a walk that runs out of
// hops onto an off-site URL. These do, against the real endpoint values.
import "./nonet.mjs"
import { afterEach, expect, test } from "bun:test"
import { _internal } from "./index.mjs"
const { allowedHost, newJar, cookieHeader, collectCookies, followUntil, ENDPOINTS } = _internal
const PROD = {
console: "https://platform.sensenova.cn",
iam: "https://iam.sensecoreapi.cn",
token: "https://signin.sensecore.cn/oauth2/token",
jwks: "https://signin.sensecore.cn/.well-known/jwks.json",
api: "https://token.sensenova.cn/v1",
}
let was
afterEach(() => {
if (was) Object.assign(ENDPOINTS, was)
was = undefined
})
function production() {
was = { ...ENDPOINTS }
Object.assign(ENDPOINTS, PROD)
}
test("an endpoint authorises its own scheme, not merely its host", () => {
production()
expect(allowedHost("https://platform.sensenova.cn/oauth2/auth")).toBe(true)
// the same host over http is a downgrade of it, not it. Matching the host
// alone let this through the endpoint exemption, ahead of the https check
// below — and the jar keys off .host too, so the session cookie would have
// travelled in the clear. The fake never caught this because its endpoints
// are 127.0.0.1, where the exemption doesn't match the name under test.
expect(allowedHost("http://platform.sensenova.cn/continue")).toBe(false)
expect(allowedHost("http://token.sensenova.cn/v1/models")).toBe(false)
expect(allowedHost("http://signin.sensecore.cn/oauth2/token")).toBe(false)
})
test("the SenseTime domains are walkable and look-alikes are not", () => {
production()
expect(allowedHost("https://console.sensenova.cn/x")).toBe(true)
expect(allowedHost("https://evil.example/?login_challenge=stolen")).toBe(false)
// a suffix that reads like the domain is not the domain
expect(allowedHost("https://sensenova.cn.evil.example/x")).toBe(false)
expect(allowedHost("https://notsensecore.cn/x")).toBe(false)
expect(allowedHost("not a url")).toBe(false)
})
test("a cookie goes back only to the host that set it", () => {
const jar = newJar()
const set = (url, line) => collectCookies(new Response(null, { headers: { "set-cookie": line } }), jar, url)
set("https://platform.sensenova.cn/oauth2/auth", "hydra_session=s-1; Path=/")
set("https://iam.sensecoreapi.cn/iam/login", "iam_csrf=c-9; Path=/")
expect(cookieHeader(jar, "https://platform.sensenova.cn/callback")).toBe("hydra_session=s-1")
expect(cookieHeader(jar, "https://iam.sensecoreapi.cn/iam/authn/v1/auth/nova/login")).toBe("iam_csrf=c-9")
// neither leaks to the other, and a third host gets nothing at all
expect(cookieHeader(jar, "https://evil.example/")).toBe("")
// dropping the url is what once sent the IAM request out with no cookie: the
// answer is an empty header, never every cookie in the jar
expect(cookieHeader(jar, undefined)).toBe("")
})
test("a walk that runs out of hops does not hand back an off-site target", async () => {
// the check inside the loop stops the fetching; the one after it has to stop
// the *returning*. To reach it the off-site URL must arrive on the last hop,
// so the loop has no iteration left in which to notice.
production()
let hops = 0
let base = ""
const server = Bun.serve({
port: 0,
hostname: "127.0.0.1",
fetch() {
hops++
if (hops >= 8)
return new Response(null, { status: 302, headers: { location: "https://evil.example/final?login_challenge=stolen" } })
return new Response(null, { status: 302, headers: { location: `${base}/step` } })
},
})
base = `http://127.0.0.1:${server.port}`
Object.assign(ENDPOINTS, { console: base })
try {
await expect(followUntil(`${base}/start`, /login_challenge=/, newJar())).rejects.toThrow(
"left the SenseNova hosts",
)
// it really did spend the whole budget before the last hop answered
expect(hops).toBe(8)
} finally {
server.stop(true)
}
})