]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { body, param, query } 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 } 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 servePluginStaticDirectoryValidator = (pluginType: PluginType) => [ | |
14 | param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'), | |
15 | param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version'), | |
16 | param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'), | |
17 | ||
18 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
19 | logger.debug('Checking servePluginStaticDirectory parameters', { parameters: req.params }) | |
20 | ||
21 | if (areValidationErrors(req, res)) return | |
22 | ||
23 | const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType) | |
24 | const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName) | |
25 | ||
26 | if (!plugin || plugin.version !== req.params.pluginVersion) { | |
27 | return res.sendStatus(404) | |
28 | } | |
29 | ||
30 | res.locals.registeredPlugin = plugin | |
31 | ||
32 | return next() | |
33 | } | |
34 | ] | |
35 | ||
36 | const listPluginsValidator = [ | |
37 | query('pluginType') | |
38 | .optional() | |
39 | .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'), | |
40 | query('uninstalled') | |
41 | .optional() | |
42 | .customSanitizer(toBooleanOrNull) | |
43 | .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'), | |
44 | ||
45 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
46 | logger.debug('Checking listPluginsValidator parameters', { parameters: req.query }) | |
47 | ||
48 | if (areValidationErrors(req, res)) return | |
49 | ||
50 | return next() | |
51 | } | |
52 | ] | |
53 | ||
54 | const installOrUpdatePluginValidator = [ | |
55 | body('npmName') | |
56 | .optional() | |
57 | .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'), | |
58 | body('path') | |
59 | .optional() | |
60 | .custom(isSafePath).withMessage('Should have a valid safe path'), | |
61 | ||
62 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
63 | logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body }) | |
64 | ||
65 | if (areValidationErrors(req, res)) return | |
66 | ||
67 | const body: InstallOrUpdatePlugin = req.body | |
68 | if (!body.path && !body.npmName) { | |
69 | return res.status(400) | |
70 | .json({ error: 'Should have either a npmName or a path' }) | |
71 | .end() | |
72 | } | |
73 | ||
74 | return next() | |
75 | } | |
76 | ] | |
77 | ||
78 | const uninstallPluginValidator = [ | |
79 | body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'), | |
80 | ||
81 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
82 | logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body }) | |
83 | ||
84 | if (areValidationErrors(req, res)) return | |
85 | ||
86 | return next() | |
87 | } | |
88 | ] | |
89 | ||
90 | const existingPluginValidator = [ | |
91 | param('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid plugin name'), | |
92 | ||
93 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
94 | logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params }) | |
95 | ||
96 | if (areValidationErrors(req, res)) return | |
97 | ||
98 | const plugin = await PluginModel.loadByNpmName(req.params.npmName) | |
99 | if (!plugin) { | |
100 | return res.status(404) | |
101 | .json({ error: 'Plugin not found' }) | |
102 | .end() | |
103 | } | |
104 | ||
105 | res.locals.plugin = plugin | |
106 | ||
107 | return next() | |
108 | } | |
109 | ] | |
110 | ||
111 | const updatePluginSettingsValidator = [ | |
112 | body('settings').exists().withMessage('Should have settings'), | |
113 | ||
114 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
115 | logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body }) | |
116 | ||
117 | if (areValidationErrors(req, res)) return | |
118 | ||
119 | return next() | |
120 | } | |
121 | ] | |
122 | ||
123 | const listAvailablePluginsValidator = [ | |
124 | query('search') | |
125 | .optional() | |
126 | .exists().withMessage('Should have a valid search'), | |
127 | query('pluginType') | |
128 | .optional() | |
129 | .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'), | |
130 | query('currentPeerTubeEngine') | |
131 | .optional() | |
132 | .custom(isPluginVersionValid).withMessage('Should have a valid current peertube engine'), | |
133 | ||
134 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
135 | logger.debug('Checking enabledPluginValidator parameters', { parameters: req.query }) | |
136 | ||
137 | if (areValidationErrors(req, res)) return | |
138 | ||
139 | if (CONFIG.PLUGINS.INDEX.ENABLED === false) { | |
140 | return res.status(400) | |
141 | .json({ error: 'Plugin index is not enabled' }) | |
142 | .end() | |
143 | } | |
144 | ||
145 | return next() | |
146 | } | |
147 | ] | |
148 | ||
149 | // --------------------------------------------------------------------------- | |
150 | ||
151 | export { | |
152 | servePluginStaticDirectoryValidator, | |
153 | updatePluginSettingsValidator, | |
154 | uninstallPluginValidator, | |
155 | listAvailablePluginsValidator, | |
156 | existingPluginValidator, | |
157 | installOrUpdatePluginValidator, | |
158 | listPluginsValidator | |
159 | } |