homebridge-openwrt-control 0.3.9 → 0.3.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  - After update to v0.1.0 the plugin need to be reconfigured!
11
11
 
12
+ ## [0.3.10] - (24.09.2026)
13
+
14
+ ### Changes
15
+
16
+ - added: RESTFul request rate limit, 600 requests per minute per client and at most 1000 clients per minute, answered with `429` and `Retry-After`, one log warning per client and minute.
17
+
12
18
  ## [0.3.9] - (23.09.2026)
13
19
 
14
20
  ### Changes
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "OpenWrt Control",
3
3
  "name": "homebridge-openwrt-control",
4
- "version": "0.3.9",
4
+ "version": "0.3.10",
5
5
  "description": "Homebridge plugin to control OpenWrt flashed devices.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
package/src/restful.js CHANGED
@@ -23,6 +23,49 @@ class RestFul extends EventEmitter {
23
23
  try {
24
24
  const app = express();
25
25
  app.set('json spaces', 2);
26
+
27
+ // Request rate limit per client, protects Homebridge from request floods (CWE-770)
28
+ const rateLimitWindow = 60 * 1000;
29
+ const rateLimitMax = 600;
30
+ const rateLimitClients = 1000;
31
+ const clients = new Map();
32
+ let clientsLimitWarned = 0;
33
+ app.use((req, res, next) => {
34
+ const now = Date.now();
35
+
36
+ // Drop expired windows so the map cannot grow without limit
37
+ if (clients.size > 100) {
38
+ for (const [ip, client] of clients) {
39
+ if (now - client.start >= rateLimitWindow) clients.delete(ip);
40
+ }
41
+ }
42
+
43
+ let client = clients.get(req.ip);
44
+ if (!client || now - client.start >= rateLimitWindow) {
45
+ // Hard limit of tracked addresses, new addresses wait until older windows expire
46
+ if (!client && clients.size >= rateLimitClients) {
47
+ if (now - clientsLimitWarned >= rateLimitWindow) {
48
+ clientsLimitWarned = now;
49
+ if (this.logWarn) this.emit('warn', `RESTFul too many clients (${rateLimitClients}) in one minute, new clients are rejected until the window ends`);
50
+ }
51
+ res.set('Retry-After', String(Math.ceil(rateLimitWindow / 1000)));
52
+ return res.status(429).json({ error: 'RESTFul Too Many Requests' });
53
+ }
54
+ client = { count: 0, start: now, warned: false };
55
+ clients.set(req.ip, client);
56
+ }
57
+
58
+ if (++client.count > rateLimitMax) {
59
+ // Warn once per client and window, a flood must not flood the log too
60
+ if (!client.warned && this.logWarn) this.emit('warn', `RESTFul rate limit ${rateLimitMax} requests per minute exceeded by: ${req.ip}, further requests are rejected until the window ends`);
61
+ client.warned = true;
62
+ res.set('Retry-After', String(Math.ceil((client.start + rateLimitWindow - now) / 1000)));
63
+ return res.status(429).json({ error: 'RESTFul Too Many Requests' });
64
+ }
65
+
66
+ next();
67
+ });
68
+
26
69
  app.use(json());
27
70
 
28
71
  // Optional token auth, when a token is configured every route requires "Authorization: Bearer <token>"