⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(api): read the raw body in task permission middleware
Andrej Acevski6bfe74dec514
diff --git a/apps/api/src/billing/index.ts b/apps/api/src/billing/index.tsindex 44b33a9a8..56ae1031b 100644--- a/apps/api/src/billing/index.ts+++ b/apps/api/src/billing/index.ts@@ -51,7 +51,14 @@ async function requireBillingManager(userId: string, workspaceId: string) {// Excluded from the app-wide auth middleware: authenticity comes from the// provider's webhook signature instead of a session.+// Billing is a Kaneo Cloud concern: on a self-hosted instance isBillingEnabled()+// is false and these routes report an always-active entitlement. They stay+// served, but `hide` keeps them out of the published OpenAPI document so the+// self-hosted API reference does not advertise a paid tier that does not exist.+const cloudOnly = { hide: true } as const;+const webhookRoute = createRoute({+ ...cloudOnly,method: "post",operationId: "handleBillingWebhook",path: "/webhook",@@ -68,6 +75,7 @@ const webhookRoute = createRoute({});const getWorkspaceBillingRoute = createRoute({+ ...cloudOnly,method: "get",operationId: "getWorkspaceBilling",path: "/{workspaceId}",@@ -86,6 +94,7 @@ const getWorkspaceBillingRoute = createRoute({});const createCheckoutRoute = createRoute({+ ...cloudOnly,method: "post",operationId: "createBillingCheckout",path: "/{workspaceId}/checkout",@@ -108,6 +117,7 @@ const createCheckoutRoute = createRoute({});const createPortalRoute = createRoute({+ ...cloudOnly,method: "post",operationId: "createBillingPortalSession",path: "/{workspaceId}/portal",diff --git a/apps/api/src/task/controllers/require-task-permission.ts b/apps/api/src/task/controllers/require-task-permission.tsindex aeb8a8d45..a3607a41b 100644--- a/apps/api/src/task/controllers/require-task-permission.ts+++ b/apps/api/src/task/controllers/require-task-permission.ts@@ -1,16 +1,11 @@import { eq } from "drizzle-orm";import type { Context, Next } from "hono";+import { HTTPException } from "hono/http-exception";import { requireEntitlement } from "../../billing/require-entitlement-middleware";import db from "../../database";import { taskTable } from "../../database/schema";import { requireWorkspacePermission } from "../../utils/require-workspace-permission";-type TaskEnv = {- Variables: {- userId: string;- };-};-type BulkTaskOperation =| "updateStatus"| "updatePriority"@@ -20,28 +15,42 @@ type BulkTaskOperation =| "removeLabel"| "updateDueDate";-type BulkTaskContext = Context<- TaskEnv,- string,- { out: { json: { operation: BulkTaskOperation } } }->;--type TaskAssigneeContext = Context<- TaskEnv,- string,- {- out: {- param: { id: string };- json: { userId?: string };- };+const BULK_OPERATIONS: readonly BulkTaskOperation[] = [+ "updateStatus",+ "updatePriority",+ "updateAssignee",+ "delete",+ "addLabel",+ "removeLabel",+ "updateDueDate",+];++// These run as route middleware, which @hono/zod-openapi registers before the+// request validators, so c.req.valid() is not populated yet. Read the raw body+// instead. The validator still rejects a malformed body immediately after.+async function readJsonBody(c: Context): Promise<Record<string, unknown>> {+ const raw = (await c.req.json().catch(() => ({}))) as unknown;+ if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {+ return {};}->;+ return raw as Record<string, unknown>;+}-export async function requireBulkTaskPermission(- c: BulkTaskContext,- next: Next,-) {- const { operation } = c.req.valid("json");+async function bulkOperation(c: Context): Promise<BulkTaskOperation> {+ const { operation } = await readJsonBody(c);+ if (+ typeof operation !== "string" ||+ !(BULK_OPERATIONS as readonly string[]).includes(operation)+ ) {+ throw new HTTPException(400, {+ message: `operation must be one of: ${BULK_OPERATIONS.join(", ")}`,+ });+ }+ return operation as BulkTaskOperation;+}++export async function requireBulkTaskPermission(c: Context, next: Next) {+ const operation = await bulkOperation(c);if (operation === "delete") {return requireWorkspacePermission({ task: ["delete"] })(c, next);@@ -58,11 +67,8 @@ export async function requireBulkTaskPermission(return requireWorkspacePermission({ task: ["update"] })(c, next);}-export async function requireBulkTaskEntitlement(- c: BulkTaskContext,- next: Next,-) {- const { operation } = c.req.valid("json");+export async function requireBulkTaskEntitlement(c: Context, next: Next) {+ const operation = await bulkOperation(c);if (operation === "delete" ||@@ -75,19 +81,18 @@ export async function requireBulkTaskEntitlement(return requireEntitlement(c, next);}-export async function requireTaskAssigneePermission(- c: TaskAssigneeContext,- next: Next,-) {- const { id } = c.req.valid("param");- const { userId } = c.req.valid("json");+export async function requireTaskAssigneePermission(c: Context, next: Next) {+ const id = c.req.param("id");+ const { userId } = await readJsonBody(c);+ const nextAssignee = typeof userId === "string" ? userId : null;+const [existingTask] = await db.select({ userId: taskTable.userId }).from(taskTable)- .where(eq(taskTable.id, id))+ .where(eq(taskTable.id, id ?? "")).limit(1);- if (existingTask && existingTask.userId !== (userId || null)) {+ if (existingTask && existingTask.userId !== nextAssignee) {return requireWorkspacePermission({ task: ["assign"] })(c, next);}diff --git a/apps/docs/openapi.json b/apps/docs/openapi.jsonindex b4902fd4d..bfecd5329 100644--- a/apps/docs/openapi.json+++ b/apps/docs/openapi.json@@ -215,147 +215,6 @@"idToken"]},- "BillingWebhookResult": {- "type": "object",- "properties": {- "processed": {- "type": "boolean"- },- "duplicate": {- "type": "boolean",- "description": "True when this event id was already applied, so the delivery was a safe no-op."- }- },- "required": [- "processed",- "duplicate"- ]- },- "WorkspaceBilling": {- "type": "object",- "properties": {- "billingEnabled": {- "type": "boolean",- "description": "False when the instance has no billing configured."- },- "entitlement": {- "$ref": "#/components/schemas/BillingEntitlement"- },- "foundingFree": {- "type": "boolean"- },- "trialEndsAt": {- "type": [- "string",- "null"- ],- "format": "date-time"- },- "plan": {- "type": [- "string",- "null"- ],- "description": "`personal` or `team`."- },- "billingInterval": {- "type": [- "string",- "null"- ],- "description": "`monthly` or `annual`."- },- "status": {- "type": [- "string",- "null"- ],- "description": "The provider's subscription status."- },- "seats": {- "type": "number"- },- "currentPeriodEnd": {- "type": [- "string",- "null"- ],- "format": "date-time"- },- "canceledAt": {- "type": [- "string",- "null"- ],- "format": "date-time"- },- "hasCustomer": {- "type": "boolean",- "description": "Whether a billing customer exists yet. The portal link requires one."- }- },- "required": [- "billingEnabled",- "entitlement",- "foundingFree",- "trialEndsAt",- "plan",- "billingInterval",- "status",- "seats",- "currentPeriodEnd",- "canceledAt",- "hasCustomer"- ]- },- "BillingEntitlement": {- "type": "object",- "properties": {- "active": {- "type": "boolean",- "description": "Whether the workspace currently has paid-tier access."- },- "reason": {- "type": "string",- "enum": [- "billing_disabled",- "founding_free",- "subscription",- "trial",- "expired"- ],- "description": "Why access is granted or denied. `billing_disabled` means the instance is not running billing at all, so everything is unlocked."- }- },- "required": [- "active",- "reason"- ]- },- "BillingCheckout": {- "type": "object",- "properties": {- "checkoutUrl": {- "type": "string",- "description": "Redirect the browser here to complete payment."- }- },- "required": [- "checkoutUrl"- ]- },- "BillingPortal": {- "type": "object",- "properties": {- "portalUrl": {- "type": "string",- "description": "A short-lived link to the provider's customer portal."- }- },- "required": [- "portalUrl"- ]- },"ProjectListItem": {"allOf": [{@@ -5550,234 +5409,6 @@}}},- "/billing/webhook": {- "post": {- "operationId": "handleBillingWebhook",- "tags": [- "Billing"- ],- "summary": "Billing webhook",- "description": "Receive a Creem subscription event. Authenticated by signature, not by session, and idempotent per event id.",- "security": [],- "responses": {- "200": {- "description": "The event was accepted",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/BillingWebhookResult"- }- }- }- },- "400": {- "description": "Signature verification failed",- "content": {- "text/plain": {- "schema": {- "type": "string"- }- }- }- },- "404": {- "description": "Billing is not enabled on this instance",- "content": {- "text/plain": {- "schema": {- "type": "string"- }- }- }- }- }- }- },- "/billing/{workspaceId}": {- "get": {- "operationId": "getWorkspaceBilling",- "tags": [- "Billing"- ],- "summary": "Get workspace billing",- "description": "Get the billing state and entitlement for a workspace. When the instance has no billing configured this reports billingEnabled: false and an always-active entitlement.",- "parameters": [- {- "schema": {- "type": "string"- },- "required": true,- "name": "workspaceId",- "in": "path"- }- ],- "responses": {- "200": {- "description": "Billing state for the workspace",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/WorkspaceBilling"- }- }- }- },- "401": {- "description": "Missing or invalid credentials"- },- "403": {- "description": "No access to the workspace",- "content": {- "text/plain": {- "schema": {- "type": "string"- }- }- }- }- }- }- },- "/billing/{workspaceId}/checkout": {- "post": {- "operationId": "createBillingCheckout",- "tags": [- "Billing"- ],- "summary": "Create checkout",- "description": "Create a Creem checkout session for a workspace plan and return the URL to redirect the browser to. Workspace owners and admins only.",- "parameters": [- {- "schema": {- "type": "string"- },- "required": true,- "name": "workspaceId",- "in": "path"- }- ],- "requestBody": {- "required": true,- "content": {- "application/json": {- "schema": {- "type": "object",- "properties": {- "plan": {- "type": "string",- "enum": [- "personal",- "team"- ]- },- "interval": {- "type": "string",- "enum": [- "monthly",- "annual"- ]- }- },- "required": [- "plan",- "interval"- ]- }- }- }- },- "responses": {- "200": {- "description": "The checkout session",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/BillingCheckout"- }- }- }- },- "400": {- "description": "Invalid plan or interval",- "content": {- "text/plain": {- "schema": {- "type": "string"- }- }- }- },- "401": {- "description": "Missing or invalid credentials"- },- "403": {- "description": "Not a workspace owner or admin",- "content": {- "text/plain": {- "schema": {- "type": "string"- }- }- }- }- }- }- },- "/billing/{workspaceId}/portal": {- "post": {- "operationId": "createBillingPortalSession",- "tags": [- "Billing"- ],- "summary": "Create portal session",- "description": "Generate a Creem customer portal link for the workspace subscription. Workspace owners and admins only, and only once a billing customer exists.",- "parameters": [- {- "schema": {- "type": "string"- },- "required": true,- "name": "workspaceId",- "in": "path"- }- ],- "responses": {- "200": {- "description": "The portal link",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/BillingPortal"- }- }- }- },- "400": {- "description": "No billing customer exists for this workspace yet",- "content": {- "text/plain": {- "schema": {- "type": "string"- }- }- }- },- "401": {- "description": "Missing or invalid credentials"- },- "403": {- "description": "Not a workspace owner or admin",- "content": {- "text/plain": {- "schema": {- "type": "string"- }- }- }- }- }- }- },"/project": {"get": {"operationId": "listProjects",