⚡ 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): return 400 for invalid gitlab url or project path
zaralX12d225a0511a
diff --git a/apps/api/src/gitlab-integration/controllers/create-gitlab-integration.ts b/apps/api/src/gitlab-integration/controllers/create-gitlab-integration.tsindex c0cd4aa3b..22ec8f7cb 100644--- a/apps/api/src/gitlab-integration/controllers/create-gitlab-integration.ts+++ b/apps/api/src/gitlab-integration/controllers/create-gitlab-integration.ts@@ -17,6 +17,10 @@ import {GitlabApiError,verifyGitlabToken,} from "../../plugins/gitlab/utils/gitlab-api";+import {+ parseGitlabBaseUrl,+ parseGitlabProjectPath,+} from "../utils/normalize-input";function pickSettings(config: Partial<GitlabConfig>): Partial<GitlabConfig> {const settings: Partial<GitlabConfig> = {};@@ -56,8 +60,8 @@ async function createGitlabIntegration({throw new HTTPException(404, { message: "Project not found" });}- const normalizedBase = normalizeGitlabBaseUrl(baseUrl);- const normalizedPath = normalizeProjectPath(projectPath);+ const normalizedBase = parseGitlabBaseUrl(baseUrl);+ const normalizedPath = parseGitlabProjectPath(projectPath);const existingIntegration = await db.query.integrationTable.findFirst({where: and(diff --git a/apps/api/src/gitlab-integration/controllers/list-gitlab-projects.ts b/apps/api/src/gitlab-integration/controllers/list-gitlab-projects.tsindex 8172bee6e..66f35964f 100644--- a/apps/api/src/gitlab-integration/controllers/list-gitlab-projects.ts+++ b/apps/api/src/gitlab-integration/controllers/list-gitlab-projects.ts@@ -1,10 +1,10 @@import { HTTPException } from "hono/http-exception";import type { GitlabTokenType } from "../../plugins/gitlab/config";-import { normalizeGitlabBaseUrl } from "../../plugins/gitlab/config";import {createGitlabClient,verifyGitlabToken,} from "../../plugins/gitlab/utils/gitlab-api";+import { parseGitlabBaseUrl } from "../utils/normalize-input";type ProjectRow = {id: number;@@ -27,7 +27,7 @@ async function listGitlabProjects({accessToken: string;tokenType: GitlabTokenType;}): Promise<{ projects: ProjectRow[] }> {- const normalized = normalizeGitlabBaseUrl(baseUrl);+ const normalized = parseGitlabBaseUrl(baseUrl);try {await verifyGitlabToken(normalized, accessToken, tokenType);diff --git a/apps/api/src/gitlab-integration/controllers/verify-gitlab-access.ts b/apps/api/src/gitlab-integration/controllers/verify-gitlab-access.tsindex f162f52da..c5eb919ec 100644--- a/apps/api/src/gitlab-integration/controllers/verify-gitlab-access.ts+++ b/apps/api/src/gitlab-integration/controllers/verify-gitlab-access.ts@@ -1,12 +1,15 @@import { HTTPException } from "hono/http-exception";import type { GitlabTokenType } from "../../plugins/gitlab/config";-import { normalizeGitlabBaseUrl } from "../../plugins/gitlab/config";import {createGitlabClient,GitlabApiError,type GitlabProject,verifyGitlabToken,} from "../../plugins/gitlab/utils/gitlab-api";+import {+ parseGitlabBaseUrl,+ parseGitlabProjectPath,+} from "../utils/normalize-input";// Developer is the lowest role that can create the priority/status labels.const REQUIRED_ACCESS_LEVEL = 30;@@ -45,9 +48,10 @@ async function verifyGitlabAccess({tokenType: GitlabTokenType;projectPath: string;}) {- try {- const normalized = normalizeGitlabBaseUrl(baseUrl);+ const normalized = parseGitlabBaseUrl(baseUrl);+ const normalizedPath = parseGitlabProjectPath(projectPath);+ try {try {await verifyGitlabToken(normalized, accessToken, tokenType);} catch (error) {@@ -67,7 +71,7 @@ async function verifyGitlabAccess({tokenType,});- const project = await client.getProject(projectPath);+ const project = await client.getProject(normalizedPath);// Group and instance tokens have no project access level.const accessLevel = highestAccessLevel(project);diff --git a/apps/api/src/gitlab-integration/schema.ts b/apps/api/src/gitlab-integration/schema.tsindex abdbd4007..9631bab30 100644--- a/apps/api/src/gitlab-integration/schema.ts+++ b/apps/api/src/gitlab-integration/schema.ts@@ -23,7 +23,7 @@ export const verifyGitlabBody = z.object({});export const createGitlabBody = z.object({- baseUrl: z.string().min(1),+ baseUrl: z.url(),accessToken: z.string().optional().openapi({description: "Omit to keep the token already stored for this project.",}),diff --git a/apps/api/src/gitlab-integration/utils/normalize-input.ts b/apps/api/src/gitlab-integration/utils/normalize-input.tsnew file mode 100644index 000000000..dfc11a807--- /dev/null+++ b/apps/api/src/gitlab-integration/utils/normalize-input.ts@@ -0,0 +1,23 @@+import { HTTPException } from "hono/http-exception";+import {+ normalizeGitlabBaseUrl,+ normalizeProjectPath,+} from "../../plugins/gitlab/config";++function orBadRequest<T>(normalize: () => T): T {+ try {+ return normalize();+ } catch (error) {+ throw new HTTPException(400, {+ message: error instanceof Error ? error.message : "Invalid GitLab input",+ });+ }+}++export function parseGitlabBaseUrl(url: string): string {+ return orBadRequest(() => normalizeGitlabBaseUrl(url));+}++export function parseGitlabProjectPath(path: string): string {+ return orBadRequest(() => normalizeProjectPath(path));+}diff --git a/apps/docs/openapi.json b/apps/docs/openapi.jsonindex f7b90fb4c..99be3446b 100644--- a/apps/docs/openapi.json+++ b/apps/docs/openapi.json@@ -11740,7 +11740,7 @@"properties": {"baseUrl": {"type": "string",- "minLength": 1+ "format": "uri"},"accessToken": {"type": "string",diff --git a/tests/api/gitlab-integration/create-gitlab-integration.test.ts b/tests/api/gitlab-integration/create-gitlab-integration.test.tsindex 60051d0f5..fb173f794 100644--- a/tests/api/gitlab-integration/create-gitlab-integration.test.ts+++ b/tests/api/gitlab-integration/create-gitlab-integration.test.ts@@ -118,3 +118,20 @@ describe("createGitlabIntegration on an existing integration", () => {});});});++describe("createGitlabIntegration input", () => {+ it("rejects a malformed project path with a 400", async () => {+ mocks.existingIntegration.mockResolvedValue(undefined);++ await expect(+ createGitlabIntegration({+ projectId: "project-1",+ baseUrl: "https://gitlab.com",+ accessToken: "token",+ tokenType: "private",+ projectPath: "acme/../web",+ }),+ ).rejects.toMatchObject({ status: 400 });+ expect(mocks.updateSet).not.toHaveBeenCalled();+ });+});diff --git a/tests/api/gitlab-integration/verify-gitlab-access.test.ts b/tests/api/gitlab-integration/verify-gitlab-access.test.tsnew file mode 100644index 000000000..81be77901--- /dev/null+++ b/tests/api/gitlab-integration/verify-gitlab-access.test.ts@@ -0,0 +1,77 @@+import { HTTPException } from "hono/http-exception";+import { beforeEach, describe, expect, it, vi } from "vitest";++const { mockGitlabFetch } = vi.hoisted(() => ({+ mockGitlabFetch: vi.fn(),+}));++vi.mock("../../../apps/api/src/plugins/gitlab/utils/gitlab-api", () => ({+ GitlabApiError: class GitlabApiError extends Error {},+ verifyGitlabToken: (...args: unknown[]) => mockGitlabFetch(...args),+ createGitlabClient: () => ({+ getProject: (...args: unknown[]) => mockGitlabFetch(...args),+ }),+}));++const { default: verifyGitlabAccess } = await import(+ "../../../apps/api/src/gitlab-integration/controllers/verify-gitlab-access"+);++async function statusOf(promise: Promise<unknown>) {+ try {+ await promise;+ return null;+ } catch (error) {+ return error instanceof HTTPException ? error.status : "not-http";+ }+}++beforeEach(() => {+ vi.clearAllMocks();+});++describe("verifyGitlabAccess input", () => {+ it("rejects a base URL with a query string as a bad request", async () => {+ const status = await statusOf(+ verifyGitlabAccess({+ baseUrl: "https://gitlab.example.com/?next=/admin",+ accessToken: "token",+ tokenType: "private",+ projectPath: "acme/web",+ }),+ );++ expect(status).toBe(400);+ expect(mockGitlabFetch).not.toHaveBeenCalled();+ });++ it("rejects a project path without a namespace as a bad request", async () => {+ const status = await statusOf(+ verifyGitlabAccess({+ baseUrl: "https://gitlab.com",+ accessToken: "token",+ tokenType: "private",+ projectPath: "web",+ }),+ );++ expect(status).toBe(400);+ expect(mockGitlabFetch).not.toHaveBeenCalled();+ });++ it("asks GitLab for the normalized project path", async () => {+ mockGitlabFetch+ .mockResolvedValueOnce({ id: 1 })+ .mockResolvedValueOnce({ id: 2, visibility: "private" });++ const result = await verifyGitlabAccess({+ baseUrl: "https://gitlab.com/",+ accessToken: "token",+ tokenType: "private",+ projectPath: "/acme/web/",+ });++ expect(result.projectExists).toBe(true);+ expect(mockGitlabFetch).toHaveBeenLastCalledWith("acme/web");+ });+});