plugins.ts 6.5 KB

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