attachments.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. try {
  2. Attachments = new FS.Collection('attachments', {
  3. stores: [
  4. // XXX Add a new store for cover thumbnails so we don't load big images in
  5. // the general board view
  6. new FS.Store.GridFS('attachments', {
  7. // If the uploaded document is not an image we need to enforce browser
  8. // download instead of execution. This is particularly important for HTML
  9. // files that the browser will just execute if we don't serve them with the
  10. // appropriate `application/octet-stream` MIME header which can lead to user
  11. // data leaks. I imagine other formats (like PDF) can also be attack vectors.
  12. // See https://github.com/wekan/wekan/issues/99
  13. // XXX Should we use `beforeWrite` option of CollectionFS instead of
  14. // collection-hooks?
  15. // We should use `beforeWrite`.
  16. beforeWrite: (fileObj) => {
  17. if (!fileObj.isImage()) {
  18. return {
  19. type: 'application/octet-stream',
  20. };
  21. }
  22. return {};
  23. },
  24. }),
  25. ],
  26. });
  27. } catch (err) { console.log(err); throw err; }
  28. if (Meteor.isServer) {
  29. Attachments.allow({
  30. insert(userId, doc) {
  31. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  32. },
  33. update(userId, doc) {
  34. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  35. },
  36. remove(userId, doc) {
  37. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  38. },
  39. // We authorize the attachment download either:
  40. // - if the board is public, everyone (even unconnected) can download it
  41. // - if the board is private, only board members can download it
  42. download(userId, doc) {
  43. const board = Boards.findOne(doc.boardId);
  44. if (board.isPublic()) {
  45. return true;
  46. } else {
  47. return board.hasMember(userId);
  48. }
  49. },
  50. fetch: ['boardId'],
  51. });
  52. }
  53. // XXX Enforce a schema for the Attachments CollectionFS
  54. if (Meteor.isServer) {
  55. Attachments.files.after.insert((userId, doc) => {
  56. // If the attachment doesn't have a source field
  57. // or its source is different than import
  58. if (!doc.source || doc.source !== 'import') {
  59. // Add activity about adding the attachment
  60. Activities.insert({
  61. userId,
  62. type: 'card',
  63. activityType: 'addAttachment',
  64. attachmentId: doc._id,
  65. boardId: doc.boardId,
  66. cardId: doc.cardId,
  67. });
  68. } else {
  69. // Don't add activity about adding the attachment as the activity
  70. // be imported and delete source field
  71. Attachments.update( {_id: doc._id}, {$unset: { source : '' } } );
  72. }
  73. });
  74. Attachments.files.after.remove((userId, doc) => {
  75. Activities.remove({
  76. attachmentId: doc._id,
  77. });
  78. });
  79. }