attachments.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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) {
  125. return allowIsBoardMember(userId, ReactiveCache.getBoard(fileObj.boardId));
  126. },
  127. remove(userId, fileObj) {
  128. return allowIsBoardMember(userId, ReactiveCache.getBoard(fileObj.boardId));
  129. },
  130. fetch: ['meta'],
  131. });
  132. Meteor.methods({
  133. // Validate image URL to prevent SVG-based DoS attacks
  134. validateImageUrl(imageUrl) {
  135. check(imageUrl, String);
  136. if (!imageUrl) {
  137. return { valid: false, reason: 'Empty URL' };
  138. }
  139. // Block SVG files and data URIs
  140. if (imageUrl.endsWith('.svg') || imageUrl.startsWith('data:image/svg')) {
  141. if (process.env.DEBUG === 'true') {
  142. console.warn('Blocked potentially malicious SVG image URL:', imageUrl);
  143. }
  144. return { valid: false, reason: 'SVG images are blocked for security reasons' };
  145. }
  146. // Block data URIs that could contain malicious content
  147. if (imageUrl.startsWith('data:')) {
  148. if (process.env.DEBUG === 'true') {
  149. console.warn('Blocked data URI image URL:', imageUrl);
  150. }
  151. return { valid: false, reason: 'Data URIs are blocked for security reasons' };
  152. }
  153. // Validate URL format
  154. try {
  155. const url = new URL(imageUrl);
  156. // Only allow http and https protocols
  157. if (!['http:', 'https:'].includes(url.protocol)) {
  158. return { valid: false, reason: 'Only HTTP and HTTPS protocols are allowed' };
  159. }
  160. } catch (e) {
  161. return { valid: false, reason: 'Invalid URL format' };
  162. }
  163. return { valid: true };
  164. },
  165. moveAttachmentToStorage(fileObjId, storageDestination) {
  166. check(fileObjId, String);
  167. check(storageDestination, String);
  168. const fileObj = ReactiveCache.getAttachment(fileObjId);
  169. moveToStorage(fileObj, storageDestination, fileStoreStrategyFactory);
  170. },
  171. renameAttachment(fileObjId, newName) {
  172. check(fileObjId, String);
  173. check(newName, String);
  174. const fileObj = ReactiveCache.getAttachment(fileObjId);
  175. rename(fileObj, newName, fileStoreStrategyFactory);
  176. },
  177. validateAttachment(fileObjId) {
  178. check(fileObjId, String);
  179. const fileObj = ReactiveCache.getAttachment(fileObjId);
  180. const isValid = Promise.await(isFileValid(fileObj, attachmentUploadMimeTypes, attachmentUploadSize, attachmentUploadExternalProgram));
  181. if (!isValid) {
  182. Attachments.remove(fileObjId);
  183. }
  184. },
  185. validateAttachmentAndMoveToStorage(fileObjId, storageDestination) {
  186. check(fileObjId, String);
  187. check(storageDestination, String);
  188. Meteor.call('validateAttachment', fileObjId);
  189. const fileObj = ReactiveCache.getAttachment(fileObjId);
  190. if (fileObj) {
  191. Meteor.defer(() => Meteor.call('moveAttachmentToStorage', fileObjId, storageDestination));
  192. }
  193. },
  194. });
  195. Meteor.startup(() => {
  196. Attachments.collection.createIndex({ 'meta.cardId': 1 });
  197. const storagePath = fileStoreStrategyFactory.storagePath;
  198. if (!fs.existsSync(storagePath)) {
  199. console.log("create storagePath because it doesn't exist: " + storagePath);
  200. fs.mkdirSync(storagePath, { recursive: true });
  201. }
  202. });
  203. // Add backward compatibility methods
  204. Attachments.getAttachmentWithBackwardCompatibility = getAttachmentWithBackwardCompatibility;
  205. Attachments.getAttachmentsWithBackwardCompatibility = getAttachmentsWithBackwardCompatibility;
  206. }
  207. export default Attachments;