diff --git a/packages/cli/src/commands/auth/whoami/command.ts b/packages/cli/src/commands/auth/whoami/command.ts index 1405444470e..b78889d4653 100644 --- a/packages/cli/src/commands/auth/whoami/command.ts +++ b/packages/cli/src/commands/auth/whoami/command.ts @@ -11,6 +11,9 @@ export default command({ let authLine: string; if (ctx.authSource === "oauth") { authLine = "Session"; + } else if (ctx.authSource === "host") { + authLine = + "Superset app (the account that runs this terminal's workspace)"; } else if (ctx.authSource === "override") { authLine = "API key (from --api-key flag or SUPERSET_API_KEY env)"; } else { diff --git a/packages/cli/src/commands/middleware.ts b/packages/cli/src/commands/middleware.ts index 3ecc0debb0b..4bda9c708a2 100644 --- a/packages/cli/src/commands/middleware.ts +++ b/packages/cli/src/commands/middleware.ts @@ -2,9 +2,17 @@ import { middleware } from "@superset/cli-framework"; import { trackCommandInvoked } from "../lib/analytics"; import { resolveAuth } from "../lib/resolve-auth"; +/** A host this spawns outlives the hour a borrowed host token lasts. */ +const COMMANDS_NEEDING_OWN_LOGIN = new Set(["start"]); + export default middleware(async (opts) => { const options = opts.options as { apiKey?: string }; - const { config, api, bearer, authSource } = await resolveAuth(options.apiKey); + const { config, api, bearer, authSource } = await resolveAuth( + options.apiKey, + { + useHostToken: !COMMANDS_NEEDING_OWN_LOGIN.has(opts.commandPath[0] ?? ""), + }, + ); trackCommandInvoked({ api, diff --git a/packages/cli/src/lib/host-api-token.ts b/packages/cli/src/lib/host-api-token.ts new file mode 100644 index 00000000000..275ef8dc591 --- /dev/null +++ b/packages/cli/src/lib/host-api-token.ts @@ -0,0 +1,41 @@ +import type { AppRouter as HostServiceRouter } from "@superset/host-service/trpc"; +import { createTRPCClient, httpBatchLink } from "@trpc/client"; +import SuperJSON from "superjson"; +import { isProcessAlive, readManifest } from "./host/manifest"; + +/** Long enough for the host to refresh its token over the network first. */ +const HOST_API_TOKEN_TIMEOUT_MS = 5_000; + +/** + * In a Superset terminal, the API token of the account that runs the + * workspace's host, so the CLI acts as that account and not its own login. + * Null outside one, or when the host is down or too old to give one. + */ +export async function readHostApiToken( + timeoutMs = HOST_API_TOKEN_TIMEOUT_MS, +): Promise { + const organizationId = process.env.SUPERSET_ORGANIZATION_ID?.trim(); + if (!organizationId || !process.env.SUPERSET_WORKSPACE_ID) return null; + if (process.env.SUPERSET_SANDBOX_WORKSPACE_ID) return null; + + const manifest = readManifest(organizationId); + if (!manifest || !isProcessAlive(manifest.pid)) return null; + + const host = createTRPCClient({ + links: [ + httpBatchLink({ + url: `${manifest.endpoint}/trpc`, + transformer: SuperJSON, + headers: { Authorization: `Bearer ${manifest.authToken}` }, + }), + ], + }); + try { + const { token } = await host.host.apiToken.query(undefined, { + signal: AbortSignal.timeout(timeoutMs), + }); + return token; + } catch { + return null; + } +} diff --git a/packages/cli/src/lib/resolve-auth.test.ts b/packages/cli/src/lib/resolve-auth.test.ts index 4f139050306..8b6f81d534d 100644 --- a/packages/cli/src/lib/resolve-auth.test.ts +++ b/packages/cli/src/lib/resolve-auth.test.ts @@ -11,6 +11,7 @@ process.env.SUPERSET_HOME_DIR = tempHome; const { resolveAuth } = await import("./resolve-auth"); const { readConfig, writeConfig } = await import("./config"); +const { removeManifest, writeManifest } = await import("./host/manifest"); function clearConfig(): void { writeConfig({}); @@ -20,13 +21,18 @@ function clearConfig(): void { // would leak into every test. Clear it for the suite, restore in afterAll. const originalEnvKey = process.env.SUPERSET_API_KEY; const originalOrganizationId = process.env.SUPERSET_ORGANIZATION_ID; +const originalWorkspaceId = process.env.SUPERSET_WORKSPACE_ID; +const originalSandboxWorkspaceId = process.env.SUPERSET_SANDBOX_WORKSPACE_ID; delete process.env.SUPERSET_API_KEY; delete process.env.SUPERSET_ORGANIZATION_ID; +delete process.env.SUPERSET_WORKSPACE_ID; +delete process.env.SUPERSET_SANDBOX_WORKSPACE_ID; afterEach(() => { clearConfig(); delete process.env.SUPERSET_API_KEY; delete process.env.SUPERSET_ORGANIZATION_ID; + delete process.env.SUPERSET_WORKSPACE_ID; }); afterAll(() => { @@ -43,6 +49,14 @@ afterAll(() => { } else { process.env.SUPERSET_ORGANIZATION_ID = originalOrganizationId; } + if (originalWorkspaceId === undefined) { + delete process.env.SUPERSET_WORKSPACE_ID; + } else { + process.env.SUPERSET_WORKSPACE_ID = originalWorkspaceId; + } + if (originalSandboxWorkspaceId !== undefined) { + process.env.SUPERSET_SANDBOX_WORKSPACE_ID = originalSandboxWorkspaceId; + } }); describe("resolveAuth", () => { @@ -148,3 +162,72 @@ describe("resolveAuth", () => { expect(result.authSource).toBe("config"); }); }); + +describe("resolveAuth in a Superset terminal", () => { + let hostResponse: () => Response; + const host = Bun.serve({ port: 0, fetch: () => hostResponse() }); + + function enterTerminal(): void { + process.env.SUPERSET_ORGANIZATION_ID = "org_workspace"; + process.env.SUPERSET_WORKSPACE_ID = "ws_1"; + writeManifest({ + pid: process.pid, + endpoint: `http://127.0.0.1:${host.port}`, + authToken: "host-secret", + startedAt: Date.now(), + organizationId: "org_workspace", + }); + } + + afterEach(() => removeManifest("org_workspace")); + afterAll(() => host.stop(true)); + + it("acts as the account that runs the workspace's host", async () => { + enterTerminal(); + writeConfig({ apiKey: "sk_live_other_account" }); + hostResponse = () => + Response.json([{ result: { data: { json: { token: "host-jwt" } } } }]); + + const result = await resolveAuth(undefined); + + expect(result.bearer).toBe("host-jwt"); + expect(result.authSource).toBe("host"); + expect(result.config.organizationId).toBe("org_workspace"); + }); + + it("falls back to its own login when the host gives no token", async () => { + enterTerminal(); + writeConfig({ apiKey: "sk_live_own" }); + hostResponse = () => + Response.json( + [ + { + error: { + json: { + message: "No procedure", + code: -32004, + data: { code: "NOT_FOUND", httpStatus: 404 }, + }, + }, + }, + ], + { status: 404 }, + ); + + const result = await resolveAuth(undefined); + + expect(result.bearer).toBe("sk_live_own"); + expect(result.authSource).toBe("config"); + }); + + it("keeps its own login for a command that outlives the host token", async () => { + enterTerminal(); + writeConfig({ apiKey: "sk_live_own" }); + hostResponse = () => + Response.json([{ result: { data: { json: { token: "host-jwt" } } } }]); + + const result = await resolveAuth(undefined, { useHostToken: false }); + + expect(result.bearer).toBe("sk_live_own"); + }); +}); diff --git a/packages/cli/src/lib/resolve-auth.ts b/packages/cli/src/lib/resolve-auth.ts index 4e82ce4b890..804ffe514a2 100644 --- a/packages/cli/src/lib/resolve-auth.ts +++ b/packages/cli/src/lib/resolve-auth.ts @@ -8,8 +8,9 @@ import { type SupersetConfig, writeConfig, } from "./config"; +import { readHostApiToken } from "./host-api-token"; -export type AuthSource = "override" | "config" | "oauth" | "sandbox"; +export type AuthSource = "override" | "host" | "config" | "oauth" | "sandbox"; export type ResolvedAuth = { config: SupersetConfig; @@ -20,6 +21,7 @@ export type ResolvedAuth = { export async function resolveAuth( apiKeyOption: string | undefined, + { useHostToken = true }: { useHostToken?: boolean } = {}, ): Promise { let config = readConfig(); @@ -29,10 +31,15 @@ export async function resolveAuth( apiKeyOption?.trim() || process.env.SUPERSET_API_KEY?.trim(); let bearer: string | undefined; let authSource: AuthSource; + const hostToken = + !overrideKey && useHostToken ? await readHostApiToken() : null; if (overrideKey) { bearer = overrideKey; authSource = "override"; + } else if (hostToken) { + bearer = hostToken; + authSource = "host"; } else if (config.apiKey?.trim()) { bearer = config.apiKey.trim(); authSource = "config"; diff --git a/packages/host-service/src/app.ts b/packages/host-service/src/app.ts index 97b43d1123b..bb8a1f7eaac 100644 --- a/packages/host-service/src/app.ts +++ b/packages/host-service/src/app.ts @@ -1,7 +1,10 @@ import { createNodeWebSocket } from "@hono/node-ws"; import { trpcServer } from "@hono/trpc-server"; import { Octokit } from "@octokit/rest"; -import { SUPERSET_USER_ID_HEADER } from "@superset/shared/host-routing"; +import { + SUPERSET_RELAYED_HEADER, + SUPERSET_USER_ID_HEADER, +} from "@superset/shared/host-routing"; import { SANDBOX_PORTS } from "@superset/shared/sandbox-contract"; /** One frame of a 1920x1200 display is ~9 MB; this is a stalled reader, not a burst. */ @@ -516,6 +519,8 @@ export function createApp(options: CreateAppOptions): CreateAppResult { c.req.header("x-superset-client-machine-id") ?? undefined, userId: c.req.header(SUPERSET_USER_ID_HEADER)?.trim() || undefined, browserBridge: config.browserBridge, + apiAuth: providers.auth, + isLocalCaller: c.req.header(SUPERSET_RELAYED_HEADER) === undefined, } as Record; }, }), diff --git a/packages/host-service/src/trpc/router/host/host.test.ts b/packages/host-service/src/trpc/router/host/host.test.ts new file mode 100644 index 00000000000..922a99f4dc3 --- /dev/null +++ b/packages/host-service/src/trpc/router/host/host.test.ts @@ -0,0 +1,117 @@ +import { describe, expect, it } from "bun:test"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { TRPCError } from "@trpc/server"; +import { + ConfigFileSessionTokenSource, + JwtApiAuthProvider, +} from "../../../providers/auth"; +import type { HostServiceContext } from "../../../types"; +import { hostRouter } from "./host"; + +const apiAuth = { + getHeaders: async () => ({ Authorization: "Bearer owner-jwt" }), + invalidateCache: () => {}, +}; + +const callerFor = (context: Partial) => + hostRouter.createCaller({ + isAuthenticated: true, + apiAuth, + ...context, + } as HostServiceContext); + +describe("host.apiToken", () => { + it("gives a caller on this machine the token the host uses", async () => { + const caller = callerFor({ isLocalCaller: true }); + expect(await caller.apiToken()).toEqual({ token: "owner-jwt" }); + }); + + it.each([ + ["through the relay", false], + ["of unknown origin", undefined], + ])("refuses a caller %s", async (_name, isLocalCaller) => { + const error = (await callerFor({ isLocalCaller }) + .apiToken() + .catch((thrown: unknown) => thrown)) as TRPCError; + expect(error.code).toBe("FORBIDDEN"); + }); +}); + +describe("host.apiToken with a token near expiry", () => { + const jwtExpiringIn = (seconds: number) => + [ + "header", + Buffer.from( + JSON.stringify({ exp: Math.floor(Date.now() / 1000) + seconds }), + ).toString("base64url"), + "signature", + ].join("."); + + const sourceOf = (tokens: string[]) => { + let invalidated = 0; + return { + apiAuth: { + getHeaders: async () => ({ + Authorization: `Bearer ${tokens[Math.min(invalidated, tokens.length - 1)]}`, + }), + invalidateCache: () => { + invalidated++; + }, + }, + }; + }; + + it("refreshes the expired OAuth token of a host from `superset start`", async () => { + const fresh = jwtExpiringIn(3600); + const tokenEndpoint = Bun.serve({ + port: 0, + fetch: () => + Response.json({ + access_token: fresh, + refresh_token: "rt_next", + expires_in: 3600, + }), + }); + const dir = mkdtempSync(join(tmpdir(), "host-api-token-")); + try { + const configPath = join(dir, "config.json"); + writeFileSync( + configPath, + JSON.stringify({ + auth: { + accessToken: jwtExpiringIn(-60), + refreshToken: "rt_stored", + expiresAt: Date.now() - 60_000, + }, + }), + ); + const apiUrl = `http://127.0.0.1:${tokenEndpoint.port}`; + const source = new ConfigFileSessionTokenSource({ configPath, apiUrl }); + const apiAuth = new JwtApiAuthProvider({ + getSessionToken: () => source.getSessionToken(), + onInvalidateCache: () => source.invalidateCache(), + apiUrl, + }); + + const caller = callerFor({ isLocalCaller: true, apiAuth }); + + expect(await caller.apiToken()).toEqual({ token: fresh }); + } finally { + tokenEndpoint.stop(true); + rmSync(dir, { recursive: true, force: true }); + } + }); + + it("refuses when the refreshed token still expires within minutes", async () => { + const source = sourceOf([jwtExpiringIn(-60), jwtExpiringIn(30)]); + const error = (await callerFor({ + isLocalCaller: true, + apiAuth: source.apiAuth, + }) + .apiToken() + .catch((thrown: unknown) => thrown)) as TRPCError; + expect(error.code).toBe("PRECONDITION_FAILED"); + }); +}); diff --git a/packages/host-service/src/trpc/router/host/host.ts b/packages/host-service/src/trpc/router/host/host.ts index 701acb356ec..49f64fd78e7 100644 --- a/packages/host-service/src/trpc/router/host/host.ts +++ b/packages/host-service/src/trpc/router/host/host.ts @@ -5,11 +5,34 @@ import { getHostInstallSource, HOST_SERVICE_VERSION, } from "../../../install-source"; +import type { ApiAuthProvider } from "../../../providers/auth/types"; import type { ApiClient } from "../../../types"; -import { protectedProcedure, router } from "../../index"; +import { machineOnlyProcedure, protectedProcedure, router } from "../../index"; import { rethrowCloudUnreachable } from "./cloud-api-error"; const ORGANIZATION_CACHE_TTL_MS = 60 * 60 * 1000; +const MIN_API_TOKEN_LIFETIME_MS = 5 * 60 * 1000; + +async function readBearer(apiAuth: ApiAuthProvider): Promise { + const authorization = (await apiAuth.getHeaders()).Authorization; + return authorization?.startsWith("Bearer ") ? authorization.slice(7) : null; +} + +/** + * A host started by `superset start` hands back the stored OAuth access token + * as-is, expired or not; it only refreshes after a 401. + */ +function outlivesCommand(token: string): boolean { + try { + const payload = JSON.parse( + Buffer.from(token.split(".")[1] ?? "", "base64url").toString("utf8"), + ) as { exp?: unknown }; + if (typeof payload.exp !== "number") return true; + return payload.exp * 1000 - Date.now() > MIN_API_TOKEN_LIFETIME_MS; + } catch { + return true; + } +} let cachedOrganization: { data: { id: string; name: string; slug: string }; @@ -63,4 +86,31 @@ export const hostRouter = router({ uptime: process.uptime(), }; }), + /** + * The API token of the account that runs this host, so the CLI in one of + * its terminals acts as that account instead of its own login. The relay + * adds the host secret too, so a teammate there would pass every other + * check and leave with the owner's credential. + */ + apiToken: machineOnlyProcedure.query(async ({ ctx }) => { + if (ctx.isLocalCaller !== true || !ctx.apiAuth) { + throw new TRPCError({ + code: "FORBIDDEN", + message: "The API token is only given to callers on this machine.", + }); + } + const apiAuth = ctx.apiAuth; + let token = await readBearer(apiAuth); + if (token && !outlivesCommand(token)) { + apiAuth.invalidateCache(); + token = await readBearer(apiAuth); + } + if (!token || !outlivesCommand(token)) { + throw new TRPCError({ + code: "PRECONDITION_FAILED", + message: "This host has no unexpired API token to give.", + }); + } + return { token }; + }), }); diff --git a/packages/host-service/src/tunnel/tunnel-client.integration.test.ts b/packages/host-service/src/tunnel/tunnel-client.integration.test.ts index 462def58fca..494e85f51c4 100644 --- a/packages/host-service/src/tunnel/tunnel-client.integration.test.ts +++ b/packages/host-service/src/tunnel/tunnel-client.integration.test.ts @@ -19,6 +19,7 @@ async function exerciseDial({ fetch(request) { requests++; expect(request.headers.get("authorization")).toBe("Bearer test-secret"); + expect(request.headers.get("x-superset-relayed")).toBe("1"); return new Response("proxied successfully"); }, }); diff --git a/packages/host-service/src/tunnel/tunnel-client.ts b/packages/host-service/src/tunnel/tunnel-client.ts index 45308152713..d70613c0f0b 100644 --- a/packages/host-service/src/tunnel/tunnel-client.ts +++ b/packages/host-service/src/tunnel/tunnel-client.ts @@ -1,3 +1,4 @@ +import { SUPERSET_RELAYED_HEADER } from "@superset/shared/host-routing"; import { describeRelayClose, type HttpDialFrame, @@ -361,6 +362,7 @@ export class TunnelClient { method: header.method, headers: { ...header.headers, + [SUPERSET_RELAYED_HEADER]: "1", Authorization: `Bearer ${this.options.hostServiceSecret}`, }, body: size > 0 ? body : undefined, diff --git a/packages/host-service/src/types.ts b/packages/host-service/src/types.ts index 1ab6869547f..02fbdf008e7 100644 --- a/packages/host-service/src/types.ts +++ b/packages/host-service/src/types.ts @@ -5,6 +5,7 @@ import type { TRPCClient } from "@trpc/client"; import type { HostDb } from "./db"; import type { EventBus } from "./events"; import type { PageWatchManager } from "./page-watch/index.ts"; +import type { ApiAuthProvider } from "./providers/auth/types"; import type { WorkspaceFilesystemManager } from "./runtime/filesystem"; import type { GitCredentialProvider, GitFactory } from "./runtime/git"; import type { PullRequestRuntimeManager } from "./runtime/pull-requests"; @@ -43,6 +44,10 @@ export interface HostServiceContext { userId?: string; /** Present only when a desktop app spawned this host (has browser panes). */ browserBridge?: BrowserBridgeConfig; + /** The credential this host calls the API with. */ + apiAuth?: ApiAuthProvider; + /** True only for a request that did not come through the relay. */ + isLocalCaller?: boolean; } export interface BrowserBridgeConfig { diff --git a/packages/shared/src/host-routing.ts b/packages/shared/src/host-routing.ts index c78f93fdbbd..5da514c2120 100644 --- a/packages/shared/src/host-routing.ts +++ b/packages/shared/src/host-routing.ts @@ -33,6 +33,13 @@ export function parseHostRoutingKey( */ export const SUPERSET_USER_ID_HEADER = "x-superset-user-id"; +/** + * Set by a host's tunnel on every request it forwards from the relay. The + * tunnel also adds the pre-shared secret, so this header is the only way the + * host can tell a teammate on the relay from a local caller. + */ +export const SUPERSET_RELAYED_HEADER = "x-superset-relayed"; + /** * Headers a relay forwards to a host over its tunnel. The relay is the only * party that verified the caller's JWT, so it is the one that names the user: diff --git a/packages/trpc/src/trpc.test.ts b/packages/trpc/src/trpc.test.ts index 488c022e66c..b253c13e3ef 100644 --- a/packages/trpc/src/trpc.test.ts +++ b/packages/trpc/src/trpc.test.ts @@ -5,11 +5,12 @@ import { stub } from "../test/stub"; import { posthog } from "./lib/analytics"; const CREATOR_ORGS = ["box-org", "other-org"]; +let membership: { id: string } | undefined = { id: "membership" }; stub(db.query.members, { findMany: async () => CREATOR_ORGS.map((organizationId) => ({ organizationId })), - findFirst: async () => ({ id: "membership" }), + findFirst: async () => membership, }); stub(posthog, { capture: () => {}, isFeatureEnabled: async () => false }); @@ -153,3 +154,24 @@ describe("the organization a cloud workspace acts in", () => { expect(await client.user.myOrganization()).toBe("other-org"); }); }); + +describe("a caller outside the organization it asks for", () => { + test("is told which account it is signed in as and how to fix it", async () => { + membership = undefined; + try { + const client = callerFor({ + sandboxCaller: null, + organization: "foreign-org", + }); + const error = (await client.user + .myOrganization() + .catch((thrown: unknown) => thrown)) as TRPCError; + expect(error.code).toBe("FORBIDDEN"); + expect(error.message).toContain("foreign-org"); + expect(error.message).toContain("creator@example.com"); + expect(error.message).toContain("superset auth login"); + } finally { + membership = { id: "membership" }; + } + }); +}); diff --git a/packages/trpc/src/trpc.ts b/packages/trpc/src/trpc.ts index 73c9d2c3b56..8cae0c1a154 100644 --- a/packages/trpc/src/trpc.ts +++ b/packages/trpc/src/trpc.ts @@ -185,7 +185,9 @@ export const protectedProcedure = t.procedure eq(members.organizationId, headerOrgId), ), }); - if (!membership) throw notAMemberOfOrganization(headerOrgId); + if (!membership) { + throw notAMemberOfOrganization(headerOrgId, ctx.session.user.email); + } activeOrganizationId = headerOrgId; } @@ -197,11 +199,19 @@ export const protectedProcedure = t.procedure * SUPERSET_ORGANIZATION_ID, or the organization it last logged into), so the * id in this error is usually one the caller never typed. Saying where it * came from is what turns "not a member" into something actionable. + * + * A Superset terminal sets SUPERSET_ORGANIZATION_ID to its workspace's + * organization. A CLI there that cannot use the app's account (an older host, + * or SUPERSET_API_KEY set) and holds another account's login fails here. */ -function notAMemberOfOrganization(organizationId: string): TRPCError { +function notAMemberOfOrganization( + organizationId: string, + signedInAs?: string, +): TRPCError { + const account = signedInAs ? ` (signed in as ${signedInAs})` : ""; return new TRPCError({ code: "FORBIDDEN", - message: `Not a member of organization ${organizationId}, which was asked for in the x-superset-organization-id header`, + message: `Not a member of organization ${organizationId}, which was asked for in the x-superset-organization-id header${account}. Run \`superset auth login\` with an account in that organization, or unset SUPERSET_API_KEY if it names another account.`, }); } @@ -233,13 +243,14 @@ const inBoxOrganization = ( function resolveActiveOrganizationId( organizationIds: string[], requestedOrganizationId: string | null, + signedInAs?: string, ): string | null { if (!requestedOrganizationId) { return organizationIds[0] ?? null; } if (!organizationIds.includes(requestedOrganizationId)) { - throw notAMemberOfOrganization(requestedOrganizationId); + throw notAMemberOfOrganization(requestedOrganizationId, signedInAs); } return requestedOrganizationId; @@ -304,7 +315,11 @@ export const jwtProcedure = t.procedure userId, organizationIds, activeOrganizationId: headerOrgId - ? resolveActiveOrganizationId(organizationIds, headerOrgId) + ? resolveActiveOrganizationId( + organizationIds, + headerOrgId, + ctx.session.user.email, + ) : (ctx.session.session.activeOrganizationId ?? organizationIds[0] ?? null),