Files
LEOLI 8765cf8f9c atomcode 0.3.0: AtomGit CodingPlan provider plugin (LILEOO, #716)
Adds packages/atomcode (@magpie-community/opencode-atomcode-auth 0.3.0), an AtomGit CodingPlan provider: browser OAuth and AtomCode CLI sign-in (~/.atomcode/auth.toml), signed requests to AtomGit's own gateway, per-account models and quota, daily claim, a loopback-only diagnostics page, and OpenCode 1.x/2.x entries. Registry and README entries added. By LILEOO.

Reviewed at c57b099fff4689c0198c1c6414f04b27614b4092. The last blocking point (an inline TOML comment on a known line went into the uid and was written back) is fixed: values end at an unquoted #, quoted/escaped/literal strings keep their #, and a rewritten line keeps its comment.

Run on a separate test machine (Bun 1.4.2, sandbox HOME):
- PR head merged onto origin/main 24da52b8: bun scripts/check.mjs exit 0 (atomcode ok); whole-repo bun test 6103 pass, 0 fail. origin/main alone: 5942 pass, 0 fail.
- packages/atomcode at the head: bun test 161 pass; node --test test/*.test.mjs 161 pass.
- The new auth-file test against 55ff272's auth-file.mjs: red (1 fail), green at the head.
- Probe: the review's file (id = "123456" # c) parses to uid 123456 and renders back as id = "123456" # c; also #c with no space, CRLF with a tab before #, "1#2", and a literal-string id all round-trip to the same uid. At 55ff272 each gave a uid with the comment in it.

Not tried: a real AtomGit account. A [user] header with a trailing comment is refused ("no user id"), not corrupted; the CLI never writes one. First npm version needs an owner publish; until then magpie installs it from the dist branch.
2026-11-05 16:54:02 +08:00

135 lines
5.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test } from "node:test";
import assert from "node:assert/strict";
import {
credentialsOf,
originOf,
readCappedResponseText,
redactText,
trustedServiceURL,
trustedURL,
} from "../internal.mjs";
/**
* 这两个校验函数是"地址不许乱跑"的统一收口点:模型地址、端点配置、请求目标
* 都走同一套判定。用例钉住它的边界,改动时不会悄悄放宽。
*/
test("trustedURL:https + 白名单域名(含子域)被采纳", () => {
const domains = ["atomgit.com", "gitcode.com"];
assert.equal(trustedURL("https://llm-api.atomgit.com/v1", { domains }), "https://llm-api.atomgit.com/v1");
assert.equal(trustedURL("https://api.gitcode.com/api/v5", { domains }), "https://api.gitcode.com/api/v5");
assert.equal(trustedURL("https://atomgit.com", { domains }), "https://atomgit.com");
assert.equal(trustedURL(" https://acs.atomgit.com ", { domains }), "https://acs.atomgit.com");
});
test("trustedURL:明文、内嵌凭证、相近域名、非法输入一律拒绝", () => {
const domains = ["atomgit.com", "gitcode.com"];
assert.equal(trustedURL("http://acs.atomgit.com", { domains }), undefined);
assert.equal(trustedURL("https://user:pass@acs.atomgit.com", { domains }), undefined);
assert.equal(trustedURL("https://evil.example", { domains }), undefined);
assert.equal(trustedURL("https://evil-atomgit.com", { domains }), undefined);
assert.equal(trustedURL("https://acs.atomgit.com.evil.example", { domains }), undefined);
assert.equal(trustedURL("", { domains }), undefined);
assert.equal(trustedURL(undefined, { domains }), undefined);
assert.equal(trustedURL(123, { domains }), undefined);
assert.equal(trustedURL("https://acs.atomgit.com", {}), undefined, "没有白名单时不得放行");
assert.equal(
trustedURL("https://acs.atomgit.com:8443", { domains }),
undefined,
"非 443 端口必须拒绝",
);
});
test("trustedURL:基础端点默认拒绝 query/hash,登录跳转可显式放行", () => {
const domains = ["atomgit.com"];
assert.equal(trustedURL("https://atomgit.com/login?state=1", { domains }), undefined);
assert.equal(
trustedURL("https://atomgit.com/login?state=1", { domains, allowSearch: true }),
"https://atomgit.com/login?state=1",
);
});
test("trustedServiceURL:合法覆盖被采纳", () => {
assert.equal(
trustedServiceURL("https://api.gitcode.com/api/v6", { fallback: "https://api.gitcode.com/api/v5" }),
"https://api.gitcode.com/api/v6",
);
});
test("trustedServiceURL:非法值回退默认端点并上报", () => {
const rejected = [];
const fallback = "https://acs.atomgit.com";
const out = trustedServiceURL("https://evil.example", { fallback, onReject: (v) => rejected.push(v) });
assert.equal(out, fallback);
assert.deepEqual(rejected, ["https://evil.example"]);
});
test("trustedServiceURL:未配置时不当作被拒,直接给默认值", () => {
const rejected = [];
const fallback = "https://acs.atomgit.com";
assert.equal(trustedServiceURL(undefined, { fallback, onReject: (v) => rejected.push(v) }), fallback);
assert.equal(trustedServiceURL("", { fallback, onReject: (v) => rejected.push(v) }), fallback);
assert.deepEqual(rejected, []);
});
test("trustedServiceURL:上报回调自己抛错也不影响返回值", () => {
const out = trustedServiceURL("https://evil.example", {
fallback: "https://acs.atomgit.com",
onReject: () => {
throw new Error("logger exploded");
},
});
assert.equal(out, "https://acs.atomgit.com");
});
test("originOf:协议与端口进入比较,非法输入 → undefined", () => {
assert.equal(originOf("https://llm-api.atomgit.com/v1/chat/completions"), "https://llm-api.atomgit.com");
assert.notEqual(
originOf("http://llm-api.atomgit.com/v1"),
originOf("https://llm-api.atomgit.com/v1"),
"明文降级必须与 https 判定不同",
);
assert.notEqual(
originOf("https://llm-api.atomgit.com:8443/v1"),
originOf("https://llm-api.atomgit.com/v1"),
"不同端口是不同来源",
);
assert.equal(originOf("not a url"), undefined);
assert.equal(originOf(undefined), undefined);
});
test("credentialsOf:缺少 access 时无法签名,uid 统一转字符串", () => {
assert.equal(credentialsOf({}), undefined);
assert.equal(credentialsOf({ access: "" }), undefined);
assert.equal(credentialsOf(null), undefined);
assert.equal(credentialsOf({ access: "tok", uid: 42 }).uid, "42");
});
test("redactText:令牌、邮箱和长密钥都不会进入日志", () => {
const secret = "a".repeat(64);
const text = redactText(
`access_token=access-secret refresh_token: refresh-secret Bearer bearer-secret alice@example.com ${secret}`,
);
assert.match(text, /access_token=\[redacted\]/);
assert.match(text, /refresh_token=\[redacted\]/);
assert.doesNotMatch(text, /access-secret|refresh-secret|bearer-secret|alice@example\.com/);
assert.doesNotMatch(text, new RegExp(secret));
});
test("readCappedResponseText:流式读取超过上限时立即取消", async () => {
let pulls = 0;
const chunk = new Uint8Array(64 * 1024);
const stream = new ReadableStream({
pull(controller) {
pulls += 1;
controller.enqueue(chunk);
},
});
await assert.rejects(
() => readCappedResponseText(new Response(stream), { label: "metadata" }),
/metadata exceeds 1 MiB/,
);
assert.ok(pulls <= 30, `read too far: ${pulls} chunks`);
});