attachments.js 2.8 KB

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