@nestjs/common 11.0.19 → 11.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/common",
3
- "version": "11.0.19",
3
+ "version": "11.0.21",
4
4
  "description": "Nest - modern, fast, powerful node.js web framework (@common)",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "homepage": "https://nestjs.com",
@@ -20,6 +20,7 @@
20
20
  "dependencies": {
21
21
  "file-type": "20.4.1",
22
22
  "iterare": "1.2.1",
23
+ "load-esm": "1.0.2",
23
24
  "tslib": "2.8.1",
24
25
  "uid": "2.0.2"
25
26
  },
@@ -8,6 +8,11 @@ export type FileTypeValidatorOptions = {
8
8
  * @default false
9
9
  */
10
10
  skipMagicNumbersValidation?: boolean;
11
+ /**
12
+ * If `true`, and magic number check fails, fallback to mimetype comparison.
13
+ * @default false
14
+ */
15
+ fallbackToMimetype?: boolean;
11
16
  };
12
17
  /**
13
18
  * Defines the built-in FileTypeValidator. It validates incoming files by examining
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FileTypeValidator = void 0;
4
4
  const file_validator_interface_1 = require("./file-validator.interface");
5
+ const load_esm_1 = require("load-esm");
5
6
  /**
6
7
  * Defines the built-in FileTypeValidator. It validates incoming files by examining
7
8
  * their magic numbers using the file-type package, providing more reliable file type validation
@@ -13,26 +14,50 @@ const file_validator_interface_1 = require("./file-validator.interface");
13
14
  */
14
15
  class FileTypeValidator extends file_validator_interface_1.FileValidator {
15
16
  buildErrorMessage(file) {
17
+ const expected = this.validationOptions.fileType;
16
18
  if (file?.mimetype) {
17
- return `Validation failed (current file type is ${file.mimetype}, expected type is ${this.validationOptions.fileType})`;
19
+ const baseMessage = `Validation failed (current file type is ${file.mimetype}, expected type is ${expected})`;
20
+ /**
21
+ * If fallbackToMimetype is enabled, this means the validator failed to detect the file type
22
+ * via magic number inspection (e.g. due to an unknown or too short buffer),
23
+ * and instead used the mimetype string provided by the client as a fallback.
24
+ *
25
+ * This message clarifies that fallback logic was used, in case users rely on file signatures.
26
+ */
27
+ if (this.validationOptions.fallbackToMimetype) {
28
+ return `${baseMessage} - magic number detection failed, used mimetype fallback`;
29
+ }
30
+ return baseMessage;
18
31
  }
19
- return `Validation failed (expected type is ${this.validationOptions.fileType})`;
32
+ return `Validation failed (expected type is ${expected})`;
20
33
  }
21
34
  async isValid(file) {
22
35
  if (!this.validationOptions) {
23
36
  return true;
24
37
  }
25
38
  const isFileValid = !!file && 'mimetype' in file;
39
+ // Skip magic number validation if set
26
40
  if (this.validationOptions.skipMagicNumbersValidation) {
27
41
  return (isFileValid && !!file.mimetype.match(this.validationOptions.fileType));
28
42
  }
29
- if (!isFileValid || !file.buffer) {
43
+ if (!isFileValid || !file.buffer)
30
44
  return false;
31
- }
32
45
  try {
33
- const { fileTypeFromBuffer } = (await eval('import ("file-type")'));
46
+ const { fileTypeFromBuffer } = await (0, load_esm_1.loadEsm)('file-type');
34
47
  const fileType = await fileTypeFromBuffer(file.buffer);
35
- return (!!fileType && !!fileType.mime.match(this.validationOptions.fileType));
48
+ if (fileType) {
49
+ // Match detected mime type against allowed type
50
+ return !!fileType.mime.match(this.validationOptions.fileType);
51
+ }
52
+ /**
53
+ * Fallback logic: If file-type cannot detect magic number (e.g. file too small),
54
+ * Optionally fall back to mimetype string for compatibility.
55
+ * This is useful for plain text, CSVs, or files without recognizable signatures.
56
+ */
57
+ if (this.validationOptions.fallbackToMimetype) {
58
+ return !!file.mimetype.match(this.validationOptions.fileType);
59
+ }
60
+ return false;
36
61
  }
37
62
  catch {
38
63
  return false;