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.

Refactor workspace permissions and scopes to use 'workspaces.settings.write' instead of 'workspaces.write'; add script to remove deprecated workspace scopes from tokens.

Kiran K314c29dd089e
diff --git a/apps/web/app/(ee)/api/stripe/integration/route.ts b/apps/web/app/(ee)/api/stripe/integration/route.ts
index 56fb76add2..1d7684db96 100644
--- a/apps/web/app/(ee)/api/stripe/integration/route.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/route.ts
@@ -93,7 +93,7 @@ export const PATCH = withWorkspace(
}
},
{
- requiredPermissions: ["workspaces.write"],
+ 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 1b1eefb4c4..e6b9b03bb7 100644
--- a/apps/web/lib/api/oauth/constants.ts
+++ b/apps/web/lib/api/oauth/constants.ts
@@ -19,7 +19,7 @@ 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.write",
+ "workspaces.settings.write",
"links.read",
"links.write",
"tags.read",
@@ -36,7 +36,7 @@ export const OAUTH_SCOPES = [
// Scope descriptions
export const OAUTH_SCOPE_DESCRIPTIONS = {
- "workspaces.write": "Read and Write access to workspace",
+ "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 bf20756812..694092ed77 100644
--- a/apps/web/lib/api/rbac/permissions.ts
+++ b/apps/web/lib/api/rbac/permissions.ts
@@ -3,6 +3,7 @@ import { WorkspaceRole } from "@dub/prisma/client";
export const PERMISSION_ACTIONS = [
"workspaces.read",
"workspaces.write",
+ "workspaces.settings.write",
"links.read",
"links.write",
"tags.read",
@@ -59,6 +60,11 @@ 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 95517a72e8..ed51b43158 100644
--- a/apps/web/lib/api/tokens/scopes.ts
+++ b/apps/web/lib/api/tokens/scopes.ts
@@ -14,6 +14,7 @@ export const SCOPES = [
"domains.write",
"workspaces.read",
"workspaces.write",
+ "workspaces.settings.write",
"webhooks.read",
"webhooks.write",
"groups.read",
@@ -116,6 +117,13 @@ 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"],
diff --git a/apps/web/lib/zod/schemas/oauth.ts b/apps/web/lib/zod/schemas/oauth.ts
index 46251cc7ce..cc68a2c9b0 100644
--- a/apps/web/lib/zod/schemas/oauth.ts
+++ b/apps/web/lib/zod/schemas/oauth.ts
@@ -62,7 +62,7 @@ export const authorizeRequestSchema = z.object({
return scopes;
})
.refine((scopes) => scopes.every((scope) => OAUTH_SCOPES.includes(scope)), {
- message: "Invalid scopes",
+ message: "One or more provided OAuth scopes are invalid or unsupported.",
}),
code_challenge: z.string().max(190).optional(),
code_challenge_method: z
diff --git a/apps/web/scripts/remove-workspace-scopes.ts b/apps/web/scripts/remove-workspace-scopes.ts
new file mode 100644
index 0000000000..437d0d92a8
--- /dev/null
+++ b/apps/web/scripts/remove-workspace-scopes.ts
@@ -0,0 +1,53 @@
+import { prisma } from "@dub/prisma";
+import "dotenv-flow/config";
+
+async function main() {
+ // Find all restricted tokens that have the workspace.* scopes
+ const tokens = await prisma.restrictedToken.findMany({
+ where: {
+ scopes: {
+ contains: "workspaces.",
+ },
+ installationId: {
+ not: null,
+ },
+ },
+ select: {
+ id: true,
+ scopes: true,
+ lastUsed: true,
+ },
+ take: 100,
+ });
+
+ console.table(tokens);
+
+ // Remove the workspace.* scopes from the tokens
+ const results = await Promise.allSettled(
+ tokens.map((token) => {
+ if (!token.scopes) return Promise.resolve(null);
+
+ // Split by spaces, filter out "workspaces.read" and "workspaces.write", and join back
+ const updatedScopes = token.scopes
+ .split(" ")
+ .filter((scope) => !scope.startsWith("workspaces."))
+ .join(" ")
+ .trim();
+
+ return prisma.restrictedToken.update({
+ where: {
+ id: token.id,
+ },
+ data: {
+ scopes: updatedScopes || null,
+ },
+ });
+ }),
+ );
+
+ console.log(
+ `Updated ${results.filter((r) => r.status === "fulfilled").length} tokens.`,
+ );
+}
+
+main();
diff --git a/packages/prisma/schema/token.prisma b/packages/prisma/schema/token.prisma
index bba8a560b2..6987fa497e 100644
--- a/packages/prisma/schema/token.prisma
+++ b/packages/prisma/schema/token.prisma
@@ -18,7 +18,7 @@ model RestrictedToken {
name String
hashedKey String @unique
partialKey String
- scopes String? // space separated (Eg: "links:write", "domains:read")
+ scopes String? // space separated (Eg: "links.write domains.read")
expires DateTime?
lastUsed DateTime?
createdAt DateTime @default(now())
diff --git a/packages/stripe-app/src/utils/oauth.ts b/packages/stripe-app/src/utils/oauth.ts
index ed9ccb5617..9645013540 100644
--- a/packages/stripe-app/src/utils/oauth.ts
+++ b/packages/stripe-app/src/utils/oauth.ts
@@ -22,7 +22,7 @@ export function getOAuthUrl({
response_type: "code",
code_challenge: challenge,
code_challenge_method: "S256",
- scope: "workspaces.write",
+ scope: "workspaces.settings.write",
state,
};