Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: Fix share link staging publish permission (#5780)

Oleg Isonenf9bd8b0cd74b
diff --git a/packages/project-build/src/db/build.test.ts b/packages/project-build/src/db/build.test.ts
index fc4f907eea..1214f682cb 100644
--- a/packages/project-build/src/db/build.test.ts
+++ b/packages/project-build/src/db/build.test.ts
@@ -14,9 +14,11 @@ import {
loadDevBuildByProjectId,
createBuild,
unpublishBuild,
+ __testing__,
} from "./build";
const server = createTestServer();
+const { canTokenPublishDeployment } = __testing__;
const uid = () => `proj-${Math.random().toString(36).slice(2)}`;
@@ -161,6 +163,78 @@ describe("createBuild (msw)", () => {
});
});
+// ---------------------------------------------------------------------------
+// canTokenPublishDeployment
+// ---------------------------------------------------------------------------
+
+describe("canTokenPublishDeployment", () => {
+ const token = {
+ token: "token-1",
+ projectId: "proj-1",
+ name: "Share link",
+ createdAt: new Date().toISOString(),
+ canClone: true,
+ canCopy: true,
+ canPublish: false,
+ };
+
+ test("allows builder tokens to publish to the staging domain", () => {
+ expect(
+ canTokenPublishDeployment(
+ { ...token, relation: "builders" },
+ {
+ destination: "saas",
+ domains: ["project-domain"],
+ assetsDomain: "project-domain",
+ excludeWstdDomainFromSearch: false,
+ }
+ )
+ ).toBe(true);
+ });
+
+ test("rejects builder tokens publishing to custom domains", () => {
+ expect(
+ canTokenPublishDeployment(
+ { ...token, relation: "builders" },
+ {
+ destination: "saas",
+ domains: ["project-domain", "example.com"],
+ assetsDomain: "project-domain",
+ excludeWstdDomainFromSearch: true,
+ }
+ )
+ ).toBe(false);
+ });
+
+ test("rejects builder tokens publishing static builds", () => {
+ expect(
+ canTokenPublishDeployment(
+ { ...token, relation: "builders" },
+ {
+ destination: "static",
+ name: "build.zip",
+ assetsDomain: "project-domain",
+ templates: [],
+ }
+ )
+ ).toBe(false);
+ });
+
+ test("allows tokens with full publish permission", () => {
+ expect(
+ canTokenPublishDeployment(
+ { ...token, relation: "editors", canPublish: true },
+ {
+ destination: "saas",
+ domains: ["example.com"],
+ assetsDomain: "project-domain",
+ excludeWstdDomainFromSearch: true,
+ }
+ )
+ ).toBe(true);
+ });
+});
+
// ---------------------------------------------------------------------------
// unpublishBuild
// ---------------------------------------------------------------------------
diff --git a/packages/project-build/src/db/build.ts b/packages/project-build/src/db/build.ts
index c486147940..e7249b28e2 100644
--- a/packages/project-build/src/db/build.ts
+++ b/packages/project-build/src/db/build.ts
@@ -84,6 +84,26 @@ const parseCompactBuild = async (
}
};
+type AuthorizationToken = Awaited<ReturnType<typeof authDb.getTokenInfo>>;
+
+const canTokenPublishDeployment = (
+ token: AuthorizationToken,
+ deployment: Deployment
+) => {
+ if (token.canPublish) {
+ return true;
+ }
+
+ if (token.relation !== "builders" || deployment.destination !== "saas") {
+ return false;
+ }
+
+ return (
+ deployment.domains.length > 0 &&
+ deployment.domains.every((domain) => domain === deployment.assetsDomain)
+ );
+};
+
export const loadRawBuildById = async (
context: AppContext,
id: Build["id"]
@@ -317,17 +337,16 @@ export const createProductionBuild = async (
throw new AuthorizationError("You don't have access to build this project");
}
- // Get token permissions
if (context.authorization.type === "token") {
- const permissions = await authDb.getTokenPermissions(
- {
- projectId: props.projectId,
- token: context.authorization.authToken,
- },
+ const token = await authDb.getTokenInfo(
+ context.authorization.authToken,
context
);
- if (!permissions.canPublish) {
+ if (
+ token.projectId !== props.projectId ||
+ canTokenPublishDeployment(token, props.deployment) === false
+ ) {
throw new AuthorizationError(
"The token does not have permission to build this project."
);
@@ -350,3 +369,5 @@ export const createProductionBuild = async (
id: build.data,
};
};
+
+export const __testing__ = { canTokenPublishDeployment };