Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,243 @@
|
||||
"use strict";
|
||||
|
||||
// adpanel:planner-agent-perms — pure helpers for the permission envelope
|
||||
// stamped onto ai_agent_jobs rows created by the scheduler's
|
||||
// "planner_next_to_agent" task type.
|
||||
//
|
||||
// Threat model: a planner item is authored by any subuser with planner_access,
|
||||
// but the scheduler executes it as an ADPanel Agent job under the SCHEDULE
|
||||
// CREATOR's identity (frequently an admin). Each link is individually
|
||||
// authorized, yet the composition lets the author's text perform agent
|
||||
// actions (file operations, console commands, server power actions, backup
|
||||
// create/restore/delete) with the creator's permissions. Two complementary
|
||||
// clamps remove that privilege delta. The item author is created_by_email,
|
||||
// which the planner PUT route re-stamps to the editor on every content
|
||||
// rewrite, so authorship always tracks the text that will execute:
|
||||
//
|
||||
// 1. ENQUEUE-TIME FILE SNAPSHOT (this module's file-only helpers): the
|
||||
// per-flag AND of the creator's and the item author's file permissions
|
||||
// is persisted on ai_agent_jobs.perm_file_envelope; the executor gates
|
||||
// every FILE_* operation on it. Persisted file-only for legacy
|
||||
// compatibility with the original shipped column contract.
|
||||
// 2. FIRE-TIME AUTHOR INTERSECTION (new): the executor resolves the item
|
||||
// author's effective perms per target server at job/tool execution time
|
||||
// and ANDs them onto the creator's projection for EVERY agent
|
||||
// capability — file and non-file (console_write, server_stop/start,
|
||||
// backups_*). See applyAiAgentPermClamp in index.js, which composes
|
||||
// intersectPerms + computeSchedulerPlannerClampedPerms below.
|
||||
|
||||
// Exactly the flags the agent executor (executeAiAgentFileOperations in
|
||||
// index.js) consults when applying FILE_* directives parsed from model
|
||||
// output:
|
||||
// list/read -> files_read
|
||||
// create/mkdir -> files_create
|
||||
// write -> files_write
|
||||
// create (overwrite -> files_write (panel-side backstop probe against
|
||||
// backstop probe) daemons that ignore the create/update mode)
|
||||
// delete -> files_delete
|
||||
// files_upload and files_archive are unreachable through FILE_* directives
|
||||
// and are deliberately excluded. Non-file capabilities are NOT part of the
|
||||
// persisted snapshot — that stays file-only for legacy compatibility with
|
||||
// rows already in the column — but they are no longer out of scope: the
|
||||
// fire-time author intersection (item 2 in the header) clamps them.
|
||||
const PLANNER_AGENT_FILE_PERM_FLAGS = Object.freeze([
|
||||
"files_read",
|
||||
"files_create",
|
||||
"files_write",
|
||||
"files_delete",
|
||||
]);
|
||||
|
||||
// Fail-closed normalization of a persisted envelope (the raw
|
||||
// ai_agent_jobs.perm_file_envelope column value, which the driver may hand
|
||||
// back as a JSON string or an already-parsed object). Returns null when the
|
||||
// value is absent or unparseable — callers treat null as "no envelope", which
|
||||
// preserves the pre-envelope creator-perms behavior for legacy in-flight
|
||||
// rows. Unknown keys are dropped and every kept flag must be strictly true.
|
||||
// adpanel:planner-agent-envelope-closure — READ-time consumers no longer call
|
||||
// this directly: its null collapses ABSENT and UNPARSEABLE into one value, so
|
||||
// a PRESENT-but-corrupt column silently fell back to the creator-only perms
|
||||
// (fail open) instead of failing closed like the author axis. Read-time
|
||||
// consumers use classifyFilePermEnvelope (below) for the absent-vs-unparseable
|
||||
// distinction; this helper stays the WRITE-time normalizer (the only writer,
|
||||
// insertAiAgentChatJob, normalizes the IN-PROCESS valid envelope) and the one
|
||||
// parser classify delegates to.
|
||||
function normalizeFilePermEnvelope(value) {
|
||||
let raw = value;
|
||||
if (typeof raw === "string") {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) return null;
|
||||
try {
|
||||
raw = JSON.parse(trimmed);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
|
||||
const envelope = {};
|
||||
for (const flag of PLANNER_AGENT_FILE_PERM_FLAGS) {
|
||||
envelope[flag] = raw[flag] === true;
|
||||
}
|
||||
return envelope;
|
||||
}
|
||||
|
||||
// adpanel:planner-agent-envelope-closure — the fail-closed envelope carried by
|
||||
// classifyFilePermEnvelope's unparseable state: every file flag false, built
|
||||
// from the same flag list so it stays in lockstep with the envelope surface.
|
||||
// Internal on purpose — read-time consumers receive it through
|
||||
// classifyFilePermEnvelope, because normalizeFilePermEnvelope's null here
|
||||
// would make applyFilePermEnvelope a no-op and fail OPEN.
|
||||
function allFalseFilePermEnvelope() {
|
||||
const envelope = {};
|
||||
for (const flag of PLANNER_AGENT_FILE_PERM_FLAGS) {
|
||||
envelope[flag] = false;
|
||||
}
|
||||
return envelope;
|
||||
}
|
||||
|
||||
// adpanel:planner-agent-envelope-closure — classify (not just normalize) the
|
||||
// persisted file envelope so a PRESENT but corrupt value can be distinguished
|
||||
// from an ABSENT one: absent (null/undefined/empty string) keeps the legacy
|
||||
// creator-only fallback for pre-envelope rows; valid returns the strict-true
|
||||
// flag envelope; unparseable (bad JSON / non-object / array) must fail
|
||||
// closed at the read-time consumers exactly like the author axis does
|
||||
// (pinnedPlannerAuthorEmailFromEnvelope). The only writer always emits valid
|
||||
// JSON, so unparseable can only appear through out-of-band mutation (truncated
|
||||
// TEXT, hand-edited row, future migration bug). Returns
|
||||
// { state: "absent"|"unparseable"|"valid", envelope }: the envelope is null
|
||||
// ONLY for the absent state; the unparseable state carries the ALL-FALSE
|
||||
// envelope so consumers apply it rather than no-op. Parsing is delegated to
|
||||
// normalizeFilePermEnvelope (one parser, two presentations); only the
|
||||
// null/undefined/empty-string absent boundary is decided here, because
|
||||
// normalize collapses absent and unparseable into the same null.
|
||||
//
|
||||
// One deliberate divergence from pinnedPlannerAuthorEmailFromEnvelope: an
|
||||
// object that parses but names nothing ({}) is "valid" here (every file flag
|
||||
// strict-false — grants nothing) while the author pin calls it "absent" and
|
||||
// re-derives the live author. Both readings narrow — the file axis grants
|
||||
// nothing either way, and the writer never persists an empty envelope — so
|
||||
// the asymmetry is safe; do not "unify" it by making {} unparseable, which
|
||||
// would change the valid-empty semantics without any failure it prevents.
|
||||
function classifyFilePermEnvelope(value) {
|
||||
if (value === null || value === undefined) return { state: "absent", envelope: null };
|
||||
if (typeof value === "string" && !value.trim()) return { state: "absent", envelope: null };
|
||||
const envelope = normalizeFilePermEnvelope(value);
|
||||
if (envelope) return { state: "valid", envelope };
|
||||
return { state: "unparseable", envelope: allFalseFilePermEnvelope() };
|
||||
}
|
||||
|
||||
// Per-flag AND of two perms projections (the shape produced by
|
||||
// getEffectivePermsForUserOnServer). A missing/false flag on either side, an
|
||||
// unresolvable side (null — e.g. the author account was deleted, suspended,
|
||||
// or lost server access), or an unexpected value all fail closed to false.
|
||||
function intersectFilePerms(executorPerms, authorPerms) {
|
||||
const envelope = {};
|
||||
for (const flag of PLANNER_AGENT_FILE_PERM_FLAGS) {
|
||||
envelope[flag] = !!(executorPerms && executorPerms[flag] === true) && !!(authorPerms && authorPerms[flag] === true);
|
||||
}
|
||||
return envelope;
|
||||
}
|
||||
|
||||
// Clamp a perms projection down to the envelope: every envelope flag is ANDed
|
||||
// onto the base projection, so the envelope can narrow but never widen what
|
||||
// the executor user currently holds (a creator suspended or stripped of
|
||||
// access after enqueue stays fully gated by their now-false perms). Returns
|
||||
// the input untouched when there is no envelope; never mutates the base.
|
||||
function applyFilePermEnvelope(perms, envelope) {
|
||||
if (!envelope) return perms;
|
||||
const merged = { ...(perms || {}) };
|
||||
for (const flag of PLANNER_AGENT_FILE_PERM_FLAGS) {
|
||||
merged[flag] = !!(perms && perms[flag] === true) && envelope[flag] === true;
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
// Generalized per-flag AND of two FULL perms projections (the shape produced
|
||||
// by getEffectivePermsForUserOnServer) over the UNION of their keys. Unlike
|
||||
// intersectFilePerms this needs no flag list: whatever either side declares is
|
||||
// intersected. Fail-closed on every uncertain input — a flag missing or
|
||||
// non-true on either side, a null/unusable projection, or an unexpected value
|
||||
// all yield false for that flag. Used by the fire-time author intersection so
|
||||
// non-file capabilities (console, power, backups) are clamped with the same
|
||||
// discipline as file capabilities. Returns a new object; never mutates either
|
||||
// input.
|
||||
function intersectPerms(a, b) {
|
||||
const left = a && typeof a === "object" && !Array.isArray(a) ? a : {};
|
||||
const right = b && typeof b === "object" && !Array.isArray(b) ? b : {};
|
||||
const keys = new Set([...Object.keys(left), ...Object.keys(right)]);
|
||||
const result = {};
|
||||
for (const key of keys) {
|
||||
result[key] = left[key] === true && right[key] === true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Generalized envelope clamp: for every key present in `envelope`, AND that
|
||||
// key onto a copy of `perms`. Keys the envelope does not mention pass through
|
||||
// untouched, so an envelope can narrow but never widen the base. Returns the
|
||||
// input untouched when the envelope is falsy; never mutates. This is the
|
||||
// key-general counterpart of applyFilePermEnvelope (which additionally drops
|
||||
// unknown keys from the persisted snapshot — keep using that one for the
|
||||
// legacy file envelope column, whose unknown-key-dropping contract is pinned
|
||||
// by tests).
|
||||
function applyPermEnvelope(perms, envelope) {
|
||||
if (!envelope || typeof envelope !== "object" || Array.isArray(envelope)) return perms;
|
||||
const merged = { ...(perms || {}) };
|
||||
for (const key of Object.keys(envelope)) {
|
||||
merged[key] = !!(perms && perms[key] === true) && envelope[key] === true;
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
// Pure decision core of the fire-time scheduler-planner clamp (see
|
||||
// applyAiAgentPermClamp in index.js, which resolves the author and calls
|
||||
// this). Inputs:
|
||||
// creatorPerms — the schedule creator's effective perms on the server the
|
||||
// capability is being exercised against (resolved fresh at
|
||||
// fire time).
|
||||
// authorPerms — the planner item author's effective perms on that same
|
||||
// server, or null when unresolvable there.
|
||||
// fileEnvelope — the NORMALIZED enqueue-time file snapshot (or null when
|
||||
// the target server is not the job's own server, or the
|
||||
// row predates the column).
|
||||
// authorResolved — false when the author account could not be verified at
|
||||
// all (item deleted, authorEmail empty, author account
|
||||
// deleted or suspended since enqueue).
|
||||
// authorIsAdmin — true when the author is an admin: the intersection with
|
||||
// an all-true author is the identity, so the intersect is
|
||||
// skipped rather than fabricating an all-true object.
|
||||
//
|
||||
// Branches:
|
||||
// authorResolved + authorIsAdmin -> creatorPerms (+ file envelope).
|
||||
// authorResolved, regular author -> intersectPerms(creatorPerms,
|
||||
// authorPerms) (+ file envelope). An author with no access to the target
|
||||
// server arrives here as authorPerms null, which intersects to all-false
|
||||
// on that server (per-server fail-closed).
|
||||
// !authorResolved -> deny every capability. The persisted envelope is a
|
||||
// ceiling, not a grant that can survive loss of the author identity.
|
||||
//
|
||||
// The persisted file envelope is applied as a final AND on every branch (on
|
||||
// the job's own server only — the caller passes null elsewhere): strictly
|
||||
// narrowing, so it preserves the enqueue-time file contract on top of the
|
||||
// fresh fire-time intersection. Never mutates the inputs.
|
||||
function computeSchedulerPlannerClampedPerms({ creatorPerms, authorPerms, fileEnvelope, authorResolved, authorIsAdmin = false }) {
|
||||
let clamped;
|
||||
if (!authorResolved) {
|
||||
clamped = intersectPerms(creatorPerms, null);
|
||||
} else if (authorIsAdmin) {
|
||||
clamped = creatorPerms;
|
||||
} else {
|
||||
clamped = intersectPerms(creatorPerms, authorPerms);
|
||||
}
|
||||
return applyFilePermEnvelope(clamped, fileEnvelope);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
PLANNER_AGENT_FILE_PERM_FLAGS,
|
||||
normalizeFilePermEnvelope,
|
||||
classifyFilePermEnvelope,
|
||||
intersectFilePerms,
|
||||
applyFilePermEnvelope,
|
||||
intersectPerms,
|
||||
applyPermEnvelope,
|
||||
computeSchedulerPlannerClampedPerms,
|
||||
};
|
||||
Reference in new issue
Block a user