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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
Skip validation (#1484)
Ivan Starkovc73462ed65dd
diff --git a/packages/prisma-client/src/prisma.ts b/packages/prisma-client/src/prisma.tsindex 697802cad6..ccaf6860fe 100644--- a/packages/prisma-client/src/prisma.ts+++ b/packages/prisma-client/src/prisma.ts@@ -4,14 +4,27 @@ const { PrismaClientKnownRequestError, Decimal } = Prisma;declare global {// allow global `var` declarations// eslint-disable-next-line no-var- var prisma: PrismaClient | undefined;+ var prisma:+ | PrismaClient<+ {+ log: "query"[];+ },+ "query"+ >+ | undefined;}// this fixes the issue with `warn(prisma-client) There are already 10 instances of Prisma Client actively running.`// explanation here// https://www.prisma.io/docs/guides/database/troubleshooting-orm/help-articles/nextjs-prisma-client-dev-practices-export const prisma =- global.prisma || new PrismaClient(/* { log: ["query"] } */);+export const prisma = global.prisma || new PrismaClient({ log: ["query"] });++prisma.$on("query", (e) => {+ // eslint-disable-next-line no-console+ console.log("Params: " + e.params);+ // eslint-disable-next-line no-console+ console.log("Duration: " + e.duration + "ms");+});if (process.env.NODE_ENV !== "production") {global.prisma = prisma;diff --git a/packages/project-build/src/db/breakpoints.ts b/packages/project-build/src/db/breakpoints.tsindex 537d6dca9a..62723e1668 100644--- a/packages/project-build/src/db/breakpoints.ts+++ b/packages/project-build/src/db/breakpoints.ts@@ -13,8 +13,13 @@ import {type AppContext,} from "@webstudio-is/trpc-interface/server";-export const parseBreakpoints = (breakpointsString: string): Breakpoints => {- const breakpointssList = BreakpointsList.parse(JSON.parse(breakpointsString));+export const parseBreakpoints = (+ breakpointsString: string,+ skipValidation = false+): Breakpoints => {+ const breakpointssList = skipValidation+ ? (JSON.parse(breakpointsString) as BreakpointsList)+ : BreakpointsList.parse(JSON.parse(breakpointsString));return new Map(breakpointssList.map((item) => [item.id, item]));};diff --git a/packages/project-build/src/db/build.ts b/packages/project-build/src/db/build.tsindex 90820e78f0..14608f381a 100644--- a/packages/project-build/src/db/build.ts+++ b/packages/project-build/src/db/build.ts@@ -1,7 +1,8 @@+/* eslint no-console: ["error", { allow: ["time", "timeEnd"] }] */+import { nanoid } from "nanoid";import {type Build as DbBuild,- type Project,prisma,Prisma,} from "@webstudio-is/prisma-client";@@ -23,43 +24,48 @@ import { parseProps, serializeProps } from "./props";import { parseInstances, serializeInstances } from "./instances";const parseBuild = async (build: DbBuild): Promise<Build> => {- const pages = Pages.parse(JSON.parse(build.pages));- return {- id: build.id,- projectId: build.projectId,- isDev: build.isDev,- isProd: build.isProd,- createdAt: build.createdAt.toISOString(),- pages,- breakpoints: Array.from(parseBreakpoints(build.breakpoints)),- styles: Array.from(parseStyles(build.styles)),- styleSources: Array.from(parseStyleSources(build.styleSources)),- styleSourceSelections: Array.from(- parseStyleSourceSelections(build.styleSourceSelections)- ),- props: Array.from(parseProps(build.props)),- instances: Array.from(parseInstances(build.instances)),- };-};--export const loadBuildById = async ({- projectId,- buildId,-}: {- projectId: Project["id"];- buildId: Build["id"];-}): Promise<Build> => {- const build = await prisma.build.findUnique({- where: {- id_projectId: { projectId, id: buildId },- },- });-- if (build === null) {- throw new Error("Build not found");+ // Hardcode skipValidation to true for now+ const skipValidation = true;+ // eslint-disable-next-line no-console+ console.time("parseBuild");+ try {+ const pages = skipValidation+ ? (JSON.parse(build.pages) as Pages)+ : Pages.parse(JSON.parse(build.pages));++ const breakpoints = Array.from(+ parseBreakpoints(build.breakpoints, skipValidation)+ );+ const styles = Array.from(parseStyles(build.styles, skipValidation));+ const styleSources = Array.from(+ parseStyleSources(build.styleSources, skipValidation)+ );+ const styleSourceSelections = Array.from(+ parseStyleSourceSelections(build.styleSourceSelections, skipValidation)+ );+ const props = Array.from(parseProps(build.props, skipValidation));+ const instances = Array.from(+ parseInstances(build.instances, skipValidation)+ );++ return {+ id: build.id,+ projectId: build.projectId,+ isDev: build.isDev,+ isProd: build.isProd,+ createdAt: build.createdAt.toISOString(),+ pages,+ breakpoints,+ styles,+ styleSources,+ styleSourceSelections,+ props,+ instances,+ };+ } finally {+ // eslint-disable-next-line no-console+ console.timeEnd("parseBuild");}-- return parseBuild(build);};export async function loadBuildByProjectId(diff --git a/packages/project-build/src/db/instances.ts b/packages/project-build/src/db/instances.tsindex 737ce55416..ed00d28a11 100644--- a/packages/project-build/src/db/instances.ts+++ b/packages/project-build/src/db/instances.ts@@ -6,8 +6,13 @@ import {} from "@webstudio-is/trpc-interface/server";import { Instances, InstancesList } from "../schema/instances";-export const parseInstances = (instancesString: string): Instances => {- const instancesList = InstancesList.parse(JSON.parse(instancesString));+export const parseInstances = (+ instancesString: string,+ skipValidation = false+): Instances => {+ const instancesList = skipValidation+ ? (JSON.parse(instancesString) as InstancesList)+ : InstancesList.parse(JSON.parse(instancesString));return new Map(instancesList.map((prop) => [prop.id, prop]));};diff --git a/packages/project-build/src/db/props.ts b/packages/project-build/src/db/props.tsindex 7de986cdfe..f7046f7e04 100644--- a/packages/project-build/src/db/props.ts+++ b/packages/project-build/src/db/props.ts@@ -6,8 +6,13 @@ import {} from "@webstudio-is/trpc-interface/server";import { Props, PropsList } from "../schema/props";-export const parseProps = (propsString: string): Props => {- const propsList = PropsList.parse(JSON.parse(propsString));+export const parseProps = (+ propsString: string,+ skipValidation = false+): Props => {+ const propsList = skipValidation+ ? (JSON.parse(propsString) as PropsList)+ : PropsList.parse(JSON.parse(propsString));return new Map(propsList.map((prop) => [prop.id, prop]));};diff --git a/packages/project-build/src/db/style-source-selections.ts b/packages/project-build/src/db/style-source-selections.tsindex f90618453c..bdf865eb1d 100644--- a/packages/project-build/src/db/style-source-selections.ts+++ b/packages/project-build/src/db/style-source-selections.ts@@ -10,11 +10,13 @@ import {} from "../schema/style-source-selections";export const parseStyleSourceSelections = (- styleSourceSelectionsString: string+ styleSourceSelectionsString: string,+ skipValidation = false): StyleSourceSelections => {- const styleSourceSelectionsList = StyleSourceSelectionsList.parse(- JSON.parse(styleSourceSelectionsString)- );+ const styleSourceSelectionsList = skipValidation+ ? (JSON.parse(styleSourceSelectionsString) as StyleSourceSelectionsList)+ : StyleSourceSelectionsList.parse(JSON.parse(styleSourceSelectionsString));+return new Map(styleSourceSelectionsList.map((item) => [item.instanceId, item]));diff --git a/packages/project-build/src/db/style-sources.ts b/packages/project-build/src/db/style-sources.tsindex f19394d011..fa192e06a6 100644--- a/packages/project-build/src/db/style-sources.ts+++ b/packages/project-build/src/db/style-sources.ts@@ -7,10 +7,13 @@ import {import type { Build } from "../types";import { StyleSourcesList, StyleSources } from "../schema/style-sources";-export const parseStyleSources = (styleSourceString: string): StyleSources => {- const styleSourcesList = StyleSourcesList.parse(- JSON.parse(styleSourceString)- );+export const parseStyleSources = (+ styleSourceString: string,+ skipValidation = false+): StyleSources => {+ const styleSourcesList = skipValidation+ ? (JSON.parse(styleSourceString) as StyleSourcesList)+ : StyleSourcesList.parse(JSON.parse(styleSourceString));return new Map(styleSourcesList.map((item) => [item.id, item]));};diff --git a/packages/project-build/src/db/styles.ts b/packages/project-build/src/db/styles.tsindex 2bd9639432..d2aeb3036a 100644--- a/packages/project-build/src/db/styles.ts+++ b/packages/project-build/src/db/styles.ts@@ -7,8 +7,14 @@ import {import type { Build } from "../types";import { Styles, StylesList, getStyleDeclKey } from "../schema/styles";-export const parseStyles = (stylesString: string): Styles => {- const stylesList = StylesList.parse(JSON.parse(stylesString));+export const parseStyles = (+ stylesString: string,+ skipValidation = false+): Styles => {+ const stylesList = skipValidation+ ? (JSON.parse(stylesString) as StylesList)+ : StylesList.parse(JSON.parse(stylesString));+return new Map(stylesList.map((item) => [getStyleDeclKey(item), item]));};diff --git a/packages/trpc-interface/src/authorize/project.server.ts b/packages/trpc-interface/src/authorize/project.server.tsindex 85b86fd8cc..55711594e6 100644--- a/packages/trpc-interface/src/authorize/project.server.ts+++ b/packages/trpc-interface/src/authorize/project.server.ts@@ -37,77 +37,86 @@ export const hasProjectPermit = async (},context: AppContext) => {- const { authorization } = context;- const { authorizeTrpc } = authorization;+ const start = Date.now();- const checks = [];- const namespace = "Project";+ try {+ const { authorization } = context;+ const { authorizeTrpc } = authorization;- // Allow load production build env i.e. "published" site- if (props.permit === "view" && context.authorization.buildEnv === "prod") {- return true;- }+ const checks = [];+ const namespace = "Project";- // Allow load webstudiois for clone- // @todo Rethink permissions for this use-case- // The plan is to make new permission for projects which are allowed to be publicly clonable by anyone- // https://github.com/webstudio-is/webstudio-builder/issues/1038- if (- props.permit === "view" &&- props.projectId === "62154aaef0cb0860ccf85d6e"- ) {- return true;- }+ // Allow load production build env i.e. "published" site+ if (props.permit === "view" && context.authorization.buildEnv === "prod") {+ return true;+ }- // Check if the user is allowed to access the project- if (authorization.userId !== undefined) {- checks.push(- authorizeTrpc.check.query({- subjectSet: {- namespace: "User",- id: authorization.userId,- },- namespace,- id: props.projectId,- permit: props.permit,- })- );- }+ // Allow load webstudiois for clone+ // @todo Rethink permissions for this use-case+ // The plan is to make new permission for projects which are allowed to be publicly clonable by anyone+ // https://github.com/webstudio-is/webstudio-builder/issues/1038+ if (+ props.permit === "view" &&+ props.projectId === "62154aaef0cb0860ccf85d6e"+ ) {+ return true;+ }- // Check if the special link with a token allows to access the project- // Token doesn't have own permit, do not check it- if (authorization.authToken !== undefined && props.permit !== "own") {- checks.push(- authorizeTrpc.check.query({- namespace,- id: props.projectId,- subjectSet: {- id: authorization.authToken,- namespace: "Token",- },- permit: props.permit,- })- );- }+ // Check if the user is allowed to access the project+ if (authorization.userId !== undefined) {+ checks.push(+ authorizeTrpc.check.query({+ subjectSet: {+ namespace: "User",+ id: authorization.userId,+ },+ namespace,+ id: props.projectId,+ permit: props.permit,+ })+ );+ }- if (checks.length === 0) {- return false;- }+ // Check if the special link with a token allows to access the project+ // Token doesn't have own permit, do not check it+ if (authorization.authToken !== undefined && props.permit !== "own") {+ checks.push(+ authorizeTrpc.check.query({+ namespace,+ id: props.projectId,+ subjectSet: {+ id: authorization.authToken,+ namespace: "Token",+ },+ permit: props.permit,+ })+ );+ }++ if (checks.length === 0) {+ return false;+ }- const authResults = await Promise.allSettled(checks);+ const authResults = await Promise.allSettled(checks);- for (const authResult of authResults) {- if (authResult.status === "rejected") {- throw new Error(`Authorization call failed ${authResult.reason}`);+ for (const authResult of authResults) {+ if (authResult.status === "rejected") {+ throw new Error(`Authorization call failed ${authResult.reason}`);+ }}- }- const allowed = authResults.some(- (authResult) =>- authResult.status === "fulfilled" && authResult.value.allowed- );+ const allowed = authResults.some(+ (authResult) =>+ authResult.status === "fulfilled" && authResult.value.allowed+ );++ return allowed;+ } finally {+ const diff = Date.now() - start;- return allowed;+ // eslint-disable-next-line no-console+ console.log(`hasProjectPermit execution ${diff}ms`);+ }};/**@@ -122,21 +131,30 @@ export const getProjectPermit = async <T extends AuthPermit>(},context: AppContext): Promise<T | undefined> => {- const permitToCheck = props.permits;+ const start = Date.now();- const permits = await Promise.allSettled(- permitToCheck.map((permit) =>- hasProjectPermit({ projectId: props.projectId, permit }, context)- )- );+ try {+ const permitToCheck = props.permits;- for (const permit of permits) {- if (permit.status === "rejected") {- throw new Error(`Authorization call failed ${permit.reason}`);- }+ const permits = await Promise.allSettled(+ permitToCheck.map((permit) =>+ hasProjectPermit({ projectId: props.projectId, permit }, context)+ )+ );- if (permit.value === true) {- return permitToCheck[permits.indexOf(permit)];+ for (const permit of permits) {+ if (permit.status === "rejected") {+ throw new Error(`Authorization call failed ${permit.reason}`);+ }++ if (permit.value === true) {+ return permitToCheck[permits.indexOf(permit)];+ }}+ } finally {+ const diff = Date.now() - start;++ // eslint-disable-next-line no-console+ console.log(`getProjectPermit execution ${diff}ms`);}};diff --git a/turbo.json b/turbo.jsonindex f4d3dad16a..ac137c0b2b 100644--- a/turbo.json+++ b/turbo.json@@ -1,6 +1,6 @@{"$schema": "https://turbo.build/schema.json",- "globalEnv": ["AUTH_SECRET", "DATABASE_URL"],+ "globalEnv": ["AUTH_SECRET", "DATABASE_URL", "NODE_ENV"],"pipeline": {"build": {"dependsOn": ["^build"],