Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(gitea): restrict webhook secret access (#1377)

Tin Sever37207a0e2cf0
diff --git a/apps/api/src/gitea-integration/controllers/get-gitea-integration.ts b/apps/api/src/gitea-integration/controllers/get-gitea-integration.ts
index 40478596d..545a46a9e 100644
--- a/apps/api/src/gitea-integration/controllers/get-gitea-integration.ts
+++ b/apps/api/src/gitea-integration/controllers/get-gitea-integration.ts
@@ -14,7 +14,10 @@ function maskToken(token: string): string {
return `${token.slice(0, 4)}••••••${token.slice(-4)}`;
}
-async function getGiteaIntegration(projectId: string) {
+async function getGiteaIntegration(
+ projectId: string,
+ includeWebhookSecret = false,
+) {
const integration = await db.query.integrationTable.findFirst({
where: and(
eq(integrationTable.projectId, projectId),
@@ -40,7 +43,7 @@ async function getGiteaIntegration(projectId: string) {
repositoryName: config.repositoryName,
maskedAccessToken: maskToken(config.accessToken),
webhookUrl: `${apiBase.replace(/\/$/, "")}/gitea-integration/webhook/${integration.id}`,
- webhookSecret: config.webhookSecret ?? "",
+ webhookSecret: includeWebhookSecret ? (config.webhookSecret ?? "") : "",
branchPattern: config.branchPattern || defaultGiteaConfig.branchPattern,
commentTaskLinkOnGiteaIssue: config.commentTaskLinkOnGiteaIssue !== false,
isActive: integration.isActive,
diff --git a/apps/api/src/gitea-integration/index.ts b/apps/api/src/gitea-integration/index.ts
index 1f3b6e518..0a4e56bdb 100644
--- a/apps/api/src/gitea-integration/index.ts
+++ b/apps/api/src/gitea-integration/index.ts
@@ -9,9 +9,15 @@ import { integrationTable, projectTable } from "../database/schema";
import { type GiteaConfig, validateGiteaConfig } from "../plugins/gitea/config";
import { handleGiteaWebhookRequest } from "../plugins/gitea/webhook-handler";
import { giteaIntegrationSchema } from "../schemas";
-import { requireWorkspacePermission } from "../utils/require-workspace-permission";
+import {
+ hasWorkspacePermission,
+ requireWorkspacePermission,
+} from "../utils/require-workspace-permission";
import { validateWorkspaceAccess } from "../utils/validate-workspace-access";
-import { workspaceAccess } from "../utils/workspace-access-middleware";
+import {
+ workspaceAccess,
+ workspaceAccessMiddleware,
+} from "../utils/workspace-access-middleware";
import createGiteaIntegration from "./controllers/create-gitea-integration";
import deleteGiteaIntegration from "./controllers/delete-gitea-integration";
import getGiteaIntegration from "./controllers/get-gitea-integration";
@@ -143,10 +149,18 @@ const giteaIntegration = new Hono<{
},
}),
validator("param", v.object({ projectId: v.string() })),
- workspaceAccess.fromProject("projectId"),
+ workspaceAccessMiddleware({
+ sources: [{ type: "lookup", resource: "project", idKey: "projectId" }],
+ }),
async (c) => {
const { projectId } = c.req.valid("param");
- const integration = await getGiteaIntegration(projectId);
+ const includeWebhookSecret = await hasWorkspacePermission(c, {
+ workspace: ["manage_settings"],
+ });
+ const integration = await getGiteaIntegration(
+ projectId,
+ includeWebhookSecret,
+ );
if (!integration) {
return c.json(null, 200);
}
@@ -192,7 +206,7 @@ const giteaIntegration = new Hono<{
repositoryOwner: body.repositoryOwner,
repositoryName: body.repositoryName,
});
- const integration = await getGiteaIntegration(projectId);
+ const integration = await getGiteaIntegration(projectId, true);
if (!integration) {
throw new HTTPException(500, { message: "Failed to load integration" });
}
@@ -279,7 +293,7 @@ const giteaIntegration = new Hono<{
),
);
- const updated = await getGiteaIntegration(projectId);
+ const updated = await getGiteaIntegration(projectId, true);
if (!updated) {
throw new HTTPException(500, { message: "Failed to load integration" });
}
diff --git a/apps/api/src/utils/require-workspace-permission.ts b/apps/api/src/utils/require-workspace-permission.ts
index 976f53ded..3bcfc8e1d 100644
--- a/apps/api/src/utils/require-workspace-permission.ts
+++ b/apps/api/src/utils/require-workspace-permission.ts
@@ -84,10 +84,56 @@ function satisfies(
return true;
}
+export async function hasWorkspacePermission(
+ c: Context,
+ permissions: PermissionMap,
+) {
+ const workspaceId = c.get("workspaceId");
+ if (!workspaceId) return false;
+
+ const apiKey = c.get("apiKey") as
+ | { permissions?: Record<string, string[]> | null }
+ | undefined;
+ if (apiKey?.permissions && !satisfies(apiKey.permissions, permissions)) {
+ return false;
+ }
+
+ if (await isInstanceAdmin(c)) {
+ return true;
+ }
+
+ const userId = c.get("userId");
+ if (!userId) return false;
+
+ const [member] = await db
+ .select({ role: schema.workspaceUserTable.role })
+ .from(schema.workspaceUserTable)
+ .where(
+ and(
+ eq(schema.workspaceUserTable.workspaceId, workspaceId),
+ eq(schema.workspaceUserTable.userId, userId),
+ ),
+ )
+ .limit(1);
+
+ if (!member?.role) return false;
+
+ // Prefer the DB row when present so admin-edited defaults
+ // (viewer/member/admin) take effect immediately. Falls back to the
+ // compiled-in static definitions only when no row exists — protects
+ // viewer/member/admin users from a 403 if their workspace somehow
+ // missed the seed (e.g., seed failed during workspace creation and
+ // the boot-time backfill hasn't run yet).
+ const statements =
+ (await customRoleStatements(workspaceId, member.role)) ??
+ builtInRoleStatements(member.role);
+
+ return Boolean(statements && satisfies(statements, permissions));
+}
+
export function requireWorkspacePermission(permissions: PermissionMap) {
return async (c: Context, next: Next) => {
- const workspaceId = c.get("workspaceId");
- if (!workspaceId) {
+ if (!c.get("workspaceId")) {
throw new HTTPException(500, {
message: "workspaceId not set in context",
});
@@ -100,41 +146,10 @@ export function requireWorkspacePermission(permissions: PermissionMap) {
throw new HTTPException(403, { message: "Insufficient API key scope" });
}
- if (await isInstanceAdmin(c)) {
- return next();
- }
-
- const userId = c.get("userId");
- if (!userId) {
- throw new HTTPException(401, { message: "Unauthorized" });
- }
-
- const [member] = await db
- .select({ role: schema.workspaceUserTable.role })
- .from(schema.workspaceUserTable)
- .where(
- and(
- eq(schema.workspaceUserTable.workspaceId, workspaceId),
- eq(schema.workspaceUserTable.userId, userId),
- ),
- )
- .limit(1);
-
- if (!member?.role) {
- throw new HTTPException(403, { message: "Insufficient permissions" });
- }
-
- // Prefer the DB row when present so admin-edited defaults
- // (viewer/member/admin) take effect immediately. Falls back to the
- // compiled-in static definitions only when no row exists — protects
- // viewer/member/admin users from a 403 if their workspace somehow
- // missed the seed (e.g., seed failed during workspace creation and
- // the boot-time backfill hasn't run yet).
- const statements =
- (await customRoleStatements(workspaceId, member.role)) ??
- builtInRoleStatements(member.role);
-
- if (!statements || !satisfies(statements, permissions)) {
+ if (!(await hasWorkspacePermission(c, permissions))) {
+ if (!c.get("userId")) {
+ throw new HTTPException(401, { message: "Unauthorized" });
+ }
throw new HTTPException(403, { message: "Insufficient permissions" });
}