attachments.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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( {_id: doc._id}, {$unset: { source : '' } } );
  70. }
  71. });
  72. Attachments.files.after.remove((userId, doc) => {
  73. Activities.remove({
  74. attachmentId: doc._id,
  75. });
  76. });
  77. }