"use strict"; const { randomUUID } = require("node:crypto"); const { constants } = require("node:fs"); const fs = require("node:fs/promises"); const path = require("node:path"); const { PANEL_SOCKET_DIRECTORY, AgentRpcError } = require("./private-rpc"); const PROPOSAL_ID = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/; const MAX_PROPOSAL_BYTES = 8192; const MAX_PROPOSAL_AGE_MS = 30_000; const MAX_LOCAL_PROPOSALS = 2; let outstanding = 0; function proposalPath(id) { if (typeof id !== "string" || !PROPOSAL_ID.test(id)) throw new AgentRpcError(400, "agent_invalid_proposal"); return path.join(PANEL_SOCKET_DIRECTORY, `credential-${id}.json`); } async function withCredentialProposal(settings, operation) { const body = Buffer.from(JSON.stringify({ apiKey: settings.apiKey || "", baseUrl: settings.baseUrl || "", modelIds: settings.modelIds || [], modelToValidate: settings.modelToValidate })); if (body.length > MAX_PROPOSAL_BYTES) throw new AgentRpcError(400, "agent_invalid_proposal"); if (outstanding >= MAX_LOCAL_PROPOSALS) throw new AgentRpcError(503, "agent_discovery_busy"); outstanding += 1; const id = randomUUID(); const filename = proposalPath(id); try { await fs.writeFile(filename, body, { flag: "wx", mode: 0o600 }); return await operation(id); } finally { outstanding -= 1; await fs.unlink(filename).catch(() => {}); } } async function readCredentialProposal(id) { const filename = proposalPath(id); let file; try { file = await fs.open(filename, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); const stat = await file.stat(); if (!stat.isFile() || stat.uid !== process.getuid() || (stat.mode & 0o077) || stat.size < 1 || stat.size > MAX_PROPOSAL_BYTES || Date.now() - stat.mtimeMs > MAX_PROPOSAL_AGE_MS) { throw new AgentRpcError(400, "agent_invalid_proposal"); } const bytes = Buffer.alloc(stat.size + 1); let offset = 0; while (offset < bytes.length) { const { bytesRead } = await file.read(bytes, offset, bytes.length - offset, offset); if (bytesRead === 0) break; offset += bytesRead; } // adpanel:agent-credential-handoff - check the whole bounded file, then let only the unlink winner use it. await fs.unlink(filename); if (offset !== stat.size) throw new AgentRpcError(400, "agent_invalid_proposal"); const settings = JSON.parse(bytes.subarray(0, offset).toString("utf8")); if (typeof settings?.apiKey !== "string" || typeof settings.baseUrl !== "string") { throw new AgentRpcError(400, "agent_invalid_proposal"); } return settings; } catch (error) { if (error instanceof SyntaxError || error.code === "ENOENT") throw new AgentRpcError(400, "agent_invalid_proposal"); throw error; } finally { await file?.close(); } } async function pruneCredentialProposals() { const entries = await fs.readdir(PANEL_SOCKET_DIRECTORY); for (const entry of entries) { if (!/^credential-[a-f0-9-]{36}\.json$/.test(entry)) continue; const filename = path.join(PANEL_SOCKET_DIRECTORY, entry); try { const state = await fs.lstat(filename); if (Date.now() - state.mtimeMs > MAX_PROPOSAL_AGE_MS) await fs.unlink(filename); } catch (error) { if (error.code !== "ENOENT") throw error; } } } module.exports = { withCredentialProposal, readCredentialProposal, pruneCredentialProposals };