attachments.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. },
  22. }),
  23. ],
  24. });
  25. if (Meteor.isServer) {
  26. Attachments.allow({
  27. insert(userId, doc) {
  28. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  29. },
  30. update(userId, doc) {
  31. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  32. },
  33. remove(userId, doc) {
  34. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  35. },
  36. // We authorize the attachment download either:
  37. // - if the board is public, everyone (even unconnected) can download it
  38. // - if the board is private, only board members can download it
  39. download(userId, doc) {
  40. const board = Boards.findOne(doc.boardId);
  41. if (board.isPublic()) {
  42. return true;
  43. } else {
  44. return board.hasMember(userId);
  45. }
  46. },
  47. fetch: ['boardId'],
  48. });
  49. }
  50. // XXX Enforce a schema for the Attachments CollectionFS
  51. if (Meteor.isServer) {
  52. Attachments.files.after.insert((userId, doc) => {
  53. // If the attachment doesn't have a source field
  54. // or its source is different than import
  55. if (!doc.source || doc.source !== 'import') {
  56. // Add activity about adding the attachment
  57. Activities.insert({
  58. userId,
  59. type: 'card',
  60. activityType: 'addAttachment',
  61. attachmentId: doc._id,
  62. boardId: doc.boardId,
  63. cardId: doc.cardId,
  64. });
  65. } else {
  66. // Don't add activity about adding the attachment as the activity
  67. // be imported and delete source field
  68. Attachments.update( {_id: doc._id} , {$unset: { source : "" } } );
  69. }
  70. });
  71. Attachments.files.after.remove((userId, doc) => {
  72. Activities.remove({
  73. attachmentId: doc._id,
  74. });
  75. });
  76. }