All files / utils fileUploadPolicy.js

100% Statements 10/10
87.5% Branches 7/8
100% Functions 1/1
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84              30x                                                           30x                                                             30x     46x 43x 31x 31x     30x            
// Shared by uploads/presigned-url.js (enforced at upload time) and
// schemas/item-data-schema.js (re-checked at item save time, since an item's
// `data` can be written directly via the API without ever calling the
// presigned-url endpoint). `file`/`file_set` fields accept a curated
// allowlist of document/media/archive/image types instead of the free-form
// "anything goes" a generic file upload would otherwise allow — HTML, SVG,
// and any executable/script type are deliberately never on this list.
const ALLOWED_FILE_MIME_TYPES = new Set([
  // Documents
  'application/pdf',
  'application/msword',
  'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  'application/vnd.ms-excel',
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  'application/vnd.ms-powerpoint',
  'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  'application/vnd.oasis.opendocument.text',
  'application/vnd.oasis.opendocument.spreadsheet',
  'application/vnd.oasis.opendocument.presentation',
  'application/rtf',
  'text/plain',
  'text/csv',
  // Archives
  'application/zip',
  // Media
  'audio/mpeg',
  'audio/wav',
  'video/mp4',
  'video/quicktime',
  // Images, for generic attachments (scans, photos) distinct from the
  // dedicated image/image_set field types
  'image/jpeg',
  'image/png',
  'image/webp',
  'image/gif'
])
 
const ALLOWED_FILE_EXTENSIONS = new Set([
  'pdf',
  'doc',
  'docx',
  'xls',
  'xlsx',
  'ppt',
  'pptx',
  'odt',
  'ods',
  'odp',
  'rtf',
  'txt',
  'csv',
  'zip',
  'mp3',
  'wav',
  'mp4',
  'mov',
  'jpg',
  'jpeg',
  'png',
  'webp',
  'gif'
])
 
// A generic file has no client-side resize/compression pipeline the way
// images do, so this is enforced server-side via an S3 presigned POST
// `content-length-range` condition (see uploads/presigned-url.js) — unlike
// the image path, which relies on a client-side-only check and a presigned
// PUT that S3 will accept at any size.
const MAX_FILE_SIZE_BYTES = 20 * 1024 * 1024 // 20MB
 
function isAllowedFileUpload(fileName, fileType) {
  if (typeof fileName !== 'string' || typeof fileType !== 'string') return false
  if (!ALLOWED_FILE_MIME_TYPES.has(fileType)) return false
  const ext = (fileName.split('.').pop() || '').toLowerCase()
  return ALLOWED_FILE_EXTENSIONS.has(ext)
}
 
module.exports = {
  ALLOWED_FILE_MIME_TYPES,
  ALLOWED_FILE_EXTENSIONS,
  MAX_FILE_SIZE_BYTES,
  isAllowedFileUpload
}