Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Enhance Stripe integration by adding token validation and authorization checks; remove 'workspaces.settings.write' scope from various files to streamline permissions.

Kiran K821f94d690e1
diff --git a/apps/web/app/(ee)/api/stripe/integration/route.ts b/apps/web/app/(ee)/api/stripe/integration/route.ts
index 1d7684db96..fb06b17562 100644
--- a/apps/web/app/(ee)/api/stripe/integration/route.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/route.ts
@@ -16,7 +16,7 @@ const CORS_HEADERS = new Headers({
// PATCH /api/stripe/integration - update a workspace with a stripe connect account id
export const PATCH = withWorkspace(
- async ({ req, workspace, session }) => {
+ async ({ req, workspace, session, token }) => {
const body = await parseRequestBody(req);
const { stripeAccountId } = z
.object({
@@ -24,6 +24,29 @@ export const PATCH = withWorkspace(
})
.parse(body);
+ if (!token?.installationId) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: "You are not authorized to update the stripe integration.",
+ });
+ }
+
+ const installation = await prisma.installedIntegration.findUnique({
+ where: {
+ id: token.installationId,
+ },
+ select: {
+ integrationId: true,
+ },
+ });
+
+ if (!installation || installation.integrationId !== STRIPE_INTEGRATION_ID) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: "You are not authorized to update the stripe integration.",
+ });
+ }
+
try {
const response = await prisma.project.update({
where: {
@@ -93,7 +116,6 @@ export const PATCH = withWorkspace(
}
},
{
- requiredPermissions: ["workspaces.settings.write"],
requiredPlan: [
"business",
"business plus",
diff --git a/apps/web/lib/api/oauth/constants.ts b/apps/web/lib/api/oauth/constants.ts
index e6b9b03bb7..b971b906aa 100644
--- a/apps/web/lib/api/oauth/constants.ts
+++ b/apps/web/lib/api/oauth/constants.ts
@@ -19,7 +19,6 @@ export const OAUTH_CONFIG = {
// Keep it separate from the actual scopes to avoid confusion
// We don't want all the scopes to be requested by an app
export const OAUTH_SCOPES = [
- "workspaces.settings.write",
"links.read",
"links.write",
"tags.read",
@@ -36,7 +35,6 @@ export const OAUTH_SCOPES = [
// Scope descriptions
export const OAUTH_SCOPE_DESCRIPTIONS = {
- "workspaces.settings.write": "Update limited workspace information",
"links.read": "Read access to links",
"links.write": "Read and Write access to links",
"tags.read": "Read access to tags",
diff --git a/apps/web/lib/api/rbac/permissions.ts b/apps/web/lib/api/rbac/permissions.ts
index 694092ed77..bf20756812 100644
--- a/apps/web/lib/api/rbac/permissions.ts
+++ b/apps/web/lib/api/rbac/permissions.ts
@@ -3,7 +3,6 @@ import { WorkspaceRole } from "@dub/prisma/client";
export const PERMISSION_ACTIONS = [
"workspaces.read",
"workspaces.write",
- "workspaces.settings.write",
"links.read",
"links.write",
"tags.read",
@@ -60,11 +59,6 @@ export const ROLE_PERMISSIONS: {
description: "update or delete the current workspace",
roles: ["owner"],
},
- {
- action: "workspaces.settings.write",
- description: "update limited workspace settings",
- roles: ["owner"],
- },
{
action: "domains.read",
description: "access domains",
diff --git a/apps/web/lib/api/tokens/scopes.ts b/apps/web/lib/api/tokens/scopes.ts
index 658e6b392f..95517a72e8 100644
--- a/apps/web/lib/api/tokens/scopes.ts
+++ b/apps/web/lib/api/tokens/scopes.ts
@@ -14,7 +14,6 @@ export const SCOPES = [
"domains.write",
"workspaces.read",
"workspaces.write",
- "workspaces.settings.write",
"webhooks.read",
"webhooks.write",
"groups.read",
@@ -117,13 +116,6 @@ export const RESOURCE_SCOPES: {
type: "write",
resource: "workspaces",
},
- {
- scope: "workspaces.settings.write",
- roles: ["owner"],
- permissions: ["workspaces.settings.write"],
- type: "write",
- resource: "workspaces",
- },
{
scope: "analytics.read",
roles: ["owner", "member"],
@@ -234,10 +226,7 @@ export const getScopesByResourceForRole = (role: WorkspaceRole) => {
const groupedByResource = {};
const allowedScopes = RESOURCE_SCOPES.map((scope) => {
- if (
- scope.roles.includes(role) &&
- scope.scope !== "workspaces.settings.write"
- ) {
+ if (scope.roles.includes(role)) {
return scope;
}
}).filter(Boolean);
diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.ts
index fae669f42c..e84d991e4f 100644
--- a/apps/web/lib/auth/workspace.ts
+++ b/apps/web/lib/auth/workspace.ts
@@ -41,6 +41,7 @@ interface WithWorkspaceHandler {
session,
workspace,
permissions,
+ token,
}: {
req: Request;
params: Record<string, string>;
@@ -49,6 +50,7 @@ interface WithWorkspaceHandler {
session: Session;
permissions: PermissionAction[];
workspace: WorkspaceWithUsers;
+ token: TokenCacheItem | null;
}): Promise<Response>;
}
@@ -464,6 +466,7 @@ export const withWorkspace = (
session,
workspace,
permissions,
+ token,
});
} catch (error) {
// Log the conversion events for debugging purposes
diff --git a/apps/web/lib/zod/schemas/oauth.ts b/apps/web/lib/zod/schemas/oauth.ts
index cc68a2c9b0..e6b8a0b68e 100644
--- a/apps/web/lib/zod/schemas/oauth.ts
+++ b/apps/web/lib/zod/schemas/oauth.ts
@@ -51,14 +51,17 @@ export const authorizeRequestSchema = z.object({
.nullable()
.transform((scope) => {
// split by comma or space or plus sign
- const scopes = [
- ...new Set((scope ?? "").split(/[,\s+]/).filter(Boolean)),
- ];
+ let scopes = [...new Set((scope ?? "").split(/[,\s+]/).filter(Boolean))];
if (!scopes.includes("user.read")) {
scopes.push("user.read");
}
+ // remove workspaces.read and write
+ // We can remove this filter after existing integrations are updated
+ // Doing this to prevent zod throwing an error when the scopes are invalid
+ scopes = scopes.filter((scope) => !scope.startsWith("workspaces."));
+
return scopes;
})
.refine((scopes) => scopes.every((scope) => OAUTH_SCOPES.includes(scope)), {
diff --git a/packages/stripe-app/src/utils/oauth.ts b/packages/stripe-app/src/utils/oauth.ts
index 9645013540..66d7f759e6 100644
--- a/packages/stripe-app/src/utils/oauth.ts
+++ b/packages/stripe-app/src/utils/oauth.ts
@@ -22,7 +22,6 @@ export function getOAuthUrl({
response_type: "code",
code_challenge: challenge,
code_challenge_method: "S256",
- scope: "workspaces.settings.write",
state,
};