Add per-enterprise custom HTTP headers and cookies
Some Seal tenants sit behind a gateway that only forwards requests carrying its own session cookie. Corp configs can now set seal.headers/seal.cookies, which the client merges into every request; cookies are rendered into one Cookie header and the session bearer token still wins over any configured Authorization. corps add-seal accepts both fields and applies them when verifying the tenant, so a gated enterprise can be registered in one step.
This commit is contained in:
1 parent
fc06434813
commit
bb8b5edec6
6 files changed
+203
-6
No files matched your search
@@ -1,5 +1,10 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { CorpConfigSchema, GlobalConfigSchema } from "./types.js";
|
||||
import {
|
||||
CorpConfigSchema,
|
||||
GlobalConfigSchema,
|
||||
SealCookieMapSchema,
|
||||
SealHeaderMapSchema
|
||||
} from "./types.js";
|
||||
|
||||
describe("CorpConfigSchema", () => {
|
||||
test("accepts Hose key/password aliases", () => {
|
||||
@@ -78,6 +83,59 @@ describe("CorpConfigSchema", () => {
|
||||
expect(config.source.staffId).toBe("seal");
|
||||
expect(config.source.tenantSlug).toBe("xiezihao");
|
||||
});
|
||||
|
||||
test("leaves custom headers and cookies absent by default", () => {
|
||||
const config = CorpConfigSchema.parse({
|
||||
id: "direct",
|
||||
name: "Direct",
|
||||
source: {
|
||||
type: "direct",
|
||||
token: "seal-token",
|
||||
sealUrl: "https://direct.sealai.cc"
|
||||
}
|
||||
});
|
||||
|
||||
expect(config.seal.headers).toBeUndefined();
|
||||
expect(config.seal.cookies).toBeUndefined();
|
||||
});
|
||||
|
||||
test("accepts custom headers and cookies on the Seal endpoint", () => {
|
||||
const config = CorpConfigSchema.parse({
|
||||
id: "gated",
|
||||
name: "Gated",
|
||||
seal: {
|
||||
url: "https://gated.sealai.cc",
|
||||
headers: { "X-Tenant": "acme" },
|
||||
cookies: { sdp_app_session_20142: "abc" }
|
||||
},
|
||||
source: {
|
||||
type: "direct",
|
||||
token: "seal-token",
|
||||
sealUrl: "https://gated.sealai.cc"
|
||||
}
|
||||
});
|
||||
|
||||
expect(config.seal.headers).toEqual({ "X-Tenant": "acme" });
|
||||
expect(config.seal.cookies).toEqual({ sdp_app_session_20142: "abc" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("Seal custom header schemas", () => {
|
||||
test("rejects an Authorization header supplied through config", () => {
|
||||
expect(SealHeaderMapSchema.safeParse({ Authorization: "Bearer x" }).success).toBe(false);
|
||||
expect(SealHeaderMapSchema.safeParse({ authorization: "Bearer x" }).success).toBe(false);
|
||||
});
|
||||
|
||||
test("rejects malformed header and cookie names", () => {
|
||||
expect(SealHeaderMapSchema.safeParse({ "bad name": "v" }).success).toBe(false);
|
||||
expect(SealCookieMapSchema.safeParse({ "bad;name": "v" }).success).toBe(false);
|
||||
});
|
||||
|
||||
test("rejects cookie values containing separators", () => {
|
||||
expect(SealCookieMapSchema.safeParse({ name: "v;other=1" }).success).toBe(false);
|
||||
expect(SealCookieMapSchema.safeParse({ name: "line\nbreak" }).success).toBe(false);
|
||||
expect(SealCookieMapSchema.safeParse({ name: "ok-value" }).success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("GlobalConfigSchema", () => {
|
||||
|
||||
@@ -58,9 +58,38 @@ export const SealSourceSchema = z.discriminatedUnion("type", [
|
||||
NativeSealSourceSchema
|
||||
]);
|
||||
|
||||
// RFC 7230 token: shared by HTTP header names and cookie names.
|
||||
const HTTP_TOKEN_PATTERN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
||||
|
||||
export const SealHeaderMapSchema = z.record(
|
||||
z.string().regex(HTTP_TOKEN_PATTERN, "Invalid HTTP header name"),
|
||||
z.string()
|
||||
).superRefine((headers, ctx) => {
|
||||
for (const name of Object.keys(headers)) {
|
||||
if (name.toLowerCase() === "authorization") {
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
path: [name],
|
||||
message: "seal.headers cannot set Authorization; the Seal session supplies the bearer token"
|
||||
});
|
||||
}
|
||||
}
|
||||
}).optional();
|
||||
|
||||
export const SealCookieMapSchema = z.record(
|
||||
z.string().regex(HTTP_TOKEN_PATTERN, "Invalid cookie name"),
|
||||
z.string().refine((value) => !/[\u0000-\u001f\u007f;]/.test(value), {
|
||||
message: "Cookie value cannot contain control characters or semicolons"
|
||||
})
|
||||
).optional();
|
||||
|
||||
export const SealEndpointSchema = z.object({
|
||||
url: z.string().url().optional(),
|
||||
tenantSlug: z.string().optional(),
|
||||
// Extra headers/cookies for Seal deployments reachable only through an
|
||||
// upstream gateway that expects its own session cookie.
|
||||
headers: SealHeaderMapSchema,
|
||||
cookies: SealCookieMapSchema,
|
||||
endpoints: z.object({
|
||||
approvalStylePreferences: z.string().default("api/v1/agent/ai-approval/config")
|
||||
}).default({
|
||||
|
||||
Reference in new issue
Block a user