Skip to content

Commit 213c949

Browse files
committed
fix: login limiter, make it opt it by default
1 parent f17067f commit 213c949

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,12 @@ Advanced manual setups are also supported. Check the following environment varia
165165
| ZU_DEFAULT_USERNAME | unset (`docker-compose.yml`: admin) | Default username that will be set on the first run |
166166
| ZU_DEFAULT_PASSWORD | unset (`docker-compose.yml`: zero-ui) | Default password that will be set on the first run |
167167
| ZU_DATAPATH | `data/db.json` | ZeroUI data storage path |
168-
| ZU_DISABLE_AUTH | unset | If set to true, automatically log in all users. This is useful if ZeroUI is protected by an authentication proxy. Note that when this value is changed, the localStorage of instances of logged-in panels should be cleared |
168+
| ZU_DISABLE_AUTH | `false` | If set to true, automatically log in all users. This is useful if ZeroUI is protected by an authentication proxy. Note that when this value is changed, the localStorage of instances of logged-in panels should be cleared |
169169
| ZU_LAST_SEEN_FETCH | `true`| Enables [Last Seen feature](https://github.com/dec0dOS/zero-ui/issues/40) |
170170
| ZU_LAST_SEEN_SCHEDULE | `*/5 * * * *` | Last Seen cron-like schedule |
171+
| ZU_LOGIN_LIMIT | `false` | Enable rate limiter for /login endpoint |
171172
| ZU_LOGIN_LIMIT_WINDOW | 30 | The duration of the IP ban in minutes |
172-
| ZT_LOGIN_LIMIT_ATTEMPTS | 50 | Login attemps before ban |
173+
| ZU_LOGIN_LIMIT_ATTEMPTS | 50 | Login attemps before ban |
173174

174175
ZeroUI could be deployed as a regular nodejs web application, but it requires a ZeroTier controller that is installed with the `zerotier-one` package. For more info about the network controller, you could read [here](https://github.com/zerotier/ZeroTierOne/tree/master/controller/#readme).
175176

backend/routes/auth.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
import express from "express";
22
import rateLimit from "express-rate-limit";
3+
34
const router = express.Router();
45

56
import * as auth from "../services/auth.js";
67

78
const loginLimiter = rateLimit({
89
windowMs: (Number(process.env.ZU_LOGIN_LIMIT_WINDOW) || 30) * 60 * 1000, // 30 minutes
9-
max: Number(process.env.ZT_LOGIN_LIMIT_ATTEMPTS) || 50, // limit each IP to 50 requests per windowMs
10+
max: Number(process.env.ZU_LOGIN_LIMIT_ATTEMPTS) || 50, // limit each IP to 50 requests per windowMs
1011
message: {
1112
status: 429,
1213
error: "Too many login attempts, please try again in 15 minutes.",
1314
},
1415
});
1516

17+
const loginLimiterWrapper = (req, res, next) => {
18+
if (
19+
process.env.NODE_ENV === "production" &&
20+
process.env.ZU_LOGIN_LIMIT === "true"
21+
) {
22+
return loginLimiter(req, res, next);
23+
} else {
24+
return next();
25+
}
26+
};
27+
1628
router.get("/login", async function (req, res) {
1729
if (process.env.ZU_DISABLE_AUTH === "true") {
1830
res.send({ enabled: false });
@@ -21,7 +33,7 @@ router.get("/login", async function (req, res) {
2133
}
2234
});
2335

24-
router.post("/login", loginLimiter, async function (req, res) {
36+
router.post("/login", loginLimiterWrapper, async function (req, res) {
2537
if (req.body.username && req.body.password) {
2638
auth.authorize(req.body.username, req.body.password, function (err, user) {
2739
if (user) {

0 commit comments

Comments
 (0)