badmfck-api-server 6.0.3 → 6.0.5
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 +71 -14
- package/dist/apiServer/APIService.d.ts +5 -0
- package/dist/apiServer/APIService.js +20 -1
- package/dist/apiServer/deployment/BackupManager.js +6 -0
- package/dist/apiServer/deployment/ConfigService.d.ts +67 -3
- package/dist/apiServer/deployment/ConfigService.js +5 -1
- package/dist/apiServer/deployment/DeployerService.d.ts +1 -4
- package/dist/apiServer/deployment/DeployerService.js +17 -40
- package/dist/apiServer/deployment/HashPassword.d.ts +1 -0
- package/dist/apiServer/deployment/HashPassword.js +2 -1
- package/dist/apiServer/deployment/ProjectConfigWizard.d.ts +1 -0
- package/dist/apiServer/deployment/ProjectConfigWizard.js +171 -0
- package/dist/apiServer/deployment/cli.js +10 -0
- package/dist/apiServer/documentation/Documentation.js +5 -2
- package/dist/apiServer/helper/IpAllowList.d.ts +8 -0
- package/dist/apiServer/helper/IpAllowList.js +40 -0
- package/dist/apiServer/monitor/AccessStore.d.ts +2 -0
- package/dist/apiServer/monitor/AccessStore.js +12 -2
- package/dist/apiServer/monitor/Monitor.d.ts +4 -0
- package/dist/apiServer/monitor/Monitor.js +78 -5
- package/dist/apiServer/monitor/MonitorAuth.js +3 -1
- package/dist/apiServer/monitor/index.html +112 -67
- package/dist/apiServer/review/Review.js +1 -1
- package/dist/apiServer/security/BlockedIps.d.ts +46 -0
- package/dist/apiServer/security/BlockedIps.js +196 -0
- package/monitoring.help.md +40 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,14 +23,15 @@ Node.js 21+ is required.
|
|
|
23
23
|
6. [Outgoing HTTP client](#outgoing-http-client)
|
|
24
24
|
7. [Logging and redaction](#logging-and-redaction)
|
|
25
25
|
8. [Monitor console (`sm/*`)](#monitor-console-sm)
|
|
26
|
-
9. [
|
|
27
|
-
10. [
|
|
28
|
-
11. [
|
|
29
|
-
12. [
|
|
30
|
-
13. [
|
|
31
|
-
14. [
|
|
32
|
-
15. [
|
|
33
|
-
16. [
|
|
26
|
+
9. [IP blocking](#ip-blocking)
|
|
27
|
+
10. [Authentication](#authentication)
|
|
28
|
+
11. [WebSocket server](#websocket-server)
|
|
29
|
+
12. [Deployer and CLI](#deployer-and-cli)
|
|
30
|
+
13. [Blue-green deployments](#blue-green-deployments)
|
|
31
|
+
14. [Backups and restore](#backups-and-restore)
|
|
32
|
+
15. [Deploy review](#deploy-review)
|
|
33
|
+
16. [Options reference](#options-reference)
|
|
34
|
+
17. [Security](#security)
|
|
34
35
|
|
|
35
36
|
---
|
|
36
37
|
|
|
@@ -307,11 +308,62 @@ monitorAuthDir: "/var/lib/my-service", // where 2FA enrollments are kept
|
|
|
307
308
|
change is ignored and the previous access stays in force. The reason is logged.
|
|
308
309
|
- **Location and permissions.** Keep the file outside the deploy folders and `chmod 600` it. A warning
|
|
309
310
|
is logged if other users can read it.
|
|
311
|
+
- **Restrict by address.** `monitorAllowedIps` in `access` (inline or in `access.json`) limits every
|
|
312
|
+
`sm/*` request to the given IPs and CIDR subnets, the page and sign-in included. A session token
|
|
313
|
+
taken elsewhere is therefore useless outside those addresses too:
|
|
314
|
+
|
|
315
|
+
```jsonc
|
|
316
|
+
{ "monitor": [ … ], "monitorAllowedIps": ["10.8.0.0/24", "203.0.113.7"] }
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
Other addresses get `403`. Changes in `access.json` apply without a restart, and an invalid entry
|
|
320
|
+
rejects the change. Behind nginx, pass `X-Real-IP` (see [`trustedProxies`](#options-reference)),
|
|
321
|
+
otherwise every request appears to come from `127.0.0.1`.
|
|
322
|
+
- **Block addresses.** Admins can block an address or a subnet from the threats page, permanently or
|
|
323
|
+
for a number of hours. See [IP blocking](#ip-blocking).
|
|
310
324
|
- **Keep `monitorAuthDir` outside the deploy folders.** Otherwise a deploy wipes the enrollments and
|
|
311
325
|
everyone has to enroll again.
|
|
312
326
|
|
|
313
327
|
---
|
|
314
328
|
|
|
329
|
+
## IP blocking
|
|
330
|
+
|
|
331
|
+
Blocked addresses get a `403` on every request, whether HTTP, WebSocket, the monitor or the
|
|
332
|
+
documentation:
|
|
333
|
+
|
|
334
|
+
```json
|
|
335
|
+
{ "error": { "code": 403, "message": "Access from your address is blocked", "httpStatus": 403 } }
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
The check runs first, before body parsing, CORS and the interceptor, so a blocked client costs almost
|
|
339
|
+
nothing. The list lives in `blocked-ips.json`, in the project folder (for blue-green, the folder above
|
|
340
|
+
the slots, shared by both). It is re-read on change:
|
|
341
|
+
|
|
342
|
+
```jsonc
|
|
343
|
+
[
|
|
344
|
+
{ "ip": "203.0.113.7", "reason": "credential stuffing", "until": "2026-12-01T00:00:00Z" },
|
|
345
|
+
{ "ip": "198.51.100.0/24", "reason": "scanner" } // no "until" → permanent
|
|
346
|
+
]
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
- **How to edit.** Edit the file by hand, or block and unblock from the monitor (`sm/block`,
|
|
350
|
+
`sm/unblock`; `admin` role). The monitor records who blocked and when. The threats page marks
|
|
351
|
+
addresses that are already blocked.
|
|
352
|
+
- **Expiry.** Entries past `until` stop applying by themselves.
|
|
353
|
+
- **Invalid edits.** An invalid file is ignored, and the previous list stays in force.
|
|
354
|
+
- **Reasons stay private.** `reason` is shown only in the monitor, never to the client.
|
|
355
|
+
- **Safety rules.** Loopback is never blocked, so a proxy without `X-Real-IP` can't take the service
|
|
356
|
+
down. An admin can't block their own address from the monitor.
|
|
357
|
+
- **Deploys and restores.** The deployer keeps the file through clean deploys and restores, because
|
|
358
|
+
it is runtime state, not part of a release.
|
|
359
|
+
- **Options.** `blockedIps: { file, message }` sets another location or the text of the error;
|
|
360
|
+
`blockedIps: false` turns blocking off.
|
|
361
|
+
|
|
362
|
+
Blocking in the service suits targeted cases: a particular attacker or a scanner subnet. To stop a
|
|
363
|
+
flood, block at nginx or with fail2ban, where requests are dropped before they reach Node.
|
|
364
|
+
|
|
365
|
+
---
|
|
366
|
+
|
|
315
367
|
## Authentication
|
|
316
368
|
|
|
317
369
|
The library protects four things, each in its own way:
|
|
@@ -351,8 +403,8 @@ Repeat password:
|
|
|
351
403
|
history or `ps`. Without a terminal (in CI, for example), the command refuses to run.
|
|
352
404
|
- **Password policy.** The minimum length is 12 characters, checked by the command.
|
|
353
405
|
- **Salting.** Every run gives a different hash for the same password. All of them are valid.
|
|
354
|
-
- **Plain text is refused.** A password stored in plain text is rejected at login with
|
|
355
|
-
|
|
406
|
+
- **Plain text is refused.** A password stored in plain text is rejected at login with the same answer
|
|
407
|
+
as a wrong password. The server log says why, and a startup error names the affected logins.
|
|
356
408
|
|
|
357
409
|
Put the result into `access.monitor` (inline or in an `access.json` file) and add a role:
|
|
358
410
|
|
|
@@ -468,8 +520,8 @@ documentation users their own passwords, never ones they use elsewhere.
|
|
|
468
520
|
- **Project token.** Deploying, switching and restoring also require the project's own `token`. The
|
|
469
521
|
project config on the server holds only its hash. Run `npx badmfck project-token`: it prints a new
|
|
470
522
|
random token for `deploy.json` and its hash for the project config. To keep an existing token, run
|
|
471
|
-
`npx badmfck project-token --hash`. A token stored in plain text is refused
|
|
472
|
-
|
|
523
|
+
`npx badmfck project-token --hash`. A token stored in plain text is refused like a wrong token. The
|
|
524
|
+
server log says why, and a startup error names the affected projects.
|
|
473
525
|
- **CLI version.** Users stored as a hash need a CLI from version 6 or later.
|
|
474
526
|
|
|
475
527
|
---
|
|
@@ -563,7 +615,10 @@ rejects the whole change, and the previous setup stays in force.
|
|
|
563
615
|
> it, every request appears to come from `127.0.0.1`. Never add `127.0.0.1` to `allowed_ips` on a
|
|
564
616
|
> deployer behind a proxy on the same host: that would let every request through.
|
|
565
617
|
|
|
566
|
-
A project config (`projects_path/<name>.json`)
|
|
618
|
+
A project config (`projects_path/<name>.json`). The easiest way to make one is `npx badmfck project-config`:
|
|
619
|
+
it asks for everything below, generates the token, takes config values as hidden input, checks the
|
|
620
|
+
result against the deployer's schema, and saves the project config and a matching `deploy.json` with
|
|
621
|
+
permissions `600`.
|
|
567
622
|
|
|
568
623
|
```jsonc
|
|
569
624
|
{
|
|
@@ -648,6 +703,7 @@ git: it holds credentials.
|
|
|
648
703
|
| `badmfck restore [config] [backup] [--yes]` | restore a backup |
|
|
649
704
|
| `badmfck hash-password [--deployer]` | hash a password for the monitor or `setup.json` |
|
|
650
705
|
| `badmfck project-token [--hash]` | generate a project token and its hash, or hash an existing token |
|
|
706
|
+
| `badmfck project-config` | build a project config for the deployer step by step (plus a `deploy.json` skeleton) |
|
|
651
707
|
|
|
652
708
|
To pass a config path through npm, use `--`: `npm run deploy -- deploy/prod.json`.
|
|
653
709
|
|
|
@@ -911,12 +967,13 @@ so no restart is needed.
|
|
|
911
967
|
| `isProductionEnvironment` | `false` | production mode |
|
|
912
968
|
| `interceptor` | — | authentication for your endpoints |
|
|
913
969
|
| `preproducer` / `postproducer` | — | hooks before the handler / before the response is sent |
|
|
914
|
-
| `access` | `{}` | `{ monitor, documentation }
|
|
970
|
+
| `access` | `{}` | `{ monitor, documentation, monitorAllowedIps }`, or a path to a JSON file reloaded on change |
|
|
915
971
|
| `monitorAuthDir` / `monitorAuthFile` | in memory | where 2FA enrollments are stored |
|
|
916
972
|
| `metricsDir` | in memory | persist metrics; 10 weeks are kept |
|
|
917
973
|
| `logFiles` | pm2 auto-detected | log files the monitor may tail (for example nginx) |
|
|
918
974
|
| `threatMonitoring` | `true` | probe and scan detection; observation only |
|
|
919
975
|
| `trustedProxies` | loopback | proxies whose `X-Real-IP` / `X-Forwarded-For` are trusted |
|
|
976
|
+
| `blockedIps` | `blocked-ips.json` in the project folder | `{ file, message }` for the blocked-address list; `false` turns it off |
|
|
920
977
|
| `websocketServer` | — | enables the WebSocket server, see above |
|
|
921
978
|
| `deployer` | — | turns this service into a deployer |
|
|
922
979
|
| `blueGreenWatchMs` | `5000` | how often to check the active slot; `0` disables |
|
|
@@ -62,6 +62,10 @@ export interface APIServiceOptions<TInterceptor = unknown> {
|
|
|
62
62
|
monitorAuthFile?: string;
|
|
63
63
|
threatMonitoring?: boolean;
|
|
64
64
|
trustedProxies?: string[];
|
|
65
|
+
blockedIps?: false | {
|
|
66
|
+
file?: string;
|
|
67
|
+
message?: string;
|
|
68
|
+
};
|
|
65
69
|
}
|
|
66
70
|
type InferInterceptor<T> = T extends IInterceptor<infer U> ? U : unknown;
|
|
67
71
|
export declare const getDefaultOptions: <I extends IInterceptor<any> | undefined>(overrides?: Omit<Partial<APIServiceOptions<InferInterceptor<NonNullable<I>>>>, "interceptor"> & {
|
|
@@ -86,6 +90,7 @@ export declare const REQ_WS_SERVER: Req<void, WebSocketServer | null>;
|
|
|
86
90
|
export declare const REQ_INTERNAL_CALL: Req<HTTPRequestVO, IError | any>;
|
|
87
91
|
export declare const REQ_MONITOR_USERS: Req<void, IMonitorUser[]>;
|
|
88
92
|
export declare const REQ_DOC_USERS: Req<void, IDocumentationUser[]>;
|
|
93
|
+
export declare const REQ_MONITOR_ALLOWED_IPS: Req<void, string[]>;
|
|
89
94
|
export declare const S_APP_STARTED: Signal<void>;
|
|
90
95
|
export declare function Initializer(services: IBaseService[]): Promise<void>;
|
|
91
96
|
export declare class APIService<TInterceptorResult = any, TInternalCallParams = any> extends BaseService {
|
|
@@ -36,7 +36,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.APIService = exports.S_APP_STARTED = exports.REQ_DOC_USERS = exports.REQ_MONITOR_USERS = exports.REQ_INTERNAL_CALL = exports.REQ_WS_SERVER = exports.REQ_HTTP_SERVER = exports.REQ_HTTP_REQUESTS_COUNT = exports.REQ_HTTP_LOG = exports.getDefaultOptions = void 0;
|
|
39
|
+
exports.APIService = exports.S_APP_STARTED = exports.REQ_MONITOR_ALLOWED_IPS = exports.REQ_DOC_USERS = exports.REQ_MONITOR_USERS = exports.REQ_INTERNAL_CALL = exports.REQ_WS_SERVER = exports.REQ_HTTP_SERVER = exports.REQ_HTTP_REQUESTS_COUNT = exports.REQ_HTTP_LOG = exports.getDefaultOptions = void 0;
|
|
40
40
|
exports.Initializer = Initializer;
|
|
41
41
|
const express_1 = __importDefault(require("express"));
|
|
42
42
|
const BaseService_1 = require("./BaseService");
|
|
@@ -63,6 +63,7 @@ const DeployerService_1 = require("./deployment/DeployerService");
|
|
|
63
63
|
const BlueGreenState_1 = require("./deployment/BlueGreenState");
|
|
64
64
|
const ThreatMonitor_1 = require("./monitor/ThreatMonitor");
|
|
65
65
|
const AccessStore_1 = require("./monitor/AccessStore");
|
|
66
|
+
const BlockedIps_1 = require("./security/BlockedIps");
|
|
66
67
|
const MonitorAuth_1 = require("./monitor/MonitorAuth");
|
|
67
68
|
const ClientIp_1 = require("./helper/ClientIp");
|
|
68
69
|
const Deployer_1 = require("./deployment/Deployer");
|
|
@@ -106,6 +107,7 @@ exports.REQ_WS_SERVER = new badmfck_signal_1.Req(undefined, "REQ_WS_SERVER");
|
|
|
106
107
|
exports.REQ_INTERNAL_CALL = new badmfck_signal_1.Req(undefined, "REQ_INTERNAL_CALL");
|
|
107
108
|
exports.REQ_MONITOR_USERS = new badmfck_signal_1.Req(undefined, "REQ_MONITOR_USERS");
|
|
108
109
|
exports.REQ_DOC_USERS = new badmfck_signal_1.Req(undefined, "REQ_DOC_USERS");
|
|
110
|
+
exports.REQ_MONITOR_ALLOWED_IPS = new badmfck_signal_1.Req(undefined, "REQ_MONITOR_ALLOWED_IPS");
|
|
109
111
|
exports.S_APP_STARTED = new badmfck_signal_1.default("S_APP_STARTED");
|
|
110
112
|
const activeServices = [];
|
|
111
113
|
const PACKAGE_VERSION = (() => {
|
|
@@ -172,6 +174,8 @@ class APIService extends BaseService_1.BaseService {
|
|
|
172
174
|
}
|
|
173
175
|
}
|
|
174
176
|
this.options.corsHostWhiteList = Array.from(new Set(list));
|
|
177
|
+
if (this.options.blockedIps !== false)
|
|
178
|
+
BlockedIps_1.BlockedIps.configure(this.options.blockedIps?.file);
|
|
175
179
|
const access = new AccessStore_1.AccessStore(this.options.access, (prev, next) => {
|
|
176
180
|
const now = new Map((next.monitor ?? []).map(u => [u.login, u.password]));
|
|
177
181
|
for (const u of prev.monitor ?? []) {
|
|
@@ -186,6 +190,7 @@ class APIService extends BaseService_1.BaseService {
|
|
|
186
190
|
});
|
|
187
191
|
exports.REQ_MONITOR_USERS.listener = async () => access.monitor;
|
|
188
192
|
exports.REQ_DOC_USERS.listener = async () => access.documentation;
|
|
193
|
+
exports.REQ_MONITOR_ALLOWED_IPS.listener = async () => access.monitorAllowedIps;
|
|
189
194
|
this.options.endpoints.push(new Monitor_1.Monitor(this.options));
|
|
190
195
|
this.options.endpoints.push(new Documentation_1.Documentation(this.options));
|
|
191
196
|
this.options.endpoints.push(new ExternalServiceEndpoint_1.ExternalServiceEndpoint());
|
|
@@ -233,6 +238,16 @@ class APIService extends BaseService_1.BaseService {
|
|
|
233
238
|
};
|
|
234
239
|
const app = (0, express_1.default)();
|
|
235
240
|
const trustedProxies = new Set(this.options.trustedProxies ?? ClientIp_1.DEFAULT_TRUSTED_PROXIES);
|
|
241
|
+
if (this.options.blockedIps !== false) {
|
|
242
|
+
const message = this.options.blockedIps?.message || "Access from your address is blocked";
|
|
243
|
+
app.use((req, res, next) => {
|
|
244
|
+
const ip = (0, ClientIp_1.clientIp)(req, trustedProxies);
|
|
245
|
+
if (!BlockedIps_1.BlockedIps.isBlocked(ip))
|
|
246
|
+
return next();
|
|
247
|
+
BlockedIps_1.BlockedIps.noteHit(ip);
|
|
248
|
+
res.status(403).json({ error: { code: 403, message, httpStatus: 403 }, data: null, httpStatus: 403, version: this.options.appVersion });
|
|
249
|
+
});
|
|
250
|
+
}
|
|
236
251
|
if (this.options.threatMonitoring !== false) {
|
|
237
252
|
app.use((req, res, next) => {
|
|
238
253
|
res.on("finish", () => {
|
|
@@ -588,6 +603,10 @@ class APIService extends BaseService_1.BaseService {
|
|
|
588
603
|
return;
|
|
589
604
|
}
|
|
590
605
|
const ip = (0, ClientIp_1.clientIp)(req, trustedProxies);
|
|
606
|
+
if (BlockedIps_1.BlockedIps.isBlocked(ip)) {
|
|
607
|
+
BlockedIps_1.BlockedIps.noteHit(ip);
|
|
608
|
+
return refuse(socket, 403, "Forbidden");
|
|
609
|
+
}
|
|
591
610
|
const origin = req.headers.origin;
|
|
592
611
|
if (origin && !this.noCors) {
|
|
593
612
|
let originNorm = "";
|
|
@@ -8,6 +8,7 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
9
|
const child_process_1 = require("child_process");
|
|
10
10
|
const util_1 = require("util");
|
|
11
|
+
const BlockedIps_1 = require("../security/BlockedIps");
|
|
11
12
|
const execFileAsync = (0, util_1.promisify)(child_process_1.execFile);
|
|
12
13
|
exports.BACKUP_META = ".badmfck-backup.json";
|
|
13
14
|
exports.MAX_BACKUPS = 10;
|
|
@@ -157,6 +158,11 @@ class BackupManager {
|
|
|
157
158
|
await fs_1.default.promises.rm(staging, { recursive: true, force: true }).catch(() => { });
|
|
158
159
|
throw e;
|
|
159
160
|
}
|
|
161
|
+
for (const keep of [BlockedIps_1.BLOCKED_IPS_FILE]) {
|
|
162
|
+
const live = path_1.default.join(target, keep);
|
|
163
|
+
if (fs_1.default.existsSync(live))
|
|
164
|
+
await fs_1.default.promises.copyFile(live, path_1.default.join(staging, keep)).catch(() => { });
|
|
165
|
+
}
|
|
160
166
|
const hadTarget = fs_1.default.existsSync(target);
|
|
161
167
|
if (hadTarget)
|
|
162
168
|
await fs_1.default.promises.rename(target, old);
|
|
@@ -33,8 +33,11 @@ declare const _TConfig: {
|
|
|
33
33
|
readonly pm2_attributes: readonly [""];
|
|
34
34
|
readonly $__pm2_attributes_optional: true;
|
|
35
35
|
readonly chmod: 0;
|
|
36
|
+
readonly $__chmod_optional: true;
|
|
36
37
|
readonly email: readonly [""];
|
|
38
|
+
readonly $__email_optional: true;
|
|
37
39
|
readonly phone: readonly [""];
|
|
40
|
+
readonly $__phone_optional: true;
|
|
38
41
|
readonly liveness: "";
|
|
39
42
|
readonly $__liveness_optional: true;
|
|
40
43
|
readonly readiness: "";
|
|
@@ -77,6 +80,67 @@ declare const _TConfig: {
|
|
|
77
80
|
readonly $__secrets_optional: true;
|
|
78
81
|
};
|
|
79
82
|
export type IConfig = ValidationModel<typeof _TConfig>;
|
|
83
|
+
export declare const PROJECT_CONFIG_MODEL: {
|
|
84
|
+
readonly name: "";
|
|
85
|
+
readonly destination: "";
|
|
86
|
+
readonly description: "";
|
|
87
|
+
readonly token: "";
|
|
88
|
+
readonly unpack: true;
|
|
89
|
+
readonly ignoreDirectoryClean: false;
|
|
90
|
+
readonly backup: true;
|
|
91
|
+
readonly install: true;
|
|
92
|
+
readonly migrate: false;
|
|
93
|
+
readonly $__migrate_optional: true;
|
|
94
|
+
readonly pm2: true;
|
|
95
|
+
readonly pm2_attributes: readonly [""];
|
|
96
|
+
readonly $__pm2_attributes_optional: true;
|
|
97
|
+
readonly chmod: 0;
|
|
98
|
+
readonly $__chmod_optional: true;
|
|
99
|
+
readonly email: readonly [""];
|
|
100
|
+
readonly $__email_optional: true;
|
|
101
|
+
readonly phone: readonly [""];
|
|
102
|
+
readonly $__phone_optional: true;
|
|
103
|
+
readonly liveness: "";
|
|
104
|
+
readonly $__liveness_optional: true;
|
|
105
|
+
readonly readiness: "";
|
|
106
|
+
readonly $__readiness_optional: true;
|
|
107
|
+
readonly config: {};
|
|
108
|
+
readonly bluegreen: {
|
|
109
|
+
readonly blue_config: {};
|
|
110
|
+
readonly blue_pm2_attributes: readonly [""];
|
|
111
|
+
readonly $__blue_pm2_attributes_optional: true;
|
|
112
|
+
readonly green_config: {};
|
|
113
|
+
readonly green_pm2_attributes: readonly [""];
|
|
114
|
+
readonly $__green_pm2_attributes_optional: true;
|
|
115
|
+
readonly on_switch: readonly [""];
|
|
116
|
+
readonly $__on_switch_optional: true;
|
|
117
|
+
};
|
|
118
|
+
readonly $__bluegreen_optional: true;
|
|
119
|
+
readonly nginx: {
|
|
120
|
+
readonly config_dir: "";
|
|
121
|
+
readonly $__config_dir_optional: true;
|
|
122
|
+
readonly domain: "";
|
|
123
|
+
readonly $__domain_optional: true;
|
|
124
|
+
readonly upstream_name: "";
|
|
125
|
+
readonly $__upstream_name_optional: true;
|
|
126
|
+
};
|
|
127
|
+
readonly $__nginx_optional: true;
|
|
128
|
+
readonly review: {
|
|
129
|
+
readonly reviewers: readonly [""];
|
|
130
|
+
readonly mode: "";
|
|
131
|
+
readonly $__mode_values: readonly ["any", "all"];
|
|
132
|
+
readonly ttlHours: 24;
|
|
133
|
+
readonly $__ttlHours_optional: true;
|
|
134
|
+
};
|
|
135
|
+
readonly $__review_optional: true;
|
|
136
|
+
readonly secrets: {
|
|
137
|
+
readonly dir: "";
|
|
138
|
+
readonly $__dir_optional: true;
|
|
139
|
+
readonly file: "";
|
|
140
|
+
readonly $__file_optional: true;
|
|
141
|
+
};
|
|
142
|
+
readonly $__secrets_optional: true;
|
|
143
|
+
};
|
|
80
144
|
export declare const REQ_DEPLOYMENT_CONFIG: Req<void, Map<string, {
|
|
81
145
|
name: string;
|
|
82
146
|
destination: string;
|
|
@@ -89,9 +153,9 @@ export declare const REQ_DEPLOYMENT_CONFIG: Req<void, Map<string, {
|
|
|
89
153
|
migrate?: boolean | undefined;
|
|
90
154
|
pm2: boolean;
|
|
91
155
|
pm2_attributes?: string[] | undefined;
|
|
92
|
-
chmod
|
|
93
|
-
email
|
|
94
|
-
phone
|
|
156
|
+
chmod?: number | undefined;
|
|
157
|
+
email?: string[] | undefined;
|
|
158
|
+
phone?: string[] | undefined;
|
|
95
159
|
liveness?: string | undefined;
|
|
96
160
|
readiness?: string | undefined;
|
|
97
161
|
config: {};
|
|
@@ -36,7 +36,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.ConfigService = exports.S_CONFIG_UPDATED = exports.REQ_DEPLOYMENT_CONFIG = void 0;
|
|
39
|
+
exports.ConfigService = exports.S_CONFIG_UPDATED = exports.REQ_DEPLOYMENT_CONFIG = exports.PROJECT_CONFIG_MODEL = void 0;
|
|
40
40
|
const fs_1 = __importDefault(require("fs"));
|
|
41
41
|
const path_1 = __importDefault(require("path"));
|
|
42
42
|
const crypto_1 = __importDefault(require("crypto"));
|
|
@@ -75,8 +75,11 @@ const _TConfig = {
|
|
|
75
75
|
pm2_attributes: [""],
|
|
76
76
|
$__pm2_attributes_optional: true,
|
|
77
77
|
chmod: 0,
|
|
78
|
+
$__chmod_optional: true,
|
|
78
79
|
email: [""],
|
|
80
|
+
$__email_optional: true,
|
|
79
81
|
phone: [""],
|
|
82
|
+
$__phone_optional: true,
|
|
80
83
|
liveness: "",
|
|
81
84
|
$__liveness_optional: true,
|
|
82
85
|
readiness: "",
|
|
@@ -118,6 +121,7 @@ const _TConfig = {
|
|
|
118
121
|
},
|
|
119
122
|
$__secrets_optional: true,
|
|
120
123
|
};
|
|
124
|
+
exports.PROJECT_CONFIG_MODEL = _TConfig;
|
|
121
125
|
exports.REQ_DEPLOYMENT_CONFIG = new badmfck_signal_1.Req();
|
|
122
126
|
exports.S_CONFIG_UPDATED = new badmfck_signal_1.default();
|
|
123
127
|
class ConfigService extends BaseService_1.BaseService {
|
|
@@ -5,7 +5,6 @@ import { UploadedFile } from "express-fileupload";
|
|
|
5
5
|
import { ValidationModel } from "../..";
|
|
6
6
|
import { IDeploymentNotifier } from "./Notifier";
|
|
7
7
|
import { IError } from "../structures/Interfaces";
|
|
8
|
-
import net from "net";
|
|
9
8
|
import { IPendingReview, ReviewStore } from "./ReviewStore";
|
|
10
9
|
declare const _TSetup: {
|
|
11
10
|
readonly users: readonly [{
|
|
@@ -276,17 +275,15 @@ export declare class DeployerService extends BaseService {
|
|
|
276
275
|
}>;
|
|
277
276
|
private _statusUrl;
|
|
278
277
|
private _writeBlueGreenFiles;
|
|
279
|
-
static PLAINTEXT_TOKEN_ERROR: string;
|
|
280
278
|
private _projectByToken;
|
|
281
279
|
tokensMatch(a: string | undefined, b: string | undefined): boolean;
|
|
282
280
|
static buildAllowList(entries: readonly string[] | undefined | null): {
|
|
283
|
-
list: net.BlockList | null;
|
|
281
|
+
list: import("node:net").BlockList | null;
|
|
284
282
|
error?: string;
|
|
285
283
|
};
|
|
286
284
|
isIpAllowed(ip: string | undefined): boolean;
|
|
287
285
|
private _ipAllowed;
|
|
288
286
|
private _warnPlaintextUsers;
|
|
289
|
-
static PLAINTEXT_ERROR: string;
|
|
290
287
|
private _checkUserAuth;
|
|
291
288
|
private static _extractPort;
|
|
292
289
|
private _renderNginx;
|
|
@@ -16,7 +16,6 @@ const util_1 = require("util");
|
|
|
16
16
|
const execFileAsync = (0, util_1.promisify)(child_process_1.execFile);
|
|
17
17
|
const fs_1 = __importDefault(require("fs"));
|
|
18
18
|
const crypto_1 = __importDefault(require("crypto"));
|
|
19
|
-
const net_1 = __importDefault(require("net"));
|
|
20
19
|
const Watchdog_1 = require("./Watchdog");
|
|
21
20
|
const NginxHelper_1 = require("./NginxHelper");
|
|
22
21
|
const BlueGreenState_1 = require("./BlueGreenState");
|
|
@@ -24,6 +23,8 @@ const BackupManager_1 = require("./BackupManager");
|
|
|
24
23
|
const PasswordHash_1 = require("../helper/PasswordHash");
|
|
25
24
|
const ReviewStore_1 = require("./ReviewStore");
|
|
26
25
|
const SecretsInjector_1 = require("./SecretsInjector");
|
|
26
|
+
const IpAllowList_1 = require("../helper/IpAllowList");
|
|
27
|
+
const BlockedIps_1 = require("../security/BlockedIps");
|
|
27
28
|
const _TSetup = {
|
|
28
29
|
users: [{
|
|
29
30
|
login: "",
|
|
@@ -961,17 +962,14 @@ class DeployerService extends BaseService_1.BaseService {
|
|
|
961
962
|
}
|
|
962
963
|
}
|
|
963
964
|
}
|
|
964
|
-
static PLAINTEXT_TOKEN_ERROR = "Project token is built incorrectly — the project config must hold a hash from `npx badmfck project-token --hash`, not the token itself.";
|
|
965
965
|
async _projectByToken(configurations, name, token) {
|
|
966
966
|
const config = Array.from(configurations.values()).find(c => c.name === name);
|
|
967
967
|
if (!config || typeof token !== "string" || !token)
|
|
968
968
|
return {};
|
|
969
969
|
if (PasswordHash_1.PasswordHash.isHash(config.token))
|
|
970
970
|
return (await PasswordHash_1.PasswordHash.verify(token, config.token)) ? { config } : {};
|
|
971
|
-
if (config.token && this.tokensMatch(config.token, token))
|
|
972
|
-
(0, LogService_1.logError)(`Deployer: project "${name}" refused —
|
|
973
|
-
return { error: DeployerService.PLAINTEXT_TOKEN_ERROR };
|
|
974
|
-
}
|
|
971
|
+
if (config.token && this.tokensMatch(config.token, token))
|
|
972
|
+
(0, LogService_1.logError)(`Deployer: project "${name}" refused — the right token was sent, but the project config stores it as plain text; store a hash: npx badmfck project-token --hash`);
|
|
975
973
|
return {};
|
|
976
974
|
}
|
|
977
975
|
tokensMatch(a, b) {
|
|
@@ -980,38 +978,13 @@ class DeployerService extends BaseService_1.BaseService {
|
|
|
980
978
|
return crypto_1.default.timingSafeEqual(ha, hb);
|
|
981
979
|
}
|
|
982
980
|
static buildAllowList(entries) {
|
|
983
|
-
|
|
984
|
-
if (items.length === 0)
|
|
985
|
-
return { list: null };
|
|
986
|
-
const list = new net_1.default.BlockList();
|
|
987
|
-
for (const item of items) {
|
|
988
|
-
const [addr, prefix, extra] = item.split("/");
|
|
989
|
-
const family = net_1.default.isIP(addr);
|
|
990
|
-
if (!family || extra !== undefined)
|
|
991
|
-
return { list: null, error: `"${item}" is not an IP address or subnet` };
|
|
992
|
-
const type = family === 6 ? "ipv6" : "ipv4";
|
|
993
|
-
if (prefix === undefined) {
|
|
994
|
-
list.addAddress(addr, type);
|
|
995
|
-
continue;
|
|
996
|
-
}
|
|
997
|
-
const bits = Number(prefix), max = family === 6 ? 128 : 32;
|
|
998
|
-
if (!/^\d+$/.test(prefix) || bits > max)
|
|
999
|
-
return { list: null, error: `"${item}" has an invalid prefix (0–${max})` };
|
|
1000
|
-
list.addSubnet(addr, bits, type);
|
|
1001
|
-
}
|
|
1002
|
-
return { list };
|
|
981
|
+
return IpAllowList_1.IpAllowList.build(entries);
|
|
1003
982
|
}
|
|
1004
983
|
isIpAllowed(ip) {
|
|
1005
984
|
return this._ipAllowed(ip);
|
|
1006
985
|
}
|
|
1007
986
|
_ipAllowed(ip) {
|
|
1008
|
-
|
|
1009
|
-
if (!list)
|
|
1010
|
-
return true;
|
|
1011
|
-
const family = net_1.default.isIP(String(ip ?? ""));
|
|
1012
|
-
if (!family)
|
|
1013
|
-
return false;
|
|
1014
|
-
return list.check(String(ip), family === 6 ? "ipv6" : "ipv4");
|
|
987
|
+
return IpAllowList_1.IpAllowList.allows(IpAllowList_1.IpAllowList.build(this.setup?.allowed_ips).list, ip);
|
|
1015
988
|
}
|
|
1016
989
|
_warnPlaintextUsers() {
|
|
1017
990
|
if (!DeployerService.buildAllowList(this.setup?.allowed_ips).list)
|
|
@@ -1020,11 +993,10 @@ class DeployerService extends BaseService_1.BaseService {
|
|
|
1020
993
|
if (plain.length)
|
|
1021
994
|
(0, LogService_1.logError)(`Deployer: password stored as plain text in setup.json for ${plain.map(l => '"' + l + '"').join(", ")} — these users are refused; store a hash instead: npx badmfck hash-password --deployer`);
|
|
1022
995
|
}
|
|
1023
|
-
static PLAINTEXT_ERROR = "Password is built incorrectly — setup.json must hold a hash from `npx badmfck hash-password --deployer`, not the password itself.";
|
|
1024
996
|
async _checkUserAuth(authHeader, loginHeader, ip) {
|
|
1025
997
|
if (!this._ipAllowed(ip)) {
|
|
1026
998
|
(0, LogService_1.logWarn)(`Deployer: request refused from ${ip ?? "unknown"} — not in allowed_ips`);
|
|
1027
|
-
return { error: "
|
|
999
|
+
return { error: "Forbidden.", status: 403 };
|
|
1028
1000
|
}
|
|
1029
1001
|
if (!authHeader)
|
|
1030
1002
|
return {};
|
|
@@ -1042,17 +1014,17 @@ class DeployerService extends BaseService_1.BaseService {
|
|
|
1042
1014
|
if (user && PasswordHash_1.PasswordHash.isHash(user.password))
|
|
1043
1015
|
return (await PasswordHash_1.PasswordHash.verify(token, user.password)) ? { user } : {};
|
|
1044
1016
|
}
|
|
1045
|
-
let plainMatch =
|
|
1017
|
+
let plainMatch = null;
|
|
1046
1018
|
for (const user of users) {
|
|
1047
1019
|
if (PasswordHash_1.PasswordHash.isHash(user.password) || !user.password)
|
|
1048
1020
|
continue;
|
|
1049
1021
|
const expected = crypto_1.default.createHash("sha256").update(user.login + ":" + user.password).digest("hex");
|
|
1050
1022
|
if (this.tokensMatch(token, expected))
|
|
1051
|
-
plainMatch =
|
|
1023
|
+
plainMatch = user.login;
|
|
1052
1024
|
}
|
|
1053
1025
|
if (plainMatch) {
|
|
1054
|
-
(0, LogService_1.logError)(
|
|
1055
|
-
return {
|
|
1026
|
+
(0, LogService_1.logError)(`Deployer: "${plainMatch}" sent the right credentials, but setup.json stores the password as plain text — refused; store a hash: npx badmfck hash-password --deployer`);
|
|
1027
|
+
return {};
|
|
1056
1028
|
}
|
|
1057
1029
|
if (!login)
|
|
1058
1030
|
(0, LogService_1.logWarn)("Deployer auth failed without X-Deployer-Login — update the badmfck CLI on the client");
|
|
@@ -1401,7 +1373,7 @@ class DeployerService extends BaseService_1.BaseService {
|
|
|
1401
1373
|
}
|
|
1402
1374
|
if (!ignoreDirectoryClean) {
|
|
1403
1375
|
fs_1.default.readdirSync(destination).forEach((file) => {
|
|
1404
|
-
if (file === "__tmp")
|
|
1376
|
+
if (file === "__tmp" || file === BlockedIps_1.BLOCKED_IPS_FILE)
|
|
1405
1377
|
return;
|
|
1406
1378
|
try {
|
|
1407
1379
|
fs_1.default.rmSync(path_1.default.resolve(destination, file), { recursive: true });
|
|
@@ -1411,6 +1383,11 @@ class DeployerService extends BaseService_1.BaseService {
|
|
|
1411
1383
|
}
|
|
1412
1384
|
});
|
|
1413
1385
|
}
|
|
1386
|
+
if (fs_1.default.existsSync(path_1.default.resolve(destination, BlockedIps_1.BLOCKED_IPS_FILE)))
|
|
1387
|
+
try {
|
|
1388
|
+
fs_1.default.rmSync(path_1.default.resolve(destination, "__tmp", BlockedIps_1.BLOCKED_IPS_FILE), { force: true });
|
|
1389
|
+
}
|
|
1390
|
+
catch (_) { }
|
|
1414
1391
|
try {
|
|
1415
1392
|
(0, child_process_1.execFileSync)("cp", ["-RPf", path_1.default.resolve(destination, "__tmp") + "/.", destination]);
|
|
1416
1393
|
}
|
|
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.askHidden = askHidden;
|
|
6
7
|
exports.HashPassword = HashPassword;
|
|
7
8
|
exports.ProjectToken = ProjectToken;
|
|
8
9
|
const crypto_1 = __importDefault(require("crypto"));
|
|
@@ -13,9 +14,9 @@ const MonitorAuth_1 = require("../monitor/MonitorAuth");
|
|
|
13
14
|
const cliCommon_1 = require("./cliCommon");
|
|
14
15
|
function askHidden(question) {
|
|
15
16
|
return new Promise(resolve => {
|
|
16
|
-
process_1.stdout.write(question);
|
|
17
17
|
const wasRaw = !!process_1.stdin.isRaw;
|
|
18
18
|
process_1.stdin.setRawMode(true);
|
|
19
|
+
process_1.stdout.write(question);
|
|
19
20
|
process_1.stdin.setEncoding("utf8");
|
|
20
21
|
process_1.stdin.resume();
|
|
21
22
|
let value = "";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function ProjectConfigWizard(): Promise<void>;
|