From 458ef9e75461d0009d84e58f926c424a9ef6283c Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 28 Aug 2024 13:16:22 +0200 Subject: [PATCH] Trim on output --- .../server/src/middleware/trimViewRowInfo.ts | 33 ++++++++++++------- packages/server/src/sdk/app/views/index.ts | 10 ++++-- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/packages/server/src/middleware/trimViewRowInfo.ts b/packages/server/src/middleware/trimViewRowInfo.ts index 55efaee732..2d29a88a6f 100644 --- a/packages/server/src/middleware/trimViewRowInfo.ts +++ b/packages/server/src/middleware/trimViewRowInfo.ts @@ -1,10 +1,10 @@ -import { Ctx, Row } from "@budibase/types" +import { Ctx, Row, ViewV2 } from "@budibase/types" import sdk from "../sdk" import { Next } from "koa" import { getSourceId } from "../api/controllers/row/utils" -export default async (ctx: Ctx, next: Next) => { +export default async (ctx: Ctx, next: Next) => { const { body } = ctx.request const viewId = getSourceId(ctx).viewId ?? body._viewId @@ -14,22 +14,31 @@ export default async (ctx: Ctx, next: Next) => { } // don't need to trim delete requests - if (ctx?.method?.toLowerCase() !== "delete") { - await trimViewFields(ctx.request.body, viewId) + const trimFields = ctx?.method?.toLowerCase() !== "delete" + if (!trimFields) { + return next() } - return next() + const view = await sdk.views.get(viewId) + ctx.request.body = await trimNonViewFields(ctx.request.body, view, "WRITE") + + await next() + + ctx.body = await trimNonViewFields(ctx.body, view, "READ") } // have to mutate the koa context, can't return -export async function trimViewFields(body: Row, viewId: string): Promise { - const view = await sdk.views.get(viewId) - const allowedKeys = sdk.views.allowedFields(view) +export async function trimNonViewFields( + row: Row, + view: ViewV2, + permission: "WRITE" | "READ" +): Promise { + row = { ...row } + const allowedKeys = sdk.views.allowedFields(view, permission) // have to mutate the context, can't update reference - const toBeRemoved = Object.keys(body).filter( - key => !allowedKeys.includes(key) - ) + const toBeRemoved = Object.keys(row).filter(key => !allowedKeys.includes(key)) for (let removeKey of toBeRemoved) { - delete body[removeKey] + delete row[removeKey] } + return row } diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 1c09f710d7..0fc692b108 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -139,14 +139,20 @@ export async function remove(viewId: string): Promise { return pickApi(tableId).remove(viewId) } -export function allowedFields(view: View | ViewV2) { +export function allowedFields( + view: View | ViewV2, + permission: "WRITE" | "READ" +) { return [ ...Object.keys(view?.schema || {}).filter(key => { if (!isV2(view)) { return true } const fieldSchema = view.schema![key] - return fieldSchema.visible && !fieldSchema.readonly + if (permission === "WRITE") { + return fieldSchema.visible && !fieldSchema.readonly + } + return fieldSchema.visible }), ...PROTECTED_EXTERNAL_COLUMNS, ...PROTECTED_INTERNAL_COLUMNS,