Skip to content

Commit

Permalink
feat(webserver): Attempt to block external access to valetudo
Browse files Browse the repository at this point in the history
thanks @ccoors
  • Loading branch information
Hypfer committed Dec 18, 2021
1 parent b306398 commit 10b1662
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 1 deletion.
3 changes: 3 additions & 0 deletions backend/lib/doc/Configuration.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
},
"basicAuth": {
"$ref": "#/components/schemas/BasicAuthConfigDTO"
},
"blockExternalAccess": {
"type": "boolean"
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion backend/lib/res/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"enabled": false,
"username": "valetudo",
"password": "valetudo"
}
},
"blockExternalAccess": true
},
"zonePresets": {},
"goToLocationPresets": {},
Expand Down
6 changes: 6 additions & 0 deletions backend/lib/webserver/WebServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class WebServer {
this.app.use(bodyParser.json());

this.app.disable("x-powered-by");


if (this.webserverConfig.blockExternalAccess) {
this.app.use(Middlewares.ExternalAccessCheckMiddleware);
}

this.app.use(Middlewares.CSPMiddleware);
this.app.use(Middlewares.VersionMiddleware);
this.app.use(Middlewares.ServerMiddleware);
Expand Down
18 changes: 18 additions & 0 deletions backend/lib/webserver/middlewares/ExternalAccessCheckMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const isInSubnet = require("is-in-subnet");
const Logger = require("../../Logger");

/**
*
* @param {object} req
* @param {object} res
* @param {Function} next
*/
module.exports = function checkExternalAccess(req, res, next) {
if (!isInSubnet.isPrivate(req.ip) && !isInSubnet.isLocalhost(req.ip)) {
Logger.warn(`Blocked external request to ${req.url} from ${req.ip}`);

res.status(401).send("External access to Valetudo is blocked.");
} else {
next();
}
};
1 change: 1 addition & 0 deletions backend/lib/webserver/middlewares/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
CSPMiddleware: require("./CSPMiddleware"),
ExternalAccessCheckMiddleware: require("./ExternalAccessCheckMiddleware"),
ServerMiddleware: require("./ServerMiddleware"),
VersionMiddleware: require("./VersionMiddleware")
};
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"express-dynamic-middleware": "1.0.0",
"express-list-endpoints": "6.0.0",
"express-rate-limit": "5.5.1",
"is-in-subnet": "4.0.1",
"mqtt": "4.2.8",
"nested-object-assign": "1.0.4",
"nested-property": "4.0.0",
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 10b1662

Please sign in to comment.