Skip to content

Commit

Permalink
Improve security by only allowing valid adapter names
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamSwiss committed Jan 17, 2025
1 parent 62f3e0a commit b0ed584
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions express/backend/src/api/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const router = Router();
router.get("/api/adapter/:name/stats/now", async function (req, res) {
try {
const { name } = req.params;
if (!isValidAdapterName(name)) {
res.status(404).send("Adapter not found");
return;
}

const db = await dbConnect();
const rawStatistics = db.rawStatistics();

Expand All @@ -23,7 +28,7 @@ router.get("/api/adapter/:name/stats/now", async function (req, res) {
.limit(1)
.toArray();
if (stats.length === 0) {
res.status(404).send(`Adapter ${name} not found`);
res.status(404).send("Adapter not found");
return;
}

Expand All @@ -42,6 +47,10 @@ router.get("/api/adapter/:name/stats/now", async function (req, res) {
router.get("/api/adapter/:name/stats/history", async function (req, res) {
try {
const { name } = req.params;
if (!isValidAdapterName(name)) {
res.status(404).send("Adapter not found");
return;
}
const db = await dbConnect();
const rawStatistics = db.rawStatistics();
const repoAdapters = db.repoAdapters();
Expand Down Expand Up @@ -96,7 +105,7 @@ router.get("/api/adapter/:name/stats/history", async function (req, res) {

console.log(result);
if (Object.keys(result.counts).length === 0) {
res.status(404).send(`Adapter ${name} not found`);
res.status(404).send("Adapter not found");
return;
}

Expand All @@ -107,4 +116,14 @@ router.get("/api/adapter/:name/stats/history", async function (req, res) {
}
});

function isValidAdapterName(name: string) {
const forbiddenChars = /[^a-z0-9\-_]/g;
if (forbiddenChars.test(name)) {
return false;
}

// the name must start with a letter
return /^[a-z]/.test(name);
}

export default router;

0 comments on commit b0ed584

Please sign in to comment.