Skip to content

Commit

Permalink
Fix open redirect abuse via "strip trailing slash" middleware
Browse files Browse the repository at this point in the history
Fixes #231.
  • Loading branch information
Krinkle committed Sep 9, 2024
1 parent be93160 commit 3c97d3d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"date-utils": "~1.2.21",
"ent": "~2.2.0",
"errorhandler": "~1.5.0",
"express": "~4.16.3",
"express": "~4.19.2",
"grunt": "~1.6.1",
"grunt-autoprefixer": "~3.0.4",
"grunt-contrib-clean": "~1.1.0",
Expand Down
12 changes: 10 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,17 @@ app.use(bodyParser.json());
app.set('views', path.join(__dirname, 'src', 'tmpl'));
app.set('view engine', 'pug');

// strip slashes
/**
* Strip trailing slashes
*
* Redirect "/foo/" to "/foo". Browsers interpret absolute paths in Location
* as relative to the current origin.
*
* Avoid redirecting to a paths that browsers may interpret as URLs to other sites,
* such as "//foo" or "http://". https://github.com/gruntjs/gruntjs.com/issues/231
*/
app.use(function (req, res, next) {
if (req.url.substr(-1) === '/' && req.url.length > 1) {
if (req.url.startsWith('/') && !req.url.startsWith('//') && req.url.length >= 2 && req.url.slice(-1) === '/') {
res.redirect(301, req.url.slice(0, -1));
} else {
next();
Expand Down

0 comments on commit 3c97d3d

Please sign in to comment.