Skip to content

Commit 4d4360e

Browse files
Fix code scanning alert no. 10: Reflected cross-site scripting (#822)
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 292f554 commit 4d4360e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

samples/javascript/whiteboard/server.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ app
7979
if (!allowedMimeTypes.includes(file.mimetype)) {
8080
return res.status(400).send('Invalid file type.');
8181
}
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+
}
8293
diagram.background = {
8394
id: Math.random().toString(36).substr(2, 8),
8495
data: file.data,
@@ -93,7 +104,8 @@ app
93104
.get('/background/:id', (req, res) => {
94105
if (diagram.background && diagram.background.id === req.params.id) {
95106
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');
97109
res.send(diagram.background.data);
98110
} else res.status(404).end();
99111
});

0 commit comments

Comments
 (0)