Skip to content

Commit 7d9583f

Browse files
committed
fix code scanner issues
1 parent 5a61773 commit 7d9583f

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

dev-server/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ if (!fs.existsSync(distPath)) {
2323

2424
const app = express();
2525

26+
// Rate limit to 100 req/min per IP
27+
const rateLimit = new Map();
28+
setInterval(() => rateLimit.clear(), 60000); // Clear every minute to prevent memory growth
29+
app.use((req, res, next) => {
30+
const ip = req.ip;
31+
const count = (rateLimit.get(ip) || 0) + 1;
32+
rateLimit.set(ip, count);
33+
if (count > 100) return res.status(429).send("Too many requests");
34+
next();
35+
});
36+
2637
app.use(
2738
cors({
2839
origin: (origin, callback) => {
@@ -86,7 +97,12 @@ app.post("/upload", function (req, res) {
8697
// Sanitize filename to prevent path traversal
8798
const newFileName = path.basename(uploadedFile.originalFilename);
8899
const fileSavePath = __dirname;
89-
const newFilePath = path.join(fileSavePath, newFileName);
100+
const newFilePath = path.resolve(fileSavePath, newFileName);
101+
102+
// Verify path is within allowed directory
103+
if (!newFilePath.startsWith(fileSavePath + path.sep)) {
104+
return res.status(400).json({ success: false, message: "Invalid filename" });
105+
}
90106

91107
// Move the uploaded file to the desired directory
92108
fs.rename(uploadedFile.filepath, newFilePath, (err) => {

samples/scenarios/image-file-scanning.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ <h1>Scan An Existing Image</h1>
7575
}
7676
} catch (ex) {
7777
console.error("File scan error:", ex);
78-
resultContainer.innerHTML = `<p>Error: ${ex?.message || ex}</p>`;
78+
const p = document.createElement("p");
79+
p.textContent = `Error: ${ex?.message || ex}`;
80+
resultContainer.replaceChildren(p);
7981
}
8082
};
8183
</script>

0 commit comments

Comments
 (0)