⚡ 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): harden auth error handling and device query parsing
Tina71932c24cfb
diff --git a/apps/api/src/index.ts b/apps/api/src/index.tsindex ceb958e60..ba29a9624 100644--- a/apps/api/src/index.ts+++ b/apps/api/src/index.ts@@ -318,7 +318,7 @@ export function createApp() {// Better Auth serves GET /auth/device as JSON. Browsers that open the API URL// directly expect a page — redirect full document navigations to the web app.const authDeviceQuerySchema = v.object({- user_code: v.string(),+ user_code: v.optional(v.string()),ui: v.optional(v.picklist(["1"])),});@@ -334,7 +334,7 @@ export function createApp() {{name: "user_code",in: "query",- required: true,+ required: false,schema: {type: "string",},@@ -374,10 +374,11 @@ export function createApp() {const clientUrl = (process.env.KANEO_CLIENT_URL || "http://localhost:5173").replace(/\/$/, "");- return c.redirect(- `${clientUrl}/device?user_code=${encodeURIComponent(userCode)}`,- 302,- );+ const deviceUrl = new URL(`${clientUrl}/device`);+ if (userCode) {+ deviceUrl.searchParams.set("user_code", userCode);+ }+ return c.redirect(deviceUrl.toString(), 302);}return auth.handler(c.req.raw);},@@ -413,7 +414,7 @@ export function createApp() {throw error;}console.error("API authentication failed:", error);- throw new HTTPException(401, { message: "Unauthorized" });+ throw new HTTPException(500, { message: "Internal Server Error" });}return next();});diff --git a/apps/api/src/utils/authenticate-api-request.ts b/apps/api/src/utils/authenticate-api-request.tsindex 74ba860f2..aa5302cbe 100644--- a/apps/api/src/utils/authenticate-api-request.ts+++ b/apps/api/src/utils/authenticate-api-request.ts@@ -10,21 +10,36 @@ async function getSessionFromBearerOnlyHeaders(c: Context) {return auth.api.getSession({ headers });}-function parseBearerToken(authHeader: string | undefined) {+function parseBearerToken(authHeader: string | undefined): {+ token: string | null;+ malformed: boolean;+} {if (!authHeader) {- return null;+ return { token: null, malformed: false };}const match = authHeader.match(/^Bearer\s+(.+)$/i);if (!match) {- return null;+ return { token: null, malformed: true };}- return match[1].replace(/\s+/g, "").trim();+ const token = match[1]?.replace(/\s+/g, "").trim();+ if (!token) {+ return { token: null, malformed: true };+ }++ return {+ token,+ malformed: false,+ };}export async function authenticateApiRequest(c: Context): Promise<void> {- const token = parseBearerToken(c.req.header("Authorization"));+ const { token, malformed } = parseBearerToken(c.req.header("Authorization"));+ if (malformed) {+ throw new HTTPException(401, { message: "Unauthorized" });+ }+if (token) {const apiKeyResult = await verifyApiKey(token);if (apiKeyResult?.valid && apiKeyResult.key) {@@ -67,7 +82,11 @@ export async function resolveAssetBearerOrCookie(c: Context): Promise<{userId: string;apiKeyId?: string;}> {- const token = parseBearerToken(c.req.header("Authorization"));+ const { token, malformed } = parseBearerToken(c.req.header("Authorization"));+ if (malformed) {+ throw new HTTPException(401, { message: "Unauthorized" });+ }+if (token) {const apiKeyResult = await verifyApiKey(token);if (apiKeyResult?.valid && apiKeyResult.key) {