markdown-setting.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. const loggerFactory = require('@alias/logger');
  2. // eslint-disable-next-line no-unused-vars
  3. const logger = loggerFactory('growi:routes:apiv3:user-group');
  4. const express = require('express');
  5. const router = express.Router();
  6. const { body } = require('express-validator/check');
  7. const ErrorV3 = require('../../models/vo/error-apiv3');
  8. const validator = {
  9. lineBreak: [
  10. body('isEnabledLinebreaks').isBoolean(),
  11. body('isEnabledLinebreaksInComments').isBoolean(),
  12. ],
  13. presentationSetting: [
  14. body('pageBreakSeparator').isInt().not().isEmpty(),
  15. ],
  16. xssSetting: [
  17. body('isEnabledXss').isBoolean(),
  18. body('tagWhiteList').toArray(),
  19. body('attrWhiteList').toArray(),
  20. ],
  21. };
  22. /**
  23. * @swagger
  24. * tags:
  25. * name: MarkDownSetting
  26. */
  27. /**
  28. * @swagger
  29. *
  30. * components:
  31. * schemas:
  32. * LineBreakParams:
  33. * type: object
  34. * properties:
  35. * isEnabledLinebreaks:
  36. * type: boolean
  37. * description: enable lineBreak
  38. * isEnabledLinebreaksInComments:
  39. * type: boolean
  40. * description: enable lineBreak in comment
  41. * PresentationParams:
  42. * type: object
  43. * properties:
  44. * pageBreakSeparator:
  45. * type: number
  46. * description: number of pageBreakSeparator
  47. * pageBreakCustomSeparator:
  48. * type: string
  49. * description: string of pageBreakCustomSeparator
  50. * XssParams:
  51. * type: object
  52. * properties:
  53. * isEnabledPrevention:
  54. * type: boolean
  55. * description: enable xss
  56. * xssOption:
  57. * type: number
  58. * description: number of xss option
  59. * tagWhiteList:
  60. * type: array
  61. * description: array of tag whiteList
  62. * items:
  63. * type: string
  64. * description: tag whitelist
  65. * attrWhiteList:
  66. * type: array
  67. * description: array of attr whiteList
  68. * items:
  69. * type: string
  70. * description: attr whitelist
  71. */
  72. module.exports = (crowi) => {
  73. const loginRequiredStrictly = require('../../middleware/login-required')(crowi);
  74. const adminRequired = require('../../middleware/admin-required')(crowi);
  75. const csrf = require('../../middleware/csrf')(crowi);
  76. const { ApiV3FormValidator } = crowi.middlewares;
  77. /**
  78. * @swagger
  79. *
  80. * /markdown-setting/lineBreak:
  81. * put:
  82. * tags: [MarkDownSetting]
  83. * description: Update lineBreak setting
  84. * requestBody:
  85. * required: true
  86. * content:
  87. * application/json:
  88. * schema:
  89. * type: object
  90. * properties:
  91. * isEnabledLinebreaks:
  92. * description: enable lineBreak
  93. * type: boolean
  94. * isEnabledLinebreaksInComments:
  95. * description: enable lineBreak in comment
  96. * type: boolean
  97. * responses:
  98. * 200:
  99. * description: Succeeded to update lineBreak setting
  100. * content:
  101. * application/json:
  102. * schema:
  103. * properties:
  104. * status:
  105. * $ref: '#/components/schemas/lineBreakParams'
  106. */
  107. router.put('/lineBreak', loginRequiredStrictly, adminRequired, csrf, validator.lineBreak, ApiV3FormValidator, async(req, res) => {
  108. const requestLineBreakParams = {
  109. 'markdown:isEnabledLinebreaks': req.body.isEnabledLinebreaks,
  110. 'markdown:isEnabledLinebreaksInComments': req.body.isEnabledLinebreaksInComments,
  111. };
  112. try {
  113. await crowi.configManager.updateConfigsInTheSameNamespace('markdown', requestLineBreakParams);
  114. const lineBreaksParams = {
  115. isEnabledLinebreaks: await crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  116. isEnabledLinebreaksInComments: await crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  117. };
  118. return res.apiv3({ lineBreaksParams });
  119. }
  120. catch (err) {
  121. const msg = 'Error occurred in updating lineBreak';
  122. logger.error('Error', err);
  123. return res.apiv3Err(new ErrorV3(msg, 'update-lineBreak-failed'));
  124. }
  125. });
  126. /**
  127. * @swagger
  128. *
  129. * /markdown-setting/presentation:
  130. * put:
  131. * tags: [MarkDownSetting]
  132. * description: Update presentation
  133. * requestBody:
  134. * required: true
  135. * content:
  136. * application/json:
  137. * schema:
  138. * type: object
  139. * properties:
  140. * pageBreakSeparator:
  141. * description: number of pageBreakSeparator
  142. * type: number
  143. * pageBreakCustomSeparator:
  144. * description: string of pageBreakCustomSeparator
  145. * type: string
  146. * responses:
  147. * 200:
  148. * description: Succeeded to update presentation setting
  149. * content:
  150. * application/json:
  151. * schema:
  152. * properties:
  153. * status:
  154. * $ref: '#/components/schemas/presentationParams'
  155. */
  156. router.put('/presentation', loginRequiredStrictly, adminRequired, csrf, validator.presentationSetting, ApiV3FormValidator, async(req, res) => {
  157. if (req.body.pageBreakSeparator === 3 && req.body.pageBreakCustomSeparator === '') {
  158. return res.apiv3Err(new ErrorV3('customRegularExpression is required'));
  159. }
  160. const requestPresentationParams = {
  161. 'markdown:presentation:pageBreakSeparator': req.body.pageBreakSeparator,
  162. 'markdown:presentation:pageBreakCustomSeparator': req.body.pageBreakCustomSeparator,
  163. };
  164. try {
  165. await crowi.configManager.updateConfigsInTheSameNamespace('markdown', requestPresentationParams);
  166. const presentationParams = {
  167. pageBreakSeparator: await crowi.configManager.getConfig('markdown', 'markdown:presentation:pageBreakSeparator'),
  168. pageBreakCustomSeparator: await crowi.configManager.getConfig('markdown', 'markdown:presentation:pageBreakCustomSeparator') || '',
  169. };
  170. return res.apiv3({ presentationParams });
  171. }
  172. catch (err) {
  173. const msg = 'Error occurred in updating presentation';
  174. logger.error('Error', err);
  175. return res.apiv3Err(new ErrorV3(msg, 'update-presentation-failed'));
  176. }
  177. });
  178. /**
  179. * @swagger
  180. *
  181. * /markdown-setting/xss:
  182. * put:
  183. * tags: [MarkDownSetting]
  184. * description: Update xss
  185. * requestBody:
  186. * required: true
  187. * content:
  188. * application/json:
  189. * schema:
  190. * type: object
  191. * properties:
  192. * isEnabledPrevention:
  193. * description: enable xss
  194. * type: boolean
  195. * xssOption:
  196. * description: number of xss option
  197. * type: number
  198. * tagWhiteList:
  199. * description: array of tag whiteList
  200. * type: array
  201. * items:
  202. * type: string
  203. * description: tag whitelist
  204. * attrWhiteList:
  205. * description: array of attr whiteList
  206. * type: array
  207. * items:
  208. * type: string
  209. * description: attr whitelist
  210. * responses:
  211. * 200:
  212. * description: Succeeded to update xss setting
  213. * content:
  214. * application/json:
  215. * schema:
  216. * properties:
  217. * status:
  218. * $ref: '#/components/schemas/xssParams'
  219. */
  220. router.put('/xss', loginRequiredStrictly, adminRequired, csrf, validator.xssSetting, ApiV3FormValidator, async(req, res) => {
  221. if (req.body.isEnabledXss && req.body.xssOption == null) {
  222. return res.apiv3Err(new ErrorV3('xss option is required'));
  223. }
  224. const reqestXssParams = {
  225. 'markdown:xss:isEnabledPrevention': req.body.isEnabledXss,
  226. 'markdown:xss:option': req.body.xssOption,
  227. 'markdown:xss:tagWhiteList': req.body.tagWhiteList,
  228. 'markdown:xss:attrWhiteList': req.body.attrWhiteList,
  229. };
  230. try {
  231. await crowi.configManager.updateConfigsInTheSameNamespace('markdown', reqestXssParams);
  232. const xssParams = {
  233. isEnabledXss: await crowi.configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
  234. xssOption: await crowi.configManager.getConfig('markdown', 'markdown:xss:option'),
  235. tagWhiteList: await crowi.configManager.getConfig('markdown', 'markdown:xss:tagWhiteList'),
  236. attrWhiteList: await crowi.configManager.getConfig('markdown', 'markdown:xss:attrWhiteList'),
  237. };
  238. return res.apiv3({ xssParams });
  239. }
  240. catch (err) {
  241. const msg = 'Error occurred in updating xss';
  242. logger.error('Error', err);
  243. return res.apiv3Err(new ErrorV3(msg, 'update-xss-failed'));
  244. }
  245. });
  246. return router;
  247. };