Skip to content

Commit fad8fba

Browse files
author
Manny
committed
Framework fix for open redirect vulnerability
1 parent ec0db45 commit fad8fba

File tree

4 files changed

+91
-28
lines changed

4 files changed

+91
-28
lines changed

core/Web.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ class Web {
122122
}
123123

124124
this.app = express();
125+
126+
fixOpenRedirect(this);
127+
125128
this.events = {};
126129
this.modelCache = new Object();
127130
this.plugins = [];
@@ -763,6 +766,42 @@ function defaultRedirectToHttpsMiddleware(req, res) {
763766
res.end();
764767
}
765768

769+
function fixOpenRedirect(web) {
770+
// Fix for open redirect security
771+
let redirectSafe = web.app.response.redirect;
772+
web.app.response.redirectSafe = function(url) {
773+
return redirectSafe.call(this, url);
774+
}
775+
776+
var addHostOnceFlag = true;
777+
778+
web.app.response.redirect = function(url) {
779+
780+
if (url.indexOf('://') != -1) {
781+
782+
let req = this.req;
783+
784+
if (addHostOnceFlag) {
785+
var host = req.protocol + '://' + req.headers.host;
786+
web.conf.allowedRedirectHosts.push(host);
787+
addHostOnceFlag = false;
788+
console.log("Added host once: " + host);
789+
}
790+
791+
const found = web.conf.allowedRedirectHosts.find(el => url.indexOf(el) == 0);
792+
793+
if (!found) {
794+
var ip = web.utils.getClientIp(req);
795+
796+
console.warn("Open redirect was triggered: ", req.method, req.user ? req.user.email : "unsigned user", ip, "accessed", req.url, req.headers['user-agent']);
797+
throw new Error("Action not allowed.");
798+
}
799+
800+
}
801+
return redirectSafe.call(this, url);
802+
}
803+
}
804+
766805

767806
function startServer(web, cb) {
768807

core/conf/conf-default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = function(webSel) {
1111

1212
dataDir: 'data',
1313
tmpDir: 'data/tmp',
14+
allowedRedirectHosts: [],
1415

1516
extendWeb: {
1617
enabled: true,

package-lock.json

Lines changed: 49 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "oils",
3-
"version": "7.8.6",
3+
"version": "8.0.0",
44
"description": "A slightly opinionated web framework built on top of Express 4.",
55
"keywords": [
66
"OilsJs",
@@ -41,7 +41,7 @@
4141
"method-override": "~3.0.0",
4242
"moment": "~2.24.0",
4343
"moment-timezone": "^0.5.27",
44-
"mongoose": "^5.10.6",
44+
"mongoose": "^5.12.2",
4545
"nanoid": "^3.1.20",
4646
"nomnom": "1.8.x",
4747
"nunjucks": "^3.2.1",

0 commit comments

Comments
 (0)