From 10c1c18f16ab9b05e0b315fed79fefd560377df0 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 5 Dec 2024 13:44:08 +0000 Subject: [PATCH] More typing fixes. --- packages/backend-core/package.json | 1 + .../backend-core/src/middleware/auditLog.ts | 2 +- .../src/middleware/authenticated.ts | 21 +++++++++++++------ .../backend-core/src/middleware/tenancy.ts | 2 +- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/backend-core/package.json b/packages/backend-core/package.json index a4381b4200..3248fd8ceb 100644 --- a/packages/backend-core/package.json +++ b/packages/backend-core/package.json @@ -83,6 +83,7 @@ "@types/semver": "7.3.7", "@types/tar-fs": "2.0.1", "@types/uuid": "8.3.4", + "@types/koa": "2.13.4", "chance": "1.1.8", "ioredis-mock": "8.9.0", "jest": "29.7.0", diff --git a/packages/backend-core/src/middleware/auditLog.ts b/packages/backend-core/src/middleware/auditLog.ts index f4cbb8b45f..d529e8f908 100644 --- a/packages/backend-core/src/middleware/auditLog.ts +++ b/packages/backend-core/src/middleware/auditLog.ts @@ -1,5 +1,5 @@ import { Ctx } from "@budibase/types" -import { Middleware, Next } from "koa" +import type { Middleware, Next } from "koa" // this middleware exists purely to be overridden by middlewares supplied by the @budibase/pro library const middleware = (async (ctx: Ctx, next: Next) => { diff --git a/packages/backend-core/src/middleware/authenticated.ts b/packages/backend-core/src/middleware/authenticated.ts index b713f509e0..6713cc7687 100644 --- a/packages/backend-core/src/middleware/authenticated.ts +++ b/packages/backend-core/src/middleware/authenticated.ts @@ -22,6 +22,7 @@ import { } from "@budibase/types" import { ErrorCode, InvalidAPIKeyError } from "../errors" import tracer from "dd-trace" +import type { Middleware, Next } from "koa" const ONE_MINUTE = env.SESSION_UPDATE_PERIOD ? parseInt(env.SESSION_UPDATE_PERIOD) @@ -94,6 +95,14 @@ async function checkApiKey( }) } +function getHeader(ctx: Ctx, header: Header): string | undefined { + const contents = ctx.request.headers[header] + if (Array.isArray(contents)) { + throw new Error("Unexpected header format") + } + return contents +} + /** * This middleware is tenancy aware, so that it does not depend on other middlewares being used. * The tenancy modules should not be used here and it should be assumed that the tenancy context @@ -106,9 +115,9 @@ export default function ( } ) { const noAuthOptions = noAuthPatterns ? buildMatcherRegex(noAuthPatterns) : [] - return async (ctx: Ctx | any, next: any) => { + return (async (ctx: Ctx, next: Next) => { let publicEndpoint = false - const version = ctx.request.headers[Header.API_VER] + const version = getHeader(ctx, Header.API_VER) // the path is not authenticated const found = matches(ctx, noAuthOptions) if (found) { @@ -116,18 +125,18 @@ export default function ( } try { // check the actual user is authenticated first, try header or cookie - let headerToken = ctx.request.headers[Header.TOKEN] + let headerToken = getHeader(ctx, Header.TOKEN) const authCookie = getCookie(ctx, Cookie.Auth) || openJwt(headerToken) - let apiKey = ctx.request.headers[Header.API_KEY] + let apiKey = getHeader(ctx, Header.API_KEY) if (!apiKey && ctx.request.headers[Header.AUTHORIZATION]) { apiKey = ctx.request.headers[Header.AUTHORIZATION].split(" ")[1] } - const tenantId = ctx.request.headers[Header.TENANT_ID] + const tenantId = getHeader(ctx, Header.TENANT_ID) let authenticated: boolean = false, user: User | { tenantId: string } | undefined = undefined, internal: boolean = false, @@ -243,5 +252,5 @@ export default function ( ctx.throw(err.status || 403, err) } } - } + }) as Middleware } diff --git a/packages/backend-core/src/middleware/tenancy.ts b/packages/backend-core/src/middleware/tenancy.ts index a614ab5438..de756c0af2 100644 --- a/packages/backend-core/src/middleware/tenancy.ts +++ b/packages/backend-core/src/middleware/tenancy.ts @@ -8,7 +8,7 @@ import { GetTenantIdOptions, TenantResolutionStrategy, } from "@budibase/types" -import { Next, Middleware } from "koa" +import type { Next, Middleware } from "koa" export default function ( allowQueryStringPatterns: EndpointMatcher[],