plugins.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import express from 'express'
  2. import { body, param, query, ValidationChain } from 'express-validator'
  3. import { HttpStatusCode } from '../../../shared/models/http/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'
  7. import { isNpmPluginNameValid, isPluginNameValid, isPluginTypeValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
  8. import { logger } from '../../helpers/logger'
  9. import { CONFIG } from '../../initializers/config'
  10. import { PluginManager } from '../../lib/plugins/plugin-manager'
  11. import { PluginModel } from '../../models/server/plugin'
  12. import { areValidationErrors } from './shared'
  13. const getPluginValidator = (pluginType: PluginType, withVersion = true) => {
  14. const validators: (ValidationChain | express.Handler)[] = [
  15. param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name')
  16. ]
  17. if (withVersion) {
  18. validators.push(
  19. param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version')
  20. )
  21. }
  22. return validators.concat([
  23. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  24. logger.debug('Checking getPluginValidator parameters', { parameters: req.params })
  25. if (areValidationErrors(req, res)) return
  26. const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType)
  27. const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName)
  28. if (!plugin) {
  29. return res.fail({
  30. status: HttpStatusCode.NOT_FOUND_404,
  31. message: 'No plugin found named ' + npmName
  32. })
  33. }
  34. if (withVersion && plugin.version !== req.params.pluginVersion) {
  35. return res.fail({
  36. status: HttpStatusCode.NOT_FOUND_404,
  37. message: 'No plugin found named ' + npmName + ' with version ' + req.params.pluginVersion
  38. })
  39. }
  40. res.locals.registeredPlugin = plugin
  41. return next()
  42. }
  43. ])
  44. }
  45. const getExternalAuthValidator = [
  46. param('authName').custom(exists).withMessage('Should have a valid auth name'),
  47. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  48. logger.debug('Checking getExternalAuthValidator parameters', { parameters: req.params })
  49. if (areValidationErrors(req, res)) return
  50. const plugin = res.locals.registeredPlugin
  51. if (!plugin.registerHelpers) {
  52. return res.fail({
  53. status: HttpStatusCode.NOT_FOUND_404,
  54. message: 'No registered helpers were found for this plugin'
  55. })
  56. }
  57. const externalAuth = plugin.registerHelpers.getExternalAuths().find(a => a.authName === req.params.authName)
  58. if (!externalAuth) {
  59. return res.fail({
  60. status: HttpStatusCode.NOT_FOUND_404,
  61. message: 'No external auths were found for this plugin'
  62. })
  63. }
  64. res.locals.externalAuth = externalAuth
  65. return next()
  66. }
  67. ]
  68. const pluginStaticDirectoryValidator = [
  69. param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
  70. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  71. logger.debug('Checking pluginStaticDirectoryValidator parameters', { parameters: req.params })
  72. if (areValidationErrors(req, res)) return
  73. return next()
  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. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  86. logger.debug('Checking listPluginsValidator parameters', { parameters: req.query })
  87. if (areValidationErrors(req, res)) return
  88. return next()
  89. }
  90. ]
  91. const installOrUpdatePluginValidator = [
  92. body('npmName')
  93. .optional()
  94. .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
  95. body('pluginVersion')
  96. .optional()
  97. .custom(isPluginVersionValid).withMessage('Should have a valid plugin version'),
  98. body('path')
  99. .optional()
  100. .custom(isSafePath).withMessage('Should have a valid safe path'),
  101. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  102. logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body })
  103. if (areValidationErrors(req, res)) return
  104. const body: InstallOrUpdatePlugin = req.body
  105. if (!body.path && !body.npmName) {
  106. return res.fail({ message: 'Should have either a npmName or a path' })
  107. }
  108. if (body.pluginVersion && !body.npmName) {
  109. return res.fail({ message: 'Should have a npmName when specifying a pluginVersion' })
  110. }
  111. return next()
  112. }
  113. ]
  114. const uninstallPluginValidator = [
  115. body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
  116. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  117. logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
  118. if (areValidationErrors(req, res)) return
  119. return next()
  120. }
  121. ]
  122. const existingPluginValidator = [
  123. param('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid plugin name'),
  124. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  125. logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
  126. if (areValidationErrors(req, res)) return
  127. const plugin = await PluginModel.loadByNpmName(req.params.npmName)
  128. if (!plugin) {
  129. return res.fail({
  130. status: HttpStatusCode.NOT_FOUND_404,
  131. message: 'Plugin not found'
  132. })
  133. }
  134. res.locals.plugin = plugin
  135. return next()
  136. }
  137. ]
  138. const updatePluginSettingsValidator = [
  139. body('settings').exists().withMessage('Should have settings'),
  140. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  141. logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
  142. if (areValidationErrors(req, res)) return
  143. return next()
  144. }
  145. ]
  146. const listAvailablePluginsValidator = [
  147. query('search')
  148. .optional()
  149. .exists().withMessage('Should have a valid search'),
  150. query('pluginType')
  151. .optional()
  152. .customSanitizer(toIntOrNull)
  153. .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
  154. query('currentPeerTubeEngine')
  155. .optional()
  156. .custom(isPluginVersionValid).withMessage('Should have a valid current peertube engine'),
  157. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  158. logger.debug('Checking enabledPluginValidator parameters', { parameters: req.query })
  159. if (areValidationErrors(req, res)) return
  160. if (CONFIG.PLUGINS.INDEX.ENABLED === false) {
  161. return res.fail({ message: 'Plugin index is not enabled' })
  162. }
  163. return next()
  164. }
  165. ]
  166. // ---------------------------------------------------------------------------
  167. export {
  168. pluginStaticDirectoryValidator,
  169. getPluginValidator,
  170. updatePluginSettingsValidator,
  171. uninstallPluginValidator,
  172. listAvailablePluginsValidator,
  173. existingPluginValidator,
  174. installOrUpdatePluginValidator,
  175. listPluginsValidator,
  176. getExternalAuthValidator
  177. }