|
| 1 | +import type { checkFileRestrictionsParams, FileAllowList } from './types.js' |
| 2 | + |
| 3 | +import { APIError } from '../errors/index.js' |
| 4 | + |
| 5 | +/** |
| 6 | + * Restricted file types and their extensions. |
| 7 | + */ |
| 8 | +export const RESTRICTED_FILE_EXT_AND_TYPES: FileAllowList = [ |
| 9 | + { extensions: ['exe', 'dll'], mimeType: 'application/x-msdownload' }, |
| 10 | + { extensions: ['exe', 'com', 'app', 'action'], mimeType: 'application/x-executable' }, |
| 11 | + { extensions: ['bat', 'cmd'], mimeType: 'application/x-msdos-program' }, |
| 12 | + { extensions: ['exe', 'com'], mimeType: 'application/x-ms-dos-executable' }, |
| 13 | + { extensions: ['dmg'], mimeType: 'application/x-apple-diskimage' }, |
| 14 | + { extensions: ['deb'], mimeType: 'application/x-debian-package' }, |
| 15 | + { extensions: ['rpm'], mimeType: 'application/x-redhat-package-manager' }, |
| 16 | + { extensions: ['exe', 'dll'], mimeType: 'application/vnd.microsoft.portable-executable' }, |
| 17 | + { extensions: ['msi'], mimeType: 'application/x-msi' }, |
| 18 | + { extensions: ['jar', 'ear', 'war'], mimeType: 'application/java-archive' }, |
| 19 | + { extensions: ['desktop'], mimeType: 'application/x-desktop' }, |
| 20 | + { extensions: ['cpl'], mimeType: 'application/x-cpl' }, |
| 21 | + { extensions: ['lnk'], mimeType: 'application/x-ms-shortcut' }, |
| 22 | + { extensions: ['pkg'], mimeType: 'application/x-apple-installer' }, |
| 23 | + { extensions: ['htm', 'html', 'shtml', 'xhtml'], mimeType: 'text/html' }, |
| 24 | + { extensions: ['php', 'phtml'], mimeType: 'application/x-httpd-php' }, |
| 25 | + { extensions: ['js', 'jse'], mimeType: 'text/javascript' }, |
| 26 | + { extensions: ['jsp'], mimeType: 'application/x-jsp' }, |
| 27 | + { extensions: ['py'], mimeType: 'text/x-python' }, |
| 28 | + { extensions: ['rb'], mimeType: 'text/x-ruby' }, |
| 29 | + { extensions: ['pl'], mimeType: 'text/x-perl' }, |
| 30 | + { extensions: ['ps1', 'psc1', 'psd1', 'psh', 'psm1'], mimeType: 'application/x-powershell' }, |
| 31 | + { extensions: ['vbe', 'vbs'], mimeType: 'application/x-vbscript' }, |
| 32 | + { extensions: ['ws', 'wsc', 'wsf', 'wsh'], mimeType: 'application/x-ms-wsh' }, |
| 33 | + { extensions: ['scr'], mimeType: 'application/x-msdownload' }, |
| 34 | + { extensions: ['asp', 'aspx'], mimeType: 'application/x-asp' }, |
| 35 | + { extensions: ['hta'], mimeType: 'application/x-hta' }, |
| 36 | + { extensions: ['reg'], mimeType: 'application/x-registry' }, |
| 37 | + { extensions: ['url'], mimeType: 'application/x-url' }, |
| 38 | + { extensions: ['workflow'], mimeType: 'application/x-workflow' }, |
| 39 | + { extensions: ['command'], mimeType: 'application/x-command' }, |
| 40 | +] |
| 41 | + |
| 42 | +export const checkFileRestrictions = ({ |
| 43 | + collection, |
| 44 | + file, |
| 45 | + req, |
| 46 | +}: checkFileRestrictionsParams): void => { |
| 47 | + const { upload: uploadConfig } = collection |
| 48 | + const configMimeTypes = |
| 49 | + uploadConfig && |
| 50 | + typeof uploadConfig === 'object' && |
| 51 | + 'mimeTypes' in uploadConfig && |
| 52 | + Array.isArray(uploadConfig.mimeTypes) |
| 53 | + ? uploadConfig.mimeTypes |
| 54 | + : [] |
| 55 | + |
| 56 | + const allowRestrictedFileTypes = |
| 57 | + uploadConfig && typeof uploadConfig === 'object' && 'allowRestrictedFileTypes' in uploadConfig |
| 58 | + ? (uploadConfig as { allowRestrictedFileTypes?: boolean }).allowRestrictedFileTypes |
| 59 | + : false |
| 60 | + |
| 61 | + // Skip validation if `mimeTypes` are defined in the upload config, or `allowRestrictedFileTypes` are allowed |
| 62 | + if (allowRestrictedFileTypes || configMimeTypes.length) { |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + const isRestricted = RESTRICTED_FILE_EXT_AND_TYPES.some((type) => { |
| 67 | + const hasRestrictedExt = type.extensions.some((ext) => file.name.toLowerCase().endsWith(ext)) |
| 68 | + const hasRestrictedMime = type.mimeType === file.mimetype |
| 69 | + return hasRestrictedExt || hasRestrictedMime |
| 70 | + }) |
| 71 | + |
| 72 | + if (isRestricted) { |
| 73 | + const errorMessage = `File type '${file.mimetype}' not allowed ${file.name}: Restricted file type detected -- set 'allowRestrictedFileTypes' to true to skip this check for this Collection.` |
| 74 | + req.payload.logger.error(errorMessage) |
| 75 | + throw new APIError(errorMessage) |
| 76 | + } |
| 77 | +} |
0 commit comments