-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
240 lines (214 loc) · 7.31 KB
/
index.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { promises as fs } from "fs";
import path from "path";
/**
* File signatures based on file format specification
* Reference: https://en.wikipedia.org/wiki/List_of_file_signatures
*/
const FILE_SIGNATURES = {
// Image signatures
JPEG: [
[0xff, 0xd8, 0xff, 0xe0], // JPEG/JFIF
[0xff, 0xd8, 0xff, 0xe1], // JPEG/Exif
],
PNG: [[0x89, 0x50, 0x4e, 0x47]], // PNG signature
GIF: [[0x47, 0x49, 0x46, 0x38]], // GIF87a or GIF89a
// PDF signature
PDF: [[0x25, 0x50, 0x44, 0x46]], // %PDF
// SVG signatures - checking for XML and SVG tags
SVG: [
[0x3c, 0x3f, 0x78, 0x6d, 0x6c], // <?xml
[0x3c, 0x73, 0x76, 0x67], // <svg
],
};
/**
* Check if file buffer match with any specified signatures.
*
* @param {Buffer} buffer - File buffer to check
* @param {Array<Array<number>>} signatures - Array of valid signatures for file type
* @returns {boolean} True if buffer match with any of signatures, false otherwise
*/
const checkFileSignature = (buffer, signatures) => {
return signatures.some((signature) => {
return signature.every((byte, index) => buffer[index] === byte);
});
};
/**
* Validates content of a file by checking its signature and scanning for suspicious patterns.
*
* @param {string} filePath - Path to the file to validate
* @returns {Promise<Object>} Object containing status (boolean) and message (string)
*/
const validateFileContent = async (filePath) => {
try {
// Read file buffer and get extension
const fileBuffer = await fs.readFile(filePath);
const fileExtension = path.extname(filePath).toLowerCase();
// Convert buffer to different string formats for content checking
const fileContent = fileBuffer.toString();
const base64Content = fileBuffer.toString("base64");
const decodedContent = Buffer.from(base64Content, "base64").toString(
"utf8"
);
// Patterns that might indicate malicious content
const suspiciousPatterns = [
/<script/i, // Scripting tags
/javascript:/i, // JavaScript protocol
/<\?php/i, // PHP code
/eval\(/i, // JavaScript eval
/exec\(/i, // Command execution
/system\(/i, // System commands
/function\s*\(/i, // Function declarations
/setTimeout/i, // JavaScript timing functions
/setInterval/i, // JavaScript timing functions
/onload/i, // Event handlers
/onerror/i, // Error handlers
/ActiveXObject/i, // ActiveX objects
];
// Validate file signatures based on extension
let isValidSignature = false;
switch (fileExtension) {
case ".jpg":
case ".jpeg":
isValidSignature = checkFileSignature(fileBuffer, FILE_SIGNATURES.JPEG);
break;
case ".png":
isValidSignature = checkFileSignature(fileBuffer, FILE_SIGNATURES.PNG);
break;
case ".gif":
isValidSignature = checkFileSignature(fileBuffer, FILE_SIGNATURES.GIF);
break;
case ".svg":
isValidSignature = checkFileSignature(fileBuffer, FILE_SIGNATURES.SVG);
// SVG validation
const hasSVGTag = /<svg[^>]*>/i.test(fileContent);
const hasValidXML =
fileContent.trim().startsWith("<?xml") ||
fileContent.trim().startsWith("<svg");
isValidSignature = isValidSignature || (hasSVGTag && hasValidXML);
break;
case ".pdf":
isValidSignature = checkFileSignature(fileBuffer, FILE_SIGNATURES.PDF);
// PDF validation
const hasPDFSignature = fileContent.includes("%PDF-");
const hasEOFMarker = fileContent.includes("%%EOF");
isValidSignature = isValidSignature && hasPDFSignature && hasEOFMarker;
break;
default:
return {
status: false,
message: "Unsupported file type",
};
}
// Check if file signature is valid
if (!isValidSignature) {
return {
status: false,
message: "Invalid file signature detected",
};
}
// Check for suspicious patterns in content
for (const pattern of suspiciousPatterns) {
if (pattern.test(decodedContent) || pattern.test(fileContent)) {
return {
status: false,
message: `Suspicious pattern detected: ${pattern}`,
};
}
}
// SVG-specific security checks
if (fileExtension === ".svg") {
const svgSuspiciousPatterns = [
/xlink:href/i, // External references
/[^a-z]href=/i, // Hyperlinks
/data:/i, // Data URLs
/import/i, // Import statements
/foreignObject/i, // Foreign objects
/onload/i, // Event handlers
/onclick/i, // Click handlers
/onmouseover/i, // Mouse event handlers
/<!ENTITY/i, // XML entities
/<!DOCTYPE/i, // DOCTYPE declarations
];
for (const pattern of svgSuspiciousPatterns) {
if (pattern.test(fileContent)) {
return {
status: false,
message: `Suspicious SVG pattern detected: ${pattern}`,
};
}
}
}
// PDF-specific security checks
if (fileExtension === ".pdf") {
const pdfSuspiciousPatterns = [
/OpenAction/, // Automatic actions
/JavaScript/, // JavaScript code
/JS/, // JavaScript abbreviation
/Launch/, // Launch actions
/EmbeddedFile/, // Embedded files
/XFA/, // XML Forms Architecture
/Annots/,
/Metadata/,
];
for (const pattern of pdfSuspiciousPatterns) {
if (pattern.test(fileContent)) {
return {
status: false,
message: `Suspicious PDF pattern detected: ${pattern}`,
};
}
}
}
return {
status: true,
message: "Content validation passed",
};
} catch (error) {
return {
status: false,
message: `Content validation failed: ${error.message}`,
};
}
};
/**
* Main file validation function that checks file existence, size, extension, and content.
*
* @param {string} filePath - Path of file to validate
* @param {Object} options - Validation options
* @param {number} [options.maxSizeInBytes=5242880] - Maximum file size in bytes (default: 5MB)
* @returns {Promise<Object>} Object containing status (boolean) and message (string)
*/
const validateFile = async (filePath, options = {}) => {
try {
// Default size limit: 5MB
const DEFAULT_MAX_SIZE = 5 * 1024 * 1024;
const maxSizeInBytes = options.maxSizeInBytes ?? DEFAULT_MAX_SIZE;
// Check if file exists
const stats = await fs.stat(filePath);
// Check file size with configurable limit
if (stats.size > maxSizeInBytes) {
const sizeMB = Math.round(maxSizeInBytes / (1024 * 1024));
return {
status: false,
message: `File size exceeds limit of ${sizeMB}MB`,
};
}
// Rest of the validation code remains the same...
const allowedExtensions = [".jpg", ".jpeg", ".png", ".gif", ".pdf", ".svg"];
const fileExtension = path.extname(filePath).toLowerCase();
if (!allowedExtensions.includes(fileExtension)) {
return {
status: false,
message: "Invalid file extension",
};
}
const contentValidation = await validateFileContent(filePath);
return contentValidation;
} catch (error) {
return {
status: false,
message: `File validation failed: ${error.message}`,
};
}
};
export { validateFile, validateFileContent, checkFileSignature };