]>
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 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | |
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) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | |
35 | if (withVersion && plugin.version !== req.params.pluginVersion) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | |
36 | ||
37 | res.locals.registeredPlugin = plugin | |
38 | ||
39 | return next() | |
40 | } | |
41 | ]) | |
42 | } | |
43 | ||
44 | const getExternalAuthValidator = [ | |
45 | param('authName').custom(exists).withMessage('Should have a valid auth name'), | |
46 | ||
47 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
48 | logger.debug('Checking getExternalAuthValidator parameters', { parameters: req.params }) | |
49 | ||
50 | if (areValidationErrors(req, res)) return | |
51 | ||
52 | const plugin = res.locals.registeredPlugin | |
53 | if (!plugin.registerHelpers) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | |
54 | ||
55 | const externalAuth = plugin.registerHelpers.getExternalAuths().find(a => a.authName === req.params.authName) | |
56 | if (!externalAuth) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | |
57 | ||
58 | res.locals.externalAuth = externalAuth | |
59 | ||
60 | return next() | |
61 | } | |
62 | ] | |
63 | ||
64 | const pluginStaticDirectoryValidator = [ | |
65 | param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'), | |
66 | ||
67 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
68 | logger.debug('Checking pluginStaticDirectoryValidator parameters', { parameters: req.params }) | |
69 | ||
70 | if (areValidationErrors(req, res)) return | |
71 | ||
72 | return next() | |
73 | } | |
74 | ] | |
75 | ||
76 | const listPluginsValidator = [ | |
77 | query('pluginType') | |
78 | .optional() | |
79 | .customSanitizer(toIntOrNull) | |
80 | .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'), | |
81 | query('uninstalled') | |
82 | .optional() | |
83 | .customSanitizer(toBooleanOrNull) | |
84 | .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'), | |
85 | ||
86 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
87 | logger.debug('Checking listPluginsValidator parameters', { parameters: req.query }) | |
88 | ||
89 | if (areValidationErrors(req, res)) return | |
90 | ||
91 | return next() | |
92 | } | |
93 | ] | |
94 | ||
95 | const installOrUpdatePluginValidator = [ | |
96 | body('npmName') | |
97 | .optional() | |
98 | .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'), | |
99 | body('path') | |
100 | .optional() | |
101 | .custom(isSafePath).withMessage('Should have a valid safe path'), | |
102 | ||
103 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
104 | logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body }) | |
105 | ||
106 | if (areValidationErrors(req, res)) return | |
107 | ||
108 | const body: InstallOrUpdatePlugin = req.body | |
109 | if (!body.path && !body.npmName) { | |
110 | return res.status(HttpStatusCode.BAD_REQUEST_400) | |
111 | .json({ error: 'Should have either a npmName or a path' }) | |
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(HttpStatusCode.NOT_FOUND_404) | |
141 | .json({ error: 'Plugin not found' }) | |
142 | } | |
143 | ||
144 | res.locals.plugin = plugin | |
145 | ||
146 | return next() | |
147 | } | |
148 | ] | |
149 | ||
150 | const updatePluginSettingsValidator = [ | |
151 | body('settings').exists().withMessage('Should have settings'), | |
152 | ||
153 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
154 | logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body }) | |
155 | ||
156 | if (areValidationErrors(req, res)) return | |
157 | ||
158 | return next() | |
159 | } | |
160 | ] | |
161 | ||
162 | const listAvailablePluginsValidator = [ | |
163 | query('search') | |
164 | .optional() | |
165 | .exists().withMessage('Should have a valid search'), | |
166 | query('pluginType') | |
167 | .optional() | |
168 | .customSanitizer(toIntOrNull) | |
169 | .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'), | |
170 | query('currentPeerTubeEngine') | |
171 | .optional() | |
172 | .custom(isPluginVersionValid).withMessage('Should have a valid current peertube engine'), | |
173 | ||
174 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
175 | logger.debug('Checking enabledPluginValidator parameters', { parameters: req.query }) | |
176 | ||
177 | if (areValidationErrors(req, res)) return | |
178 | ||
179 | if (CONFIG.PLUGINS.INDEX.ENABLED === false) { | |
180 | return res.status(HttpStatusCode.BAD_REQUEST_400) | |
181 | .json({ error: 'Plugin index is not enabled' }) | |
182 | .end() | |
183 | } | |
184 | ||
185 | return next() | |
186 | } | |
187 | ] | |
188 | ||
189 | // --------------------------------------------------------------------------- | |
190 | ||
191 | export { | |
192 | pluginStaticDirectoryValidator, | |
193 | getPluginValidator, | |
194 | updatePluginSettingsValidator, | |
195 | uninstallPluginValidator, | |
196 | listAvailablePluginsValidator, | |
197 | existingPluginValidator, | |
198 | installOrUpdatePluginValidator, | |
199 | listPluginsValidator, | |
200 | getExternalAuthValidator | |
201 | } |