plugins.ts 5.3 KB

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