From f023a19c3eeeea2b014b47fae522a62eab320048 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 5 Jul 2019 15:28:49 +0200 Subject: WIP plugins: install/uninstall --- server/helpers/core-utils.ts | 13 ++++++++++++- server/helpers/custom-validators/misc.ts | 2 +- server/helpers/custom-validators/plugins.ts | 12 ++++++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index b1e9af0a1..c5b139378 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts @@ -10,7 +10,7 @@ import { isAbsolute, join } from 'path' import * as pem from 'pem' import { URL } from 'url' import { truncate } from 'lodash' -import { exec } from 'child_process' +import { exec, ExecOptions } from 'child_process' const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => { if (!oldObject || typeof oldObject !== 'object') { @@ -204,6 +204,16 @@ function sha1 (str: string | Buffer, encoding: HexBase64Latin1Encoding = 'hex') return createHash('sha1').update(str).digest(encoding) } +function execShell (command: string, options?: ExecOptions) { + return new Promise<{ err?: Error, stdout: string, stderr: string }>((res, rej) => { + exec(command, options, (err, stdout, stderr) => { + if (err) return rej({ err, stdout, stderr }) + + return res({ stdout, stderr }) + }) + }) +} + function promisify0 (func: (cb: (err: any, result: A) => void) => void): () => Promise { return function promisified (): Promise { return new Promise((resolve: (arg: A) => void, reject: (err: any) => void) => { @@ -269,6 +279,7 @@ export { sanitizeUrl, sanitizeHost, buildPath, + execShell, peertubeTruncate, sha256, diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index f72513c1c..3ef38fce1 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts @@ -9,7 +9,7 @@ function exists (value: any) { function isSafePath (p: string) { return exists(p) && (p + '').split(sep).every(part => { - return [ '', '.', '..' ].includes(part) === false + return [ '..' ].includes(part) === false }) } diff --git a/server/helpers/custom-validators/plugins.ts b/server/helpers/custom-validators/plugins.ts index ff687dc3f..2fcdc581f 100644 --- a/server/helpers/custom-validators/plugins.ts +++ b/server/helpers/custom-validators/plugins.ts @@ -17,6 +17,13 @@ function isPluginNameValid (value: string) { validator.matches(value, /^[a-z\-]+$/) } +function isNpmPluginNameValid (value: string) { + return exists(value) && + validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.NAME) && + validator.matches(value, /^[a-z\-]+$/) && + (value.startsWith('peertube-plugin-') || value.startsWith('peertube-theme-')) +} + function isPluginDescriptionValid (value: string) { return exists(value) && validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.DESCRIPTION) } @@ -55,7 +62,7 @@ function isCSSPathsValid (css: any[]) { } function isPackageJSONValid (packageJSON: PluginPackageJson, pluginType: PluginType) { - return isPluginNameValid(packageJSON.name) && + return isNpmPluginNameValid(packageJSON.name) && isPluginDescriptionValid(packageJSON.description) && isPluginEngineValid(packageJSON.engine) && isUrlValid(packageJSON.homepage) && @@ -78,5 +85,6 @@ export { isPluginVersionValid, isPluginNameValid, isPluginDescriptionValid, - isLibraryCodeValid + isLibraryCodeValid, + isNpmPluginNameValid } -- cgit v1.2.3