|
79 | 79 | if (!allowedMimeTypes.includes(file.mimetype)) { |
80 | 80 | return res.status(400).send('Invalid file type.'); |
81 | 81 | } |
| 82 | + // Validate file content (e.g., check for valid image headers) |
| 83 | + const isValidImage = (data) => { |
| 84 | + // Simple check for JPEG, PNG, GIF headers |
| 85 | + const jpegHeader = Buffer.from([0xff, 0xd8, 0xff]); |
| 86 | + const pngHeader = Buffer.from([0x89, 0x50, 0x4e, 0x47]); |
| 87 | + const gifHeader = Buffer.from([0x47, 0x49, 0x46, 0x38]); |
| 88 | + return data.slice(0, 3).equals(jpegHeader) || data.slice(0, 4).equals(pngHeader) || data.slice(0, 3).equals(gifHeader); |
| 89 | + }; |
| 90 | + if (!isValidImage(file.data)) { |
| 91 | + return res.status(400).send('Invalid file content.'); |
| 92 | + } |
82 | 93 | diagram.background = { |
83 | 94 | id: Math.random().toString(36).substr(2, 8), |
84 | 95 | data: file.data, |
|
93 | 104 | .get('/background/:id', (req, res) => { |
94 | 105 | if (diagram.background && diagram.background.id === req.params.id) { |
95 | 106 | res.setHeader('Content-Disposition', 'attachment; filename="background"'); |
96 | | - res.type(diagram.background.contentType); |
| 107 | + res.setHeader('Content-Type', diagram.background.contentType); |
| 108 | + res.setHeader('X-Content-Type-Options', 'nosniff'); |
97 | 109 | res.send(diagram.background.data); |
98 | 110 | } else res.status(404).end(); |
99 | 111 | }); |
|
0 commit comments