feat(device-core): protect enrollment identifiers

This commit is contained in:
Codex
2026-08-11 12:03:21 +03:00
parent 9f12edd736
commit 6a8c1ce1ef
7 changed files with 238 additions and 8 deletions
@@ -75,6 +75,17 @@ test("management API cannot start without its repository boundary and strong tok
}),
/device_management_token_invalid/,
);
assert.throws(
() => createControlCoreApp({
managementApiEnabled: true,
managementToken,
repository: {
health: async () => "ready",
executeManagementCommand: async () => ({}),
},
}),
/device_identifier_pepper_invalid/,
);
});
test("management API requires service auth and an idempotency key", async () => {
@@ -427,7 +438,7 @@ function ownerScopeCommand() {
}
async function startTestServer(options) {
const server = createControlCoreApp(options);
const server = createControlCoreApp({ identifierPepper, ...options });
await new Promise((resolve, reject) => {
server.once("error", reject);
server.listen(0, "127.0.0.1", resolve);
@@ -4,6 +4,7 @@ import test from "node:test";
import { createControlCoreApp } from "../src/app.mjs";
const managementToken = "test-only-management-token-with-32-bytes";
const identifierPepper = "test-only-identifier-pepper-with-32-bytes";
test("management API forwards a normalized generic Edge registration", async () => {
let executed;
@@ -116,6 +117,107 @@ test("management API forwards claim as evidence references without identity inpu
}
});
test("management API derives enrollment identity inside Core and never forwards raw IMEI", async () => {
let executed;
const runtime = await startServer({
managementApiEnabled: true,
managementToken,
repository: {
health: async () => "ready",
executeManagementCommand: async (input) => {
executed = input;
return {
replayed: false,
result: {
enrollmentIntent: {
identifier: {
kind: input.command.identifierKind,
masked: input.command.identifierMasked,
},
},
},
};
},
},
});
try {
const rawImei = "123456789012345";
const response = await fetch(
`${runtime.baseUrl}/internal/v1/management/enrollment-intents:ensure`,
{
method: "POST",
headers: {
...managementHeaders(),
"Idempotency-Key": "phase25-enrollment-0001",
},
body: JSON.stringify({
projectRef: "project:11111111-1111-4111-8111-111111111111",
enrollmentKey: "pilot-device",
routeRef: "route:22222222-2222-4222-8222-222222222222",
modelProfileRef: "arusnavi.b2.v1",
displayName: "Pilot device",
identifier: { kind: "imei", value: rawImei },
expiresAt: null,
}),
},
);
const body = await response.json();
assert.equal(response.status, 200);
assert.equal(executed.command.identifierKind, "imei");
assert.equal(executed.command.identifierMasked, "***********2345");
assert.match(executed.command.identifierDigest, /^hmac-sha256:[a-f0-9]{64}$/);
assert.equal(JSON.stringify(executed).includes(rawImei), false);
assert.equal(JSON.stringify(body).includes(rawImei), false);
} finally {
await runtime.close();
}
});
test("management API rejects client-supplied enrollment digests", async () => {
let executions = 0;
const runtime = await startServer({
managementApiEnabled: true,
managementToken,
repository: {
health: async () => "ready",
executeManagementCommand: async () => {
executions += 1;
return { replayed: false, result: {} };
},
},
});
try {
const response = await fetch(
`${runtime.baseUrl}/internal/v1/management/enrollment-intents:ensure`,
{
method: "POST",
headers: {
...managementHeaders(),
"Idempotency-Key": "phase25-enrollment-reject-0001",
},
body: JSON.stringify({
projectRef: "project:11111111-1111-4111-8111-111111111111",
enrollmentKey: "pilot-device",
routeRef: "route:22222222-2222-4222-8222-222222222222",
modelProfileRef: "arusnavi.b2.v1",
displayName: "Pilot device",
identifier: { kind: "imei", value: "123456789012345" },
identifierDigest: `hmac-sha256:${"a".repeat(64)}`,
}),
},
);
assert.equal(response.status, 400);
assert.equal(
(await response.json()).error,
"device_enrollment_input_field_unexpected",
);
assert.equal(executions, 0);
} finally {
await runtime.close();
}
});
test("management API accepts only a canonical credential reference", async () => {
let executed;
const runtime = await startServer({
@@ -195,7 +297,7 @@ test("management API accepts only a canonical credential reference", async () =>
});
async function startServer(options) {
const server = createControlCoreApp(options);
const server = createControlCoreApp({ identifierPepper, ...options });
await new Promise((resolve, reject) => {
server.once("error", reject);
server.listen(0, "127.0.0.1", resolve);
@@ -0,0 +1,19 @@
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
const repositorySource = new URL(
"../src/infrastructure-repository.mjs",
import.meta.url,
);
test("catalog, Edge and route upserts enforce irreversible lifecycle transitions", async () => {
const source = await readFile(repositorySource, "utf8");
assert.match(source, /device_adapter_versions\.lifecycle_state = 'draft'[\s\S]*excluded\.lifecycle_state in \('active', 'retired'\)/);
assert.match(source, /device_model_profiles\.lifecycle_state = 'active'[\s\S]*excluded\.lifecycle_state = 'retired'/);
assert.match(source, /device_edges\.lifecycle_state = 'suspended'[\s\S]*excluded\.lifecycle_state in \('active', 'retired'\)/);
assert.match(source, /device_routes\.lifecycle_state = 'draft'[\s\S]*excluded\.lifecycle_state in \('active', 'retired'\)/);
assert.doesNotMatch(source, /device_(?:adapter_versions|model_profiles|edges|routes)\.lifecycle_state = 'retired'[\s\S]{0,160}excluded\.lifecycle_state = 'active'/);
});