certify-shared-file.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const loggerFactory = require('@alias/logger');
  2. const url = require('url');
  3. const logger = loggerFactory('growi:middleware:certify-shared-fire');
  4. module.exports = (crowi) => {
  5. return async(req, res, next) => {
  6. const { referer } = req.headers;
  7. // Attachments cannot be viewed by clients who do not send referer.
  8. // https://github.com/weseek/growi/issues/2819
  9. if (referer == null) {
  10. return next();
  11. }
  12. const { path } = url.parse(referer);
  13. if (!path.startsWith('/share/')) {
  14. return next();
  15. }
  16. const fileId = req.params.id || null;
  17. const Attachment = crowi.model('Attachment');
  18. const ShareLink = crowi.model('ShareLink');
  19. const attachment = await Attachment.findOne({ _id: fileId });
  20. if (attachment == null) {
  21. return next();
  22. }
  23. const shareLinks = await ShareLink.find({ relatedPage: attachment.page });
  24. // If sharelinks don't exist, skip it
  25. if (shareLinks.length === 0) {
  26. return next();
  27. }
  28. // Is there a valid share link
  29. shareLinks.map((sharelink) => {
  30. if (!sharelink.isExpired()) {
  31. logger.debug('Confirmed target file belong to a share page');
  32. req.isSharedPage = true;
  33. }
  34. return;
  35. });
  36. next();
  37. };
  38. };