attachments.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { Meteor } from 'meteor/meteor';
  3. import { FilesCollection } from 'meteor/ostrio:files';
  4. import { isFileValid } from './fileValidation';
  5. import { createBucket } from './lib/grid/createBucket';
  6. import fs from 'fs';
  7. import path from 'path';
  8. import { AttachmentStoreStrategyFilesystem, AttachmentStoreStrategyGridFs, AttachmentStoreStrategyS3 } from '/models/lib/attachmentStoreStrategy';
  9. import FileStoreStrategyFactory, {moveToStorage, rename, STORAGE_NAME_FILESYSTEM, STORAGE_NAME_GRIDFS, STORAGE_NAME_S3} from '/models/lib/fileStoreStrategy';
  10. import { getAttachmentWithBackwardCompatibility, getAttachmentsWithBackwardCompatibility } from './lib/attachmentBackwardCompatibility';
  11. let attachmentUploadExternalProgram;
  12. let attachmentUploadMimeTypes = [];
  13. let attachmentUploadSize = 0;
  14. let attachmentBucket;
  15. let storagePath;
  16. if (Meteor.isServer) {
  17. attachmentBucket = createBucket('attachments');
  18. if (process.env.ATTACHMENTS_UPLOAD_MIME_TYPES) {
  19. attachmentUploadMimeTypes = process.env.ATTACHMENTS_UPLOAD_MIME_TYPES.split(',');
  20. attachmentUploadMimeTypes = attachmentUploadMimeTypes.map(value => value.trim());
  21. }
  22. if (process.env.ATTACHMENTS_UPLOAD_MAX_SIZE) {
  23. attachmentUploadSize = parseInt(process.env.ATTACHMENTS_UPLOAD_MAX_SIZE);
  24. if (isNaN(attachmentUploadSize)) {
  25. attachmentUploadSize = 0
  26. }
  27. }
  28. if (process.env.ATTACHMENTS_UPLOAD_EXTERNAL_PROGRAM) {
  29. attachmentUploadExternalProgram = process.env.ATTACHMENTS_UPLOAD_EXTERNAL_PROGRAM;
  30. if (!attachmentUploadExternalProgram.includes("{file}")) {
  31. attachmentUploadExternalProgram = undefined;
  32. }
  33. }
  34. storagePath = path.join(process.env.WRITABLE_PATH, 'attachments');
  35. }
  36. export const fileStoreStrategyFactory = new FileStoreStrategyFactory(AttachmentStoreStrategyFilesystem, storagePath, AttachmentStoreStrategyGridFs, attachmentBucket);
  37. // XXX Enforce a schema for the Attachments FilesCollection
  38. // see: https://github.com/VeliovGroup/Meteor-Files/wiki/Schema
  39. Attachments = new FilesCollection({
  40. debug: false, // Change to `true` for debugging
  41. collectionName: 'attachments',
  42. allowClientCode: true,
  43. namingFunction(opts) {
  44. let filenameWithoutExtension = ""
  45. let fileId = "";
  46. if (opts?.name) {
  47. // Client
  48. filenameWithoutExtension = opts.name.replace(/(.+)\..+/, "$1");
  49. fileId = opts.meta.fileId;
  50. delete opts.meta.fileId;
  51. } else if (opts?.file?.name) {
  52. // Server
  53. if (opts.file.extension) {
  54. filenameWithoutExtension = opts.file.name.replace(new RegExp(opts.file.extensionWithDot + "$"), "")
  55. } else {
  56. // file has no extension, so don't replace anything, otherwise the last character is removed (because extensionWithDot = '.')
  57. filenameWithoutExtension = opts.file.name;
  58. }
  59. fileId = opts.fileId;
  60. }
  61. else {
  62. // should never reach here
  63. filenameWithoutExtension = Math.random().toString(36).slice(2);
  64. fileId = Math.random().toString(36).slice(2);
  65. }
  66. // OLD:
  67. //const ret = fileId + "-original-" + filenameWithoutExtension;
  68. // NEW: Save file only with filename of ObjectID, not including filename.
  69. // Fixes https://github.com/wekan/wekan/issues/4416#issuecomment-1510517168
  70. const ret = fileId;
  71. // remove fileId from meta, it was only stored there to have this information here in the namingFunction function
  72. return ret;
  73. },
  74. sanitize(str, max, replacement) {
  75. // keep the original filename
  76. return str;
  77. },
  78. storagePath() {
  79. const ret = fileStoreStrategyFactory.storagePath;
  80. return ret;
  81. },
  82. onAfterUpload(fileObj) {
  83. // current storage is the filesystem, update object and database
  84. Object.keys(fileObj.versions).forEach(versionName => {
  85. fileObj.versions[versionName].storage = STORAGE_NAME_FILESYSTEM;
  86. });
  87. this._now = new Date();
  88. Attachments.update({ _id: fileObj._id }, { $set: { "versions" : fileObj.versions } });
  89. Attachments.update({ _id: fileObj.uploadedAtOstrio }, { $set: { "uploadedAtOstrio" : this._now } });
  90. let storageDestination = fileObj.meta.copyStorage || STORAGE_NAME_GRIDFS;
  91. Meteor.defer(() => Meteor.call('validateAttachmentAndMoveToStorage', fileObj._id, storageDestination));
  92. },
  93. interceptDownload(http, fileObj, versionName) {
  94. const ret = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).interceptDownload(http, this.cacheControl);
  95. return ret;
  96. },
  97. onAfterRemove(files) {
  98. files.forEach(fileObj => {
  99. Object.keys(fileObj.versions).forEach(versionName => {
  100. fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).onAfterRemove();
  101. });
  102. });
  103. },
  104. // We authorize the attachment download either:
  105. // - if the board is public, everyone (even unconnected) can download it
  106. // - if the board is private, only board members can download it
  107. protected(fileObj) {
  108. // file may have been deleted already again after upload validation failed
  109. if (!fileObj) {
  110. return false;
  111. }
  112. const board = ReactiveCache.getBoard(fileObj.meta.boardId);
  113. if (board.isPublic()) {
  114. return true;
  115. }
  116. return board.hasMember(this.userId);
  117. },
  118. });
  119. if (Meteor.isServer) {
  120. Attachments.allow({
  121. insert(userId, fileObj) {
  122. return allowIsBoardMember(userId, ReactiveCache.getBoard(fileObj.boardId));
  123. },
  124. update(userId, fileObj, fields) {
  125. // Only allow updates to specific fields that don't affect security
  126. const allowedFields = ['name', 'size', 'type', 'extension', 'extensionWithDot', 'meta', 'versions'];
  127. const isAllowedField = fields.every(field => allowedFields.includes(field));
  128. if (!isAllowedField) {
  129. if (process.env.DEBUG === 'true') {
  130. console.warn('Blocked attempt to update restricted attachment fields:', fields);
  131. }
  132. return false;
  133. }
  134. return allowIsBoardMember(userId, ReactiveCache.getBoard(fileObj.boardId));
  135. },
  136. remove(userId, fileObj) {
  137. // Additional security check: ensure the file belongs to the board the user has access to
  138. if (!fileObj || !fileObj.boardId) {
  139. if (process.env.DEBUG === 'true') {
  140. console.warn('Blocked attachment removal: file has no boardId');
  141. }
  142. return false;
  143. }
  144. const board = ReactiveCache.getBoard(fileObj.boardId);
  145. if (!board) {
  146. if (process.env.DEBUG === 'true') {
  147. console.warn('Blocked attachment removal: board not found');
  148. }
  149. return false;
  150. }
  151. return allowIsBoardMember(userId, board);
  152. },
  153. fetch: ['meta', 'boardId'],
  154. });
  155. Meteor.methods({
  156. // Validate image URL to prevent SVG-based DoS attacks
  157. validateImageUrl(imageUrl) {
  158. check(imageUrl, String);
  159. if (!imageUrl) {
  160. return { valid: false, reason: 'Empty URL' };
  161. }
  162. // Block SVG files and data URIs
  163. if (imageUrl.endsWith('.svg') || imageUrl.startsWith('data:image/svg')) {
  164. if (process.env.DEBUG === 'true') {
  165. console.warn('Blocked potentially malicious SVG image URL:', imageUrl);
  166. }
  167. return { valid: false, reason: 'SVG images are blocked for security reasons' };
  168. }
  169. // Block data URIs that could contain malicious content
  170. if (imageUrl.startsWith('data:')) {
  171. if (process.env.DEBUG === 'true') {
  172. console.warn('Blocked data URI image URL:', imageUrl);
  173. }
  174. return { valid: false, reason: 'Data URIs are blocked for security reasons' };
  175. }
  176. // Validate URL format
  177. try {
  178. const url = new URL(imageUrl);
  179. // Only allow http and https protocols
  180. if (!['http:', 'https:'].includes(url.protocol)) {
  181. return { valid: false, reason: 'Only HTTP and HTTPS protocols are allowed' };
  182. }
  183. } catch (e) {
  184. return { valid: false, reason: 'Invalid URL format' };
  185. }
  186. return { valid: true };
  187. },
  188. moveAttachmentToStorage(fileObjId, storageDestination) {
  189. check(fileObjId, String);
  190. check(storageDestination, String);
  191. const fileObj = ReactiveCache.getAttachment(fileObjId);
  192. moveToStorage(fileObj, storageDestination, fileStoreStrategyFactory);
  193. },
  194. renameAttachment(fileObjId, newName) {
  195. check(fileObjId, String);
  196. check(newName, String);
  197. const currentUserId = Meteor.userId();
  198. if (!currentUserId) {
  199. throw new Meteor.Error('not-authorized', 'User must be logged in');
  200. }
  201. const fileObj = ReactiveCache.getAttachment(fileObjId);
  202. if (!fileObj) {
  203. throw new Meteor.Error('file-not-found', 'Attachment not found');
  204. }
  205. // Verify the user has permission to modify this attachment
  206. const board = ReactiveCache.getBoard(fileObj.boardId);
  207. if (!board) {
  208. throw new Meteor.Error('board-not-found', 'Board not found');
  209. }
  210. if (!allowIsBoardMember(currentUserId, board)) {
  211. if (process.env.DEBUG === 'true') {
  212. console.warn(`Blocked unauthorized attachment rename attempt: user ${currentUserId} tried to rename attachment ${fileObjId} in board ${fileObj.boardId}`);
  213. }
  214. throw new Meteor.Error('not-authorized', 'You do not have permission to modify this attachment');
  215. }
  216. rename(fileObj, newName, fileStoreStrategyFactory);
  217. },
  218. validateAttachment(fileObjId) {
  219. check(fileObjId, String);
  220. const fileObj = ReactiveCache.getAttachment(fileObjId);
  221. const isValid = Promise.await(isFileValid(fileObj, attachmentUploadMimeTypes, attachmentUploadSize, attachmentUploadExternalProgram));
  222. if (!isValid) {
  223. Attachments.remove(fileObjId);
  224. }
  225. },
  226. validateAttachmentAndMoveToStorage(fileObjId, storageDestination) {
  227. check(fileObjId, String);
  228. check(storageDestination, String);
  229. Meteor.call('validateAttachment', fileObjId);
  230. const fileObj = ReactiveCache.getAttachment(fileObjId);
  231. if (fileObj) {
  232. Meteor.defer(() => Meteor.call('moveAttachmentToStorage', fileObjId, storageDestination));
  233. }
  234. },
  235. });
  236. Meteor.startup(() => {
  237. Attachments.collection.createIndex({ 'meta.cardId': 1 });
  238. const storagePath = fileStoreStrategyFactory.storagePath;
  239. if (!fs.existsSync(storagePath)) {
  240. console.log("create storagePath because it doesn't exist: " + storagePath);
  241. fs.mkdirSync(storagePath, { recursive: true });
  242. }
  243. });
  244. // Add backward compatibility methods
  245. Attachments.getAttachmentWithBackwardCompatibility = getAttachmentWithBackwardCompatibility;
  246. Attachments.getAttachmentsWithBackwardCompatibility = getAttachmentsWithBackwardCompatibility;
  247. }
  248. export default Attachments;