Skip to content

Commit

Permalink
validate
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkav committed Jan 9, 2024
1 parent 7eb584a commit 92b64bd
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions lib/routes/bins/update.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
const debug = require("debug")("mockbin");
const validate = require("har-validator");
const path = require("path");

module.exports = function (req, res, next) {
const id = req.params.uuid;
const path = req.params[0];
const compoundId = id + path;

let mock = req.jsonBody;
if (!mock) {
res.body = {
errors: "Response HAR is required",
};
next();
}

// overritten by application/x-www-form-urlencoded or multipart/form-data
if (req.simple.postData.text) {
Expand All @@ -19,6 +17,26 @@ module.exports = function (req, res, next) {
debug(e);
}
}
if (!mock) {
res.status(400);
res.body = {
errors: "Response HAR is required",
};
next();
return;
}

const isAlphanumericAndSlashes = /^[a-zA-Z0-9\/]+$/i.test(path);
const isPathSupported = isAlphanumericAndSlashes && !path.includes("//");

if (path && !isPathSupported) {
res.status(400);
res.body = {
errors: `Unsupported path ${path}`,
};
next();
return;
}

// provide optional values before validation
mock.redirectURL = "";
Expand All @@ -35,13 +53,6 @@ module.exports = function (req, res, next) {
.response(mock)
.then(
function () {
const path = req.params[0];
const isPathSupported =
/^[a-zA-Z0-9\/]+$/i.test(path) && !path.includes("//");
if (path && !isPathSupported) {
throw new Error(`Unsupported path: ${path}`);
}
const compoundId = id + path;
this.client.set(`bin:${compoundId}`, JSON.stringify(mock));

res.view = "redirect";
Expand Down

0 comments on commit 92b64bd

Please sign in to comment.