attachments.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. ],
  7. });
  8. if (Meteor.isServer) {
  9. Attachments.allow({
  10. insert(userId, doc) {
  11. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  12. },
  13. update(userId, doc) {
  14. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  15. },
  16. remove(userId, doc) {
  17. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  18. },
  19. // We authorize the attachment download either:
  20. // - if the board is public, everyone (even unconnected) can download it
  21. // - if the board is private, only board members can download it
  22. //
  23. // XXX We have a bug with the `userId` verification:
  24. //
  25. // https://github.com/CollectionFS/Meteor-CollectionFS/issues/449
  26. //
  27. download(userId, doc) {
  28. const query = {
  29. $or: [
  30. { 'members.userId': userId },
  31. { permission: 'public' },
  32. ],
  33. };
  34. return Boolean(Boards.findOne(doc.boardId, query));
  35. },
  36. fetch: ['boardId'],
  37. });
  38. }
  39. // XXX Enforce a schema for the Attachments CollectionFS
  40. Attachments.files.before.insert((userId, doc) => {
  41. const file = new FS.File(doc);
  42. doc.userId = userId;
  43. // If the uploaded document is not an image we need to enforce browser
  44. // download instead of execution. This is particularly important for HTML
  45. // files that the browser will just execute if we don't serve them with the
  46. // appropriate `application/octet-stream` MIME header which can lead to user
  47. // data leaks. I imagine other formats (like PDF) can also be attack vectors.
  48. // See https://github.com/wekan/wekan/issues/99
  49. // XXX Should we use `beforeWrite` option of CollectionFS instead of
  50. // collection-hooks?
  51. if (!file.isImage()) {
  52. file.original.type = 'application/octet-stream';
  53. }
  54. });
  55. if (Meteor.isServer) {
  56. Attachments.files.after.insert((userId, doc) => {
  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. });
  66. Attachments.files.after.remove((userId, doc) => {
  67. Activities.remove({
  68. attachmentId: doc._id,
  69. });
  70. });
  71. }