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.

fix the scope checking

Kiran Kf70daaec19a6
diff --git a/apps/web/app/api/analytics/public-stats/route.ts b/apps/web/app/api/analytics/public-stats/route.ts
index d04cb989aa..9fd1a70985 100644
--- a/apps/web/app/api/analytics/public-stats/route.ts
+++ b/apps/web/app/api/analytics/public-stats/route.ts
@@ -17,7 +17,7 @@ export const GET = withWorkspace(
return NextResponse.json(response);
},
{
- requiredScopes: ["links.read"],
+ requiredScopes: ["links.read", "links.write"],
},
);
diff --git a/apps/web/app/api/domains/[domain]/route.ts b/apps/web/app/api/domains/[domain]/route.ts
index ccf18a0a8e..c0cff15854 100644
--- a/apps/web/app/api/domains/[domain]/route.ts
+++ b/apps/web/app/api/domains/[domain]/route.ts
@@ -50,7 +50,7 @@ export const GET = withWorkspace(
},
{
domainChecks: true,
- requiredScopes: ["domains.read"],
+ requiredScopes: ["domains.read", "domains.write"],
},
);
diff --git a/apps/web/app/api/domains/default/route.ts b/apps/web/app/api/domains/default/route.ts
index 6eb0fd9577..98bfad089c 100644
--- a/apps/web/app/api/domains/default/route.ts
+++ b/apps/web/app/api/domains/default/route.ts
@@ -34,7 +34,7 @@ export const GET = withWorkspace(
return NextResponse.json(defaultDomainsArray);
},
{
- requiredScopes: ["domains.read"],
+ requiredScopes: ["domains.read", "domains.write"],
},
);
diff --git a/apps/web/app/api/domains/route.ts b/apps/web/app/api/domains/route.ts
index 366f6b6edf..663548a296 100644
--- a/apps/web/app/api/domains/route.ts
+++ b/apps/web/app/api/domains/route.ts
@@ -24,7 +24,7 @@ export const GET = withWorkspace(
return NextResponse.json(z.array(DomainSchema).parse(domains));
},
{
- requiredScopes: ["domains.read"],
+ requiredScopes: ["domains.read", "domains.write"],
},
);
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 2e4fcad9c1..6b2d2711a7 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -48,7 +48,7 @@ export const GET = withWorkspace(
return NextResponse.json(response, { headers });
},
{
- requiredScopes: ["links.read"],
+ requiredScopes: ["links.read", "links.write"],
},
);
diff --git a/apps/web/app/api/links/count/route.ts b/apps/web/app/api/links/count/route.ts
index 22f4292d3d..14a5db10b2 100644
--- a/apps/web/app/api/links/count/route.ts
+++ b/apps/web/app/api/links/count/route.ts
@@ -19,6 +19,6 @@ export const GET = withWorkspace(
});
},
{
- requiredScopes: ["links.read"],
+ requiredScopes: ["links.read", "links.write"],
},
);
diff --git a/apps/web/app/api/links/info/route.ts b/apps/web/app/api/links/info/route.ts
index 64bf3a277f..d9185e908c 100644
--- a/apps/web/app/api/links/info/route.ts
+++ b/apps/web/app/api/links/info/route.ts
@@ -61,6 +61,6 @@ export const GET = withWorkspace(
});
},
{
- requiredScopes: ["links.read"],
+ requiredScopes: ["links.read", "links.write"],
},
);
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index c263daa653..acbf2913bb 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -47,7 +47,7 @@ export const GET = withWorkspace(
});
},
{
- requiredScopes: ["links.read"],
+ requiredScopes: ["links.read", "links.write"],
},
);
diff --git a/apps/web/app/api/tags/route.ts b/apps/web/app/api/tags/route.ts
index 887d58867d..2b79cd2a58 100644
--- a/apps/web/app/api/tags/route.ts
+++ b/apps/web/app/api/tags/route.ts
@@ -24,7 +24,7 @@ export const GET = withWorkspace(
return NextResponse.json(tags, { headers });
},
{
- requiredScopes: ["tags.read"],
+ requiredScopes: ["tags.read", "tags.write"],
},
);
diff --git a/apps/web/app/api/tokens/[id]/route.ts b/apps/web/app/api/tokens/[id]/route.ts
index 0c378d584f..9ca423779c 100644
--- a/apps/web/app/api/tokens/[id]/route.ts
+++ b/apps/web/app/api/tokens/[id]/route.ts
@@ -42,7 +42,7 @@ export const GET = withWorkspace(
return NextResponse.json(tokenSchema.parse(token));
},
{
- requiredScopes: ["tokens.read"],
+ requiredScopes: ["tokens.read", "tokens.write"],
},
);
diff --git a/apps/web/app/api/tokens/route.ts b/apps/web/app/api/tokens/route.ts
index 1363ff08f4..46fd2cda2c 100644
--- a/apps/web/app/api/tokens/route.ts
+++ b/apps/web/app/api/tokens/route.ts
@@ -102,6 +102,6 @@ export const GET = withWorkspace(
return NextResponse.json(tokenSchema.array().parse(tokens));
},
{
- requiredScopes: ["tokens.read"],
+ requiredScopes: ["tokens.read", "tokens.write"],
},
);
diff --git a/apps/web/app/api/tokens/scopes/route.ts b/apps/web/app/api/tokens/scopes/route.ts
index 415c6a1edf..98f3ff08b8 100644
--- a/apps/web/app/api/tokens/scopes/route.ts
+++ b/apps/web/app/api/tokens/scopes/route.ts
@@ -1,8 +1,8 @@
-import { scopes } from "@/lib/api/tokens/scopes";
+import { scopeDescriptions } from "@/lib/api/tokens/scopes";
import { withWorkspace } from "@/lib/auth";
import { NextResponse } from "next/server";
-// GET /api/tokens/scopes – return the available scopes for the token
+// GET /api/tokens/scopes – returns a list of all resource scopes
export const GET = withWorkspace(async () => {
- return NextResponse.json(scopes);
+ return NextResponse.json(scopeDescriptions);
});
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/invites/route.tsx b/apps/web/app/api/workspaces/[idOrSlug]/invites/route.tsx
index 6e35de3a4a..83a763b81f 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/invites/route.tsx
+++ b/apps/web/app/api/workspaces/[idOrSlug]/invites/route.tsx
@@ -24,7 +24,7 @@ export const GET = withWorkspace(
return NextResponse.json(invites);
},
{
- requiredScopes: ["workspaces.read"],
+ requiredScopes: ["workspaces.read", "workspaces.write"],
},
);
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
index 953ab19d33..38b65ee8d4 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
@@ -47,7 +47,7 @@ export const GET = withWorkspace(
);
},
{
- requiredScopes: ["workspaces.read"],
+ requiredScopes: ["workspaces.read", "workspaces.write"],
},
);
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/saml/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/saml/route.ts
index a799bc51fd..fbb56bc153 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/saml/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/saml/route.ts
@@ -45,7 +45,7 @@ export const GET = withWorkspace(
return NextResponse.json(response);
},
{
- requiredScopes: ["workspaces.read"],
+ requiredScopes: ["workspaces.read", "workspaces.write"],
},
);
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/scim/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/scim/route.ts
index e44ae37ad5..ed0eb90e5b 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/scim/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/scim/route.ts
@@ -35,7 +35,7 @@ export const GET = withWorkspace(
});
},
{
- requiredScopes: ["workspaces.read"],
+ requiredScopes: ["workspaces.read", "workspaces.write"],
},
);
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts
index aa49f88d94..a7bd969e6d 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts
@@ -47,7 +47,7 @@ export const GET = withWorkspace(
);
},
{
- requiredScopes: ["workspaces.read"],
+ requiredScopes: ["workspaces.read", "workspaces.write"],
},
);
diff --git a/apps/web/lib/api/tokens/permissions.ts b/apps/web/lib/api/tokens/permissions.ts
index d9f312a2d3..ce8daa2e7d 100644
--- a/apps/web/lib/api/tokens/permissions.ts
+++ b/apps/web/lib/api/tokens/permissions.ts
@@ -6,20 +6,30 @@ import { Scope } from "./scopes";
export const throwIfNoAccess = ({
scopes,
requiredScopes,
+ workspaceId,
}: {
scopes: Scope[];
requiredScopes: Scope[];
+ workspaceId: string;
}) => {
if (requiredScopes.length === 0) {
return;
}
+ console.log("Checking scopes", scopes, requiredScopes);
+
for (const requiredScope of requiredScopes) {
- if (!scopes.includes(requiredScope)) {
- throw new DubApiError({
- code: "forbidden",
- message: `You do not have permission to make this action.`,
- });
+ if (scopes.includes(requiredScope)) {
+ return;
}
}
+
+ const missingScopes = requiredScopes
+ .filter((requiredScope) => !scopes.includes(requiredScope))
+ .join(", ");
+
+ throw new DubApiError({
+ code: "forbidden",
+ message: `The provided key does not have the required permissions for this endpoint on the workspace 'w_${workspaceId}'. Having the '${missingScopes}' permission would allow this request to continue.`,
+ });
};
diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.ts
index 845734973a..12b3e5f883 100644
--- a/apps/web/lib/auth/workspace.ts
+++ b/apps/web/lib/auth/workspace.ts
@@ -299,7 +299,7 @@ export const withWorkspace = (
}
// Check user has permission to make the action
- throwIfNoAccess({ scopes, requiredScopes });
+ throwIfNoAccess({ scopes, requiredScopes, workspaceId: workspace.id });
// beta feature checks
if (betaFeature) {
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index ebfeddb1de..98fd74040c 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -196,4 +196,4 @@ export const tagColors = [
"brown",
] as const;
-export type MetaTag = z.infer<typeof metaTagsSchema>;
+export type MetaTag = z.infer<typeof metaTagsSchema>;
\ No newline at end of file