mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-10-23 03:27:27 +09:00
Invalid or revoked share links now return an actionable 401 response instead of an unexpected server error. Missing project and workspace records receive distinct responses at their lookup sites. Token, project, and workspace lookups use `.maybeSingle()` and rely on database uniqueness. Missing projects have an explicit `ProjectNotFoundError`; the loader no longer interprets `PGRST116`. The API preserves `UNAUTHORIZED` and `INVALID_AUTH_TOKEN` when authorization fails during context creation, so CLI and MCP clients can offer relinking guidance without inspecting error text. Tokens without API access carry `API_ACCESS_DISABLED`. A repository audit replaces error-message matching with typed errors, stable codes, or structured diagnostics in CLI argument/configuration handling, session conflicts, preview cleanup, import retries, domain unpublishing, asset validation, JSX/expression diagnostics, CSS validation, and publish diagnostics. `AGENTS.md` now explicitly prohibits message-based error classification and recovery. The existing build-permission preflight now advertises structured import-error support. Clients upload assets before importing into older servers that do not advertise it; newer servers keep selective retries using structured missing-file details. The internal template-validation error class remains outside public JSX bindings and input descriptions. Shared Builder `parseError` normalizes tRPC and local validation errors, so UI components only add display context instead of decoding transport payloads. Simplification removes three redundant error classes, catch-and-ignore handlers, an unused formatter, and nested loader error handling. Unpublishing an already unpublished domain succeeds after authorization. CLI configuration failures use the existing shared error handling, and plan parsing catches only JSON parsing failures. Validation: affected suites passed (271 tests across 8 suites in the latest simplification checks), affected package typechecks passed, and lint, package-boundary checks, and `git diff --check` passed. The audit replaced synthetic parser branches and private-method assertions with actual tRPC round trips, PostgREST responses, and public VM behavior. Controlled regressions confirmed that removing serialized validation details and exposing the internal JSX error class both fail the revised tests. Astra reviewed the production changes at medium effort; its compatibility finding and the GitHub JSX-binding comment are fixed, and follow-up review found no remaining substantive issues.
103 lines
2.8 KiB
TypeScript
103 lines
2.8 KiB
TypeScript
import { TRPCError } from "@trpc/server";
|
|
import {
|
|
authorizeProject,
|
|
type AppContext,
|
|
type AuthPermit,
|
|
} from "@webstudio-is/trpc-interface/index.server";
|
|
import { db as authDb } from "@webstudio-is/authorization-token/index.server";
|
|
import type { BuilderApiCapability } from "@webstudio-is/project-build/contracts";
|
|
|
|
type ApiPermit = BuilderApiCapability;
|
|
type ProjectApiPermit = Extract<AuthPermit, BuilderApiCapability>;
|
|
|
|
type ApiToken = Awaited<ReturnType<typeof authDb.getTokenInfo>>;
|
|
|
|
export const loadApiToken = async (ctx: AppContext) => {
|
|
if (ctx.authorization.type !== "token") {
|
|
throw new TRPCError({
|
|
code: "FORBIDDEN",
|
|
message: "Builder API requires an API token",
|
|
});
|
|
}
|
|
|
|
return await authDb.getTokenInfo(ctx.authorization.authToken, ctx);
|
|
};
|
|
|
|
export const getTokenPermits = (token: ApiToken, ctx: AppContext) => {
|
|
const permits: ApiPermit[] = [...authDb.getTokenProjectPermits(token)];
|
|
if (
|
|
token.canUseApi === true &&
|
|
ctx.planFeatures.allowAdditionalPermissions === true
|
|
) {
|
|
permits.push("api");
|
|
}
|
|
return permits;
|
|
};
|
|
|
|
export const assertApiTokenPermit = async (ctx: AppContext) => {
|
|
const token = await loadApiToken(ctx);
|
|
const permits = getTokenPermits(token, ctx);
|
|
if (permits.includes("api") === false) {
|
|
throw new TRPCError({
|
|
code: "FORBIDDEN",
|
|
message: "Authorization token cannot use Builder API",
|
|
cause: { webstudioCode: "API_ACCESS_DISABLED" },
|
|
});
|
|
}
|
|
return { token, permits };
|
|
};
|
|
|
|
const assertProjectPermit = async (
|
|
ctx: AppContext,
|
|
projectId: string,
|
|
permit: ProjectApiPermit
|
|
) => {
|
|
const allowed = await authorizeProject.hasProjectPermit(
|
|
{ projectId, permit },
|
|
ctx
|
|
);
|
|
if (allowed === false) {
|
|
throw new TRPCError({
|
|
code: "FORBIDDEN",
|
|
message: "You don't have access to this project",
|
|
});
|
|
}
|
|
};
|
|
|
|
export const assertApiProjectPermit = async (
|
|
ctx: AppContext,
|
|
projectId: string,
|
|
permit: ProjectApiPermit
|
|
) => {
|
|
if (ctx.authorization.type === "user") {
|
|
await assertProjectPermit(ctx, projectId, permit);
|
|
const permits: ProjectApiPermit[] = [permit];
|
|
if (
|
|
permit === "edit" &&
|
|
(await authorizeProject.hasProjectPermit(
|
|
{ projectId, permit: "build" },
|
|
ctx
|
|
))
|
|
) {
|
|
permits.push("build");
|
|
}
|
|
return { type: "user" as const, permits };
|
|
}
|
|
|
|
const tokenAuth = await assertApiTokenPermit(ctx);
|
|
if (tokenAuth.token.projectId !== projectId) {
|
|
throw new TRPCError({
|
|
code: "FORBIDDEN",
|
|
message: "Authorization token is not valid for project",
|
|
});
|
|
}
|
|
if (tokenAuth.permits.includes(permit) === false) {
|
|
throw new TRPCError({
|
|
code: "FORBIDDEN",
|
|
message: `Authorization token does not have ${permit} permission`,
|
|
});
|
|
}
|
|
await assertProjectPermit(ctx, projectId, permit);
|
|
return { type: "token" as const, ...tokenAuth };
|
|
};
|