Files
OmniRoute/tests/unit/claude-oauth-tool-cloak.test.ts
Diego Rodrigues de Sa e Souza e7f6b1d130 feat(radar): flag-gated signed free-model catalog overlay (#9515)
* feat(dashboard): add RADAR_ENABLED flag (default off)

* feat(db): radar feed cache + settings with encrypted supporter key

* feat(radar): signed feed sync with pinned key and version floor

- feedSchema.ts: Zod v4 schema mirroring the server feed format
  (discriminated union on budget.kind, enum constraints, etc.)
- pinnedKeys.ts: Ed25519 SPKI-DER pinned key + env override for forks
- verify.ts: signature verification over exact wire bytes, never throws
- sync.ts: full download/verify/validate/cache pipeline with injectable
  deps, feature-flag gate, opt-in gate, version floor (numeric compare),
  and sanitized error reasons (no stack traces)
- 40 tests covering: contract hash, key handling, sig verification,
  schema validation, version compare, all sync paths (disabled, opt_out,
  invalid_signature, invalid_schema, stale, updated, error), auth header
  injection, and cache-untouched assertions for every failure mode

* feat(radar): read-time overlay merge rules over the free catalog

Pure function applyFeed() merges the cached Radar feed over the static
baseline catalog at read time, honoring 4 rules:

1. Feed never overwrites a local override field.
2. enabled:false disables the entry with disabledBy:"radar" provenance.
3. User-added entry NOT in the feed survives untouched.
4. User deletion tombstone prevents feed resurrection.

getRadarCatalog() accessor in index.ts: flag off / no cache / corrupt
payload all fall back to baseline. Valid cache applies the overlay and
returns feed metadata (version, tier, fetchedAt).

TDD: 19 tests (4 rules + dedup + origin + accessor flag/cache/corrupt/
valid/bad-feed + baselineToMergedEntries converter).

* feat(dashboard): radar catalog and guided setup screens

- API routes: GET /api/radar/catalog, POST /api/radar/sync, POST /api/radar/settings
  - All gated on RADAR_ENABLED flag (404 when off)
  - Error responses via buildErrorBody(), never raw stack/message
  - Settings never echoes clear supporter key (masked omr_****<last4>)
  - Sync delegates to syncRadar() server-side, never proxies feed URL
- Dashboard pages:
  - /dashboard/radar: 4 states (flag off, opt-in pending, empty, populated)
  - /dashboard/radar/setup?provider=X: guided setup with steps, key URL, test connection
  - Uses existing Card component and next-intl patterns
- Sidebar: radar entry in costs group with icon
- i18n: pt-BR and en keys for radarPage and radarSetupPage namespaces
- Tests:
  - radar-api-routes.test.ts: 11 tests (flag-off 404, flag-on shape, error sanitization)
  - radar-page-state.test.ts: 5 tests (pure state logic)
  - All 90 radar tests pass (including prior 74)

* docs(radar): module doc and flag-off inertia test

Add docs/frameworks/RADAR.md covering the flag gate, the separate data-sync
opt-in and privacy promise, the Ed25519 signature/pinned-key security model,
tiers, the read-time overlay merge rules, and the self-hosting env vars —
plus index entries in CLAUDE.md/AGENTS.md/docs/README.md/REPOSITORY_MAP.md.

Document RADAR_FEED_URL and RADAR_FEED_PUBKEY in .env.example and
docs/reference/ENVIRONMENT.md to satisfy check:env-doc-sync, which was
failing on this branch since the sync.ts commit added the reads.

Add tests/unit/radar-inertia.test.ts as the single canonical place asserting
the "RADAR_ENABLED off => zero behavioral delta" claim end to end: the three
/api/radar/* routes 404, the flag resolves to the definition default with no
override, getRadarCatalog() returns exactly the baseline without touching the
cache, and computeFreeModelTotals() keeps its pinned values with the Radar
module imported alongside it.

* fix(db): renumber radar migration to 135 after collision with 134

The base branch introduced 134_proxy_logs_egress_ip while this branch carried
134_radar_cache_settings; the migration runner rejects duplicate numeric prefixes.
This migration has never been applied to a real database (the PR is unmerged), so
no retroactive isSchemaAlreadyApplied guard is needed.

* i18n(radar): translate radar catalog and setup strings to all locales

The UI-coverage ratchet measures (present - placeholder) / total_en, so the
__MISSING__ sentinels that i18n:sync-ui writes do not count as covered — only
real translations restore the metric. Scoped to this PR's namespaces
(radarPage, radarSetupPage, sidebar.radar*) instead of a bulk sync, which would
have pulled ~978 unrelated pending keys into this diff.

Placeholders and code identifiers verified preserved across all 1682 strings.

* fix(radar): trust the served-tier header instead of the signed body field

The signed feed body always carries tier:"live" by design (one signed
artifact per version — rewriting the field server-side per request
would break the exact-bytes Ed25519 signature). The server now returns
the tier ACTUALLY served via the x-omniroute-feed-tier response
header, so free users on a delayed community snapshot no longer see
"Ao vivo (tempo real)" in the UI.

sync.ts now reads and validates that header (falling back to the
body's tier only when the header is absent or holds an unrecognized
value) and stores the served tier in the cache; index.ts already
surfaces cache.tier to the UI unchanged.

* test(combo): shorten an assert message that exceeded the line limit

The assertion added by #9507 was 104 chars, so prettier reformatted it into
five lines on the next commit that touched the file, pushing it past its
frozen size (3449) and failing check:file-size. The message is shortened
(the issue reference stays in the comment directly above); the assertion
itself is unchanged, and the file is back to 3448 lines and prettier-clean.

* i18n(radar): use the canonical zh-TW glossary terms

The machine translation produced retired renderings the glossary gate blocks:
供應商 for provider (canonical 提供者) and 文檔 for documentation (canonical 文件).
Fixed across the 11 affected radar strings; tests/unit/i18n-glossary-consistency-check.test.ts
is back to 17/17.

* fix(radar): point the default feed URL at the domain that exists

radar.omniroute.dev was a placeholder for a domain that was never registered,
so an out-of-the-box sync would fail DNS resolution for every user. The live
feed is served from radar.omniroute.online (the subdomain the design always
specified), now behind Cloudflare TLS. Forks still override it via
RADAR_FEED_URL.

---------

Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com>
2026-08-06 05:58:58 -03:00

468 lines
18 KiB
TypeScript

/**
* Native Claude OAuth tool cloak + schema sanitizer.
*
* Anthropic's first-party Messages API rejects native-Claude-OAuth requests
* that carry (a) invalid tool input_schemas (truncation placeholders / non-array
* keywords) or (b) tool names it fingerprints as a third-party agent harness —
* both surfaced as a misleading `400 out of extra usage` placeholder. These
* tests cover the request-side sanitizer + name cloak; the response side is
* reversed via the existing per-request _toolNameMap.
*/
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import {
cloakThirdPartyToolNames,
needsThirdPartyCloak,
isAnthropicServerToolType,
} from "../../open-sse/services/claudeCodeToolRemapper.ts";
import {
sanitizeClaudeToolSchema,
sanitizeClaudeToolSchemas,
} from "../../open-sse/translator/helpers/schemaCoercion.ts";
import { stripVersionedToolModelPrefix } from "../../open-sse/executors/base.ts";
type AnyRecord = Record<string, unknown>;
const schemaOf = (tools: unknown, i = 0): AnyRecord =>
(tools as AnyRecord[])[i].input_schema as AnyRecord;
describe("sanitizeClaudeToolSchemas", () => {
it("drops a non-array enum placeholder", () => {
const tools = [
{
name: "x",
input_schema: { type: "object", properties: { m: { type: "string", enum: "[MaxDepth]" } } },
},
];
const props = (schemaOf(sanitizeClaudeToolSchemas(tools)).properties as AnyRecord)
.m as AnyRecord;
assert.equal("enum" in props, false);
});
it("coerces an index-keyed object enum into an array", () => {
const s = sanitizeClaudeToolSchema({
type: "object",
properties: { a: { type: "string", enum: { "0": "x", "1": "y" } } },
}) as AnyRecord;
assert.deepEqual(((s.properties as AnyRecord).a as AnyRecord).enum, ["x", "y"]);
});
it("replaces a placeholder property value with a permissive schema", () => {
const s = sanitizeClaudeToolSchema({
type: "object",
properties: { a: "[MaxDepth]" },
}) as AnyRecord;
assert.deepEqual((s.properties as AnyRecord).a, {});
});
it("leaves a valid schema intact", () => {
const input = { type: "object", properties: { a: { type: "string" } }, required: ["a"] };
assert.deepEqual(sanitizeClaudeToolSchema(input), input);
});
});
describe("cloakThirdPartyToolNames", () => {
it("aliases a blacklisted name and tracks the reverse map", () => {
const body: AnyRecord = { tools: [{ name: "mixture_of_agents" }] };
cloakThirdPartyToolNames(body);
assert.equal((body.tools as AnyRecord[])[0].name, "MixtureOfAgents");
assert.equal(
(body._toolNameMap as Map<string, string>).get("MixtureOfAgents"),
"mixture_of_agents"
);
});
it("maps known harness names to Claude Code canonical names", () => {
const body: AnyRecord = {
tools: [{ name: "read_file" }, { name: "write_file" }, { name: "terminal" }],
};
cloakThirdPartyToolNames(body);
assert.deepEqual(
(body.tools as AnyRecord[]).map((t) => t.name),
["Read", "Write", "Bash"]
);
});
it("PascalCases unmapped snake_case names", () => {
const body: AnyRecord = { tools: [{ name: "honcho_profile" }, { name: "lcm_expand_query" }] };
cloakThirdPartyToolNames(body);
assert.deepEqual(
(body.tools as AnyRecord[]).map((t) => t.name),
["HonchoProfile", "LcmExpandQuery"]
);
});
it("leaves genuine Claude Code tool names untouched", () => {
const body: AnyRecord = { tools: [{ name: "Bash" }, { name: "Read" }, { name: "TodoWrite" }] };
cloakThirdPartyToolNames(body);
assert.deepEqual(
(body.tools as AnyRecord[]).map((t) => t.name),
["Bash", "Read", "TodoWrite"]
);
assert.equal((body._toolNameMap as Map<string, string> | undefined)?.size ?? 0, 0);
});
it("dedupes canonical-name collisions", () => {
const body: AnyRecord = { tools: [{ name: "search_files" }, { name: "grep_search" }] };
cloakThirdPartyToolNames(body);
assert.deepEqual(
(body.tools as AnyRecord[]).map((t) => t.name),
["Grep", "Grep2"]
);
});
it("remaps tool_use blocks in message history consistently", () => {
const body: AnyRecord = {
tools: [{ name: "mixture_of_agents" }],
messages: [{ role: "assistant", content: [{ type: "tool_use", name: "mixture_of_agents" }] }],
};
cloakThirdPartyToolNames(body);
const block = ((body.messages as AnyRecord[])[0].content as AnyRecord[])[0];
assert.equal(block.name, "MixtureOfAgents");
});
it("does not leak _toolNameMap into the serialized request body", () => {
const body: AnyRecord = { tools: [{ name: "mixture_of_agents" }] };
cloakThirdPartyToolNames(body);
assert.equal(JSON.stringify(body).includes("_toolNameMap"), false);
});
it("needsThirdPartyCloak only flags non-Claude-Code names", () => {
assert.equal(needsThirdPartyCloak("Bash"), false);
assert.equal(needsThirdPartyCloak("TodoWrite"), false);
assert.equal(needsThirdPartyCloak("read_file"), true);
assert.equal(needsThirdPartyCloak("mixture_of_agents"), true);
});
it("needsThirdPartyCloak leaves mcp__ namespace untouched (#4861)", () => {
// Genuine Claude Code MCP names Anthropic accepts natively; cloaking them
// caused round-trip "Tool reference 'mcp__…' not found" 400s on claude OAuth.
assert.equal(needsThirdPartyCloak("mcp__filesystem__read_file"), false);
assert.equal(needsThirdPartyCloak("mcp__github__create_issue"), false);
assert.equal(needsThirdPartyCloak("mcp__server"), false);
});
it("preserves the reserved name of a versioned Anthropic server tool", () => {
const body: AnyRecord = {
tools: [{ type: "web_search_20250305", name: "web_search", max_uses: 5 }],
};
cloakThirdPartyToolNames(body);
// Anthropic requires tools.N.web_search_20250305.name === "web_search".
assert.equal((body.tools as AnyRecord[])[0].name, "web_search");
// No reverse-map entry needed because nothing was cloaked.
assert.equal(
(body._toolNameMap as Map<string, string> | undefined)?.has("WebSearch") ?? false,
false
);
});
it("still cloaks a genuine third-party tool sitting next to a server tool", () => {
const body: AnyRecord = {
tools: [{ type: "web_search_20250305", name: "web_search" }, { name: "mixture_of_agents" }],
};
cloakThirdPartyToolNames(body);
assert.equal((body.tools as AnyRecord[])[0].name, "web_search");
assert.equal((body.tools as AnyRecord[])[1].name, "MixtureOfAgents");
});
it("isAnthropicServerToolType detects versioned + non-versioned server tools", () => {
assert.equal(isAnthropicServerToolType("web_search_20250305"), true);
assert.equal(isAnthropicServerToolType("code_execution_20250522"), true);
assert.equal(isAnthropicServerToolType("web_search"), true);
assert.equal(isAnthropicServerToolType("web_search_preview"), true);
// Not server tools — must remain cloakable.
assert.equal(isAnthropicServerToolType("mixture_of_agents"), false);
assert.equal(isAnthropicServerToolType("Bash"), false);
assert.equal(isAnthropicServerToolType(undefined), false);
});
});
describe("sanitizeClaudeToolSchemas — boolean schema preservation", () => {
it("preserves additionalProperties: false (canonical lock-down)", () => {
const s = sanitizeClaudeToolSchema({
type: "object",
properties: { a: { type: "string" } },
additionalProperties: false,
}) as AnyRecord;
assert.equal(s.additionalProperties, false);
});
it("preserves additionalProperties: true", () => {
const s = sanitizeClaudeToolSchema({
type: "object",
additionalProperties: true,
}) as AnyRecord;
assert.equal(s.additionalProperties, true);
});
it("preserves boolean property schemas under properties", () => {
const s = sanitizeClaudeToolSchema({
type: "object",
properties: { allowed: true, denied: false },
}) as AnyRecord;
const props = s.properties as AnyRecord;
assert.equal(props.allowed, true);
assert.equal(props.denied, false);
});
it("preserves boolean unevaluatedProperties", () => {
const s = sanitizeClaudeToolSchema({
type: "object",
unevaluatedProperties: false,
}) as AnyRecord;
assert.equal(s.unevaluatedProperties, false);
});
it("still replaces a placeholder string in a slot key with permissive {}", () => {
const s = sanitizeClaudeToolSchema({
type: "object",
additionalProperties: "[MaxDepth]",
}) as AnyRecord;
assert.deepEqual(s.additionalProperties, {});
});
});
describe("cloakThirdPartyToolNames — defensive null guards", () => {
it("tolerates null/undefined entries in tools[]", () => {
const body: AnyRecord = {
tools: [null, { name: "read_file" }, undefined, { name: "Bash" }],
};
cloakThirdPartyToolNames(body);
const names = (body.tools as Array<AnyRecord | null | undefined>).map((t) => t?.name);
assert.deepEqual(names, [undefined, "Read", undefined, "Bash"]);
});
it("tolerates null/undefined entries in messages[]", () => {
const body: AnyRecord = {
tools: [{ name: "read_file" }],
messages: [
null,
{ role: "assistant", content: [{ type: "tool_use", name: "read_file" }] },
undefined,
],
};
cloakThirdPartyToolNames(body);
const block = ((body.messages as Array<AnyRecord>)[1].content as Array<AnyRecord>)[0];
assert.equal(block.name, "Read");
});
});
describe("cloakThirdPartyToolNames — non-mutating + skip option", () => {
it("does not mutate the caller's input tool objects", () => {
const original: AnyRecord = { name: "read_file" };
const body: AnyRecord = { tools: [original] };
cloakThirdPartyToolNames(body);
assert.equal(original.name, "read_file"); // input object untouched
assert.equal((body.tools as AnyRecord[])[0].name, "Read"); // body.tools reassigned with a clone
});
it("does not mutate the caller's input message blocks", () => {
const block: AnyRecord = { type: "tool_use", name: "read_file" };
const body: AnyRecord = {
tools: [{ name: "read_file" }],
messages: [{ role: "assistant", content: [block] }],
};
cloakThirdPartyToolNames(body);
assert.equal(block.name, "read_file"); // input block untouched
const out = ((body.messages as AnyRecord[])[0].content as AnyRecord[])[0];
assert.equal(out.name, "Read");
});
it("leaves names matched by the skip predicate untouched", () => {
const body: AnyRecord = { tools: [{ name: "mcp_call" }, { name: "read_file" }] };
cloakThirdPartyToolNames(body, { skip: (n) => n.startsWith("mcp_") });
assert.deepEqual(
(body.tools as AnyRecord[]).map((t) => t.name),
["mcp_call", "Read"]
);
});
});
describe("review fixes — schema sanitizer scalar / default / numeric", () => {
it("keeps a placeholder in a scalar annotation keyword as a scalar (not {})", () => {
const s = sanitizeClaudeToolSchema({
type: "object",
description: "[Object]",
properties: { a: { type: "string", description: "[Truncated]" } },
}) as AnyRecord;
assert.equal(s.description, "[Object]");
assert.equal(((s.properties as AnyRecord).a as AnyRecord).description, "[Truncated]");
});
it("preserves the valid `default` keyword on the Claude path", () => {
const s = sanitizeClaudeToolSchema({
type: "object",
properties: {
mode: { type: "string", default: "replace" },
all: { type: "boolean", default: false },
},
}) as AnyRecord;
const p = s.properties as AnyRecord;
assert.equal((p.mode as AnyRecord).default, "replace");
assert.equal((p.all as AnyRecord).default, false);
});
it("coerces numeric-string constraints inside contains (not only items)", () => {
const s = sanitizeClaudeToolSchema({
type: "array",
contains: { type: "object", properties: { n: { type: "integer", minimum: "5" } } },
}) as AnyRecord;
const n = ((s.contains as AnyRecord).properties as AnyRecord).n as AnyRecord;
assert.equal(n.minimum, 5);
});
it("still coerces a placeholder to {} in a real subschema slot", () => {
const s = sanitizeClaudeToolSchema({
type: "object",
additionalProperties: "[MaxDepth]",
}) as AnyRecord;
assert.deepEqual(s.additionalProperties, {});
});
});
describe("review fixes — established aliases + kill-switch", () => {
it("uses the established Claude Code aliases on the cloak path", () => {
const body: AnyRecord = {
tools: [
{ name: "subagents" },
{ name: "session_status" },
{ name: "webfetch" },
{ name: "todowrite" },
],
};
cloakThirdPartyToolNames(body);
assert.deepEqual(
(body.tools as AnyRecord[]).map((t) => t.name),
["SubDispatch", "CheckStatus", "WebFetch", "TodoWrite"]
);
});
it("CLAUDE_DISABLE_TOOL_NAME_CLOAK=true disables the cloak at the function level", () => {
const prev = process.env.CLAUDE_DISABLE_TOOL_NAME_CLOAK;
process.env.CLAUDE_DISABLE_TOOL_NAME_CLOAK = "true";
try {
const body: AnyRecord = { tools: [{ name: "mixture_of_agents" }] };
const map = cloakThirdPartyToolNames(body);
assert.equal((body.tools as AnyRecord[])[0].name, "mixture_of_agents");
assert.equal(map.size, 0);
} finally {
if (prev === undefined) delete process.env.CLAUDE_DISABLE_TOOL_NAME_CLOAK;
else process.env.CLAUDE_DISABLE_TOOL_NAME_CLOAK = prev;
}
});
});
describe("native claude OAuth path — versioned built-in tool model prefix stripping", () => {
// Exercises the REAL production helper from base.ts (not a re-implementation),
// so reverting the fix in base.ts breaks these assertions.
it("strips cc/ prefix from advisor_20260301 model field", () => {
const tools: AnyRecord[] = [
{ type: "advisor_20260301", name: "advisor", model: "cc/claude-opus-4-8" },
{ type: "bash_20250124", name: "Bash", model: "kiro/claude-opus-4.8" },
{ name: "Read", input_schema: { type: "object", properties: {} } },
];
stripVersionedToolModelPrefix(tools);
assert.equal(tools[0].model, "claude-opus-4-8", "cc/ prefix stripped from advisor model");
assert.equal(tools[1].model, "claude-opus-4.8", "kiro/ prefix stripped from bash model");
assert.equal("model" in tools[2], false, "regular tool untouched");
});
it("strips multi-segment prefix (claude/claude-sonnet-4-6) from versioned tool model", () => {
const tools: AnyRecord[] = [
{ type: "bash_20250124", name: "Bash", model: "claude/claude-sonnet-4-6" },
];
stripVersionedToolModelPrefix(tools);
assert.equal(tools[0].model, "claude-sonnet-4-6");
});
it("leaves bare model on versioned tool unchanged", () => {
const tools: AnyRecord[] = [
{ type: "advisor_20260301", name: "advisor", model: "claude-opus-4-8" },
];
stripVersionedToolModelPrefix(tools);
assert.equal(tools[0].model, "claude-opus-4-8");
});
it("normalizes a non-versioned tool carrying a cc/ or claude/ prefixed model (upstream #2649)", () => {
// Non-versioned server tools (Task/subagent, web_search) leak the same
// provider-prefixed model the versioned ones do; Anthropic rejects both.
// Foreign prefixes (openrouter/...) are preserved.
const tools: AnyRecord[] = [
{ type: "custom", name: "x", model: "cc/claude-opus-4-8" },
{ type: "advisor_2026", name: "y", model: "cc/claude-opus-4-8" }, // not 8 digits
{ type: "custom", name: "z", model: "claude/claude-sonnet-4-6" },
{ type: "custom", name: "w", model: "openrouter/anthropic/claude-opus-4.1" },
];
stripVersionedToolModelPrefix(tools);
assert.equal(tools[0].model, "claude-opus-4-8", "cc/ stripped from non-versioned type");
assert.equal(tools[1].model, "claude-opus-4-8", "cc/ stripped from short date suffix");
assert.equal(tools[2].model, "claude-sonnet-4-6", "claude/ stripped from non-versioned type");
assert.equal(
tools[3].model,
"openrouter/anthropic/claude-opus-4.1",
"foreign prefix preserved"
);
});
it("strips cc/ prefix from a NON-versioned server tool (Task/subagent)", () => {
const tools: AnyRecord[] = [
{
name: "Task",
description: "Launch a subagent",
model: "cc/claude-opus-4-8",
input_schema: { type: "object" },
},
];
stripVersionedToolModelPrefix(tools);
assert.equal(tools[0].model, "claude-opus-4-8", "cc/ stripped from non-versioned Task tool");
});
it("strips claude/ prefix from a NON-versioned server tool", () => {
const tools: AnyRecord[] = [
{
name: "Task",
description: "Launch a subagent",
model: "claude/claude-sonnet-4-6",
input_schema: { type: "object" },
},
];
stripVersionedToolModelPrefix(tools);
assert.equal(
tools[0].model,
"claude-sonnet-4-6",
"claude/ stripped from non-versioned Task tool"
);
});
it("preserves other-provider prefixes on a non-versioned tool (openrouter/...)", () => {
const tools: AnyRecord[] = [
{
name: "Task",
model: "openrouter/anthropic/claude-opus-4.1",
input_schema: { type: "object" },
},
];
stripVersionedToolModelPrefix(tools);
assert.equal(
tools[0].model,
"openrouter/anthropic/claude-opus-4.1",
"foreign prefix preserved"
);
});
it("is idempotent across repeated runs", () => {
const tools: AnyRecord[] = [
{ name: "Task", model: "claude/claude-sonnet-4-6", input_schema: { type: "object" } },
{ type: "advisor_20260301", name: "advisor", model: "cc/claude-opus-4-8" },
];
stripVersionedToolModelPrefix(tools);
const first = JSON.stringify(tools);
stripVersionedToolModelPrefix(tools);
assert.equal(JSON.stringify(tools), first, "second run is a no-op");
});
it("is a no-op for non-array input", () => {
assert.doesNotThrow(() => stripVersionedToolModelPrefix(undefined));
assert.doesNotThrow(() => stripVersionedToolModelPrefix({ tools: [] }));
});
});