]>
Commit | Line | Data |
---|---|---|
345da516 | 1 | import * as express from 'express' |
5e2b2e27 | 2 | import { body, param, query, ValidationChain } from 'express-validator' |
428ccb8b C |
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' | |
b5f919ac | 7 | import { isNpmPluginNameValid, isPluginNameValid, isPluginTypeValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins' |
428ccb8b C |
8 | import { logger } from '../../helpers/logger' |
9 | import { CONFIG } from '../../initializers/config' | |
345da516 | 10 | import { PluginManager } from '../../lib/plugins/plugin-manager' |
ad91e700 | 11 | import { PluginModel } from '../../models/server/plugin' |
428ccb8b | 12 | import { areValidationErrors } from './utils' |
345da516 | 13 | |
5e2b2e27 C |
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 | ] | |
345da516 | 18 | |
5e2b2e27 C |
19 | if (withVersion) { |
20 | validators.push( | |
21 | param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version') | |
22 | ) | |
23 | } | |
345da516 | 24 | |
5e2b2e27 C |
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 | ||
2d53be02 RK |
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) | |
345da516 | 36 | |
5e2b2e27 | 37 | res.locals.registeredPlugin = plugin |
345da516 | 38 | |
5e2b2e27 | 39 | return next() |
345da516 | 40 | } |
5e2b2e27 C |
41 | ]) |
42 | } | |
43 | ||
4a8d113b C |
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 | |
1896bca0 | 53 | if (!plugin.registerHelpers) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) |
4a8d113b | 54 | |
1896bca0 | 55 | const externalAuth = plugin.registerHelpers.getExternalAuths().find(a => a.authName === req.params.authName) |
2d53be02 | 56 | if (!externalAuth) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) |
4a8d113b C |
57 | |
58 | res.locals.externalAuth = externalAuth | |
59 | ||
60 | return next() | |
61 | } | |
62 | ] | |
63 | ||
5e2b2e27 C |
64 | const pluginStaticDirectoryValidator = [ |
65 | param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'), | |
345da516 | 66 | |
5e2b2e27 C |
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 | |
345da516 C |
71 | |
72 | return next() | |
73 | } | |
74 | ] | |
75 | ||
ad91e700 | 76 | const listPluginsValidator = [ |
6702a1b2 | 77 | query('pluginType') |
ad91e700 | 78 | .optional() |
a02b93ce | 79 | .customSanitizer(toIntOrNull) |
ad91e700 C |
80 | .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'), |
81 | query('uninstalled') | |
82 | .optional() | |
c8861d5d | 83 | .customSanitizer(toBooleanOrNull) |
ad91e700 C |
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 | ||
b5f919ac | 95 | const installOrUpdatePluginValidator = [ |
8d2be0ed C |
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'), | |
ad91e700 C |
102 | |
103 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
b5f919ac | 104 | logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body }) |
ad91e700 C |
105 | |
106 | if (areValidationErrors(req, res)) return | |
107 | ||
b5f919ac | 108 | const body: InstallOrUpdatePlugin = req.body |
8d2be0ed | 109 | if (!body.path && !body.npmName) { |
2d53be02 | 110 | return res.status(HttpStatusCode.BAD_REQUEST_400) |
8d2be0ed | 111 | .json({ error: 'Should have either a npmName or a path' }) |
8d2be0ed C |
112 | } |
113 | ||
ad91e700 C |
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) => { | |
dba85a1e | 122 | logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body }) |
ad91e700 C |
123 | |
124 | if (areValidationErrors(req, res)) return | |
125 | ||
126 | return next() | |
127 | } | |
128 | ] | |
129 | ||
dba85a1e | 130 | const existingPluginValidator = [ |
60cfd4cb | 131 | param('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid plugin name'), |
ad91e700 C |
132 | |
133 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
dba85a1e | 134 | logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params }) |
ad91e700 C |
135 | |
136 | if (areValidationErrors(req, res)) return | |
137 | ||
dba85a1e | 138 | const plugin = await PluginModel.loadByNpmName(req.params.npmName) |
ad91e700 | 139 | if (!plugin) { |
2d53be02 RK |
140 | return res.status(HttpStatusCode.NOT_FOUND_404) |
141 | .json({ error: 'Plugin not found' }) | |
ad91e700 C |
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 | ||
6702a1b2 | 162 | const listAvailablePluginsValidator = [ |
6702a1b2 C |
163 | query('search') |
164 | .optional() | |
165 | .exists().withMessage('Should have a valid search'), | |
166 | query('pluginType') | |
167 | .optional() | |
a02b93ce | 168 | .customSanitizer(toIntOrNull) |
6702a1b2 | 169 | .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'), |
09071200 C |
170 | query('currentPeerTubeEngine') |
171 | .optional() | |
172 | .custom(isPluginVersionValid).withMessage('Should have a valid current peertube engine'), | |
6702a1b2 C |
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) { | |
2d53be02 RK |
180 | return res.status(HttpStatusCode.BAD_REQUEST_400) |
181 | .json({ error: 'Plugin index is not enabled' }) | |
182 | .end() | |
6702a1b2 C |
183 | } |
184 | ||
185 | return next() | |
186 | } | |
187 | ] | |
188 | ||
345da516 C |
189 | // --------------------------------------------------------------------------- |
190 | ||
191 | export { | |
5e2b2e27 C |
192 | pluginStaticDirectoryValidator, |
193 | getPluginValidator, | |
ad91e700 C |
194 | updatePluginSettingsValidator, |
195 | uninstallPluginValidator, | |
6702a1b2 | 196 | listAvailablePluginsValidator, |
dba85a1e | 197 | existingPluginValidator, |
b5f919ac | 198 | installOrUpdatePluginValidator, |
4a8d113b C |
199 | listPluginsValidator, |
200 | getExternalAuthValidator | |
345da516 | 201 | } |