attachments_old.js.disabled 3.5 KB

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