]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/plugins.ts
Fix lint for emailer
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / plugins.ts
CommitLineData
345da516 1import * as express from 'express'
c8861d5d 2import { body, param, query } from 'express-validator'
345da516
C
3import { logger } from '../../helpers/logger'
4import { areValidationErrors } from './utils'
b5f919ac 5import { isNpmPluginNameValid, isPluginNameValid, isPluginTypeValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
345da516 6import { PluginManager } from '../../lib/plugins/plugin-manager'
c8861d5d 7import { isBooleanValid, isSafePath, toBooleanOrNull } from '../../helpers/custom-validators/misc'
ad91e700 8import { PluginModel } from '../../models/server/plugin'
b5f919ac
C
9import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/install-plugin.model'
10import { PluginType } from '../../../shared/models/plugins/plugin.type'
6702a1b2 11import { CONFIG } from '../../initializers/config'
345da516 12
b5f919ac 13const servePluginStaticDirectoryValidator = (pluginType: PluginType) => [
345da516
C
14 param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'),
15 param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version'),
16 param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
17
18 (req: express.Request, res: express.Response, next: express.NextFunction) => {
19 logger.debug('Checking servePluginStaticDirectory parameters', { parameters: req.params })
20
21 if (areValidationErrors(req, res)) return
22
b5f919ac
C
23 const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType)
24 const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName)
345da516
C
25
26 if (!plugin || plugin.version !== req.params.pluginVersion) {
27 return res.sendStatus(404)
28 }
29
30 res.locals.registeredPlugin = plugin
31
32 return next()
33 }
34]
35
ad91e700 36const listPluginsValidator = [
6702a1b2 37 query('pluginType')
ad91e700
C
38 .optional()
39 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
40 query('uninstalled')
41 .optional()
c8861d5d 42 .customSanitizer(toBooleanOrNull)
ad91e700
C
43 .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'),
44
45 (req: express.Request, res: express.Response, next: express.NextFunction) => {
46 logger.debug('Checking listPluginsValidator parameters', { parameters: req.query })
47
48 if (areValidationErrors(req, res)) return
49
50 return next()
51 }
52]
53
b5f919ac 54const installOrUpdatePluginValidator = [
8d2be0ed
C
55 body('npmName')
56 .optional()
57 .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
58 body('path')
59 .optional()
60 .custom(isSafePath).withMessage('Should have a valid safe path'),
ad91e700
C
61
62 (req: express.Request, res: express.Response, next: express.NextFunction) => {
b5f919ac 63 logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body })
ad91e700
C
64
65 if (areValidationErrors(req, res)) return
66
b5f919ac 67 const body: InstallOrUpdatePlugin = req.body
8d2be0ed
C
68 if (!body.path && !body.npmName) {
69 return res.status(400)
70 .json({ error: 'Should have either a npmName or a path' })
71 .end()
72 }
73
ad91e700
C
74 return next()
75 }
76]
77
78const uninstallPluginValidator = [
79 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
80
81 (req: express.Request, res: express.Response, next: express.NextFunction) => {
dba85a1e 82 logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
ad91e700
C
83
84 if (areValidationErrors(req, res)) return
85
86 return next()
87 }
88]
89
dba85a1e 90const existingPluginValidator = [
60cfd4cb 91 param('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid plugin name'),
ad91e700
C
92
93 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
dba85a1e 94 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
ad91e700
C
95
96 if (areValidationErrors(req, res)) return
97
dba85a1e 98 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
ad91e700
C
99 if (!plugin) {
100 return res.status(404)
101 .json({ error: 'Plugin not found' })
102 .end()
103 }
104
105 res.locals.plugin = plugin
106
107 return next()
108 }
109]
110
111const updatePluginSettingsValidator = [
112 body('settings').exists().withMessage('Should have settings'),
113
114 (req: express.Request, res: express.Response, next: express.NextFunction) => {
115 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
116
117 if (areValidationErrors(req, res)) return
118
119 return next()
120 }
121]
122
6702a1b2 123const listAvailablePluginsValidator = [
6702a1b2
C
124 query('search')
125 .optional()
126 .exists().withMessage('Should have a valid search'),
127 query('pluginType')
128 .optional()
129 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
09071200
C
130 query('currentPeerTubeEngine')
131 .optional()
132 .custom(isPluginVersionValid).withMessage('Should have a valid current peertube engine'),
6702a1b2
C
133
134 (req: express.Request, res: express.Response, next: express.NextFunction) => {
135 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.query })
136
137 if (areValidationErrors(req, res)) return
138
139 if (CONFIG.PLUGINS.INDEX.ENABLED === false) {
140 return res.status(400)
141 .json({ error: 'Plugin index is not enabled' })
142 .end()
143 }
144
145 return next()
146 }
147]
148
345da516
C
149// ---------------------------------------------------------------------------
150
151export {
ad91e700
C
152 servePluginStaticDirectoryValidator,
153 updatePluginSettingsValidator,
154 uninstallPluginValidator,
6702a1b2 155 listAvailablePluginsValidator,
dba85a1e 156 existingPluginValidator,
b5f919ac 157 installOrUpdatePluginValidator,
ad91e700 158 listPluginsValidator
345da516 159}