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