]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/plugins.ts
Support short uuid for GET video/playlist
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / plugins.ts
1 import * as express from 'express'
2 import { body, param, query, ValidationChain } from 'express-validator'
3 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4 import { PluginType } from '../../../shared/models/plugins/plugin.type'
5 import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/server/api/install-plugin.model'
6 import { exists, isBooleanValid, isSafePath, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
7 import { isNpmPluginNameValid, isPluginNameValid, isPluginTypeValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
8 import { logger } from '../../helpers/logger'
9 import { CONFIG } from '../../initializers/config'
10 import { PluginManager } from '../../lib/plugins/plugin-manager'
11 import { PluginModel } from '../../models/server/plugin'
12 import { areValidationErrors } from './shared'
13
14 const getPluginValidator = (pluginType: PluginType, withVersion = true) => {
15 const validators: (ValidationChain | express.Handler)[] = [
16 param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name')
17 ]
18
19 if (withVersion) {
20 validators.push(
21 param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version')
22 )
23 }
24
25 return validators.concat([
26 (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking getPluginValidator parameters', { parameters: req.params })
28
29 if (areValidationErrors(req, res)) return
30
31 const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType)
32 const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName)
33
34 if (!plugin) {
35 return res.fail({
36 status: HttpStatusCode.NOT_FOUND_404,
37 message: 'No plugin found named ' + npmName
38 })
39 }
40 if (withVersion && plugin.version !== req.params.pluginVersion) {
41 return res.fail({
42 status: HttpStatusCode.NOT_FOUND_404,
43 message: 'No plugin found named ' + npmName + ' with version ' + req.params.pluginVersion
44 })
45 }
46
47 res.locals.registeredPlugin = plugin
48
49 return next()
50 }
51 ])
52 }
53
54 const getExternalAuthValidator = [
55 param('authName').custom(exists).withMessage('Should have a valid auth name'),
56
57 (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking getExternalAuthValidator parameters', { parameters: req.params })
59
60 if (areValidationErrors(req, res)) return
61
62 const plugin = res.locals.registeredPlugin
63 if (!plugin.registerHelpers) {
64 return res.fail({
65 status: HttpStatusCode.NOT_FOUND_404,
66 message: 'No registered helpers were found for this plugin'
67 })
68 }
69
70 const externalAuth = plugin.registerHelpers.getExternalAuths().find(a => a.authName === req.params.authName)
71 if (!externalAuth) {
72 return res.fail({
73 status: HttpStatusCode.NOT_FOUND_404,
74 message: 'No external auths were found for this plugin'
75 })
76 }
77
78 res.locals.externalAuth = externalAuth
79
80 return next()
81 }
82 ]
83
84 const pluginStaticDirectoryValidator = [
85 param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
86
87 (req: express.Request, res: express.Response, next: express.NextFunction) => {
88 logger.debug('Checking pluginStaticDirectoryValidator parameters', { parameters: req.params })
89
90 if (areValidationErrors(req, res)) return
91
92 return next()
93 }
94 ]
95
96 const listPluginsValidator = [
97 query('pluginType')
98 .optional()
99 .customSanitizer(toIntOrNull)
100 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
101 query('uninstalled')
102 .optional()
103 .customSanitizer(toBooleanOrNull)
104 .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'),
105
106 (req: express.Request, res: express.Response, next: express.NextFunction) => {
107 logger.debug('Checking listPluginsValidator parameters', { parameters: req.query })
108
109 if (areValidationErrors(req, res)) return
110
111 return next()
112 }
113 ]
114
115 const installOrUpdatePluginValidator = [
116 body('npmName')
117 .optional()
118 .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
119 body('path')
120 .optional()
121 .custom(isSafePath).withMessage('Should have a valid safe path'),
122
123 (req: express.Request, res: express.Response, next: express.NextFunction) => {
124 logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body })
125
126 if (areValidationErrors(req, res)) return
127
128 const body: InstallOrUpdatePlugin = req.body
129 if (!body.path && !body.npmName) {
130 return res.fail({ message: 'Should have either a npmName or a path' })
131 }
132
133 return next()
134 }
135 ]
136
137 const uninstallPluginValidator = [
138 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
139
140 (req: express.Request, res: express.Response, next: express.NextFunction) => {
141 logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
142
143 if (areValidationErrors(req, res)) return
144
145 return next()
146 }
147 ]
148
149 const existingPluginValidator = [
150 param('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid plugin name'),
151
152 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
153 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
154
155 if (areValidationErrors(req, res)) return
156
157 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
158 if (!plugin) {
159 return res.fail({
160 status: HttpStatusCode.NOT_FOUND_404,
161 message: 'Plugin not found'
162 })
163 }
164
165 res.locals.plugin = plugin
166 return next()
167 }
168 ]
169
170 const updatePluginSettingsValidator = [
171 body('settings').exists().withMessage('Should have settings'),
172
173 (req: express.Request, res: express.Response, next: express.NextFunction) => {
174 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
175
176 if (areValidationErrors(req, res)) return
177
178 return next()
179 }
180 ]
181
182 const listAvailablePluginsValidator = [
183 query('search')
184 .optional()
185 .exists().withMessage('Should have a valid search'),
186 query('pluginType')
187 .optional()
188 .customSanitizer(toIntOrNull)
189 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
190 query('currentPeerTubeEngine')
191 .optional()
192 .custom(isPluginVersionValid).withMessage('Should have a valid current peertube engine'),
193
194 (req: express.Request, res: express.Response, next: express.NextFunction) => {
195 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.query })
196
197 if (areValidationErrors(req, res)) return
198
199 if (CONFIG.PLUGINS.INDEX.ENABLED === false) {
200 return res.fail({ message: 'Plugin index is not enabled' })
201 }
202
203 return next()
204 }
205 ]
206
207 // ---------------------------------------------------------------------------
208
209 export {
210 pluginStaticDirectoryValidator,
211 getPluginValidator,
212 updatePluginSettingsValidator,
213 uninstallPluginValidator,
214 listAvailablePluginsValidator,
215 existingPluginValidator,
216 installOrUpdatePluginValidator,
217 listPluginsValidator,
218 getExternalAuthValidator
219 }