roster-server 2.4.6 → 2.4.8

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/README.md CHANGED
@@ -11,6 +11,7 @@ Welcome to **RosterServer**, the ultimate domain host router with automatic HTTP
11
11
  - **Static Sites**: No code? No problem. Drop a folder with `index.html` (and assets) and RosterServer serves it automatically—modular static handler with path-traversal protection and strict 404s.
12
12
  - **Virtual Hosting**: Serve multiple domains from a single server.
13
13
  - **Automatic Redirects**: Redirect `www` subdomains to the root domain.
14
+ - **Optional Request Plugins**: Run synchronous filters before site handlers, including the bundled scanner blocker.
14
15
  - **Zero Configuration**: Well, almost zero. Just a tiny bit of setup.
15
16
  - **Bun compatible**: Works with both Node.js and [Bun](https://bun.sh).
16
17
 
@@ -114,6 +115,34 @@ const server = new Roster(options);
114
115
  server.start();
115
116
  ```
116
117
 
118
+ ### Blocking vulnerability scanners
119
+
120
+ RosterServer includes an optional request plugin that rejects common PHP, WordPress, repository, and sensitive-file probes before they reach a site handler. Suspicious paths always receive a `404`; after the configured number of strikes, every request from that client is rejected until the ban expires.
121
+
122
+ ```javascript
123
+ import Roster from 'roster-server';
124
+ import { createScannerBlocker } from 'roster-server/plugins/scanner-blocker.js';
125
+
126
+ const roster = new Roster(options);
127
+
128
+ roster.use(createScannerBlocker({
129
+ windowMs: 60_000,
130
+ strikeThreshold: 3,
131
+ banDurationMs: 15 * 60_000,
132
+ maxTrackedClients: 10_000,
133
+ trustProxy: false,
134
+ onBlock(event) {
135
+ // Send event to the application's existing logger if desired.
136
+ }
137
+ }));
138
+
139
+ roster.start();
140
+ ```
141
+
142
+ All operational values are required. Keep `trustProxy: false` when RosterServer receives traffic directly. Set it to `true` only when a trusted reverse proxy overwrites `X-Forwarded-For`; otherwise clients can spoof the address used for bans.
143
+
144
+ The in-memory strike and ban state is bounded by `maxTrackedClients`, belongs to one RosterServer process, and is cleared on restart. Use the optional `onBlock(event)` callback to feed a shared firewall or Fail2ban when bans must persist or span multiple workers. Routes such as `/atom` and `/articles/config` are not classified as scanner probes.
145
+
117
146
  ### Your Site Handlers
118
147
 
119
148
  Each domain has its own folder under `www`. You can use:
@@ -426,6 +455,10 @@ Loads sites, generates SSL config (production), creates VirtualServers and initi
426
455
 
427
456
  Returns the Host-header dispatch function for a given port (defaults to `defaultPort`). Handles www→non-www redirects, wildcard matching, and VirtualServer dispatch.
428
457
 
458
+ #### `roster.use(plugin)` → `Roster`
459
+
460
+ Registers a synchronous request plugin and returns `this`. Plugins receive `(req, res, { host, domain })`; return `true` after sending a response to stop dispatch, or `false`/`undefined` to continue. Plugins run in registration order before redirects and site handlers.
461
+
429
462
  #### `roster.upgradeHandler(port?)` → `(req, socket, head) => void`
430
463
 
431
464
  Returns the WebSocket upgrade dispatcher for a given port. Routes upgrades to the correct VirtualServer.
@@ -493,4 +526,4 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
493
526
 
494
527
  ---
495
528
 
496
- Happy hosting! 🎈
529
+ Happy hosting! 🎈
package/index.js CHANGED
@@ -247,6 +247,7 @@ class Roster {
247
247
  this.local = options.local || false;
248
248
  this.domains = [];
249
249
  this.sites = {};
250
+ this.plugins = [];
250
251
  this.wildcardZones = new Set(); // Root domains that have a wildcard site (e.g. "example.com" for *.example.com)
251
252
  this.domainServers = {}; // Store separate servers for each domain
252
253
  this.portServers = {}; // Store servers by port
@@ -540,6 +541,11 @@ class Roster {
540
541
 
541
542
  handleRequest(req, res) {
542
543
  const host = req.headers.host || '';
544
+ const hostWithoutPort = host.split(':')[0];
545
+ const normalizedHost = hostWithoutPort.toLowerCase();
546
+ const domain = normalizedHost.startsWith('www.') ? normalizedHost.slice(4) : normalizedHost;
547
+
548
+ if (this._runRequestPlugins(req, res, { host: normalizedHost, domain })) return;
543
549
 
544
550
  if (host.startsWith('www.')) {
545
551
  const newHost = host.slice(4);
@@ -548,7 +554,6 @@ class Roster {
548
554
  return;
549
555
  }
550
556
 
551
- const hostWithoutPort = host.split(':')[0];
552
557
  const siteApp = this.getHandlerForHost(hostWithoutPort);
553
558
  if (siteApp) {
554
559
  siteApp(req, res);
@@ -597,6 +602,25 @@ class Roster {
597
602
  return this;
598
603
  }
599
604
 
605
+ use(plugin) {
606
+ if (typeof plugin !== 'function') {
607
+ throw new Error('plugin must be a function');
608
+ }
609
+ this.plugins.push(plugin);
610
+ return this;
611
+ }
612
+
613
+ _runRequestPlugins(req, res, context) {
614
+ for (const plugin of this.plugins) {
615
+ const handled = plugin(req, res, context);
616
+ if (handled && typeof handled.then === 'function') {
617
+ throw new Error('Request plugins must be synchronous');
618
+ }
619
+ if (handled === true) return true;
620
+ }
621
+ return false;
622
+ }
623
+
600
624
  parseDomainWithPort(domainString) {
601
625
  const parts = domainString.split(':');
602
626
  if (parts.length === 2) {
@@ -737,6 +761,8 @@ class Roster {
737
761
  const hostWithoutPort = host.split(':')[0].toLowerCase();
738
762
  const domain = hostWithoutPort.startsWith('www.') ? hostWithoutPort.slice(4) : hostWithoutPort;
739
763
 
764
+ if (this._runRequestPlugins(req, res, { host: hostWithoutPort, domain })) return;
765
+
740
766
  if (hostWithoutPort.startsWith('www.')) {
741
767
  const protocol = this.local ? 'http' : 'https';
742
768
  res.writeHead(301, { Location: `${protocol}://${domain}${req.url}` });
@@ -1062,6 +1088,8 @@ class Roster {
1062
1088
  const appHandler = portData.appHandlers[domain];
1063
1089
 
1064
1090
  const dispatcher = (req, res) => {
1091
+ const host = (req.headers.host || '').split(':')[0].toLowerCase();
1092
+ if (this._runRequestPlugins(req, res, { host, domain })) return;
1065
1093
  virtualServer.fallbackHandler = appHandler;
1066
1094
  if (virtualServer.requestListeners.length > 0) {
1067
1095
  virtualServer.processRequest(req, res);
@@ -1321,4 +1349,4 @@ module.exports.wildcardRoot = wildcardRoot;
1321
1349
  module.exports.hostMatchesWildcard = hostMatchesWildcard;
1322
1350
  module.exports.wildcardSubjectForHost = wildcardSubjectForHost;
1323
1351
  module.exports.buildCertLookupCandidates = buildCertLookupCandidates;
1324
- module.exports.certCoversName = certCoversName;
1352
+ module.exports.certCoversName = certCoversName;
package/package.json CHANGED
@@ -1,53 +1,53 @@
1
1
  {
2
- "name": "roster-server",
3
- "version": "2.4.6",
4
- "description": "👾 RosterServer - A domain host router to host multiple HTTPS.",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "node --test 'test/**/*.test.js'"
8
- },
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://github.com/clasen/RosterServer.git"
12
- },
13
- "keywords": [
14
- "greenlock",
15
- "https",
16
- "domain",
17
- "multi-domain",
18
- "letsencrypt",
19
- "hosting",
20
- "ssl",
21
- "host",
22
- "vhost",
23
- "virtual-host",
24
- "socket.io",
25
- "websocket",
26
- "express",
27
- "greenlock-express",
28
- "shotx",
29
- "bun",
30
- "clasen"
31
- ],
32
- "author": "Martin Clasen",
33
- "license": "MIT",
34
- "bugs": {
35
- "url": "https://github.com/clasen/RosterServer/issues"
36
- },
37
- "homepage": "https://github.com/clasen/RosterServer#readme",
38
- "dependencies": {
39
- "@greenlock/manager": "^3.1.0",
40
- "@root/acme": "^3.1.0",
41
- "@root/csr": "^0.8.1",
42
- "@root/keypairs": "^0.10.0",
43
- "@root/mkdirp": "^1.0.0",
44
- "@root/request": "^1.6.1",
45
- "acme-dns-01-cli": "^3.0.7",
46
- "acme-http-01-standalone": "^3.0.5",
47
- "cert-info": "^1.5.1",
48
- "greenlock-store-fs": "^3.2.2",
49
- "lemonlog": "^1.2.0",
50
- "redirect-https": "^1.3.1",
51
- "safe-replace": "^1.1.0"
52
- }
53
- }
2
+ "name": "roster-server",
3
+ "version": "2.4.8",
4
+ "description": "👾 RosterServer - A domain host router to host multiple HTTPS.",
5
+ "main": "index.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/clasen/RosterServer.git"
9
+ },
10
+ "keywords": [
11
+ "greenlock",
12
+ "https",
13
+ "domain",
14
+ "multi-domain",
15
+ "letsencrypt",
16
+ "hosting",
17
+ "ssl",
18
+ "host",
19
+ "vhost",
20
+ "virtual-host",
21
+ "socket.io",
22
+ "websocket",
23
+ "express",
24
+ "greenlock-express",
25
+ "shotx",
26
+ "bun",
27
+ "clasen"
28
+ ],
29
+ "author": "Martin Clasen",
30
+ "license": "MIT",
31
+ "bugs": {
32
+ "url": "https://github.com/clasen/RosterServer/issues"
33
+ },
34
+ "homepage": "https://github.com/clasen/RosterServer#readme",
35
+ "dependencies": {
36
+ "@greenlock/manager": "^3.1.0",
37
+ "@root/acme": "^3.1.0",
38
+ "@root/csr": "^0.8.1",
39
+ "@root/keypairs": "^0.10.0",
40
+ "@root/mkdirp": "^1.0.0",
41
+ "@root/request": "^1.6.1",
42
+ "acme-dns-01-cli": "^3.0.7",
43
+ "acme-http-01-standalone": "^3.0.5",
44
+ "cert-info": "^1.5.1",
45
+ "greenlock-store-fs": "^3.2.2",
46
+ "lemonlog": "^1.2.0",
47
+ "redirect-https": "^1.3.1",
48
+ "safe-replace": "^1.1.0"
49
+ },
50
+ "scripts": {
51
+ "test": "node --test 'test/**/*.test.js'"
52
+ }
53
+ }
@@ -0,0 +1,161 @@
1
+ 'use strict';
2
+
3
+ const PHP_PATH = /(?:^|\/)[^/]*\.php(?:$|\/)/i;
4
+ const SCANNER_PATH = /(?:^|\/)(?:\.git|\.hg|\.svn|\.ssh|\.aws|cgi-bin|server-status|vendor\/phpunit|wp-admin|wp-content|wp-includes|wp-json)(?:\/|$)/i;
5
+ const SCANNER_FILE = /(?:^|\/)(?:\.env(?:\.[^/]*)?|\.htaccess|composer\.(?:json|lock)|web\.config)(?:\/|$)/i;
6
+
7
+ function requirePositiveInteger(options, name) {
8
+ const value = options[name];
9
+ if (!Number.isSafeInteger(value) || value <= 0) {
10
+ throw new Error(`${name} must be a positive integer`);
11
+ }
12
+ return value;
13
+ }
14
+
15
+ function normalizeRequestPath(url) {
16
+ let requestPath = String(url || '/').split(/[?#]/, 1)[0];
17
+ for (let attempt = 0; attempt < 2; attempt++) {
18
+ try {
19
+ const decoded = decodeURIComponent(requestPath);
20
+ if (decoded === requestPath) break;
21
+ requestPath = decoded;
22
+ } catch {
23
+ break;
24
+ }
25
+ }
26
+ return requestPath.replace(/\\/g, '/').replace(/\/+/g, '/').toLowerCase();
27
+ }
28
+
29
+ function scannerReason(url) {
30
+ const requestPath = normalizeRequestPath(url);
31
+ if (PHP_PATH.test(requestPath)) return 'php-path';
32
+ if (SCANNER_PATH.test(requestPath)) return 'scanner-path';
33
+ if (SCANNER_FILE.test(requestPath)) return 'sensitive-file';
34
+ return null;
35
+ }
36
+
37
+ function normalizeIp(value) {
38
+ const ip = String(value || '').trim();
39
+ return ip.startsWith('::ffff:') ? ip.slice(7) : ip;
40
+ }
41
+
42
+ function clientIp(req, trustProxy) {
43
+ if (trustProxy) {
44
+ const forwardedFor = req.headers?.['x-forwarded-for'];
45
+ const firstForwardedIp = Array.isArray(forwardedFor)
46
+ ? forwardedFor[0]
47
+ : String(forwardedFor || '').split(',')[0];
48
+ const normalizedForwardedIp = normalizeIp(firstForwardedIp);
49
+ if (normalizedForwardedIp) return normalizedForwardedIp;
50
+ }
51
+ return normalizeIp(req.socket?.remoteAddress);
52
+ }
53
+
54
+ function rejectRequest(req, res) {
55
+ const body = 'Not Found';
56
+ res.writeHead(404, {
57
+ 'Content-Type': 'text/plain; charset=utf-8',
58
+ 'Content-Length': Buffer.byteLength(body),
59
+ 'Cache-Control': 'no-store'
60
+ });
61
+ res.end(req.method === 'HEAD' ? undefined : body);
62
+ }
63
+
64
+ function createScannerBlocker(options) {
65
+ if (!options || typeof options !== 'object') {
66
+ throw new Error('scanner-blocker options are required');
67
+ }
68
+
69
+ const windowMs = requirePositiveInteger(options, 'windowMs');
70
+ const strikeThreshold = requirePositiveInteger(options, 'strikeThreshold');
71
+ const banDurationMs = requirePositiveInteger(options, 'banDurationMs');
72
+ const maxTrackedClients = requirePositiveInteger(options, 'maxTrackedClients');
73
+ if (typeof options.trustProxy !== 'boolean') {
74
+ throw new Error('trustProxy must be a boolean');
75
+ }
76
+ if (options.onBlock !== undefined && typeof options.onBlock !== 'function') {
77
+ throw new Error('onBlock must be a function');
78
+ }
79
+ if (options.now !== undefined && typeof options.now !== 'function') {
80
+ throw new Error('now must be a function');
81
+ }
82
+
83
+ const trustProxy = options.trustProxy;
84
+ const onBlock = options.onBlock;
85
+ const now = options.now || Date.now;
86
+ const clients = new Map();
87
+
88
+ function removeExpiredClients(timestamp) {
89
+ for (const [ip, state] of clients) {
90
+ const banExpired = state.bannedUntil !== null && timestamp >= state.bannedUntil;
91
+ const windowExpired = state.bannedUntil === null && timestamp - state.windowStartedAt >= windowMs;
92
+ if (banExpired || windowExpired) clients.delete(ip);
93
+ }
94
+ }
95
+
96
+ function addClient(ip, state, timestamp) {
97
+ if (!clients.has(ip) && clients.size >= maxTrackedClients) {
98
+ removeExpiredClients(timestamp);
99
+ }
100
+ if (!clients.has(ip) && clients.size >= maxTrackedClients) {
101
+ clients.delete(clients.keys().next().value);
102
+ }
103
+ clients.delete(ip);
104
+ clients.set(ip, state);
105
+ }
106
+
107
+ function reportBlock(details) {
108
+ if (onBlock) onBlock(details);
109
+ }
110
+
111
+ return function scannerBlocker(req, res, context = {}) {
112
+ const timestamp = now();
113
+ const ip = clientIp(req, trustProxy);
114
+ let state = ip ? clients.get(ip) : null;
115
+
116
+ if (state && state.bannedUntil !== null) {
117
+ if (timestamp < state.bannedUntil) {
118
+ rejectRequest(req, res);
119
+ reportBlock({
120
+ type: 'banned-client',
121
+ clientIp: ip,
122
+ host: context.host || '',
123
+ url: req.url || '/',
124
+ bannedUntil: state.bannedUntil
125
+ });
126
+ return true;
127
+ }
128
+ clients.delete(ip);
129
+ state = null;
130
+ }
131
+
132
+ const reason = scannerReason(req.url);
133
+ if (!reason) return false;
134
+
135
+ let bannedUntil = null;
136
+ if (ip) {
137
+ if (!state || timestamp - state.windowStartedAt >= windowMs) {
138
+ state = { strikes: 0, windowStartedAt: timestamp, bannedUntil: null };
139
+ }
140
+ state.strikes += 1;
141
+ if (state.strikes >= strikeThreshold) {
142
+ state.bannedUntil = timestamp + banDurationMs;
143
+ }
144
+ bannedUntil = state.bannedUntil;
145
+ addClient(ip, state, timestamp);
146
+ }
147
+
148
+ rejectRequest(req, res);
149
+ reportBlock({
150
+ type: 'scanner-path',
151
+ reason,
152
+ clientIp: ip,
153
+ host: context.host || '',
154
+ url: req.url || '/',
155
+ bannedUntil
156
+ });
157
+ return true;
158
+ };
159
+ }
160
+
161
+ module.exports = { createScannerBlocker };
@@ -0,0 +1,2 @@
1
+ allowBuilds:
2
+ '@root/acme': true
@@ -172,6 +172,24 @@ const httpsServer = await worker.createServingHttpsServer({ servername: 'example
172
172
  httpsServer.listen(4336);
173
173
  ```
174
174
 
175
+ ### Pattern 8: Optional Scanner Blocking
176
+ ```javascript
177
+ const Roster = require('roster-server');
178
+ const { createScannerBlocker } = require('roster-server/plugins/scanner-blocker.js');
179
+
180
+ const roster = new Roster({ local: true, wwwPath: './www' });
181
+ roster.use(createScannerBlocker({
182
+ windowMs: 60_000,
183
+ strikeThreshold: 3,
184
+ banDurationMs: 15 * 60_000,
185
+ maxTrackedClients: 10_000,
186
+ trustProxy: false
187
+ }));
188
+ roster.start();
189
+ ```
190
+
191
+ The plugin blocks common PHP, WordPress, repository, and sensitive-file probes before site handlers. All operational values are required. Set `trustProxy: true` only behind a trusted reverse proxy that overwrites `X-Forwarded-For`. Ban state is per process and in memory; use `onBlock` to integrate a shared firewall or Fail2ban.
192
+
175
193
  ## Key Configuration Options
176
194
 
177
195
  ```javascript
@@ -234,6 +252,9 @@ Convenience: wires `requestHandler` + `upgradeHandler` onto an external `http.Se
234
252
  ### `roster.register(domain, handler)`
235
253
  Manually register a domain handler. Domain can include port: `'api.com:8443'`. For wildcards use `'*.example.com'` or `'*.example.com:8080'`.
236
254
 
255
+ ### `roster.use(plugin)`
256
+ Registers a synchronous request plugin. It receives `(req, res, { host, domain })` and stops dispatch when it returns `true`.
257
+
237
258
  ### `roster.getUrl(domain)`
238
259
  Get environment-aware URL:
239
260
  - Local mode: `http://localhost:{port}`
@@ -7,6 +7,7 @@ const fs = require('fs');
7
7
  const http = require('http');
8
8
  const os = require('os');
9
9
  const Roster = require('../index.js');
10
+ const { createScannerBlocker } = require('../plugins/scanner-blocker.js');
10
11
  const {
11
12
  wildcardRoot,
12
13
  hostMatchesWildcard,
@@ -432,6 +433,13 @@ describe('Roster local mode (local: true)', () => {
432
433
  hostname: 'localhost'
433
434
  });
434
435
  const body = 'local-mode-ok';
436
+ roster.use(createScannerBlocker({
437
+ windowMs: 60_000,
438
+ strikeThreshold: 2,
439
+ banDurationMs: 300_000,
440
+ maxTrackedClients: 100,
441
+ trustProxy: false
442
+ }));
435
443
  roster.register('testlocal.example', (server) => {
436
444
  return (req, res) => {
437
445
  res.writeHead(200, { 'Content-Type': 'text/plain' });
@@ -446,6 +454,9 @@ describe('Roster local mode (local: true)', () => {
446
454
  const result = await httpGet('localhost', port, '/');
447
455
  assert.strictEqual(result.statusCode, 200);
448
456
  assert.strictEqual(result.body, body);
457
+ const probe = await httpGet('localhost', port, '/wp-login.php');
458
+ assert.strictEqual(probe.statusCode, 404);
459
+ assert.strictEqual(probe.body, 'Not Found');
449
460
  } finally {
450
461
  closePortServers(roster);
451
462
  }
@@ -0,0 +1,189 @@
1
+ 'use strict';
2
+
3
+ const { describe, it } = require('node:test');
4
+ const assert = require('node:assert');
5
+ const Roster = require('../index.js');
6
+ const { createScannerBlocker } = require('../plugins/scanner-blocker.js');
7
+
8
+ function invoke(plugin, { url = '/', ip = '192.0.2.10', headers = {}, method = 'GET' } = {}) {
9
+ let statusCode;
10
+ let responseHeaders;
11
+ let body;
12
+ const req = {
13
+ method,
14
+ url,
15
+ headers,
16
+ socket: { remoteAddress: ip }
17
+ };
18
+ const res = {
19
+ writeHead(code, nextHeaders) {
20
+ statusCode = code;
21
+ responseHeaders = nextHeaders;
22
+ },
23
+ end(nextBody) {
24
+ body = nextBody;
25
+ }
26
+ };
27
+ const handled = plugin(req, res, { host: 'example.com', domain: 'example.com' });
28
+ return { handled, statusCode, responseHeaders, body };
29
+ }
30
+
31
+ function blocker(overrides = {}) {
32
+ return createScannerBlocker({
33
+ windowMs: 60_000,
34
+ strikeThreshold: 2,
35
+ banDurationMs: 300_000,
36
+ maxTrackedClients: 100,
37
+ trustProxy: false,
38
+ ...overrides
39
+ });
40
+ }
41
+
42
+ describe('scanner-blocker plugin', () => {
43
+ it('requires every operational option explicitly', () => {
44
+ assert.throws(() => createScannerBlocker(), /options are required/);
45
+ assert.throws(() => createScannerBlocker({}), /windowMs/);
46
+ assert.throws(() => blocker({ trustProxy: undefined }), /trustProxy/);
47
+ assert.throws(() => blocker({ maxTrackedClients: 0 }), /maxTrackedClients/);
48
+ });
49
+
50
+ it('blocks PHP, WordPress, and sensitive-file probes', () => {
51
+ const urls = [
52
+ '/install.php',
53
+ '/user-new\\.php',
54
+ '/wp-json',
55
+ '/.git/config',
56
+ '/%252eenv'
57
+ ];
58
+
59
+ for (const [index, url] of urls.entries()) {
60
+ const result = invoke(blocker(), { url, ip: `192.0.2.${index + 1}` });
61
+ assert.strictEqual(result.handled, true, url);
62
+ assert.strictEqual(result.statusCode, 404, url);
63
+ assert.strictEqual(result.body, 'Not Found', url);
64
+ assert.strictEqual(result.responseHeaders['Cache-Control'], 'no-store');
65
+ }
66
+ });
67
+
68
+ it('does not classify ordinary article, atom, or config routes as scanner probes', () => {
69
+ const plugin = blocker();
70
+ assert.strictEqual(invoke(plugin, { url: '/en/an-article' }).handled, false);
71
+ assert.strictEqual(invoke(plugin, { url: '/atom' }).handled, false);
72
+ assert.strictEqual(invoke(plugin, { url: '/articles/config' }).handled, false);
73
+ });
74
+
75
+ it('bans a client after the configured number of probes', () => {
76
+ const timestamp = 1_000;
77
+ const events = [];
78
+ const plugin = blocker({
79
+ now: () => timestamp,
80
+ onBlock: event => events.push(event)
81
+ });
82
+
83
+ assert.strictEqual(invoke(plugin, { url: '/wp-login.php' }).handled, true);
84
+ assert.strictEqual(invoke(plugin, { url: '/xmlrpc.php' }).handled, true);
85
+ assert.strictEqual(invoke(plugin, { url: '/legitimate-page' }).handled, true);
86
+ assert.strictEqual(events[1].bannedUntil, 301_000);
87
+ assert.strictEqual(events[2].type, 'banned-client');
88
+
89
+ assert.strictEqual(invoke(plugin, { url: '/legitimate-page', ip: '192.0.2.11' }).handled, false);
90
+ });
91
+
92
+ it('expires bans without extending them on blocked requests', () => {
93
+ let timestamp = 1_000;
94
+ const events = [];
95
+ const plugin = blocker({
96
+ strikeThreshold: 1,
97
+ banDurationMs: 5_000,
98
+ now: () => timestamp,
99
+ onBlock: event => events.push(event)
100
+ });
101
+
102
+ invoke(plugin, { url: '/wp-login.php' });
103
+ assert.strictEqual(events[0].bannedUntil, 6_000);
104
+ timestamp = 5_999;
105
+ assert.strictEqual(invoke(plugin, { url: '/ordinary' }).handled, true);
106
+ timestamp = 6_000;
107
+ assert.strictEqual(invoke(plugin, { url: '/ordinary' }).handled, false);
108
+ });
109
+
110
+ it('trusts X-Forwarded-For only when explicitly configured', () => {
111
+ const headers = { 'x-forwarded-for': '198.51.100.1, 198.51.100.2' };
112
+ const directPlugin = blocker({ strikeThreshold: 1, trustProxy: false });
113
+ invoke(directPlugin, { url: '/install.php', headers, ip: '192.0.2.20' });
114
+ assert.strictEqual(invoke(directPlugin, { url: '/', headers, ip: '192.0.2.20' }).handled, true);
115
+ assert.strictEqual(invoke(directPlugin, { url: '/', headers, ip: '192.0.2.21' }).handled, false);
116
+
117
+ const proxyPlugin = blocker({ strikeThreshold: 1, trustProxy: true });
118
+ invoke(proxyPlugin, { url: '/install.php', headers, ip: '192.0.2.20' });
119
+ assert.strictEqual(invoke(proxyPlugin, { url: '/', headers, ip: '192.0.2.21' }).handled, true);
120
+ });
121
+
122
+ it('keeps tracked client state within the configured bound', () => {
123
+ const plugin = blocker({ maxTrackedClients: 1 });
124
+ invoke(plugin, { url: '/install.php', ip: '192.0.2.30' });
125
+ invoke(plugin, { url: '/install.php', ip: '192.0.2.31' });
126
+ invoke(plugin, { url: '/xmlrpc.php', ip: '192.0.2.30' });
127
+ assert.strictEqual(invoke(plugin, { url: '/ordinary', ip: '192.0.2.30' }).handled, false);
128
+ });
129
+
130
+ it('does not send a response body for HEAD probes', () => {
131
+ const result = invoke(blocker(), { method: 'HEAD', url: '/wp-login.php' });
132
+ assert.strictEqual(result.handled, true);
133
+ assert.strictEqual(result.body, undefined);
134
+ });
135
+ });
136
+
137
+ describe('Roster request plugins', () => {
138
+ it('runs plugins before dispatching to the site handler', async () => {
139
+ let siteRequests = 0;
140
+ const roster = new Roster({ local: true });
141
+ roster.use(createScannerBlocker({
142
+ windowMs: 60_000,
143
+ strikeThreshold: 2,
144
+ banDurationMs: 300_000,
145
+ maxTrackedClients: 100,
146
+ trustProxy: false
147
+ }));
148
+ roster.register('example.com', () => (req, res) => {
149
+ siteRequests += 1;
150
+ res.writeHead(200);
151
+ res.end('site');
152
+ });
153
+ await roster.init();
154
+
155
+ const handler = roster.requestHandler();
156
+ const probe = invoke(handler, {
157
+ url: '/wp-login.php',
158
+ headers: { host: 'example.com' }
159
+ });
160
+ assert.strictEqual(probe.statusCode, 404);
161
+ assert.strictEqual(siteRequests, 0);
162
+
163
+ const ordinary = invoke(handler, {
164
+ url: '/ordinary',
165
+ ip: '192.0.2.11',
166
+ headers: { host: 'example.com' }
167
+ });
168
+ assert.strictEqual(ordinary.statusCode, 200);
169
+ assert.strictEqual(ordinary.body, 'site');
170
+ assert.strictEqual(siteRequests, 1);
171
+ });
172
+
173
+ it('validates plugins and rejects asynchronous request plugins', async () => {
174
+ const roster = new Roster({ local: true });
175
+ assert.throws(() => roster.use({}), /plugin must be a function/);
176
+ assert.strictEqual(roster.use(() => false), roster);
177
+
178
+ const asyncRoster = new Roster({ local: true });
179
+ asyncRoster.use(async () => false);
180
+ asyncRoster.register('example.com', () => () => {});
181
+ await asyncRoster.init();
182
+ assert.throws(() => {
183
+ asyncRoster.requestHandler()(
184
+ { headers: { host: 'example.com' }, url: '/', socket: {} },
185
+ { writeHead() {}, end() {} }
186
+ );
187
+ }, /must be synchronous/);
188
+ });
189
+ });
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
package/.greenlockrc DELETED
@@ -1 +0,0 @@
1
- {"configDir":"/Users/martinclasen/Stuff/greenlock.d"}