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