Merge branch 'master' into feature/add-grid-to-standard-components

This commit is contained in:
kevmodrome 2020-10-07 16:48:59 +02:00
commit c3a87880c1
22 changed files with 91 additions and 37 deletions

View File

@ -1,4 +1,5 @@
const fs = require("fs") const fs = require("fs")
const { join } = require("../../utilities/sanitisedPath")
const readline = require("readline") const readline = require("readline")
const { budibaseAppsDir } = require("../../utilities/budibaseDir") const { budibaseAppsDir } = require("../../utilities/budibaseDir")
const ENV_FILE_PATH = "/.env" const ENV_FILE_PATH = "/.env"
@ -28,8 +29,9 @@ exports.update = async function(ctx) {
async function updateValues([key, value]) { async function updateValues([key, value]) {
let newContent = "" let newContent = ""
let keyExists = false let keyExists = false
let envPath = join(budibaseAppsDir(), ENV_FILE_PATH)
const readInterface = readline.createInterface({ const readInterface = readline.createInterface({
input: fs.createReadStream(`${budibaseAppsDir()}/${ENV_FILE_PATH}`), input: fs.createReadStream(envPath),
output: process.stdout, output: process.stdout,
console: false, console: false,
}) })
@ -47,6 +49,6 @@ async function updateValues([key, value]) {
// Add API Key if it doesn't exist in the file at all // Add API Key if it doesn't exist in the file at all
newContent = `${newContent}\n${key}=${value}` newContent = `${newContent}\n${key}=${value}`
} }
fs.writeFileSync(`${budibaseAppsDir()}/${ENV_FILE_PATH}`, newContent) fs.writeFileSync(envPath, newContent)
}) })
} }

View File

