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: Skip notifications polling for anonymous users (#5739)

Oleg Isonena8856c26479c
diff --git a/apps/builder/app/routes/auth.ws.ts b/apps/builder/app/routes/auth.ws.ts
index 2f99e8d44a..c7e240c032 100644
--- a/apps/builder/app/routes/auth.ws.ts
+++ b/apps/builder/app/routes/auth.ws.ts
@@ -12,7 +12,7 @@ const debug = createDebug(import.meta.url);
// Endpoint to force user relogin
export const loader: LoaderFunction = async ({ request }) => {
if (false === isBuilder(request)) {
- debug(`Request url is not the builder URL ${request.url}`);
+ debug("Request url is not the builder URL");
return new Response("Only builder URL is allowed", {
status: 404,
@@ -24,10 +24,6 @@ export const loader: LoaderFunction = async ({ request }) => {
allowedDestinations(request, ["document"]);
// CSRF is not needed for document only routes
- debug(
- "Authenticate request received, starting authentication and authorization process"
- );
-
return await builderAuthenticator.authenticate("ws", request, {
throwOnError: true,
});
diff --git a/apps/builder/app/routes/auth.ws_.callback.ts b/apps/builder/app/routes/auth.ws_.callback.ts
index 2922c6a0f7..734491dd2f 100644
--- a/apps/builder/app/routes/auth.ws_.callback.ts
+++ b/apps/builder/app/routes/auth.ws_.callback.ts
@@ -14,7 +14,7 @@ const debug = createDebug(import.meta.url);
export const loader: LoaderFunction = async ({ request }) => {
if (false === isBuilder(request)) {
- debug(`Request url is not the builder URL ${request.url}`);
+ debug("Request url is not the builder URL");
return new Response("Only builder URL is allowed", {
status: 404,
@@ -28,8 +28,6 @@ export const loader: LoaderFunction = async ({ request }) => {
const returnTo = (await returnToPath(request)) ?? builderPath();
- debug("Start exchanging the code for the access token");
-
await builderAuthenticator.authenticate("ws", request, {
throwOnError: true,
successRedirect: returnTo,
@@ -37,8 +35,6 @@ export const loader: LoaderFunction = async ({ request }) => {
} catch (error) {
// all redirects are basically errors and in that case we don't want to catch it
if (error instanceof Response) {
- debug("Response with Cookie");
-
const session = await builderSessionStorage.getSession(
error.headers.get("Set-Cookie")
);
diff --git a/apps/builder/app/routes/oauth.ws.authorize.tsx b/apps/builder/app/routes/oauth.ws.authorize.tsx
index 2d42564816..49cf1daf9e 100644
--- a/apps/builder/app/routes/oauth.ws.authorize.tsx
+++ b/apps/builder/app/routes/oauth.ws.authorize.tsx
@@ -93,8 +93,6 @@ export const loader: LoaderFunction = async ({ request }) => {
// or by allowedDestinations for iframe requests.
try {
- debug("Authorize request received", request.url);
-
const url = new URL(request.url);
const searchParams = Object.fromEntries(url.searchParams);
@@ -165,8 +163,6 @@ export const loader: LoaderFunction = async ({ request }) => {
const sessionData = await authenticator.isAuthenticated(request);
if (sessionData) {
- debug(`User id=${sessionData.userId} is authenticated`);
-
const isAuthorized = await isUserAuthorizedForProject(
sessionData.userId,
oAuthParams.scope.projectId
@@ -174,9 +170,7 @@ export const loader: LoaderFunction = async ({ request }) => {
// scope: Ensure the requested scope is valid, authorized, and within the permissions granted to the client.
if (false === isAuthorized) {
- debug(
- `User ${sessionData.userId} is not the owner of ${oAuthParams.scope.projectId}, denying access`
- );
+ debug("User does not have access to the project");
return oauthError(
"unauthorized_client",
"User does not have access to the project"
@@ -207,10 +201,6 @@ export const loader: LoaderFunction = async ({ request }) => {
);
}
- debug(
- `User ${sessionData.userId} is the owner of ${oAuthParams.scope.projectId}, creating token`
- );
-
// We do not use database now.
// https://datatracker.ietf.org/doc/html/rfc7636#section-4.4
const code = await createCodeToken(
@@ -230,10 +220,6 @@ export const loader: LoaderFunction = async ({ request }) => {
// state: If present, store the state parameter to return it unchanged in the response
redirectUri.searchParams.set("state", oAuthParams.state);
- debug(
- `Code ${code} created, redirecting to redirect_uri: ${redirectUri.href}`
- );
-
const bloomFilter = await session.readLoginSessionBloomFilter(request);
bloomFilter.add(oAuthParams.scope.projectId);
@@ -247,10 +233,6 @@ export const loader: LoaderFunction = async ({ request }) => {
sessionData satisfies null;
- debug(
- "User is not authenticated, saving current url to returnTo cookie and redirecting to login"
- );
-
return redirect(loginPath({ returnTo: request.url }));
} catch (error) {
if (error instanceof Response) {
diff --git a/apps/builder/app/routes/oauth.ws.token.ts b/apps/builder/app/routes/oauth.ws.token.ts
index 866e77ea05..fcc9dde3a4 100644
--- a/apps/builder/app/routes/oauth.ws.token.ts
+++ b/apps/builder/app/routes/oauth.ws.token.ts
@@ -6,7 +6,6 @@ import { fromError } from "zod-validation-error";
import env from "~/env/env.server";
import {
createAccessToken,
- readAccessToken,
readCodeToken,
verifyChallenge,
} from "~/services/token.server";
@@ -49,8 +48,6 @@ export const action = async ({ request }: ActionFunctionArgs) => {
// CSRF token checks are not necessary for dashboard-only pages.
// All POST requests from the builder or canvas app are safeguarded by preventCrossOriginCookie
- debug("Token request received");
-
const authorizationHeader = request.headers.get("Authorization");
if (authorizationHeader === null) {
@@ -78,7 +75,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
clientId !== env.AUTH_WS_CLIENT_ID ||
clientSecret !== env.AUTH_WS_CLIENT_SECRET
) {
- debug("client_id and client_secret do not match", clientId, clientSecret);
+ debug("client_id and client_secret do not match", clientId);
return json(
{
error: "invalid_client",
@@ -90,7 +87,6 @@ export const action = async ({ request }: ActionFunctionArgs) => {
}
const jsonBody = Object.fromEntries((await request.formData()).entries());
- debug("Token request received", jsonBody);
const parsedBody = TokenRequest.safeParse(jsonBody);
@@ -113,7 +109,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
const codeToken = await readCodeToken(body.code, env.AUTH_WS_CLIENT_SECRET);
if (codeToken === undefined) {
- debug("Code can not be read", body.code);
+ debug("Code can not be read");
return json(
{
error: "invalid_grant",
@@ -130,11 +126,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
codeToken.codeChallenge
);
if (false === isChallengeVerified) {
- debug(
- "Code verifier does not match",
- body.code_verifier,
- codeToken.codeChallenge
- );
+ debug("Code verifier does not match");
return json(
{
error: "invalid_grant",
@@ -150,7 +142,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
const isAuthorized = await isUserAuthorizedForProject(userId, projectId);
if (false === isAuthorized) {
- debug("User does not have access to the project", userId, projectId);
+ debug("User does not have access to the project");
return json(
{
error: "invalid_grant",
@@ -172,13 +164,6 @@ export const action = async ({ request }: ActionFunctionArgs) => {
}
);
- debug("Token created", accessToken);
-
- debug(
- "readAccessToken",
- await readAccessToken(accessToken, env.AUTH_WS_CLIENT_SECRET)
- );
-
return json(
{
access_token: accessToken,
diff --git a/apps/builder/app/services/auth-strategy/ws.server.ts b/apps/builder/app/services/auth-strategy/ws.server.ts
index d67255971e..916f56e13a 100644
--- a/apps/builder/app/services/auth-strategy/ws.server.ts
+++ b/apps/builder/app/services/auth-strategy/ws.server.ts
@@ -6,6 +6,7 @@ import {
type OAuth2StrategyVerifyParams,
} from "remix-auth-oauth2";
import { AsyncLocalStorage } from "node:async_hooks";
+import createDebugRaw from "debug";
type DynamicProps = "authorizationEndpoint" | "tokenEndpoint" | "redirectURI";
@@ -16,6 +17,9 @@ const asyncLocalStorage = new AsyncLocalStorage<
Partial<OAuth2StrategyOptions>
>();
+// remix-auth-oauth2 logs OAuth state, PKCE verifier, and full callback URLs.
+createDebugRaw.enable(`${createDebugRaw.disable()},-OAuth2Strategy`);
+
/**
* The main issue with OAuth2Strategy is that it forces us to define authorizationEndpoint, tokenEndpoint, and redirectURI
* at the time of constructing the instance.
diff --git a/apps/builder/app/services/builder-auth.server.ts b/apps/builder/app/services/builder-auth.server.ts
index db8e65bc4e..7b522ba3fa 100644
--- a/apps/builder/app/services/builder-auth.server.ts
+++ b/apps/builder/app/services/builder-auth.server.ts
@@ -44,7 +44,7 @@ builderAuthenticator.use(
);
if (accessToken === undefined) {
- debug("Invalid or expired access token", tokens.access_token);
+ debug("Invalid or expired access token");
throw new AuthorizationError("Invalid or expired access token");
}
@@ -69,8 +69,6 @@ builderAuthenticator.use(
);
}
- debug("User authenticated", accessToken.userId);
-
return await { userId: accessToken.userId, createdAt: Date.now() };
},
(request: Request): OAuth2StrategyOptionsOverrides => {
diff --git a/apps/builder/app/shared/polly/topic-resolvers.server.test.ts b/apps/builder/app/shared/polly/topic-resolvers.server.test.ts
index c45d3b36d5..acfcc6bf38 100644
--- a/apps/builder/app/shared/polly/topic-resolvers.server.test.ts
+++ b/apps/builder/app/shared/polly/topic-resolvers.server.test.ts
@@ -20,6 +20,11 @@ const createContext = (userId = "user-1"): AppContext =>
authorization: { type: "user", userId },
}) as unknown as AppContext;
+const anonymousContext = {
+ ...testContext,
+ authorization: { type: "anonymous" },
+} as unknown as AppContext;
+
// Helper: member row with workspace embedded
const memberRow = (workspaceId: string, ownerId: string, name: string) => ({
workspaceId,
@@ -28,14 +33,16 @@ const memberRow = (workspaceId: string, ownerId: string, name: string) => ({
// ─── seatSuspended ─────────────────────────────────────────────
+describe("notifications (msw)", () => {
+ test("returns empty list for non-user authorization", async () => {
+ const result = await resolveTopics(["notifications"], anonymousContext);
+ expect(result.notifications).toEqual([]);
+ });
+});
+
describe("seatSuspended (msw)", () => {
test("returns false for non-user authorization", async () => {
- const ctx = {
- ...testContext,
- authorization: { type: "anonymous" },
- } as unknown as AppContext;
-
- const result = await resolveTopics(["seatSuspended"], ctx);
+ const result = await resolveTopics(["seatSuspended"], anonymousContext);
expect(result.seatSuspended).toBe(false);
});
diff --git a/apps/builder/app/shared/polly/topic-resolvers.server.ts b/apps/builder/app/shared/polly/topic-resolvers.server.ts
index e7bc0cc773..ec65f059d1 100644
--- a/apps/builder/app/shared/polly/topic-resolvers.server.ts
+++ b/apps/builder/app/shared/polly/topic-resolvers.server.ts
@@ -13,7 +13,12 @@ import type { TopicResolvers, TopicName, SubscriptionResponse } from "./types";
* to the registry without implementing it causes a compile error.
*/
const resolvers: TopicResolvers = {
- notifications: (ctx) => notification.list(ctx),
+ notifications: (ctx) => {
+ if (ctx.authorization.type !== "user") {
+ return Promise.resolve([]);
+ }
+ return notification.list(ctx);
+ },
projectCount: (ctx) => {
if (ctx.authorization.type !== "user") {
return Promise.resolve(0);
diff --git a/apps/builder/package.json b/apps/builder/package.json
index 9da70baebf..ba63d5e36b 100644
--- a/apps/builder/package.json
+++ b/apps/builder/package.json
@@ -12,7 +12,7 @@
"build:tailwind-preflight": "tsx --conditions=webstudio ./app/shared/tailwind/preflight-bin.ts && prettier --write ./app/shared/tailwind/__generated__/preflight.ts",
"start": "remix-serve build/server/index.js",
"dev": "remix vite:dev",
- "dev:auth": "DEBUG=OAuth2Strategy,ws:* remix vite:dev",
+ "dev:auth": "DEBUG=ws:* remix vite:dev",
"typecheck": "tsgo --noEmit --emitDeclarationOnly false",
"test": "vitest run"
},