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: serve public-project assets to anonymous callers

thejesh23a483bd46341d
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index 5e0588762..7c0ca43d6 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -55,10 +55,8 @@ import task from "./task";
import taskRelation from "./task-relation";
import telegramIntegration from "./telegram-integration";
import timeEntry from "./time-entry";
-import {
- authenticateApiRequest,
- resolveAssetBearerOrCookie,
-} from "./utils/authenticate-api-request";
+import { authenticateApiRequest } from "./utils/authenticate-api-request";
+import { authorizeAssetAccess } from "./utils/authorize-asset-access";
import { getInvitationDetails } from "./utils/check-registration-allowed";
import { migrateApiKeyReferenceId } from "./utils/migrate-apikey-reference-id";
import { migrateNotificationPreferencesSchema } from "./utils/migrate-notification-preferences-schema";
@@ -312,13 +310,7 @@ export function createApp() {
throw new HTTPException(404, { message: "Asset not found" });
}
- const { userId, apiKeyId } = await resolveAssetBearerOrCookie(c);
-
- if (userId) {
- await validateWorkspaceAccess(userId, asset.workspaceId, apiKeyId);
- } else if (!asset.isPublic) {
- throw new HTTPException(401, { message: "Unauthorized" });
- }
+ await authorizeAssetAccess(c, asset);
try {
const object = await getPrivateObject(asset.objectKey);
diff --git a/apps/api/src/utils/authorize-asset-access.ts b/apps/api/src/utils/authorize-asset-access.ts
new file mode 100644
index 000000000..b45d69716
--- /dev/null
+++ b/apps/api/src/utils/authorize-asset-access.ts
@@ -0,0 +1,29 @@
+import type { Context } from "hono";
+import { resolveAssetBearerOrCookie } from "./authenticate-api-request";
+import { validateWorkspaceAccess } from "./validate-workspace-access";
+
+type AssetAccessTarget = {
+ workspaceId: string;
+ isPublic: boolean | null;
+};
+
+/**
+ * Authorizes a request for a stored asset.
+ *
+ * Assets that belong to a public project are readable by anyone, so the
+ * credential check must be skipped entirely for them —
+ * `resolveAssetBearerOrCookie` throws a 401 for anonymous callers rather than
+ * returning an empty user, so calling it first makes the public case
+ * unreachable.
+ */
+export async function authorizeAssetAccess(
+ c: Context,
+ asset: AssetAccessTarget,
+): Promise<void> {
+ if (asset.isPublic) {
+ return;
+ }
+
+ const { userId, apiKeyId } = await resolveAssetBearerOrCookie(c);
+ await validateWorkspaceAccess(userId, asset.workspaceId, apiKeyId);
+}
diff --git a/tests/api/utils/authorize-asset-access.test.ts b/tests/api/utils/authorize-asset-access.test.ts
new file mode 100644
index 000000000..b6e205234
--- /dev/null
+++ b/tests/api/utils/authorize-asset-access.test.ts
@@ -0,0 +1,111 @@
+import type { Context } from "hono";
+import { HTTPException } from "hono/http-exception";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const { state } = vi.hoisted(() => ({
+ state: {
+ resolveCalls: 0,
+ validateCalls: [] as { userId: string; workspaceId: string }[],
+ caller: "anonymous" as "anonymous" | "member" | "outsider",
+ },
+}));
+
+vi.mock("../../../apps/api/src/utils/authenticate-api-request", () => ({
+ // Mirrors the real helper: every unauthenticated path throws, so it never
+ // returns a falsy userId.
+ resolveAssetBearerOrCookie: async () => {
+ state.resolveCalls += 1;
+ if (state.caller === "anonymous") {
+ throw new HTTPException(401, { message: "Unauthorized" });
+ }
+ return { userId: `user-${state.caller}` };
+ },
+}));
+
+vi.mock("../../../apps/api/src/utils/validate-workspace-access", () => ({
+ validateWorkspaceAccess: async (userId: string, workspaceId: string) => {
+ state.validateCalls.push({ userId, workspaceId });
+ if (userId !== "user-member") {
+ throw new HTTPException(403, {
+ message: "You don't have access to this workspace",
+ });
+ }
+ },
+}));
+
+const { authorizeAssetAccess } = await import(
+ "../../../apps/api/src/utils/authorize-asset-access"
+);
+
+const context = {} as Context;
+
+async function statusOf(promise: Promise<void>) {
+ try {
+ await promise;
+ return 200;
+ } catch (error) {
+ return error instanceof HTTPException ? error.status : 500;
+ }
+}
+
+describe("authorizeAssetAccess", () => {
+ beforeEach(() => {
+ state.resolveCalls = 0;
+ state.validateCalls = [];
+ state.caller = "anonymous";
+ });
+
+ it("allows an anonymous caller to read an asset of a public project", async () => {
+ const status = await statusOf(
+ authorizeAssetAccess(context, {
+ workspaceId: "workspace-1",
+ isPublic: true,
+ }),
+ );
+
+ expect(status).toBe(200);
+ // The credential check must be skipped entirely — it throws for anonymous
+ // callers, which is what made the public branch unreachable.
+ expect(state.resolveCalls).toBe(0);
+ });
+
+ it("rejects an anonymous caller for a private asset", async () => {
+ const status = await statusOf(
+ authorizeAssetAccess(context, {
+ workspaceId: "workspace-1",
+ isPublic: false,
+ }),
+ );
+
+ expect(status).toBe(401);
+ });
+
+ it("rejects an authenticated non-member for a private asset", async () => {
+ state.caller = "outsider";
+
+ const status = await statusOf(
+ authorizeAssetAccess(context, {
+ workspaceId: "workspace-1",
+ isPublic: null,
+ }),
+ );
+
+ expect(status).toBe(403);
+ });
+
+ it("allows a workspace member to read a private asset", async () => {
+ state.caller = "member";
+
+ const status = await statusOf(
+ authorizeAssetAccess(context, {
+ workspaceId: "workspace-1",
+ isPublic: false,
+ }),
+ );
+
+ expect(status).toBe(200);
+ expect(state.validateCalls).toEqual([
+ { userId: "user-member", workspaceId: "workspace-1" },
+ ]);
+ });
+});