@ -3,12 +3,12 @@ const ClientDb = require("../../db/clientDb")
const { getPackageForBuilder, buildPage } = require("../../utilities/builder") const { getPackageForBuilder, buildPage } = require("../../utilities/builder")
const env = require("../../environment") const env = require("../../environment")
const instanceController = require("./instance") const instanceController = require("./instance")
const { resolve, join } = require("path")
const { copy, exists, readFile, writeFile } = require("fs-extra") const { copy, exists, readFile, writeFile } = require("fs-extra")
const { budibaseAppsDir } = require("../../utilities/budibaseDir") const { budibaseAppsDir } = require("../../utilities/budibaseDir")
const sqrl = require("squirrelly") const sqrl = require("squirrelly")
const setBuilderToken = require("../../utilities/builder/setBuilderToken") const setBuilderToken = require("../../utilities/builder/setBuilderToken")
const fs = require("fs-extra") const fs = require("fs-extra")
const { join, resolve } = require("../../utilities/sanitisedPath")
const { promisify } = require("util") const { promisify } = require("util")
const chmodr = require("chmodr") const chmodr = require("chmodr")
const { generateAppID, getAppParams } = require("../../db/utils") const { generateAppID, getAppParams } = require("../../db/utils")
@ -116,7 +116,7 @@ exports.delete = async function(ctx) {
const db = new CouchDB(ClientDb.name(getClientId(ctx))) const db = new CouchDB(ClientDb.name(getClientId(ctx)))
const app = await db.get(ctx.params.applicationId) const app = await db.get(ctx.params.applicationId)
const result = await db.remove(app) const result = await db.remove(app)
await fs.rmdir(`${budibaseAppsDir()}/${ctx.params.applicationId}`, { await fs.rmdir(join(budibaseAppsDir(), ctx.params.applicationId), {
recursive: true, recursive: true,
}) })

View File

@ -1,6 +1,6 @@
const CouchDB = require("../../db") const CouchDB = require("../../db")
const ClientDb = require("../../db/clientDb") const ClientDb = require("../../db/clientDb")
const { resolve, join } = require("path") const { resolve, join } = require("../../utilities/sanitisedPath")
const { const {
budibaseTempDir, budibaseTempDir,
budibaseAppsDir, budibaseAppsDir,

View File

@ -1,4 +1,5 @@
const fs = require("fs") const fs = require("fs")
const { join } = require("../../../utilities/sanitisedPath")
const AWS = require("aws-sdk") const AWS = require("aws-sdk")
const fetch = require("node-fetch") const fetch = require("node-fetch")
const { budibaseAppsDir } = require("../../../utilities/budibaseDir") const { budibaseAppsDir } = require("../../../utilities/budibaseDir")
@ -108,7 +109,7 @@ exports.uploadAppAssets = async function({
}, },
}) })
const appAssetsPath = `${budibaseAppsDir()}/${appId}/public` const appAssetsPath = join(budibaseAppsDir(), appId, "public")
const appPages = fs.readdirSync(appAssetsPath) const appPages = fs.readdirSync(appAssetsPath)
@ -116,7 +117,7 @@ exports.uploadAppAssets = async function({
for (let page of appPages) { for (let page of appPages) {
// Upload HTML, CSS and JS for each page of the web app // Upload HTML, CSS and JS for each page of the web app
walkDir(`${appAssetsPath}/${page}`, function(filePath) { walkDir(join(appAssetsPath, page), function(filePath) {
const appAssetUpload = prepareUploadForS3({ const appAssetUpload = prepareUploadForS3({
file: { file: {
path: filePath, path: filePath,

View File

@ -2,6 +2,7 @@ const fs = require("fs")
const CouchDB = require("../../db") const CouchDB = require("../../db")
const client = require("../../db/clientDb") const client = require("../../db/clientDb")
const newid = require("../../db/newid") const newid = require("../../db/newid")
const { join } = require("../../utilities/sanitisedPath")
const { downloadTemplate } = require("../../utilities/templates") const { downloadTemplate } = require("../../utilities/templates")
exports.create = async function(ctx) { exports.create = async function(ctx) {
@ -34,7 +35,9 @@ exports.create = async function(ctx) {
// replicate the template data to the instance DB // replicate the template data to the instance DB
if (template) { if (template) {
const templatePath = await downloadTemplate(...template.key.split("/")) const templatePath = await downloadTemplate(...template.key.split("/"))
const dbDumpReadStream = fs.createReadStream(`${templatePath}/db/dump.txt`) const dbDumpReadStream = fs.createReadStream(
join(templatePath, "db", "dump.txt")
)
const { ok } = await db.load(dbDumpReadStream) const { ok } = await db.load(dbDumpReadStream)
if (!ok) { if (!ok) {
ctx.throw(500, "Error loading database dump from template.") ctx.throw(500, "Error loading database dump from template.")

View File

@ -1,5 +1,5 @@
const send = require("koa-send") const send = require("koa-send")
const { resolve, join } = require("path") const { resolve, join } = require("../../utilities/sanitisedPath")
const jwt = require("jsonwebtoken") const jwt = require("jsonwebtoken")
const fetch = require("node-fetch") const fetch = require("node-fetch")
const fs = require("fs-extra") const fs = require("fs-extra")

View File

@ -1,7 +1,7 @@
const CouchDB = require("../../../db") const CouchDB = require("../../../db")
const viewTemplate = require("./viewBuilder") const viewTemplate = require("./viewBuilder")
const fs = require("fs") const fs = require("fs")
const path = require("path") const { join } = require("../../../utilities/sanitisedPath")
const os = require("os") const os = require("os")
const exporters = require("./exporters") const exporters = require("./exporters")
@ -105,7 +105,7 @@ const controller = {
const filename = `${view.name}.${format}` const filename = `${view.name}.${format}`
fs.writeFileSync(path.join(os.tmpdir(), filename), exportedFile) fs.writeFileSync(join(os.tmpdir(), filename), exportedFile)
ctx.body = { ctx.body = {
url: `/api/views/export/download/${filename}`, url: `/api/views/export/download/${filename}`,
@ -116,7 +116,7 @@ const controller = {
const filename = ctx.params.fileName const filename = ctx.params.fileName
ctx.attachment(filename) ctx.attachment(filename)
ctx.body = fs.createReadStream(path.join(os.tmpdir(), filename)) ctx.body = fs.createReadStream(join(os.tmpdir(), filename))
}, },
} }

View File

@ -6,7 +6,7 @@ const createUser = require("./steps/createUser")
const environment = require("../environment") const environment = require("../environment")
const download = require("download") const download = require("download")
const fetch = require("node-fetch") const fetch = require("node-fetch")
const path = require("path") const { join } = require("../utilities/sanitisedPath")
const os = require("os") const os = require("os")
const fs = require("fs") const fs = require("fs")
const Sentry = require("@sentry/node") const Sentry = require("@sentry/node")
@ -43,7 +43,7 @@ async function downloadPackage(name, version, bundleName) {
`${AUTOMATION_BUCKET}/${name}/${version}/${bundleName}`, `${AUTOMATION_BUCKET}/${name}/${version}/${bundleName}`,
AUTOMATION_DIRECTORY AUTOMATION_DIRECTORY
) )
return require(path.join(AUTOMATION_DIRECTORY, bundleName)) return require(join(AUTOMATION_DIRECTORY, bundleName))
} }
module.exports.getAction = async function(actionName) { module.exports.getAction = async function(actionName) {
@ -57,7 +57,7 @@ module.exports.getAction = async function(actionName) {
const pkg = MANIFEST.packages[actionName] const pkg = MANIFEST.packages[actionName]
const bundleName = buildBundleName(pkg.stepId, pkg.version) const bundleName = buildBundleName(pkg.stepId, pkg.version)
try { try {
return require(path.join(AUTOMATION_DIRECTORY, bundleName)) return require(join(AUTOMATION_DIRECTORY, bundleName))
} catch (err) { } catch (err) {
return downloadPackage(pkg.stepId, pkg.version, bundleName) return downloadPackage(pkg.stepId, pkg.version, bundleName)
} }
@ -66,7 +66,7 @@ module.exports.getAction = async function(actionName) {
module.exports.init = async function() { module.exports.init = async function() {
// set defaults // set defaults
if (!AUTOMATION_DIRECTORY) { if (!AUTOMATION_DIRECTORY) {
AUTOMATION_DIRECTORY = path.join(os.homedir(), DEFAULT_DIRECTORY) AUTOMATION_DIRECTORY = join(os.homedir(), DEFAULT_DIRECTORY)
} }
if (!AUTOMATION_BUCKET) { if (!AUTOMATION_BUCKET) {
AUTOMATION_BUCKET = DEFAULT_BUCKET AUTOMATION_BUCKET = DEFAULT_BUCKET

View File

@ -2,6 +2,7 @@ const PouchDB = require("pouchdb")
const replicationStream = require("pouchdb-replication-stream") const replicationStream = require("pouchdb-replication-stream")
const allDbs = require("pouchdb-all-dbs") const allDbs = require("pouchdb-all-dbs")
const { budibaseAppsDir } = require("../utilities/budibaseDir") const { budibaseAppsDir } = require("../utilities/budibaseDir")
const { sanitise } = require("../utilities/sanitisedPath")
const env = require("../environment") const env = require("../environment")
const COUCH_DB_URL = env.COUCH_DB_URL || `leveldb://${budibaseAppsDir()}/.data/` const COUCH_DB_URL = env.COUCH_DB_URL || `leveldb://${budibaseAppsDir()}/.data/`
@ -26,4 +27,10 @@ const Pouch = PouchDB.defaults(POUCH_DB_DEFAULTS)
allDbs(Pouch) allDbs(Pouch)
module.exports = Pouch function PouchWrapper(instance) {
Pouch.apply(this, [sanitise(instance)])
}
PouchWrapper.prototype = Object.create(Pouch.prototype)
module.exports = PouchWrapper

View File

@ -1,5 +1,5 @@
const { app, BrowserWindow, shell, dialog } = require("electron") const { app, BrowserWindow, shell, dialog } = require("electron")
const { join } = require("path") const { join } = require("./utilities/sanitisedPath")
const isDev = require("electron-is-dev") const isDev = require("electron-is-dev")
const { autoUpdater } = require("electron-updater") const { autoUpdater } = require("electron-updater")
const unhandled = require("electron-unhandled") const unhandled = require("electron-unhandled")

View File

@ -1,4 +1,4 @@
const { resolve, join } = require("path") const { resolve, join } = require("./utilities/sanitisedPath")
const { homedir } = require("os") const { homedir } = require("os")
const { app } = require("electron") const { app } = require("electron")
const fixPath = require("fix-path") const fixPath = require("fix-path")

View File

@ -1,4 +1,4 @@
const { join } = require("path") const { join } = require("./sanitisedPath")
const { homedir, tmpdir } = require("os") const { homedir, tmpdir } = require("os")
const env = require("../environment") const env = require("../environment")

View File

@ -6,7 +6,7 @@ const {
readFile, readFile,
writeJSON, writeJSON,
} = require("fs-extra") } = require("fs-extra")
const { join, resolve } = require("path") const { join, resolve } = require("../sanitisedPath")
const sqrl = require("squirrelly") const sqrl = require("squirrelly")
const { convertCssToFiles } = require("./convertCssToFiles") const { convertCssToFiles } = require("./convertCssToFiles")
const publicPath = require("./publicPath") const publicPath = require("./publicPath")

View File

@ -1,6 +1,6 @@
const crypto = require("crypto") const crypto = require("crypto")
const { ensureDir, emptyDir, writeFile } = require("fs-extra") const { ensureDir, emptyDir, writeFile } = require("fs-extra")
const { join } = require("path") const { join } = require("../sanitisedPath")
module.exports.convertCssToFiles = async (publicPagePath, pkg) => { module.exports.convertCssToFiles = async (publicPagePath, pkg) => {
const cssDir = join(publicPagePath, "css") const cssDir = join(publicPagePath, "css")

View File

@ -1,5 +1,5 @@
const { readJSON, readdir } = require("fs-extra") const { readJSON, readdir } = require("fs-extra")
const { join } = require("path") const { join } = require("../sanitisedPath")
module.exports = async appPath => { module.exports = async appPath => {
const pages = {} const pages = {}

View File

@ -8,7 +8,8 @@ const {
unlink, unlink,
rmdir, rmdir,
} = require("fs-extra") } = require("fs-extra")
const { join, dirname, resolve } = require("path") const { join, resolve } = require("../sanitisedPath")
const { dirname } = require("path")
const env = require("../../environment") const env = require("../../environment")
const buildPage = require("./buildPage") const buildPage = require("./buildPage")

View File

@ -1,6 +1,6 @@
const { appPackageFolder } = require("../createAppPackage") const { appPackageFolder } = require("../createAppPackage")
const { readJSON, readdir, stat } = require("fs-extra") const { readJSON, readdir, stat } = require("fs-extra")
const { join } = require("path") const { join } = require("../sanitisedPath")
const { keyBy } = require("lodash/fp") const { keyBy } = require("lodash/fp")
module.exports = async (config, appname, pagename) => { module.exports = async (config, appname, pagename) => {

View File

@ -1,3 +1,3 @@
const { join } = require("path") const { join } = require("../sanitisedPath")
module.exports = (appPath, pageName) => join(appPath, "public", pageName) module.exports = (appPath, pageName) => join(appPath, "public", pageName)

View File

@ -1,4 +1,4 @@
const { resolve } = require("path") const { resolve } = require("./sanitisedPath")
const { cwd } = require("process") const { cwd } = require("process")
const stream = require("stream") const stream = require("stream")
const fetch = require("node-fetch") const fetch = require("node-fetch")

View File

@ -1,5 +1,5 @@
const { exists, readFile, writeFile, ensureDir } = require("fs-extra") const { exists, readFile, writeFile, ensureDir } = require("fs-extra")
const { join, resolve } = require("path") const { join, resolve } = require("./sanitisedPath")
const Sqrl = require("squirrelly") const Sqrl = require("squirrelly")
const uuid = require("uuid") const uuid = require("uuid")

View File

@ -0,0 +1,38 @@
const path = require("path")
const regex = new RegExp(/:(?![\\/])/g)
function sanitiseArgs(args) {
let sanitised = []
for (let arg of args) {
sanitised.push(arg.replace(regex, ""))
}
return sanitised
}
/**
* Exactly the same as path.join but creates a sanitised path.
* @param args Any number of string arguments to add to a path
* @returns {string} The final path ready to use
*/
exports.join = function(...args) {
return path.join(...sanitiseArgs(args))
}
/**
* Exactly the same as path.resolve but creates a sanitised path.
* @param args Any number of string arguments to add to a path
* @returns {string} The final path ready to use
*/
exports.resolve = function(...args) {
return path.resolve(...sanitiseArgs(args))
}
/**
* Sanitise a single string
* @param string input string to sanitise
* @returns {string} the final sanitised string
*/
exports.sanitise = function(string) {
return sanitiseArgs([string])[0]
}

View File

@ -1,5 +1,5 @@
const path = require("path")
const fs = require("fs-extra") const fs = require("fs-extra")
const { join } = require("./sanitisedPath")
const os = require("os") const os = require("os")
const fetch = require("node-fetch") const fetch = require("node-fetch")
const stream = require("stream") const stream = require("stream")
@ -27,10 +27,10 @@ exports.downloadTemplate = async function(type, name) {
await streamPipeline( await streamPipeline(
response.body, response.body,
zlib.Unzip(), zlib.Unzip(),
tar.extract(path.join(budibaseAppsDir(), "templates", type)) tar.extract(join(budibaseAppsDir(), "templates", type))
) )
return path.join(budibaseAppsDir(), "templates", type, name) return join(budibaseAppsDir(), "templates", type, name)
} }
exports.exportTemplateFromApp = async function({ exports.exportTemplateFromApp = async function({
@ -39,15 +39,17 @@ exports.exportTemplateFromApp = async function({
instanceId, instanceId,
}) { }) {
// Copy frontend files // Copy frontend files
const appToExport = path.join(os.homedir(), ".budibase", appId, "pages") const appToExport = join(os.homedir(), ".budibase", appId, "pages")
const templatesDir = path.join(os.homedir(), ".budibase", "templates") const templatesDir = join(os.homedir(), ".budibase", "templates")
fs.ensureDirSync(templatesDir) fs.ensureDirSync(templatesDir)
const templateOutputPath = path.join(templatesDir, templateName) const templateOutputPath = join(templatesDir, templateName)
fs.copySync(appToExport, `${templateOutputPath}/pages`) fs.copySync(appToExport, join(templateOutputPath, "pages"))
fs.ensureDirSync(path.join(templateOutputPath, "db")) fs.ensureDirSync(join(templateOutputPath, "db"))
const writeStream = fs.createWriteStream(`${templateOutputPath}/db/dump.txt`) const writeStream = fs.createWriteStream(
join(templateOutputPath, "db", "dump.txt")
)
// perform couch dump // perform couch dump
const instanceDb = new CouchDB(instanceId) const instanceDb = new CouchDB(instanceId)