fix: align workbench cloud auth validation

This commit is contained in:
root
2026-07-18 15:01:18 +02:00
parent 635b008077
commit f6357811b0
4 changed files with 17 additions and 4 deletions
+2 -1
View File
@@ -101,6 +101,7 @@ import { handleTaskTreeProxyHttp } from "./tasktree-proxy.ts";
import { handleDashboardSummaryHttp } from "./server-dashboard-http.ts";
import { HWPOD_NODE_OPS, HWPOD_NODE_OPS_CONTRACT_VERSION } from "../../tools/src/hwpod-node-ops-contract.ts";
import { proxyWorkbenchCommandHttp, workbenchActivityDispatchRequested, workbenchTemporalProxyEnabled } from "./workbench-command-proxy.ts";
import { normalizeWorkbenchCloudAuthorization } from "../workbench/cloud-authorization.ts";
const DEFAULT_BODY_LIMIT_BYTES = 1024 * 1024;
const HWLAB_WEB_SESSION_COOKIE = "hwlab_session";
@@ -1078,7 +1079,7 @@ function navIdForRestPath(pathname, method = "GET") {
async function codeAgentOptions(request, response, options, authOptions = {}) {
if (workbenchActivityDispatchRequested(request)) {
const expected = String(options.env?.WORKBENCH_CLOUD_API_AUTHORIZATION ?? "").trim();
const expected = normalizeWorkbenchCloudAuthorization(String(options.env?.WORKBENCH_CLOUD_API_AUTHORIZATION ?? ""));
const actual = String(request.headers?.authorization ?? "").trim();
const actorId = String(request.headers?.["x-hwlab-actor-id"] ?? "").trim();
if (!expected || actual !== expected || !actorId) {
@@ -1,9 +1,16 @@
import { expect, test } from "bun:test";
import { workbenchActivityDispatchRequested } from "./workbench-command-proxy.ts";
import { normalizeWorkbenchCloudAuthorization } from "../workbench/cloud-authorization.ts";
test("accepts activity dispatch header from Node and Fetch request shapes", () => {
expect(workbenchActivityDispatchRequested({ headers: { "x-workbench-activity-dispatch": "1" } })).toBe(true);
expect(workbenchActivityDispatchRequested({ headers: new Headers({ "x-workbench-activity-dispatch": "1" }) })).toBe(true);
expect(workbenchActivityDispatchRequested({ headers: new Headers({ "x-workbench-activity-dispatch": "0" }) })).toBe(false);
});
test("normalizes the unified Workbench Cloud authorization on both sides", () => {
expect(normalizeWorkbenchCloudAuthorization(" unified-internal-key ")).toBe("Bearer unified-internal-key");
expect(normalizeWorkbenchCloudAuthorization("Bearer unified-internal-key")).toBe("Bearer unified-internal-key");
expect(normalizeWorkbenchCloudAuthorization(" ")).toBe("");
});
+3 -3
View File
@@ -1,4 +1,5 @@
import type { WorkbenchApplication } from "./contracts.ts";
import { normalizeWorkbenchCloudAuthorization } from "./cloud-authorization.ts";
export function createCloudWorkbenchApplication(options: {
baseUrl: string;
@@ -6,15 +7,14 @@ export function createCloudWorkbenchApplication(options: {
fetchImpl?: typeof fetch;
}): WorkbenchApplication {
if (!options.baseUrl) throw codedError("workbench_cloud_api_url_required", "WORKBENCH_CLOUD_API_URL is required");
const authorization = options.authorization.trim();
const authorization = normalizeWorkbenchCloudAuthorization(options.authorization);
if (!authorization) throw codedError("workbench_cloud_api_auth_required", "WORKBENCH_CLOUD_API_AUTHORIZATION is required");
const authorizationHeader = /^Bearer\s+/iu.test(authorization) ? authorization : `Bearer ${authorization}`;
const fetchImpl = options.fetchImpl ?? fetch;
const request = async (method: string, route: string, body: Record<string, unknown> | undefined, actor: { id: string; role?: string | null }) => {
const response = await fetchImpl(`${options.baseUrl.replace(/\/$/u, "")}${route}`, {
method,
headers: {
authorization: authorizationHeader,
authorization,
"content-type": "application/json",
"x-workbench-activity-dispatch": "1",
"x-hwlab-actor-id": actor.id,
@@ -0,0 +1,5 @@
export function normalizeWorkbenchCloudAuthorization(value: string) {
const authorization = value.trim();
if (!authorization) return "";
return /^Bearer\s+/iu.test(authorization) ? authorization : `Bearer ${authorization}`;
}