badmfck-api-server 6.0.2 → 6.0.4
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 +147 -4
- package/dist/apiServer/APIService.d.ts +1 -0
- package/dist/apiServer/APIService.js +5 -1
- package/dist/apiServer/deployment/ConfigService.d.ts +24 -0
- package/dist/apiServer/deployment/ConfigService.js +15 -0
- package/dist/apiServer/deployment/Deploy.js +18 -0
- package/dist/apiServer/deployment/DeployerService.d.ts +47 -6
- package/dist/apiServer/deployment/DeployerService.js +330 -39
- package/dist/apiServer/deployment/Notifier.d.ts +14 -0
- package/dist/apiServer/deployment/Notifier.js +22 -0
- package/dist/apiServer/deployment/ReviewStore.d.ts +62 -0
- package/dist/apiServer/deployment/ReviewStore.js +186 -0
- package/dist/apiServer/deployment/SecretsInjector.d.ts +21 -0
- package/dist/apiServer/deployment/SecretsInjector.js +188 -0
- 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 +1 -0
- package/dist/apiServer/monitor/Monitor.js +16 -0
- package/dist/apiServer/review/Review.d.ts +23 -0
- package/dist/apiServer/review/Review.js +149 -0
- package/dist/apiServer/review/ReviewAuth.d.ts +56 -0
- package/dist/apiServer/review/ReviewAuth.js +147 -0
- package/dist/apiServer/review/ReviewDiff.d.ts +43 -0
- package/dist/apiServer/review/ReviewDiff.js +175 -0
- package/dist/apiServer/review/index.html +501 -0
- package/package.json +2 -2
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ReviewStore = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
10
|
+
const NAME_RE = /^[A-Za-z0-9._-]+$/;
|
|
11
|
+
class ReviewStore {
|
|
12
|
+
dir;
|
|
13
|
+
constructor(projectsPath) {
|
|
14
|
+
this.dir = path_1.default.resolve(projectsPath, ".review");
|
|
15
|
+
}
|
|
16
|
+
static hashToken(token) {
|
|
17
|
+
return crypto_1.default.createHash("sha256").update(String(token)).digest("hex");
|
|
18
|
+
}
|
|
19
|
+
file(project, kind) {
|
|
20
|
+
if (!NAME_RE.test(project))
|
|
21
|
+
throw new Error("invalid project name");
|
|
22
|
+
return path_1.default.join(this.dir, `${project}.${kind}`);
|
|
23
|
+
}
|
|
24
|
+
archivePath(project) { return this.file(project, "pending.tgz"); }
|
|
25
|
+
lastArchivePath(project) { return this.file(project, "last.tgz"); }
|
|
26
|
+
ensureDir() {
|
|
27
|
+
fs_1.default.mkdirSync(this.dir, { recursive: true, mode: 0o700 });
|
|
28
|
+
}
|
|
29
|
+
static sha256File(file) {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const h = crypto_1.default.createHash("sha256");
|
|
32
|
+
fs_1.default.createReadStream(file).on("data", d => h.update(d)).on("error", reject).on("end", () => resolve(h.digest("hex")));
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
writeJson(file, value) {
|
|
36
|
+
const tmp = file + ".tmp";
|
|
37
|
+
fs_1.default.writeFileSync(tmp, JSON.stringify(value, null, 2), { encoding: "utf-8", mode: 0o600 });
|
|
38
|
+
fs_1.default.renameSync(tmp, file);
|
|
39
|
+
}
|
|
40
|
+
async create(project, file, meta) {
|
|
41
|
+
this.ensureDir();
|
|
42
|
+
const replaced = this.get(project) !== null;
|
|
43
|
+
this.remove(project);
|
|
44
|
+
const archive = this.archivePath(project);
|
|
45
|
+
await new Promise((resolve, reject) => file.mv(archive, (err) => err ? reject(err) : resolve()));
|
|
46
|
+
fs_1.default.chmodSync(archive, 0o600);
|
|
47
|
+
const tokens = {};
|
|
48
|
+
const reviewers = {};
|
|
49
|
+
for (const login of meta.reviewers) {
|
|
50
|
+
const token = crypto_1.default.randomBytes(32).toString("base64url");
|
|
51
|
+
tokens[login] = token;
|
|
52
|
+
reviewers[login] = { tokenHash: ReviewStore.hashToken(token), decision: null, at: null, ip: null };
|
|
53
|
+
}
|
|
54
|
+
const now = Date.now();
|
|
55
|
+
const pending = {
|
|
56
|
+
project,
|
|
57
|
+
state: "pending",
|
|
58
|
+
sha256: await ReviewStore.sha256File(archive),
|
|
59
|
+
size: fs_1.default.statSync(archive).size,
|
|
60
|
+
uploadedBy: meta.uploadedBy,
|
|
61
|
+
uploadedFrom: meta.uploadedFrom,
|
|
62
|
+
uploadedAt: now,
|
|
63
|
+
expiresAt: now + meta.ttlMs,
|
|
64
|
+
mode: meta.mode,
|
|
65
|
+
reviewers,
|
|
66
|
+
};
|
|
67
|
+
this.writeJson(this.file(project, "pending.json"), pending);
|
|
68
|
+
return { pending, tokens, replaced };
|
|
69
|
+
}
|
|
70
|
+
get(project) {
|
|
71
|
+
try {
|
|
72
|
+
const p = JSON.parse(fs_1.default.readFileSync(this.file(project, "pending.json"), "utf-8"));
|
|
73
|
+
return p && typeof p === "object" && p.project === project ? p : null;
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
save(pending) {
|
|
80
|
+
this.writeJson(this.file(pending.project, "pending.json"), pending);
|
|
81
|
+
}
|
|
82
|
+
findByToken(token) {
|
|
83
|
+
if (typeof token !== "string" || token.length < 20 || token.length > 200)
|
|
84
|
+
return null;
|
|
85
|
+
const hash = Buffer.from(ReviewStore.hashToken(token), "hex");
|
|
86
|
+
let entries;
|
|
87
|
+
try {
|
|
88
|
+
entries = fs_1.default.readdirSync(this.dir);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
for (const f of entries) {
|
|
94
|
+
if (!f.endsWith(".pending.json"))
|
|
95
|
+
continue;
|
|
96
|
+
const pending = this.get(f.slice(0, -".pending.json".length));
|
|
97
|
+
if (!pending || pending.expiresAt < Date.now())
|
|
98
|
+
continue;
|
|
99
|
+
for (const [login, r] of Object.entries(pending.reviewers)) {
|
|
100
|
+
const stored = Buffer.from(String(r.tokenHash), "hex");
|
|
101
|
+
if (stored.length === hash.length && crypto_1.default.timingSafeEqual(stored, hash))
|
|
102
|
+
return { pending, reviewer: login };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
async archiveIntact(pending) {
|
|
108
|
+
try {
|
|
109
|
+
return (await ReviewStore.sha256File(this.archivePath(pending.project))) === pending.sha256;
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
journal(project, event, details) {
|
|
116
|
+
try {
|
|
117
|
+
this.ensureDir();
|
|
118
|
+
const file = path_1.default.join(this.dir, `${project}.history.jsonl`);
|
|
119
|
+
if (!NAME_RE.test(project))
|
|
120
|
+
return;
|
|
121
|
+
try {
|
|
122
|
+
if (fs_1.default.statSync(file).size > 1024 * 1024)
|
|
123
|
+
fs_1.default.renameSync(file, file + ".1");
|
|
124
|
+
}
|
|
125
|
+
catch { }
|
|
126
|
+
fs_1.default.appendFileSync(file, JSON.stringify({ at: new Date().toISOString(), event, ...details }) + "\n", { encoding: "utf-8", mode: 0o600 });
|
|
127
|
+
}
|
|
128
|
+
catch { }
|
|
129
|
+
}
|
|
130
|
+
dropArchive(project) {
|
|
131
|
+
try {
|
|
132
|
+
fs_1.default.rmSync(this.archivePath(project), { force: true });
|
|
133
|
+
}
|
|
134
|
+
catch { }
|
|
135
|
+
}
|
|
136
|
+
sweep() {
|
|
137
|
+
const expired = [];
|
|
138
|
+
let entries;
|
|
139
|
+
try {
|
|
140
|
+
entries = fs_1.default.readdirSync(this.dir);
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return expired;
|
|
144
|
+
}
|
|
145
|
+
for (const f of entries) {
|
|
146
|
+
if (!f.endsWith(".pending.json"))
|
|
147
|
+
continue;
|
|
148
|
+
const pending = this.get(f.slice(0, -".pending.json".length));
|
|
149
|
+
if (!pending || pending.state !== "pending" || pending.expiresAt >= Date.now())
|
|
150
|
+
continue;
|
|
151
|
+
pending.state = "expired";
|
|
152
|
+
pending.result = { at: Date.now(), message: "Nobody decided in time — upload again to restart the review." };
|
|
153
|
+
this.save(pending);
|
|
154
|
+
this.dropArchive(pending.project);
|
|
155
|
+
this.journal(pending.project, "expired", { sha256: pending.sha256 });
|
|
156
|
+
expired.push(pending.project);
|
|
157
|
+
}
|
|
158
|
+
return expired;
|
|
159
|
+
}
|
|
160
|
+
remove(project) {
|
|
161
|
+
for (const kind of ["pending.json", "pending.tgz"])
|
|
162
|
+
try {
|
|
163
|
+
fs_1.default.rmSync(this.file(project, kind), { force: true });
|
|
164
|
+
}
|
|
165
|
+
catch { }
|
|
166
|
+
}
|
|
167
|
+
stageBaseline(project, archive) {
|
|
168
|
+
this.ensureDir();
|
|
169
|
+
const incoming = this.file(project, "incoming.tgz");
|
|
170
|
+
fs_1.default.copyFileSync(archive, incoming);
|
|
171
|
+
fs_1.default.chmodSync(incoming, 0o600);
|
|
172
|
+
}
|
|
173
|
+
commitBaseline(project) {
|
|
174
|
+
try {
|
|
175
|
+
fs_1.default.renameSync(this.file(project, "incoming.tgz"), this.lastArchivePath(project));
|
|
176
|
+
}
|
|
177
|
+
catch { }
|
|
178
|
+
}
|
|
179
|
+
dropStagedBaseline(project) {
|
|
180
|
+
try {
|
|
181
|
+
fs_1.default.rmSync(this.file(project, "incoming.tgz"), { force: true });
|
|
182
|
+
}
|
|
183
|
+
catch { }
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
exports.ReviewStore = ReviewStore;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface ISecretsSettings {
|
|
2
|
+
dir?: string;
|
|
3
|
+
file?: string;
|
|
4
|
+
}
|
|
5
|
+
export declare class SecretsInjector {
|
|
6
|
+
static resolvePath(project: string, slot: string | null, settings: ISecretsSettings | undefined, defaultDir: string | undefined, destinationRoot: string): {
|
|
7
|
+
path?: string;
|
|
8
|
+
error?: string;
|
|
9
|
+
};
|
|
10
|
+
static write(file: string, values: Record<string, unknown>): void;
|
|
11
|
+
static verify(file: string, values: Record<string, unknown>): string | null;
|
|
12
|
+
static expression(file: string, key: string): string;
|
|
13
|
+
static transform(source: string, keys: Set<string>, file: string): {
|
|
14
|
+
content: string;
|
|
15
|
+
rewritten: number;
|
|
16
|
+
leftover: boolean;
|
|
17
|
+
};
|
|
18
|
+
private static endsInBlockComment;
|
|
19
|
+
private static rewriteLiteral;
|
|
20
|
+
static parses(source: string): boolean;
|
|
21
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SecretsInjector = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const vm_1 = __importDefault(require("vm"));
|
|
10
|
+
const PLACEHOLDER = /_\{\{([A-Za-z0-9_]+)\}\}_/g;
|
|
11
|
+
class SecretsInjector {
|
|
12
|
+
static resolvePath(project, slot, settings, defaultDir, destinationRoot) {
|
|
13
|
+
const file = settings?.file?.trim(), dir = (settings?.dir?.trim() || defaultDir?.trim() || "");
|
|
14
|
+
let target;
|
|
15
|
+
if (file) {
|
|
16
|
+
if (!path_1.default.isAbsolute(file))
|
|
17
|
+
return { error: `secrets.file must be an absolute path ("${file}")` };
|
|
18
|
+
const ext = path_1.default.extname(file) || ".json";
|
|
19
|
+
target = slot ? path_1.default.join(path_1.default.dirname(file), `${path_1.default.basename(file, path_1.default.extname(file))}-${slot}${ext}`) : file;
|
|
20
|
+
}
|
|
21
|
+
else if (dir) {
|
|
22
|
+
if (!path_1.default.isAbsolute(dir))
|
|
23
|
+
return { error: `the secrets directory must be an absolute path ("${dir}")` };
|
|
24
|
+
target = path_1.default.join(dir, `${project}${slot ? "-" + slot : ""}.json`);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
return { error: "secrets is enabled, but neither secrets.dir / secrets.file nor secrets_dir in setup.json is set" };
|
|
28
|
+
}
|
|
29
|
+
target = path_1.default.resolve(target);
|
|
30
|
+
const root = path_1.default.resolve(destinationRoot);
|
|
31
|
+
if (target === root || target.startsWith(root + path_1.default.sep))
|
|
32
|
+
return { error: `the secrets file must live outside the deploy folder ("${target}" is inside "${root}")` };
|
|
33
|
+
return { path: target };
|
|
34
|
+
}
|
|
35
|
+
static write(file, values) {
|
|
36
|
+
fs_1.default.mkdirSync(path_1.default.dirname(file), { recursive: true, mode: 0o700 });
|
|
37
|
+
const tmp = file + ".tmp";
|
|
38
|
+
fs_1.default.writeFileSync(tmp, JSON.stringify(values, null, 2), { encoding: "utf-8", mode: 0o600 });
|
|
39
|
+
fs_1.default.chmodSync(tmp, 0o600);
|
|
40
|
+
fs_1.default.renameSync(tmp, file);
|
|
41
|
+
}
|
|
42
|
+
static verify(file, values) {
|
|
43
|
+
try {
|
|
44
|
+
fs_1.default.accessSync(file, fs_1.default.constants.R_OK);
|
|
45
|
+
const back = JSON.parse(fs_1.default.readFileSync(file, "utf-8"));
|
|
46
|
+
const missing = Object.keys(values).filter(k => !Object.prototype.hasOwnProperty.call(back, k));
|
|
47
|
+
return missing.length ? `${file} lacks ${missing.join(", ")}` : null;
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
return `${file} is not readable: ${e?.message ?? e}`;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
static expression(file, key) {
|
|
54
|
+
const p = JSON.stringify(file);
|
|
55
|
+
return `(function(k){var s=globalThis.__badmfckSecrets||(globalThis.__badmfckSecrets=JSON.parse(require("fs").readFileSync(${p},"utf8")));`
|
|
56
|
+
+ `if(!Object.prototype.hasOwnProperty.call(s,k))throw new Error("secret \\""+k+"\\" is missing in "+${p});return String(s[k]);})(${JSON.stringify(key)})`;
|
|
57
|
+
}
|
|
58
|
+
static transform(source, keys, file) {
|
|
59
|
+
let rewritten = 0, inBlockComment = false;
|
|
60
|
+
const lines = source.split("\n");
|
|
61
|
+
for (let n = 0; n < lines.length; n++) {
|
|
62
|
+
const line = lines[n];
|
|
63
|
+
if (!inBlockComment && line.indexOf("_{{") === -1) {
|
|
64
|
+
inBlockComment = SecretsInjector.endsInBlockComment(line, false);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const defuse = (text) => text.replace(/_\{\{/g, "_{ {");
|
|
68
|
+
let out = "", i = 0;
|
|
69
|
+
while (i < line.length) {
|
|
70
|
+
if (inBlockComment) {
|
|
71
|
+
const end = line.indexOf("*/", i);
|
|
72
|
+
if (end === -1) {
|
|
73
|
+
out += defuse(line.slice(i));
|
|
74
|
+
i = line.length;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
out += defuse(line.slice(i, end + 2));
|
|
78
|
+
i = end + 2;
|
|
79
|
+
inBlockComment = false;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const c = line[i];
|
|
83
|
+
if (c === "/" && line[i + 1] === "/") {
|
|
84
|
+
out += defuse(line.slice(i));
|
|
85
|
+
i = line.length;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
if (c === "/" && line[i + 1] === "*") {
|
|
89
|
+
inBlockComment = true;
|
|
90
|
+
out += "/*";
|
|
91
|
+
i += 2;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
95
|
+
let j = i + 1;
|
|
96
|
+
while (j < line.length && line[j] !== c)
|
|
97
|
+
j += line[j] === "\\" ? 2 : 1;
|
|
98
|
+
if (j >= line.length) {
|
|
99
|
+
out += line.slice(i);
|
|
100
|
+
i = line.length;
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
const literal = line.slice(i, j + 1);
|
|
104
|
+
const rep = SecretsInjector.rewriteLiteral(literal, c, keys, file);
|
|
105
|
+
if (rep !== literal)
|
|
106
|
+
rewritten++;
|
|
107
|
+
out += rep;
|
|
108
|
+
i = j + 1;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
out += c;
|
|
112
|
+
i++;
|
|
113
|
+
}
|
|
114
|
+
lines[n] = out;
|
|
115
|
+
}
|
|
116
|
+
const content = lines.join("\n");
|
|
117
|
+
let leftover = false;
|
|
118
|
+
for (const m of content.matchAll(PLACEHOLDER))
|
|
119
|
+
if (keys.has(m[1])) {
|
|
120
|
+
leftover = true;
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
return { content, rewritten, leftover };
|
|
124
|
+
}
|
|
125
|
+
static endsInBlockComment(line, open) {
|
|
126
|
+
let i = 0;
|
|
127
|
+
while (i < line.length) {
|
|
128
|
+
if (open) {
|
|
129
|
+
const e = line.indexOf("*/", i);
|
|
130
|
+
if (e === -1)
|
|
131
|
+
return true;
|
|
132
|
+
open = false;
|
|
133
|
+
i = e + 2;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
const c = line[i];
|
|
137
|
+
if (c === "/" && line[i + 1] === "/")
|
|
138
|
+
return false;
|
|
139
|
+
if (c === "/" && line[i + 1] === "*") {
|
|
140
|
+
open = true;
|
|
141
|
+
i += 2;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
145
|
+
let j = i + 1;
|
|
146
|
+
while (j < line.length && line[j] !== c)
|
|
147
|
+
j += line[j] === "\\" ? 2 : 1;
|
|
148
|
+
i = j + 1;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
i++;
|
|
152
|
+
}
|
|
153
|
+
return open;
|
|
154
|
+
}
|
|
155
|
+
static rewriteLiteral(literal, quote, keys, file) {
|
|
156
|
+
const body = literal.slice(1, -1);
|
|
157
|
+
if (quote === "`" && body.includes("${"))
|
|
158
|
+
return literal;
|
|
159
|
+
const parts = [];
|
|
160
|
+
let last = 0, found = false;
|
|
161
|
+
for (const m of body.matchAll(PLACEHOLDER)) {
|
|
162
|
+
if (!keys.has(m[1]))
|
|
163
|
+
continue;
|
|
164
|
+
found = true;
|
|
165
|
+
const before = body.slice(last, m.index);
|
|
166
|
+
if (before)
|
|
167
|
+
parts.push(quote + before + quote);
|
|
168
|
+
parts.push(SecretsInjector.expression(file, m[1]));
|
|
169
|
+
last = m.index + m[0].length;
|
|
170
|
+
}
|
|
171
|
+
if (!found)
|
|
172
|
+
return literal;
|
|
173
|
+
const after = body.slice(last);
|
|
174
|
+
if (after)
|
|
175
|
+
parts.push(quote + after + quote);
|
|
176
|
+
return "(" + (parts.length === 1 ? parts[0] : `""+` + parts.join("+")) + ")";
|
|
177
|
+
}
|
|
178
|
+
static parses(source) {
|
|
179
|
+
try {
|
|
180
|
+
new vm_1.default.Script(`(function (exports, require, module, __filename, __dirname) {${source}\n})`);
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
exports.SecretsInjector = SecretsInjector;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import net from "net";
|
|
2
|
+
export declare class IpAllowList {
|
|
3
|
+
static build(entries: readonly string[] | undefined | null): {
|
|
4
|
+
list: net.BlockList | null;
|
|
5
|
+
error?: string;
|
|
6
|
+
};
|
|
7
|
+
static allows(list: net.BlockList | null, ip: string | undefined | null): boolean;
|
|
8
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.IpAllowList = void 0;
|
|
7
|
+
const net_1 = __importDefault(require("net"));
|
|
8
|
+
class IpAllowList {
|
|
9
|
+
static build(entries) {
|
|
10
|
+
const items = (entries ?? []).map(e => String(e).trim()).filter(e => e.length > 0);
|
|
11
|
+
if (items.length === 0)
|
|
12
|
+
return { list: null };
|
|
13
|
+
const list = new net_1.default.BlockList();
|
|
14
|
+
for (const item of items) {
|
|
15
|
+
const [addr, prefix, extra] = item.split("/");
|
|
16
|
+
const family = net_1.default.isIP(addr);
|
|
17
|
+
if (!family || extra !== undefined)
|
|
18
|
+
return { list: null, error: `"${item}" is not an IP address or subnet` };
|
|
19
|
+
const type = family === 6 ? "ipv6" : "ipv4";
|
|
20
|
+
if (prefix === undefined) {
|
|
21
|
+
list.addAddress(addr, type);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const bits = Number(prefix), max = family === 6 ? 128 : 32;
|
|
25
|
+
if (!/^\d+$/.test(prefix) || bits > max)
|
|
26
|
+
return { list: null, error: `"${item}" has an invalid prefix (0–${max})` };
|
|
27
|
+
list.addSubnet(addr, bits, type);
|
|
28
|
+
}
|
|
29
|
+
return { list };
|
|
30
|
+
}
|
|
31
|
+
static allows(list, ip) {
|
|
32
|
+
if (!list)
|
|
33
|
+
return true;
|
|
34
|
+
const family = net_1.default.isIP(String(ip ?? ""));
|
|
35
|
+
if (!family)
|
|
36
|
+
return false;
|
|
37
|
+
return list.check(String(ip), family === 6 ? "ipv6" : "ipv4");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.IpAllowList = IpAllowList;
|
|
@@ -2,6 +2,7 @@ import type { IMonitorUser, IDocumentationUser } from "../APIService";
|
|
|
2
2
|
export interface IAccess {
|
|
3
3
|
monitor?: IMonitorUser[];
|
|
4
4
|
documentation?: IDocumentationUser[];
|
|
5
|
+
monitorAllowedIps?: string[];
|
|
5
6
|
}
|
|
6
7
|
export declare class AccessStore {
|
|
7
8
|
private onChange?;
|
|
@@ -13,6 +14,7 @@ export declare class AccessStore {
|
|
|
13
14
|
constructor(source: IAccess | string | undefined | null, onChange?: ((prev: IAccess, next: IAccess) => void) | undefined);
|
|
14
15
|
get monitor(): IMonitorUser[];
|
|
15
16
|
get documentation(): IDocumentationUser[];
|
|
17
|
+
get monitorAllowedIps(): string[];
|
|
16
18
|
static validate(value: any): string | null;
|
|
17
19
|
private loadSync;
|
|
18
20
|
private reload;
|
|
@@ -8,6 +8,7 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
9
|
const crypto_1 = __importDefault(require("crypto"));
|
|
10
10
|
const LogService_1 = require("../LogService");
|
|
11
|
+
const IpAllowList_1 = require("../helper/IpAllowList");
|
|
11
12
|
const ROLES = ["admin", "it", "analytic"];
|
|
12
13
|
class AccessStore {
|
|
13
14
|
onChange;
|
|
@@ -28,9 +29,17 @@ class AccessStore {
|
|
|
28
29
|
}
|
|
29
30
|
get monitor() { return this.current.monitor ?? []; }
|
|
30
31
|
get documentation() { return this.current.documentation ?? []; }
|
|
32
|
+
get monitorAllowedIps() { return this.current.monitorAllowedIps ?? []; }
|
|
31
33
|
static validate(value) {
|
|
32
34
|
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
33
35
|
return "root must be an object";
|
|
36
|
+
if (value.monitorAllowedIps !== undefined) {
|
|
37
|
+
if (!Array.isArray(value.monitorAllowedIps) || value.monitorAllowedIps.some((e) => typeof e !== "string"))
|
|
38
|
+
return "monitorAllowedIps must be an array of strings";
|
|
39
|
+
const ips = IpAllowList_1.IpAllowList.build(value.monitorAllowedIps);
|
|
40
|
+
if (ips.error)
|
|
41
|
+
return "monitorAllowedIps: " + ips.error;
|
|
42
|
+
}
|
|
34
43
|
for (const list of ["monitor", "documentation"]) {
|
|
35
44
|
const users = value[list];
|
|
36
45
|
if (users === undefined)
|
|
@@ -99,8 +108,9 @@ class AccessStore {
|
|
|
99
108
|
this.hash = hash;
|
|
100
109
|
this.refusedHash = "";
|
|
101
110
|
this.warnPermissions();
|
|
102
|
-
this.apply({ monitor: parsed.monitor, documentation: parsed.documentation });
|
|
103
|
-
(0, LogService_1.logInfo)(`Access ${initial ? "loaded" : "reloaded"} from ${this.file}: ${this.monitor.length} monitor, ${this.documentation.length} documentation user(s)`
|
|
111
|
+
this.apply({ monitor: parsed.monitor, documentation: parsed.documentation, monitorAllowedIps: parsed.monitorAllowedIps });
|
|
112
|
+
(0, LogService_1.logInfo)(`Access ${initial ? "loaded" : "reloaded"} from ${this.file}: ${this.monitor.length} monitor, ${this.documentation.length} documentation user(s)`
|
|
113
|
+
+ (this.monitorAllowedIps.length ? `, monitor limited to ${this.monitorAllowedIps.join(", ")}` : ""));
|
|
104
114
|
}
|
|
105
115
|
refuse(reason, initial) {
|
|
106
116
|
if (initial)
|
|
@@ -21,6 +21,7 @@ export declare class Monitor extends BaseEndpoint {
|
|
|
21
21
|
fileParsed: boolean;
|
|
22
22
|
lastTimeCacheChecked: number;
|
|
23
23
|
constructor(options: APIServiceOptions);
|
|
24
|
+
__precheck(req: HTTPRequestVO): Promise<TransferPacketVO<any> | null>;
|
|
24
25
|
loadHTMLFile(): Promise<string>;
|
|
25
26
|
html(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
26
27
|
controllers(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.Monitor = exports.S_MONITOR_RESTART_REQUESTED = exports.S_MONITOR_REGISTRATE_ACTION = void 0;
|
|
7
7
|
const badmfck_signal_1 = require("badmfck-signal");
|
|
8
8
|
const APIService_1 = require("../APIService");
|
|
9
|
+
const IpAllowList_1 = require("../helper/IpAllowList");
|
|
9
10
|
const BaseEndpoint_1 = require("../BaseEndpoint");
|
|
10
11
|
const BaseService_1 = require("../BaseService");
|
|
11
12
|
const LogService_1 = require("../LogService");
|
|
@@ -103,6 +104,21 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
|
103
104
|
} }
|
|
104
105
|
]);
|
|
105
106
|
}
|
|
107
|
+
async __precheck(req) {
|
|
108
|
+
const entries = await APIService_1.REQ_MONITOR_ALLOWED_IPS.request();
|
|
109
|
+
if (!entries || entries.length === 0)
|
|
110
|
+
return null;
|
|
111
|
+
const { list, error } = IpAllowList_1.IpAllowList.build(entries);
|
|
112
|
+
if (error) {
|
|
113
|
+
(0, LogService_1.logError)(`Monitor: monitorAllowedIps is invalid (${error}) — the monitor refuses every request until it is fixed`);
|
|
114
|
+
return { error: { ...DefaultErrors_1.default.FORBIDDEN, details: "The monitor is closed: its address allow-list is invalid." } };
|
|
115
|
+
}
|
|
116
|
+
if (!IpAllowList_1.IpAllowList.allows(list, req.ip)) {
|
|
117
|
+
(0, LogService_1.logWarn)(`Monitor: request refused from ${req.ip ?? "unknown"} — not in monitorAllowedIps`);
|
|
118
|
+
return { error: { ...DefaultErrors_1.default.FORBIDDEN, details: "The monitor is not available from this address." } };
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
106
122
|
async loadHTMLFile() {
|
|
107
123
|
if (!this.indexHtmlFile)
|
|
108
124
|
this.indexHtmlFile = fs_1.default.readFileSync(path_1.default.resolve(__dirname, "index.html")).toString("utf-8");
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { APIServiceOptions } from "../APIService";
|
|
2
|
+
import { BaseEndpoint } from "../BaseEndpoint";
|
|
3
|
+
import { HTTPRequestVO, TransferPacketVO } from "../structures/Interfaces";
|
|
4
|
+
import { DeployerService } from "../deployment/DeployerService";
|
|
5
|
+
export declare class Review extends BaseEndpoint {
|
|
6
|
+
private deployer;
|
|
7
|
+
private apiServiceOptions;
|
|
8
|
+
ignoreInDocumentation: boolean;
|
|
9
|
+
private indexHtmlFile;
|
|
10
|
+
private auth;
|
|
11
|
+
constructor(deployer: DeployerService, apiServiceOptions: APIServiceOptions);
|
|
12
|
+
private gate;
|
|
13
|
+
private session;
|
|
14
|
+
private static view;
|
|
15
|
+
html(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
16
|
+
info(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
17
|
+
login(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
18
|
+
enroll2fa(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
19
|
+
details(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
20
|
+
diff(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
21
|
+
decide(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
22
|
+
logout(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
|
23
|
+
}
|