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

add denylist for redirection to avoid open redirects #173

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading