Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
add denylist for paths
Browse files Browse the repository at this point in the history
  • Loading branch information
enekofb committed Oct 31, 2023
1 parent 14254f9 commit c00be49
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
19 changes: 17 additions & 2 deletions helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,25 @@
res.end();
}

// list of invalid redirect paths
const denylist = ['//'];

const isInvalidRedirectPath = (redirectPath) => {
return denylist.some((denylistItem) => redirectPath.includes(denylistItem));
}

/* Rewrites and redirects any url that doesn't end with a slash. */
helpers.rewriteSlash = function(req, res, next) {
if(req.url.substr(-1) == '/' && req.url.length > 1)
res.redirect(301, req.url.slice(0, -1));
if(req.url.substr(-1) == '/' && req.url.length > 1){
var redirectPath = req.url.slice(0, -1);

if (isInvalidRedirectPath(redirectPath)) {
res.status(400).send('invalid URL to redirect to');
}else {
console.log("redirecting")
res.redirect(301, redirectPath );
}
}
else
next();
}
Expand Down
30 changes: 30 additions & 0 deletions test/helpers_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,36 @@
app.use(bodyParser.json());
});

describe("#redirectHandler", function() {
it("should return bad request if invalid redirection", function(done) {
app.use(function(req, res) {
helpers.rewriteSlash(req, res, done);
});

chai.request(app).
get("//category.html/").
end(function(err, res) {
expect(res).to.have.status(400);
done();
});
});

it("should redirect if valid redirection", function(done) {
app.use(function(req, res) {
helpers.rewriteSlash(req, res, done);
});

chai.request(app).
get("/category.html/").
end(function(err, res) {
expect(res).to.have.status(301);
done();
});
});

});


describe("#errorHandler", function() {
var message, code, error, res, resErr;

Expand Down

0 comments on commit c00be49

Please sign in to comment.