Files
openclaw-zero-token/gateway/origin-check.test.ts
sjhu 571e14a236 feat: upgrade to upstream v2026.3.28
Major upgrade from e26988a38 to upstream v2026.3.28 (f9b107928).
Key changes:
- Upstream src/, ui/, extensions/ (89 bundled extensions)
- Zero-token web providers preserved in src/zero-token/
- AskOnce plugin restored and registered as CLI command
- Added missing packages: @anthropic-ai/vertex-sdk, @modelcontextprotocol/sdk
- Fixed tsconfig rootDir, skipLibCheck for plugin-sdk DTS build
- Added askonce to bundled plugin metadata and package.json exports
- Fixed AskOnce CLI command registration (missing commands metadata)
- Restored AskOnce adapter imports (correct 5-level relative paths)
- Removed stale migration artifacts from root directory
2026-03-30 17:58:12 +08:00

95 lines
3.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { checkBrowserOrigin } from "./origin-check.js";
describe("checkBrowserOrigin", () => {
it.each([
{
name: "accepts host-header fallback when explicitly enabled",
input: {
requestHost: "127.0.0.1:18789",
origin: "http://127.0.0.1:18789",
allowHostHeaderOriginFallback: true,
},
expected: { ok: true as const, matchedBy: "host-header-fallback" as const },
},
{
name: "rejects same-origin host matches when fallback is disabled",
input: {
requestHost: "gateway.example.com:18789",
origin: "https://gateway.example.com:18789",
},
expected: { ok: false as const, reason: "origin not allowed" },
},
{
name: "accepts local loopback mismatches for local clients",
input: {
requestHost: "127.0.0.1:18789",
origin: "http://localhost:5173",
isLocalClient: true,
},
expected: { ok: true as const, matchedBy: "local-loopback" as const },
},
{
name: "rejects loopback mismatches for non-local clients",
input: {
requestHost: "127.0.0.1:18789",
origin: "http://localhost:5173",
isLocalClient: false,
},
expected: { ok: false as const, reason: "origin not allowed" },
},
{
name: "accepts trimmed lowercase-normalized allowlist matches",
input: {
requestHost: "gateway.example.com:18789",
origin: "https://CONTROL.example.com",
allowedOrigins: [" https://control.example.com "],
},
expected: { ok: true as const, matchedBy: "allowlist" as const },
},
{
name: "accepts wildcard allowlists even alongside specific entries",
input: {
requestHost: "gateway.tailnet.ts.net:18789",
origin: "https://any-origin.example.com",
allowedOrigins: ["https://control.example.com", " * "],
},
expected: { ok: true as const, matchedBy: "allowlist" as const },
},
{
name: "rejects missing origin",
input: {
requestHost: "gateway.example.com:18789",
origin: "",
},
expected: { ok: false as const, reason: "origin missing or invalid" },
},
{
name: 'rejects literal "null" origin',
input: {
requestHost: "gateway.example.com:18789",
origin: "null",
},
expected: { ok: false as const, reason: "origin missing or invalid" },
},
{
name: "rejects malformed origin URLs",
input: {
requestHost: "gateway.example.com:18789",
origin: "not a url",
},
expected: { ok: false as const, reason: "origin missing or invalid" },
},
{
name: "rejects mismatched origins",
input: {
requestHost: "gateway.example.com:18789",
origin: "https://attacker.example.com",
},
expected: { ok: false as const, reason: "origin not allowed" },
},
])("$name", ({ input, expected }) => {
expect(checkBrowserOrigin(input)).toEqual(expected);
});
});