@7365admin1/core 3.100.0 → 3.100.1
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 +6 -0
- package/dist/index.d.ts +14 -1
- package/dist/index.js +25 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +24 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/test/e2e/site-camera-password.e2e.test.mjs +274 -0
- package/test/site-camera-password.test.mjs +49 -0
package/package.json
CHANGED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
// End-to-end: an ANPR camera's device password is write-only, and switching a
|
|
2
|
+
// camera on or off takes effect on its save, without an API restart.
|
|
3
|
+
//
|
|
4
|
+
// Real controller, service, repository and Dahua listener, against the
|
|
5
|
+
// throwaway harness (in-process MongoDB, loopback Redis). The "camera" is a
|
|
6
|
+
// loopback HTTP server that speaks digest auth and checks the password it is
|
|
7
|
+
// logged in with, so the test proves the listener started with the STORED
|
|
8
|
+
// password. No device, staging or production system is reached. Seeded values
|
|
9
|
+
// are fake and nothing secret is printed.
|
|
10
|
+
//
|
|
11
|
+
// Run with: yarn test:e2e
|
|
12
|
+
|
|
13
|
+
import { after, before, describe, it } from "node:test";
|
|
14
|
+
import assert from "node:assert/strict";
|
|
15
|
+
import http from "node:http";
|
|
16
|
+
import crypto from "node:crypto";
|
|
17
|
+
import { once } from "node:events";
|
|
18
|
+
import { createRequire } from "node:module";
|
|
19
|
+
import express from "express";
|
|
20
|
+
import { ObjectId } from "mongodb";
|
|
21
|
+
|
|
22
|
+
import { startHarness } from "./harness.mjs";
|
|
23
|
+
|
|
24
|
+
const require = createRequire(import.meta.url);
|
|
25
|
+
|
|
26
|
+
const PASSWORD = "CamPw-Passw0rd!";
|
|
27
|
+
const MANAGER = "campw-manager@e2e.example.com";
|
|
28
|
+
const CAM_USER = "e2e-admin";
|
|
29
|
+
const CAM_PASS = "E2E-cam-secret-1";
|
|
30
|
+
|
|
31
|
+
const md5 = (s) => crypto.createHash("md5").update(s).digest("hex");
|
|
32
|
+
|
|
33
|
+
/** A loopback "Dahua" that answers the event stream only to the right login. */
|
|
34
|
+
async function startFakeCamera() {
|
|
35
|
+
const seen = { streams: 0, authed: 0, refused: 0 };
|
|
36
|
+
const server = http.createServer((req, res) => {
|
|
37
|
+
if (!req.url.startsWith("/cgi-bin/snapManager.cgi")) {
|
|
38
|
+
res.writeHead(404).end();
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
seen.streams++;
|
|
42
|
+
const header = String(req.headers.authorization ?? "");
|
|
43
|
+
if (!header.startsWith("Digest ")) {
|
|
44
|
+
res.writeHead(401, {
|
|
45
|
+
"WWW-Authenticate": 'Digest realm="e2e", qop="auth", nonce="e2enonce", opaque="e2eopaque"',
|
|
46
|
+
});
|
|
47
|
+
res.end();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const p = Object.fromEntries(
|
|
51
|
+
[...header.slice(7).matchAll(/(\w+)="?([^",]*)"?/g)].map((m) => [m[1], m[2]]),
|
|
52
|
+
);
|
|
53
|
+
const ha1 = md5(`${p.username}:e2e:${CAM_PASS}`);
|
|
54
|
+
const ha2 = md5(`${req.method}:${p.uri}`);
|
|
55
|
+
const expected = md5(`${ha1}:${p.nonce}:${p.nc}:${p.cnonce}:${p.qop}:${ha2}`);
|
|
56
|
+
if (p.username !== CAM_USER || p.response !== expected) {
|
|
57
|
+
seen.refused++;
|
|
58
|
+
res.writeHead(403).end();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
seen.authed++;
|
|
62
|
+
// Hold the event stream open, as the device does.
|
|
63
|
+
res.writeHead(200, { "content-type": "multipart/x-mixed-replace; boundary=myboundary" });
|
|
64
|
+
res.write("--myboundary\r\n");
|
|
65
|
+
});
|
|
66
|
+
server.listen(0, "127.0.0.1");
|
|
67
|
+
await once(server, "listening");
|
|
68
|
+
return {
|
|
69
|
+
seen,
|
|
70
|
+
host: `http://127.0.0.1:${server.address().port}`,
|
|
71
|
+
stop: () => {
|
|
72
|
+
server.closeAllConnections();
|
|
73
|
+
server.close();
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function until(fn, ms = 8000) {
|
|
79
|
+
const end = Date.now() + ms;
|
|
80
|
+
while (Date.now() < end) {
|
|
81
|
+
if (await fn()) return true;
|
|
82
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
describe("site camera password is write-only; activation is picked up on save", { concurrency: 1 }, () => {
|
|
88
|
+
let h;
|
|
89
|
+
let cam;
|
|
90
|
+
let app;
|
|
91
|
+
let base;
|
|
92
|
+
let sid;
|
|
93
|
+
let dahua;
|
|
94
|
+
const id = {};
|
|
95
|
+
|
|
96
|
+
// mirrors iservice365-API-core/src/routes/site-camera.route.ts (the three
|
|
97
|
+
// routes under test), on its own server so the shared harness is untouched.
|
|
98
|
+
async function api(path, { method = "GET", body } = {}) {
|
|
99
|
+
const res = await fetch(`${base}${path}`, {
|
|
100
|
+
method,
|
|
101
|
+
headers: {
|
|
102
|
+
authorization: `Bearer ${sid}`,
|
|
103
|
+
...(body ? { "content-type": "application/json" } : {}),
|
|
104
|
+
},
|
|
105
|
+
...(body ? { body: JSON.stringify(body) } : {}),
|
|
106
|
+
});
|
|
107
|
+
const text = await res.text();
|
|
108
|
+
let json;
|
|
109
|
+
try {
|
|
110
|
+
json = JSON.parse(text);
|
|
111
|
+
} catch {
|
|
112
|
+
json = text;
|
|
113
|
+
}
|
|
114
|
+
return { status: res.status, body: json };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
before(async () => {
|
|
118
|
+
h = await startHarness();
|
|
119
|
+
cam = await startFakeCamera();
|
|
120
|
+
Object.assign(id, await seed(h, cam.host));
|
|
121
|
+
|
|
122
|
+
const nsu = require("@7365admin1/node-server-utils");
|
|
123
|
+
const { add, getAll, updateById } = h.core.useSiteCameraController();
|
|
124
|
+
const router = express.Router();
|
|
125
|
+
router.post("/", nsu.requireAuth, add);
|
|
126
|
+
router.get("/", nsu.requireAuth, getAll);
|
|
127
|
+
router.patch("/id/:id", nsu.requireAuth, updateById);
|
|
128
|
+
const server = express().use(express.json()).use("/api/site-cameras", router).use(nsu.errorHandler);
|
|
129
|
+
app = server.listen(0, "127.0.0.1");
|
|
130
|
+
await once(app, "listening");
|
|
131
|
+
base = `http://127.0.0.1:${app.address().port}/api`;
|
|
132
|
+
|
|
133
|
+
sid = await h.login(MANAGER, PASSWORD);
|
|
134
|
+
|
|
135
|
+
// What API-core's boot does with ANPR_ON. The only camera is inactive, so
|
|
136
|
+
// nothing starts now - the handler must still be kept for a later save.
|
|
137
|
+
dahua = h.core.useDahuaService();
|
|
138
|
+
await h.core.useSiteCameraService().listenToCapturedPlateNumber(() => {});
|
|
139
|
+
}, { timeout: 300000 });
|
|
140
|
+
|
|
141
|
+
after(async () => {
|
|
142
|
+
// Stop the listener before the harness goes, so nothing keeps retrying.
|
|
143
|
+
if (dahua && id.camera) {
|
|
144
|
+
await dahua.listenToCamera({ _id: new ObjectId(id.camera), type: "anpr", status: "inactive" });
|
|
145
|
+
}
|
|
146
|
+
if (app) app.close();
|
|
147
|
+
if (cam) cam.stop();
|
|
148
|
+
if (h) await h.stop();
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("0. nothing listens to the inactive camera at boot", () => {
|
|
152
|
+
assert.equal(dahua.isListening(id.camera), false);
|
|
153
|
+
assert.equal(cam.seen.streams, 0);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("1. creating an ANPR camera without a password is refused", async () => {
|
|
157
|
+
const res = await api("/site-cameras", {
|
|
158
|
+
method: "POST",
|
|
159
|
+
body: {
|
|
160
|
+
site: id.site.toString(),
|
|
161
|
+
host: "http://127.0.0.1:1",
|
|
162
|
+
username: CAM_USER,
|
|
163
|
+
password: "",
|
|
164
|
+
type: "anpr",
|
|
165
|
+
category: "visitor",
|
|
166
|
+
direction: "visitors",
|
|
167
|
+
status: "inactive",
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
assert.equal(res.status, 400, JSON.stringify(res.body));
|
|
171
|
+
assert.match(String(res.body?.message), /password/);
|
|
172
|
+
assert.equal(await h.db.collection("site.cameras").countDocuments({}), 1);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("2. the camera list never returns the password", async () => {
|
|
176
|
+
const res = await api(`/site-cameras?site=${id.site}&type=anpr`);
|
|
177
|
+
assert.equal(res.status, 200, JSON.stringify(res.body));
|
|
178
|
+
assert.equal(res.body.items.length, 1);
|
|
179
|
+
for (const item of res.body.items) assert.equal("password" in item, false);
|
|
180
|
+
assert.equal(JSON.stringify(res.body).includes(CAM_PASS), false);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("3. switching it on with the password left blank keeps the password and starts the listener", async () => {
|
|
184
|
+
// Exactly what the edit form sends after only the Status switch changed.
|
|
185
|
+
const res = await api(`/site-cameras/id/${id.camera}`, {
|
|
186
|
+
method: "PATCH",
|
|
187
|
+
body: {
|
|
188
|
+
host: cam.host,
|
|
189
|
+
username: CAM_USER,
|
|
190
|
+
password: "",
|
|
191
|
+
category: "visitor",
|
|
192
|
+
guardPost: 0,
|
|
193
|
+
direction: "visitors",
|
|
194
|
+
status: "active",
|
|
195
|
+
name: "",
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
assert.equal(res.status, 200, JSON.stringify(res.body));
|
|
199
|
+
|
|
200
|
+
const row = await h.db.collection("site.cameras").findOne({ _id: new ObjectId(id.camera) });
|
|
201
|
+
assert.equal(row.status, "active");
|
|
202
|
+
assert.equal(row.password, CAM_PASS, "the stored password must be kept");
|
|
203
|
+
|
|
204
|
+
assert.equal(dahua.isListening(id.camera), true);
|
|
205
|
+
assert.equal(
|
|
206
|
+
await until(() => cam.seen.authed > 0),
|
|
207
|
+
true,
|
|
208
|
+
"the listener must log in to the device with the stored password",
|
|
209
|
+
);
|
|
210
|
+
assert.equal(cam.seen.refused, 0);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("4. a password omitted from the edit altogether is kept too", async () => {
|
|
214
|
+
const res = await api(`/site-cameras/id/${id.camera}`, {
|
|
215
|
+
method: "PATCH",
|
|
216
|
+
body: { host: cam.host, username: CAM_USER, category: "visitor", direction: "visitors", status: "active", name: "Gate 8001" },
|
|
217
|
+
});
|
|
218
|
+
assert.equal(res.status, 200, JSON.stringify(res.body));
|
|
219
|
+
const row = await h.db.collection("site.cameras").findOne({ _id: new ObjectId(id.camera) });
|
|
220
|
+
assert.equal(row.password, CAM_PASS);
|
|
221
|
+
assert.equal(row.name, "Gate 8001");
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("5. switching it off stops the listener on its save", async () => {
|
|
225
|
+
const res = await api(`/site-cameras/id/${id.camera}`, {
|
|
226
|
+
method: "PATCH",
|
|
227
|
+
body: { host: cam.host, username: CAM_USER, password: "", category: "visitor", direction: "visitors", status: "inactive" },
|
|
228
|
+
});
|
|
229
|
+
assert.equal(res.status, 200, JSON.stringify(res.body));
|
|
230
|
+
assert.equal(dahua.isListening(id.camera), false);
|
|
231
|
+
const row = await h.db.collection("site.cameras").findOne({ _id: new ObjectId(id.camera) });
|
|
232
|
+
assert.equal(row.password, CAM_PASS);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("6. a new password on an edit replaces the stored one", async () => {
|
|
236
|
+
const res = await api(`/site-cameras/id/${id.camera}`, {
|
|
237
|
+
method: "PATCH",
|
|
238
|
+
body: { host: cam.host, username: CAM_USER, password: "E2E-rotated-2", category: "visitor", direction: "visitors", status: "inactive" },
|
|
239
|
+
});
|
|
240
|
+
assert.equal(res.status, 200, JSON.stringify(res.body));
|
|
241
|
+
const row = await h.db.collection("site.cameras").findOne({ _id: new ObjectId(id.camera) });
|
|
242
|
+
assert.equal(row.password, "E2E-rotated-2");
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
async function seed(h, host) {
|
|
247
|
+
const now = new Date().toISOString();
|
|
248
|
+
const org = new ObjectId();
|
|
249
|
+
const site = new ObjectId();
|
|
250
|
+
const role = new ObjectId();
|
|
251
|
+
|
|
252
|
+
await h.db.collection("organizations").insertOne({
|
|
253
|
+
_id: org, name: "CamPw Org", email: "campw-org@e2e.example.com", type: "org",
|
|
254
|
+
nature: "property_management_agency", status: "active", createdAt: now, defaultSite: site.toString(),
|
|
255
|
+
});
|
|
256
|
+
await h.db.collection("sites").insertOne({ _id: site, name: "CamPw Site", org, orgId: org, status: "active" });
|
|
257
|
+
await h.db.collection("roles").insertOne({ _id: role, name: "Manager", org, permissions: ["*"], status: "active" });
|
|
258
|
+
|
|
259
|
+
const user = await h.db.collection("users").insertOne({
|
|
260
|
+
email: MANAGER, name: "campw-manager", status: "active", createdAt: now,
|
|
261
|
+
password: await h.hashPassword(PASSWORD),
|
|
262
|
+
});
|
|
263
|
+
await h.db.collection("members").insertOne({
|
|
264
|
+
user: user.insertedId, org, type: "organization", role, status: "active",
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
const camera = await h.db.collection("site.cameras").insertOne({
|
|
268
|
+
site, host, username: CAM_USER, password: CAM_PASS, type: "anpr", category: "visitor",
|
|
269
|
+
direction: "visitors", guardPost: 0, status: "inactive", name: "", createdAt: now,
|
|
270
|
+
updatedAt: "", deletedAt: "",
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
return { org, site, camera: camera.insertedId.toString() };
|
|
274
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A camera's device password is write-only: an edit that leaves it blank keeps
|
|
3
|
+
* the saved one, and creating an ANPR camera still requires it.
|
|
4
|
+
*/
|
|
5
|
+
import { test } from "node:test";
|
|
6
|
+
import assert from "node:assert/strict";
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
keepStoredPassword,
|
|
10
|
+
schemaSiteCamera,
|
|
11
|
+
schemaUpdateSiteCamera,
|
|
12
|
+
} from "./.build/models/site-camera.model.mjs";
|
|
13
|
+
|
|
14
|
+
const SITE = "64b7f0000000000000000001";
|
|
15
|
+
|
|
16
|
+
for (const blank of ["", " ", null, undefined]) {
|
|
17
|
+
test(`a blank password (${JSON.stringify(blank)}) is dropped from an edit`, () => {
|
|
18
|
+
const edit = { host: "http://cam.example.net:8001", category: "visitor", status: "active", password: blank };
|
|
19
|
+
const out = keepStoredPassword(edit);
|
|
20
|
+
assert.equal("password" in out, false, "must not $set an empty password");
|
|
21
|
+
assert.equal(out.status, "active");
|
|
22
|
+
assert.equal(schemaUpdateSiteCamera.validate(out).error, undefined);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
test("a new password on an edit is kept, to replace the stored one", () => {
|
|
27
|
+
const out = keepStoredPassword({ host: "h", category: "visitor", password: "N3w-pass" });
|
|
28
|
+
assert.equal(out.password, "N3w-pass");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("a status-only edit validates", () => {
|
|
32
|
+
const out = keepStoredPassword({ host: "http://cam.example.net:8001", category: "visitor", status: "active" });
|
|
33
|
+
assert.equal(schemaUpdateSiteCamera.validate(out).error, undefined);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("creating an ANPR camera without a password is still refused", () => {
|
|
37
|
+
const base = {
|
|
38
|
+
site: SITE,
|
|
39
|
+
host: "http://cam.example.net:8001",
|
|
40
|
+
username: "admin",
|
|
41
|
+
type: "anpr",
|
|
42
|
+
category: "visitor",
|
|
43
|
+
direction: "visitors",
|
|
44
|
+
status: "active",
|
|
45
|
+
};
|
|
46
|
+
assert.match(String(schemaSiteCamera.validate(base).error?.message), /password/);
|
|
47
|
+
assert.match(String(schemaSiteCamera.validate({ ...base, password: "" }).error?.message), /password/);
|
|
48
|
+
assert.equal(schemaSiteCamera.validate({ ...base, password: "p" }).error, undefined);
|
|
49
|
+
});
|