Merge pull request #12510 from Budibase/fix/massive-attachment-export

Export/backup of large number of attachments
This commit is contained in:
Michael Drury 2023-12-06 16:50:06 +00:00 committed by GitHub
commit 958dc5a084
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 179 additions and 82 deletions

View File

@ -42,7 +42,7 @@ http {
server { server {
listen 10000 default_server; listen 10000 default_server;
server_name _; server_name _;
client_max_body_size 1000m; client_max_body_size 50000m;
ignore_invalid_headers off; ignore_invalid_headers off;
proxy_buffering off; proxy_buffering off;

View File

@ -19,6 +19,7 @@ import {
GoogleInnerConfig, GoogleInnerConfig,
OIDCInnerConfig, OIDCInnerConfig,
PlatformLogoutOpts, PlatformLogoutOpts,
SessionCookie,
SSOProviderType, SSOProviderType,
} from "@budibase/types" } from "@budibase/types"
import * as events from "../events" import * as events from "../events"
@ -44,7 +45,6 @@ export const buildAuthMiddleware = authenticated
export const buildTenancyMiddleware = tenancy export const buildTenancyMiddleware = tenancy
export const buildCsrfMiddleware = csrf export const buildCsrfMiddleware = csrf
export const passport = _passport export const passport = _passport
export const jwt = require("jsonwebtoken")
// Strategies // Strategies
_passport.use(new LocalStrategy(local.options, local.authenticate)) _passport.use(new LocalStrategy(local.options, local.authenticate))
@ -191,10 +191,10 @@ export async function platformLogout(opts: PlatformLogoutOpts) {
if (!ctx) throw new Error("Koa context must be supplied to logout.") if (!ctx) throw new Error("Koa context must be supplied to logout.")
const currentSession = getCookie(ctx, Cookie.Auth) const currentSession = getCookie<SessionCookie>(ctx, Cookie.Auth)
let sessions = await getSessionsForUser(userId) let sessions = await getSessionsForUser(userId)
if (keepActiveSession) { if (currentSession && keepActiveSession) {
sessions = sessions.filter( sessions = sessions.filter(
session => session.sessionId !== currentSession.sessionId session => session.sessionId !== currentSession.sessionId
) )

View File

@ -13,7 +13,7 @@ import { getGlobalDB, doInTenant } from "../context"
import { decrypt } from "../security/encryption" import { decrypt } from "../security/encryption"
import * as identity from "../context/identity" import * as identity from "../context/identity"
import env from "../environment" import env from "../environment"
import { Ctx, EndpointMatcher } from "@budibase/types" import { Ctx, EndpointMatcher, SessionCookie } from "@budibase/types"
import { InvalidAPIKeyError, ErrorCode } from "../errors" import { InvalidAPIKeyError, ErrorCode } from "../errors"
const ONE_MINUTE = env.SESSION_UPDATE_PERIOD const ONE_MINUTE = env.SESSION_UPDATE_PERIOD
@ -98,7 +98,9 @@ export default function (
// check the actual user is authenticated first, try header or cookie // check the actual user is authenticated first, try header or cookie
let headerToken = ctx.request.headers[Header.TOKEN] let headerToken = ctx.request.headers[Header.TOKEN]
const authCookie = getCookie(ctx, Cookie.Auth) || openJwt(headerToken) const authCookie =
getCookie<SessionCookie>(ctx, Cookie.Auth) ||
openJwt<SessionCookie>(headerToken)
let apiKey = ctx.request.headers[Header.API_KEY] let apiKey = ctx.request.headers[Header.API_KEY]
if (!apiKey && ctx.request.headers[Header.AUTHORIZATION]) { if (!apiKey && ctx.request.headers[Header.AUTHORIZATION]) {

View File

@ -3,7 +3,7 @@ import { Cookie } from "../../../constants"
import * as configs from "../../../configs" import * as configs from "../../../configs"
import * as cache from "../../../cache" import * as cache from "../../../cache"
import * as utils from "../../../utils" import * as utils from "../../../utils"
import { UserCtx, SSOProfile } from "@budibase/types" import { UserCtx, SSOProfile, DatasourceAuthCookie } from "@budibase/types"
import { ssoSaveUserNoOp } from "../sso/sso" import { ssoSaveUserNoOp } from "../sso/sso"
const GoogleStrategy = require("passport-google-oauth").OAuth2Strategy const GoogleStrategy = require("passport-google-oauth").OAuth2Strategy
@ -58,7 +58,14 @@ export async function postAuth(
const platformUrl = await configs.getPlatformUrl({ tenantAware: false }) const platformUrl = await configs.getPlatformUrl({ tenantAware: false })
let callbackUrl = `${platformUrl}/api/global/auth/datasource/google/callback` let callbackUrl = `${platformUrl}/api/global/auth/datasource/google/callback`
const authStateCookie = utils.getCookie(ctx, Cookie.DatasourceAuth) const authStateCookie = utils.getCookie<{ appId: string }>(
ctx,
Cookie.DatasourceAuth
)
if (!authStateCookie) {
throw new Error("Unable to fetch datasource auth cookie")
}
return passport.authenticate( return passport.authenticate(
new GoogleStrategy( new GoogleStrategy(

View File

@ -305,20 +305,33 @@ export async function retrieveDirectory(bucketName: string, path: string) {
let writePath = join(budibaseTempDir(), v4()) let writePath = join(budibaseTempDir(), v4())
fs.mkdirSync(writePath) fs.mkdirSync(writePath)
const objects = await listAllObjects(bucketName, path) const objects = await listAllObjects(bucketName, path)
let fullObjects = await Promise.all( let streams = await Promise.all(
objects.map(obj => retrieve(bucketName, obj.Key!)) objects.map(obj => getReadStream(bucketName, obj.Key!))
) )
let count = 0 let count = 0
const writePromises: Promise<Error>[] = []
for (let obj of objects) { for (let obj of objects) {
const filename = obj.Key! const filename = obj.Key!
const data = fullObjects[count++] const stream = streams[count++]
const possiblePath = filename.split("/") const possiblePath = filename.split("/")
if (possiblePath.length > 1) { const dirs = possiblePath.slice(0, possiblePath.length - 1)
const dirs = possiblePath.slice(0, possiblePath.length - 1) const possibleDir = join(writePath, ...dirs)
fs.mkdirSync(join(writePath, ...dirs), { recursive: true }) if (possiblePath.length > 1 && !fs.existsSync(possibleDir)) {
fs.mkdirSync(possibleDir, { recursive: true })
} }
fs.writeFileSync(join(writePath, ...possiblePath), data) const writeStream = fs.createWriteStream(join(writePath, ...possiblePath), {
mode: 0o644,
})
stream.pipe(writeStream)
writePromises.push(
new Promise((resolve, reject) => {
stream.on("finish", resolve)
stream.on("error", reject)
writeStream.on("error", reject)
})
)
} }
await Promise.all(writePromises)
return writePath return writePath
} }

View File

@ -73,6 +73,9 @@ export async function encryptFile(
const outputFileName = `${filename}.enc` const outputFileName = `${filename}.enc`
const filePath = join(dir, filename) const filePath = join(dir, filename)
if (fs.lstatSync(filePath).isDirectory()) {
throw new Error("Unable to encrypt directory")
}
const inputFile = fs.createReadStream(filePath) const inputFile = fs.createReadStream(filePath)
const outputFile = fs.createWriteStream(join(dir, outputFileName)) const outputFile = fs.createWriteStream(join(dir, outputFileName))
@ -110,6 +113,9 @@ export async function decryptFile(
outputPath: string, outputPath: string,
secret: string secret: string
) { ) {
if (fs.lstatSync(inputPath).isDirectory()) {
throw new Error("Unable to encrypt directory")
}
const { salt, iv } = await getSaltAndIV(inputPath) const { salt, iv } = await getSaltAndIV(inputPath)
const inputFile = fs.createReadStream(inputPath, { const inputFile = fs.createReadStream(inputPath, {
start: SALT_LENGTH + IV_LENGTH, start: SALT_LENGTH + IV_LENGTH,

View File

@ -11,8 +11,7 @@ import {
TenantResolutionStrategy, TenantResolutionStrategy,
} from "@budibase/types" } from "@budibase/types"
import type { SetOption } from "cookies" import type { SetOption } from "cookies"
import jwt, { Secret } from "jsonwebtoken"
const jwt = require("jsonwebtoken")
const APP_PREFIX = DocumentType.APP + SEPARATOR const APP_PREFIX = DocumentType.APP + SEPARATOR
const PROD_APP_PREFIX = "/app/" const PROD_APP_PREFIX = "/app/"
@ -60,10 +59,7 @@ export function isServingApp(ctx: Ctx) {
return true return true
} }
// prod app // prod app
if (ctx.path.startsWith(PROD_APP_PREFIX)) { return ctx.path.startsWith(PROD_APP_PREFIX)
return true
}
return false
} }
export function isServingBuilder(ctx: Ctx): boolean { export function isServingBuilder(ctx: Ctx): boolean {
@ -138,16 +134,16 @@ function parseAppIdFromUrl(url?: string) {
* opens the contents of the specified encrypted JWT. * opens the contents of the specified encrypted JWT.
* @return the contents of the token. * @return the contents of the token.
*/ */
export function openJwt(token: string) { export function openJwt<T>(token?: string): T | undefined {
if (!token) { if (!token) {
return token return undefined
} }
try { try {
return jwt.verify(token, env.JWT_SECRET) return jwt.verify(token, env.JWT_SECRET as Secret) as T
} catch (e) { } catch (e) {
if (env.JWT_SECRET_FALLBACK) { if (env.JWT_SECRET_FALLBACK) {
// fallback to enable rotation // fallback to enable rotation
return jwt.verify(token, env.JWT_SECRET_FALLBACK) return jwt.verify(token, env.JWT_SECRET_FALLBACK) as T
} else { } else {
throw e throw e
} }
@ -159,13 +155,9 @@ export function isValidInternalAPIKey(apiKey: string) {
return true return true
} }
// fallback to enable rotation // fallback to enable rotation
if ( return !!(
env.INTERNAL_API_KEY_FALLBACK && env.INTERNAL_API_KEY_FALLBACK && env.INTERNAL_API_KEY_FALLBACK === apiKey
env.INTERNAL_API_KEY_FALLBACK === apiKey )
) {
return true
}
return false
} }
/** /**
@ -173,14 +165,14 @@ export function isValidInternalAPIKey(apiKey: string) {
* @param ctx The request which is to be manipulated. * @param ctx The request which is to be manipulated.
* @param name The name of the cookie to get. * @param name The name of the cookie to get.
*/ */
export function getCookie(ctx: Ctx, name: string) { export function getCookie<T>(ctx: Ctx, name: string) {
const cookie = ctx.cookies.get(name) const cookie = ctx.cookies.get(name)
if (!cookie) { if (!cookie) {
return cookie return undefined
} }
return openJwt(cookie) return openJwt<T>(cookie)
} }
/** /**
@ -197,7 +189,7 @@ export function setCookie(
opts = { sign: true } opts = { sign: true }
) { ) {
if (value && opts && opts.sign) { if (value && opts && opts.sign) {
value = jwt.sign(value, env.JWT_SECRET) value = jwt.sign(value, env.JWT_SECRET as Secret)
} }
const config: SetOption = { const config: SetOption = {

View File

@ -53,7 +53,7 @@
$: { $: {
if (selectedImage?.url) { if (selectedImage?.url) {
selectedUrl = selectedImage?.url selectedUrl = selectedImage?.url
} else if (selectedImage) { } else if (selectedImage && isImage) {
try { try {
let reader = new FileReader() let reader = new FileReader()
reader.readAsDataURL(selectedImage) reader.readAsDataURL(selectedImage)

View File

@ -13,7 +13,7 @@
export let app export let app
export let published export let published
let includeInternalTablesRows = true let includeInternalTablesRows = true
let encypt = true let encrypt = true
let password = null let password = null
const validation = createValidationStore() const validation = createValidationStore()
@ -27,9 +27,9 @@
$: stepConfig = { $: stepConfig = {
[Step.CONFIG]: { [Step.CONFIG]: {
title: published ? "Export published app" : "Export latest app", title: published ? "Export published app" : "Export latest app",
confirmText: encypt ? "Continue" : exportButtonText, confirmText: encrypt ? "Continue" : exportButtonText,
onConfirm: () => { onConfirm: () => {
if (!encypt) { if (!encrypt) {
exportApp() exportApp()
} else { } else {
currentStep = Step.SET_PASSWORD currentStep = Step.SET_PASSWORD
@ -46,7 +46,7 @@
if (!$validation.valid) { if (!$validation.valid) {
return keepOpen return keepOpen
} }
exportApp(password) await exportApp(password)
}, },
isValid: $validation.valid, isValid: $validation.valid,
}, },
@ -109,13 +109,13 @@
text="Export rows from internal tables" text="Export rows from internal tables"
bind:value={includeInternalTablesRows} bind:value={includeInternalTablesRows}
/> />
<Toggle text="Encrypt my export" bind:value={encypt} /> <Toggle text="Encrypt my export" bind:value={encrypt} />
</Body> </Body>
{#if !encypt} <InlineAlert
<InlineAlert header={encrypt
header="Do not share your budibase application exports publicly as they may contain sensitive information such as database credentials or secret keys." ? "Please note Budibase does not encrypt attachments during the export process to ensure efficient export of large attachments."
/> : "Do not share your Budibase application exports publicly as they may contain sensitive information such as database credentials or secret keys."}
{/if} />
{/if} {/if}
{#if currentStep === Step.SET_PASSWORD} {#if currentStep === Step.SET_PASSWORD}
<Input <Input

View File

@ -9,7 +9,7 @@ import { quotas } from "@budibase/pro"
import { events, context, utils, constants } from "@budibase/backend-core" import { events, context, utils, constants } from "@budibase/backend-core"
import sdk from "../../../sdk" import sdk from "../../../sdk"
import { QueryEvent } from "../../../threads/definitions" import { QueryEvent } from "../../../threads/definitions"
import { ConfigType, Query, UserCtx } from "@budibase/types" import { ConfigType, Query, UserCtx, SessionCookie } from "@budibase/types"
import { ValidQueryNameRegex } from "@budibase/shared-core" import { ValidQueryNameRegex } from "@budibase/shared-core"
const Runner = new Thread(ThreadType.QUERY, { const Runner = new Thread(ThreadType.QUERY, {
@ -113,7 +113,7 @@ function getOAuthConfigCookieId(ctx: UserCtx) {
} }
function getAuthConfig(ctx: UserCtx) { function getAuthConfig(ctx: UserCtx) {
const authCookie = utils.getCookie(ctx, constants.Cookie.Auth) const authCookie = utils.getCookie<SessionCookie>(ctx, constants.Cookie.Auth)
let authConfigCtx: any = {} let authConfigCtx: any = {}
authConfigCtx["configId"] = getOAuthConfigCookieId(ctx) authConfigCtx["configId"] = getOAuthConfigCookieId(ctx)
authConfigCtx["sessionId"] = authCookie ? authCookie.sessionId : null authConfigCtx["sessionId"] = authCookie ? authCookie.sessionId : null

View File

@ -59,6 +59,7 @@ const environment = {
BB_ADMIN_USER_PASSWORD: process.env.BB_ADMIN_USER_PASSWORD, BB_ADMIN_USER_PASSWORD: process.env.BB_ADMIN_USER_PASSWORD,
PLUGINS_DIR: process.env.PLUGINS_DIR || "/plugins", PLUGINS_DIR: process.env.PLUGINS_DIR || "/plugins",
OPENAI_API_KEY: process.env.OPENAI_API_KEY, OPENAI_API_KEY: process.env.OPENAI_API_KEY,
MAX_IMPORT_SIZE_MB: process.env.MAX_IMPORT_SIZE_MB,
// flags // flags
ALLOW_DEV_AUTOMATIONS: process.env.ALLOW_DEV_AUTOMATIONS, ALLOW_DEV_AUTOMATIONS: process.env.ALLOW_DEV_AUTOMATIONS,
DISABLE_THREADING: process.env.DISABLE_THREADING, DISABLE_THREADING: process.env.DISABLE_THREADING,

View File

@ -1,5 +1,5 @@
import env from "./environment" import env from "./environment"
import Koa, { ExtendableContext } from "koa" import Koa from "koa"
import koaBody from "koa-body" import koaBody from "koa-body"
import http from "http" import http from "http"
import * as api from "./api" import * as api from "./api"
@ -27,6 +27,9 @@ export default function createKoaApp() {
// @ts-ignore // @ts-ignore
enableTypes: ["json", "form", "text"], enableTypes: ["json", "form", "text"],
parsedMethods: ["POST", "PUT", "PATCH", "DELETE"], parsedMethods: ["POST", "PUT", "PATCH", "DELETE"],
formidable: {
maxFileSize: parseInt(env.MAX_IMPORT_SIZE_MB || "100") * 1024 * 1024,
},
}) })
) )

View File

@ -1,3 +1,4 @@
export const DB_EXPORT_FILE = "db.txt" export const DB_EXPORT_FILE = "db.txt"
export const GLOBAL_DB_EXPORT_FILE = "global.txt" export const GLOBAL_DB_EXPORT_FILE = "global.txt"
export const STATIC_APP_FILES = ["manifest.json", "budibase-client.js"] export const STATIC_APP_FILES = ["manifest.json", "budibase-client.js"]
export const ATTACHMENT_DIRECTORY = "attachments"

View File

@ -8,13 +8,15 @@ import {
TABLE_ROW_PREFIX, TABLE_ROW_PREFIX,
USER_METDATA_PREFIX, USER_METDATA_PREFIX,
} from "../../../db/utils" } from "../../../db/utils"
import { DB_EXPORT_FILE, STATIC_APP_FILES } from "./constants" import {
DB_EXPORT_FILE,
STATIC_APP_FILES,
ATTACHMENT_DIRECTORY,
} from "./constants"
import fs from "fs" import fs from "fs"
import { join } from "path" import { join } from "path"
import env from "../../../environment" import env from "../../../environment"
import { v4 as uuid } from "uuid"
const uuid = require("uuid/v4")
import tar from "tar" import tar from "tar"
const MemoryStream = require("memorystream") const MemoryStream = require("memorystream")
@ -30,12 +32,11 @@ export interface ExportOpts extends DBDumpOpts {
encryptPassword?: string encryptPassword?: string
} }
function tarFilesToTmp(tmpDir: string, files: string[]) { async function tarFilesToTmp(tmpDir: string, files: string[]) {
const fileName = `${uuid()}.tar.gz` const fileName = `${uuid()}.tar.gz`
const exportFile = join(budibaseTempDir(), fileName) const exportFile = join(budibaseTempDir(), fileName)
tar.create( await tar.create(
{ {
sync: true,
gzip: true, gzip: true,
file: exportFile, file: exportFile,
noDirRecurse: false, noDirRecurse: false,
@ -150,19 +151,21 @@ export async function exportApp(appId: string, config?: ExportOpts) {
for (let file of fs.readdirSync(tmpPath)) { for (let file of fs.readdirSync(tmpPath)) {
const path = join(tmpPath, file) const path = join(tmpPath, file)
await encryption.encryptFile( // skip the attachments - too big to encrypt
{ dir: tmpPath, filename: file }, if (file !== ATTACHMENT_DIRECTORY) {
config.encryptPassword await encryption.encryptFile(
) { dir: tmpPath, filename: file },
config.encryptPassword
fs.rmSync(path) )
fs.rmSync(path)
}
} }
} }
// if tar requested, return where the tarball is // if tar requested, return where the tarball is
if (config?.tar) { if (config?.tar) {
// now the tmpPath contains both the DB export and attachments, tar this // now the tmpPath contains both the DB export and attachments, tar this
const tarPath = tarFilesToTmp(tmpPath, fs.readdirSync(tmpPath)) const tarPath = await tarFilesToTmp(tmpPath, fs.readdirSync(tmpPath))
// cleanup the tmp export files as tarball returned // cleanup the tmp export files as tarball returned
fs.rmSync(tmpPath, { recursive: true, force: true }) fs.rmSync(tmpPath, { recursive: true, force: true })

View File

@ -6,17 +6,20 @@ import {
AutomationTriggerStepId, AutomationTriggerStepId,
RowAttachment, RowAttachment,
} from "@budibase/types" } from "@budibase/types"
import { getAutomationParams, TABLE_ROW_PREFIX } from "../../../db/utils" import { getAutomationParams } from "../../../db/utils"
import { budibaseTempDir } from "../../../utilities/budibaseDir" import { budibaseTempDir } from "../../../utilities/budibaseDir"
import { DB_EXPORT_FILE, GLOBAL_DB_EXPORT_FILE } from "./constants" import {
DB_EXPORT_FILE,
GLOBAL_DB_EXPORT_FILE,
ATTACHMENT_DIRECTORY,
} from "./constants"
import { downloadTemplate } from "../../../utilities/fileSystem" import { downloadTemplate } from "../../../utilities/fileSystem"
import { ObjectStoreBuckets } from "../../../constants" import { ObjectStoreBuckets } from "../../../constants"
import { join } from "path" import { join } from "path"
import fs from "fs" import fs from "fs"
import sdk from "../../" import sdk from "../../"
import { v4 as uuid } from "uuid"
const uuid = require("uuid/v4") import tar from "tar"
const tar = require("tar")
type TemplateType = { type TemplateType = {
file?: { file?: {
@ -114,12 +117,11 @@ async function getTemplateStream(template: TemplateType) {
} }
} }
export function untarFile(file: { path: string }) { export async function untarFile(file: { path: string }) {
const tmpPath = join(budibaseTempDir(), uuid()) const tmpPath = join(budibaseTempDir(), uuid())
fs.mkdirSync(tmpPath) fs.mkdirSync(tmpPath)
// extract the tarball // extract the tarball
tar.extract({ await tar.extract({
sync: true,
cwd: tmpPath, cwd: tmpPath,
file: file.path, file: file.path,
}) })
@ -130,9 +132,11 @@ async function decryptFiles(path: string, password: string) {
try { try {
for (let file of fs.readdirSync(path)) { for (let file of fs.readdirSync(path)) {
const inputPath = join(path, file) const inputPath = join(path, file)
const outputPath = inputPath.replace(/\.enc$/, "") if (!inputPath.endsWith(ATTACHMENT_DIRECTORY)) {
await encryption.decryptFile(inputPath, outputPath, password) const outputPath = inputPath.replace(/\.enc$/, "")
fs.rmSync(inputPath) await encryption.decryptFile(inputPath, outputPath, password)
fs.rmSync(inputPath)
}
} }
} catch (err: any) { } catch (err: any) {
if (err.message === "incorrect header check") { if (err.message === "incorrect header check") {
@ -162,7 +166,7 @@ export async function importApp(
const isDirectory = const isDirectory =
template.file && fs.lstatSync(template.file.path).isDirectory() template.file && fs.lstatSync(template.file.path).isDirectory()
if (template.file && (isTar || isDirectory)) { if (template.file && (isTar || isDirectory)) {
const tmpPath = isTar ? untarFile(template.file) : template.file.path const tmpPath = isTar ? await untarFile(template.file) : template.file.path
if (isTar && template.file.password) { if (isTar && template.file.password) {
await decryptFiles(tmpPath, template.file.password) await decryptFiles(tmpPath, template.file.password)
} }

View File

@ -56,6 +56,7 @@ import {
import API from "./api" import API from "./api"
import { cloneDeep } from "lodash" import { cloneDeep } from "lodash"
import jwt, { Secret } from "jsonwebtoken"
mocks.licenses.init(pro) mocks.licenses.init(pro)
@ -391,7 +392,7 @@ class TestConfiguration {
sessionId: "sessionid", sessionId: "sessionid",
tenantId: this.getTenantId(), tenantId: this.getTenantId(),
} }
const authToken = auth.jwt.sign(authObj, coreEnv.JWT_SECRET) const authToken = jwt.sign(authObj, coreEnv.JWT_SECRET as Secret)
// returning necessary request headers // returning necessary request headers
await cache.user.invalidateUser(userId) await cache.user.invalidateUser(userId)
@ -412,7 +413,7 @@ class TestConfiguration {
sessionId: "sessionid", sessionId: "sessionid",
tenantId, tenantId,
} }
const authToken = auth.jwt.sign(authObj, coreEnv.JWT_SECRET) const authToken = jwt.sign(authObj, coreEnv.JWT_SECRET as Secret)
const headers: any = { const headers: any = {
Accept: "application/json", Accept: "application/json",

View File

@ -249,7 +249,9 @@ export async function outputProcessing<T extends Row[] | Row>(
continue continue
} }
row[property].forEach((attachment: RowAttachment) => { row[property].forEach((attachment: RowAttachment) => {
attachment.url ??= objectStore.getAppFileUrl(attachment.key) if (!attachment.url) {
attachment.url = objectStore.getAppFileUrl(attachment.key)
}
}) })
} }
} else if ( } else if (

View File

@ -3,6 +3,7 @@ import {
FieldType, FieldType,
FieldTypeSubtypes, FieldTypeSubtypes,
INTERNAL_TABLE_SOURCE_ID, INTERNAL_TABLE_SOURCE_ID,
RowAttachment,
Table, Table,
TableSourceType, TableSourceType,
} from "@budibase/types" } from "@budibase/types"
@ -70,6 +71,49 @@ describe("rowProcessor - outputProcessing", () => {
) )
}) })
it("should handle attachments correctly", async () => {
const table: Table = {
_id: generator.guid(),
name: "TestTable",
type: "table",
sourceId: INTERNAL_TABLE_SOURCE_ID,
sourceType: TableSourceType.INTERNAL,
schema: {
attach: {
type: FieldType.ATTACHMENT,
name: "attach",
constraints: {},
},
},
}
const row: { attach: RowAttachment[] } = {
attach: [
{
size: 10,
name: "test",
extension: "jpg",
key: "test.jpg",
},
],
}
const output = await outputProcessing(table, row, { squash: false })
expect(output.attach[0].url).toBe(
"/files/signed/prod-budi-app-assets/test.jpg"
)
row.attach[0].url = ""
const output2 = await outputProcessing(table, row, { squash: false })
expect(output2.attach[0].url).toBe(
"/files/signed/prod-budi-app-assets/test.jpg"
)
row.attach[0].url = "aaaa"
const output3 = await outputProcessing(table, row, { squash: false })
expect(output3.attach[0].url).toBe("aaaa")
})
it("process output even when the field is not empty", async () => { it("process output even when the field is not empty", async () => {
const table: Table = { const table: Table = {
_id: generator.guid(), _id: generator.guid(),

View File

@ -0,0 +1,9 @@
export interface DatasourceAuthCookie {
appId: string
provider: string
}
export interface SessionCookie {
sessionId: string
userId: string
}

View File

@ -9,3 +9,4 @@ export * from "./app"
export * from "./global" export * from "./global"
export * from "./pagination" export * from "./pagination"
export * from "./searchFilter" export * from "./searchFilter"
export * from "./cookies"

View File

@ -15,6 +15,7 @@ import {
PasswordResetRequest, PasswordResetRequest,
PasswordResetUpdateRequest, PasswordResetUpdateRequest,
GoogleInnerConfig, GoogleInnerConfig,
DatasourceAuthCookie,
} from "@budibase/types" } from "@budibase/types"
import env from "../../../environment" import env from "../../../environment"
@ -148,7 +149,13 @@ export const datasourcePreAuth = async (ctx: any, next: any) => {
} }
export const datasourceAuth = async (ctx: any, next: any) => { export const datasourceAuth = async (ctx: any, next: any) => {
const authStateCookie = getCookie(ctx, Cookie.DatasourceAuth) const authStateCookie = getCookie<DatasourceAuthCookie>(
ctx,
Cookie.DatasourceAuth
)
if (!authStateCookie) {
throw new Error("Unable to retrieve datasource authentication cookie")
}
const provider = authStateCookie.provider const provider = authStateCookie.provider
const { middleware } = require(`@budibase/backend-core`) const { middleware } = require(`@budibase/backend-core`)
const handler = middleware.datasource[provider] const handler = middleware.datasource[provider]

View File

@ -35,6 +35,7 @@ import {
ConfigType, ConfigType,
} from "@budibase/types" } from "@budibase/types"
import API from "./api" import API from "./api"
import jwt, { Secret } from "jsonwebtoken"
class TestConfiguration { class TestConfiguration {
server: any server: any
@ -209,7 +210,7 @@ class TestConfiguration {
sessionId: "sessionid", sessionId: "sessionid",
tenantId: user.tenantId, tenantId: user.tenantId,
} }
const authCookie = auth.jwt.sign(authToken, coreEnv.JWT_SECRET) const authCookie = jwt.sign(authToken, coreEnv.JWT_SECRET as Secret)
return { return {
Accept: "application/json", Accept: "application/json",
...this.cookieHeader([`${constants.Cookie.Auth}=${authCookie}`]), ...this.cookieHeader([`${constants.Cookie.Auth}=${authCookie}`]),
@ -327,7 +328,7 @@ class TestConfiguration {
// CONFIGS - OIDC // CONFIGS - OIDC
getOIDConfigCookie(configId: string) { getOIDConfigCookie(configId: string) {
const token = auth.jwt.sign(configId, coreEnv.JWT_SECRET) const token = jwt.sign(configId, coreEnv.JWT_SECRET as Secret)
return this.cookieHeader([[`${constants.Cookie.OIDC_CONFIG}=${token}`]]) return this.cookieHeader([[`${constants.Cookie.OIDC_CONFIG}=${token}`]])
} }