Manage shared ARNavi VPS contours and application keys from infrastructure
This commit is contained in:
@@ -63,6 +63,12 @@ export function createDeviceCoreClient({ baseUrl, token, fetchImpl = fetch } = {
|
||||
|
||||
return {
|
||||
configured: true,
|
||||
async b2Infrastructure(actor, input, requestId) {
|
||||
return request("/internal/v1/b2/infrastructure", actor, {
|
||||
method: "POST", headers: { "Content-Type": "application/json", "Idempotency-Key": requestId },
|
||||
body: JSON.stringify(input),
|
||||
}).then((body) => body.result);
|
||||
},
|
||||
async listProjects(actor) {
|
||||
return request("/internal/v1/query/projects", actor)
|
||||
.then((body) => body.projects);
|
||||
@@ -1297,7 +1303,7 @@ function normalizeBaseUrl(value) {
|
||||
}
|
||||
|
||||
function safeCoreError(value) {
|
||||
return typeof value === "string" && /^device_[a-z0-9._:-]{2,120}$/.test(value)
|
||||
return typeof value === "string" && /^(?:device|b2)_[a-z0-9._:-]{2,120}$/.test(value)
|
||||
? value
|
||||
: "device_core_unavailable";
|
||||
}
|
||||
|
||||
@@ -171,6 +171,19 @@ export function createDeviceManagerServer({
|
||||
const workspace = await coreClient.getWorkspace(context.actor, projectRef);
|
||||
return sendJson(response, 200, { ok: true, workspace });
|
||||
}
|
||||
if (request.method === "POST" && url.pathname === "/api/device-manager/b2/infrastructure") {
|
||||
if (!coreClient.b2Infrastructure) throw serviceError("b2_infrastructure_unavailable", 503);
|
||||
// This route handles keys. Require JSON and an explicit same-origin
|
||||
// browser request in addition to the authenticated server-side session.
|
||||
if (request.headers.origin !== `${auth.authRequired ? 'https:' : url.protocol}//${request.headers.host}` || !String(request.headers["content-type"] || "").startsWith("application/json")) {
|
||||
throw serviceError("device_manager_origin_required", 403);
|
||||
}
|
||||
const requestId = singleHeader(request.headers["idempotency-key"]);
|
||||
if (!/^[a-f0-9]{32}$/.test(requestId)) throw serviceError("b2_request_id_invalid", 400);
|
||||
const input = await readJsonBody(request, 64 * 1024);
|
||||
const result = await coreClient.b2Infrastructure(context.actor, input, requestId);
|
||||
return sendJson(response, 200, { ok: true, result });
|
||||
}
|
||||
const command = mutationRoutes.get(url.pathname);
|
||||
if (request.method === "POST" && command) {
|
||||
const idempotencyKey = singleHeader(request.headers["idempotency-key"]);
|
||||
@@ -373,7 +386,7 @@ function normalizeStatus(value) {
|
||||
|
||||
function safeError(error) {
|
||||
const value = String(error?.message || "");
|
||||
return /^(?:device|nodedc)_[a-z0-9._:-]{2,160}$/.test(value)
|
||||
return /^(?:device|nodedc|b2)_[a-z0-9._:-]{2,160}$/.test(value)
|
||||
? value
|
||||
: "device_manager_internal_error";
|
||||
}
|
||||
|
||||
@@ -207,3 +207,29 @@ async function postJson(url, body, headers = {}) {
|
||||
assert.equal(payload.ok, true);
|
||||
return payload;
|
||||
}
|
||||
|
||||
test('B2 key management rejects cross-origin requests and never caches returned credentials', async (t) => {
|
||||
const auth = createDeviceManagerAuth({ env: { NODEDC_DEVICE_MANAGER_AUTH_REQUIRED: 'false' } });
|
||||
const calls = [];
|
||||
const coreClient = { ...createLocalPreviewDeviceCore(), async b2Infrastructure(actor, body, id) {
|
||||
calls.push({ actor, body, id }); return { connection: { client_secret: 'synthetic-once-only' } };
|
||||
} };
|
||||
const server = createDeviceManagerServer({ auth, coreClient });
|
||||
await new Promise((resolve, reject) => { server.once('error', reject); server.listen(0, '127.0.0.1', resolve); });
|
||||
t.after(() => new Promise((resolve) => server.close(resolve)));
|
||||
const origin = `http://127.0.0.1:${server.address().port}`;
|
||||
const headers = { 'content-type': 'application/json', 'idempotency-key': 'a'.repeat(32) };
|
||||
const body = JSON.stringify({ action: 'key-issue', projectRef: 'project:synthetic', hostRef: 'host:synthetic' });
|
||||
for (const supplied of [undefined, 'https://attacker.invalid']) {
|
||||
const denied = await fetch(origin + '/api/device-manager/b2/infrastructure', { method: 'POST', headers: { ...headers, ...(supplied ? { origin: supplied } : {}) }, body });
|
||||
assert.equal(denied.status, 403);
|
||||
}
|
||||
assert.equal(calls.length, 0);
|
||||
const response = await fetch(origin + '/api/device-manager/b2/infrastructure', { method: 'POST',
|
||||
headers: { ...headers, origin, 'X-NODEDC-User-Ref': 'user:spoofed' }, body });
|
||||
assert.equal(response.status, 200);
|
||||
assert.match(response.headers.get('cache-control'), /no-store/);
|
||||
assert.equal((await response.json()).result.connection.client_secret, 'synthetic-once-only');
|
||||
assert.equal(calls.length, 1);
|
||||
assert.equal(calls[0].actor.userRef, 'user:local-device-admin');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user