aegiscode 7.0.31 → 7.0.33
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/LICENSING.md +186 -0
- package/package.json +4 -2
- package/src/app.js +1 -1
- package/src/chatflow.js +1 -1
- package/src/commands.js +3 -1
- package/src/config.js +1 -1
- package/src/diff.js +1 -1
- package/src/diffview.js +1 -1
- package/src/engine.js +1 -1
- package/src/history.js +1 -1
- package/src/models.js +1 -1
- package/src/overlays.js +1 -1
- package/src/panels.js +1 -1
- package/vendor/client/brain-catalog.js +1 -1
- package/vendor/client/byok-catalog.js +1 -1
- package/vendor/client/credits.js +45 -0
- package/vendor/client/foreign-sessions.js +3 -0
- package/vendor/client/naming.js +1 -1
- package/vendor/client/prefs.js +1 -1
- package/vendor/client/session-search.js +3 -0
- package/vendor/desktop/lib/local/claude-pro.js +1 -1
- package/vendor/desktop/lib/local/engine.js +1 -1
- package/vendor/desktop/lib/local/integrity.js +42 -2
- package/vendor/desktop/lib/local/perm-rules.js +206 -0
- package/vendor/desktop/lib/local/perm-store.js +298 -0
- package/vendor/desktop/lib/local/perm-write.js +199 -0
- package/vendor/desktop/lib/local/pro-sessions.js +3 -0
- package/vendor/desktop/lib/local/sandbox-appcontainer.js +242 -0
- package/vendor/desktop/lib/local/sandbox-bwrap.js +316 -0
- package/vendor/desktop/lib/local/sandbox-seatbelt.js +380 -0
- package/vendor/desktop/lib/local/sandbox.js +493 -0
- package/vendor/desktop/lib/local/shell.js +155 -20
- package/vendor/desktop/lib/local/tools.js +29 -4
- package/vendor/desktop/lib/settings.js +1 -1
- package/vendor/desktop/main.js +774 -37
- package/vendor/desktop/renderer/usage.js +1 -1
- package/vendor/mcp/admission.js +3 -0
- package/vendor/mcp/server.js +1 -1
- package/vendor/one/src/child.js +1 -1
- package/vendor/one/src/permissions.js +1 -1
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
// Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
|
|
2
|
+
// AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
|
|
3
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
4
|
+
//
|
|
5
|
+
// ── The permission rules a user can SEE and EDIT ──────────────────────────
|
|
6
|
+
//
|
|
7
|
+
// §2 taught the desktop gate to *read* `~/.aegiscode/permissions.json`; §3 gave
|
|
8
|
+
// the approval card a fourth answer that *writes* one exact rule. Both left the
|
|
9
|
+
// same hole: once a rule was stored, nothing in the window could show it, and
|
|
10
|
+
// the only way to take it back was to hand-edit JSON. A permission you cannot
|
|
11
|
+
// revoke from the app is a permission you have lost track of — and the whole
|
|
12
|
+
// point of the tier work is that the user, not the agent, holds the list.
|
|
13
|
+
//
|
|
14
|
+
// ## What this module is, and is not
|
|
15
|
+
//
|
|
16
|
+
// It is a THIN VIEW over `cli/src/config.js`'s rule store — the same
|
|
17
|
+
// `loadPermissions` / `addPermissionRule` / `removePermissionRule` /
|
|
18
|
+
// `savePermissions` pair `/permissions` uses, resolved through the same
|
|
19
|
+
// `loadShared` seam as `perm-rules.js` and `perm-write.js`. It never writes
|
|
20
|
+
// JSON itself, so there is one schema and one file format (D8 rule 1).
|
|
21
|
+
//
|
|
22
|
+
// It is NOT the decision path. `engine.js` consults `perm-rules.js`
|
|
23
|
+
// (`rulesVerdict`) at the gate; this module is only what the Settings card
|
|
24
|
+
// lists and mutates. Keeping them apart matters: a bug in the editor must not
|
|
25
|
+
// be able to change what the gate decides without also changing the file, and
|
|
26
|
+
// the gate re-reads the file on every call rather than trusting a cached view.
|
|
27
|
+
//
|
|
28
|
+
// ## Why every mutation is validated against the real matcher
|
|
29
|
+
//
|
|
30
|
+
// A rule the grammar cannot parse matches NOTHING (`matchRule` returns false on
|
|
31
|
+
// a failed `RULE_RE`). Storing one would be worse than storing nothing: the
|
|
32
|
+
// card would list a permission that has no effect, so a user would believe
|
|
33
|
+
// `exec(rm -rf *)` was denied while the gate still raised a card, or believe an
|
|
34
|
+
// allow was in force while the agent kept asking. So `addRule` composes the
|
|
35
|
+
// rule, then asks the REAL `matchRule` whether it matches its own subject —
|
|
36
|
+
// a rule that cannot match itself is refused with the reason. That check can
|
|
37
|
+
// only be trusted because it is the same function the gate uses.
|
|
38
|
+
//
|
|
39
|
+
// ## `*` is legitimate HERE and refused in §3, deliberately
|
|
40
|
+
//
|
|
41
|
+
// `perm-write.js` refuses to persist a subject containing `*` because the user
|
|
42
|
+
// clicked approve on ONE command, and a `*` inside it would silently widen what
|
|
43
|
+
// they approved. This module is the opposite situation: the user is typing the
|
|
44
|
+
// pattern and means it. `exec(npm run *)` is the entire reason tiers exist. So
|
|
45
|
+
// the wildcard is allowed here and the *typo* cases are what get caught — an
|
|
46
|
+
// unparseable rule, or one whose tool name the engine never calls.
|
|
47
|
+
|
|
48
|
+
'use strict';
|
|
49
|
+
|
|
50
|
+
const { loadShared } = require('./perm-rules.js');
|
|
51
|
+
|
|
52
|
+
/** The three lists, in the order the card renders them. `deny` first: it is
|
|
53
|
+
* the floor, and a user looking for "what can never run" should not have to
|
|
54
|
+
* scroll past the allows to find it. */
|
|
55
|
+
const LISTS = Object.freeze(['deny', 'allow', 'ask']);
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Resolve the CLI rule store, or explain why not.
|
|
59
|
+
*
|
|
60
|
+
* @param {object} [opts]
|
|
61
|
+
* @param {object} [opts.config] injected config module (tests) — when present
|
|
62
|
+
* the real file is never read or written
|
|
63
|
+
* @param {string} [opts.dir] module-layout override (tests)
|
|
64
|
+
* @returns {{ok: boolean, config?: object, reason?: string}}
|
|
65
|
+
*/
|
|
66
|
+
function resolveStore(opts = {}) {
|
|
67
|
+
if (opts.config) {
|
|
68
|
+
// An injected store is validated like any other. Short-circuiting on it
|
|
69
|
+
// would mean the real install (the only caller that injects nothing) is the
|
|
70
|
+
// one path the checks never cover — and a store missing `savePermissions`
|
|
71
|
+
// would then answer `ok: true` and fail at the WRITE, after the UI had
|
|
72
|
+
// already told the user the rule was stored.
|
|
73
|
+
const missing = ['loadPermissions', 'savePermissions', 'addPermissionRule', 'removePermissionRule']
|
|
74
|
+
.find((fn) => typeof opts.config[fn] !== 'function');
|
|
75
|
+
if (missing) return { ok: false, reason: `the permissions store has no ${missing}()` };
|
|
76
|
+
return { ok: true, config: opts.config };
|
|
77
|
+
}
|
|
78
|
+
const shared = loadShared(opts.dir);
|
|
79
|
+
if (!shared) return { ok: false, reason: 'one/src/permissions.js not found' };
|
|
80
|
+
let config;
|
|
81
|
+
try {
|
|
82
|
+
config = shared.permissions.loadConfigStore();
|
|
83
|
+
} catch (e) {
|
|
84
|
+
return { ok: false, reason: (e && e.message) || String(e) };
|
|
85
|
+
}
|
|
86
|
+
if (!config) return { ok: false, reason: 'the permissions store is unavailable' };
|
|
87
|
+
for (const fn of ['loadPermissions', 'savePermissions', 'addPermissionRule', 'removePermissionRule']) {
|
|
88
|
+
if (typeof config[fn] !== 'function') {
|
|
89
|
+
return { ok: false, reason: `the permissions store has no ${fn}()` };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return { ok: true, config };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The rule set as the card consumes it.
|
|
97
|
+
*
|
|
98
|
+
* `explicitAsk` is stripped: it is DERIVED from `defaultMode` at read time
|
|
99
|
+
* (`cli/src/config.js` loadPermissions), and sending it to the renderer invites
|
|
100
|
+
* a round-trip that writes it back — the disagreeing-file bug `saveRuleMode`
|
|
101
|
+
* documents. The editor only ever sends a list + a pattern back.
|
|
102
|
+
*
|
|
103
|
+
* @returns {{ok: boolean, rules?: {defaultMode: string, deny: string[], allow: string[], ask: string[]}, source?: string, reason?: string}}
|
|
104
|
+
*/
|
|
105
|
+
function listRules(opts = {}) {
|
|
106
|
+
const store = resolveStore(opts);
|
|
107
|
+
if (!store.ok) return { ok: false, reason: store.reason };
|
|
108
|
+
let raw;
|
|
109
|
+
try {
|
|
110
|
+
raw = store.config.loadPermissions();
|
|
111
|
+
} catch (e) {
|
|
112
|
+
return { ok: false, reason: (e && e.message) || String(e) };
|
|
113
|
+
}
|
|
114
|
+
const r = raw && typeof raw === 'object' ? raw : {};
|
|
115
|
+
const list = (v) => (Array.isArray(v) ? v.map(String) : []);
|
|
116
|
+
return {
|
|
117
|
+
ok: true,
|
|
118
|
+
source: 'store',
|
|
119
|
+
rules: {
|
|
120
|
+
defaultMode: r.defaultMode === 'allow' || r.defaultMode === 'deny' ? r.defaultMode : 'ask',
|
|
121
|
+
deny: list(r.deny),
|
|
122
|
+
allow: list(r.allow),
|
|
123
|
+
ask: list(r.ask),
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Does this rule actually parse, and does it match the subject a user's own
|
|
130
|
+
* example would produce?
|
|
131
|
+
*
|
|
132
|
+
* `RULE_RE` is `^(\w+)(?:\((.*)\))?$`, so "parses" and "matches something" are
|
|
133
|
+
* different questions: `exec(` parses as nothing at all, while `nonsense(*)`
|
|
134
|
+
* parses fine and can never match, because no tool is called `nonsense`. Both
|
|
135
|
+
* are useless rules, and both are refused here rather than stored as a line the
|
|
136
|
+
* user will stare at wondering why it does nothing.
|
|
137
|
+
*
|
|
138
|
+
* The tool-name half is checked with `canonTool`, so a user typing the
|
|
139
|
+
* reference spelling (`Bash`, `Edit`) is accepted exactly as the gate would
|
|
140
|
+
* accept it — one vocabulary, two spellings.
|
|
141
|
+
*
|
|
142
|
+
* @param {string} rule
|
|
143
|
+
* @param {object} [opts]
|
|
144
|
+
* @param {string} [opts.dir] layout override (tests)
|
|
145
|
+
* @returns {{ok: boolean, reason?: string, warning?: string}}
|
|
146
|
+
*/
|
|
147
|
+
function validateRule(rule, opts = {}) {
|
|
148
|
+
const text = String(rule == null ? '' : rule).trim();
|
|
149
|
+
if (!text) return { ok: false, reason: 'a rule cannot be empty' };
|
|
150
|
+
if (/[\n\r]/.test(text)) return { ok: false, reason: 'a rule must be a single line' };
|
|
151
|
+
|
|
152
|
+
const shared = loadShared(opts.dir);
|
|
153
|
+
if (!shared) return { ok: false, reason: 'one/src/permissions.js not found' };
|
|
154
|
+
|
|
155
|
+
let evaluator;
|
|
156
|
+
try {
|
|
157
|
+
evaluator = shared.permissions.loadEvaluator();
|
|
158
|
+
} catch (e) {
|
|
159
|
+
return { ok: false, reason: (e && e.message) || String(e) };
|
|
160
|
+
}
|
|
161
|
+
const { matchRule, subjectFor, canonTool } = evaluator;
|
|
162
|
+
if (typeof matchRule !== 'function' || typeof subjectFor !== 'function') {
|
|
163
|
+
return { ok: false, reason: 'the rule evaluator is incomplete' };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// The grammar itself. Duplicating RULE_RE here would be a second dialect that
|
|
167
|
+
// could drift from the one the gate uses; instead, a rule that matches
|
|
168
|
+
// nothing is rejected, and the reason names the shape.
|
|
169
|
+
const m = /^(\w+)(?:\((.*)\))?$/.exec(text);
|
|
170
|
+
if (!m) return { ok: false, reason: 'a rule has the form tool(pattern), e.g. exec(npm test)' };
|
|
171
|
+
|
|
172
|
+
const tool = m[1];
|
|
173
|
+
const known = typeof canonTool === 'function' ? KNOWN_TOOLS.has(canonTool(tool)) : true;
|
|
174
|
+
if (!known) {
|
|
175
|
+
return { ok: false, reason: `"${tool}" is not a tool this app calls — nothing would ever match it` };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Self-match: build a subject the rule claims to govern and ask the matcher.
|
|
179
|
+
// `exec(npm test)` must match "npm test"; a rule that cannot match its own
|
|
180
|
+
// pattern is one the grammar stored but the gate will never honour.
|
|
181
|
+
const pattern = m[2];
|
|
182
|
+
if (pattern !== undefined && pattern !== '') {
|
|
183
|
+
let selfMatch = false;
|
|
184
|
+
try {
|
|
185
|
+
selfMatch = matchRule(text, tool, pattern);
|
|
186
|
+
} catch {
|
|
187
|
+
selfMatch = false;
|
|
188
|
+
}
|
|
189
|
+
if (!selfMatch) {
|
|
190
|
+
return { ok: false, reason: 'this pattern cannot match itself, so it would never fire' };
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// A bare tool rule matches EVERY call to that tool, and an allow is consulted
|
|
194
|
+
// before the multi-`cd` rail (`evalPermission`: deny, allow,
|
|
195
|
+
// isMultiDirCommand, ask, explicitAsk) — so `allow: ["exec"]` permanently
|
|
196
|
+
// disables that rail, silently, while the card still looks healthy. It is a
|
|
197
|
+
// legitimate thing to want (it is what "full access" means), so it is not
|
|
198
|
+
// refused the way perm-write.js refuses an empty subject: it is *warned*.
|
|
199
|
+
// Removing the ability to express it would push users to `/confirm off`,
|
|
200
|
+
// which is the same state with no visible rule at all.
|
|
201
|
+
if (pattern === undefined || pattern === '') {
|
|
202
|
+
return {
|
|
203
|
+
ok: true,
|
|
204
|
+
warning: `a bare ${tool}() rule matches every ${tool} call, including commands that change directory more than once`,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
return { ok: true };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** The capabilities `cli/src/permissions.js`'s TOOL_ALIAS folds onto. A rule
|
|
211
|
+
* naming anything else is inert, which `validateRule` refuses. */
|
|
212
|
+
const KNOWN_TOOLS = new Set(['shell', 'read', 'write', 'edit', 'list', 'glob', 'grep', 'task']);
|
|
213
|
+
|
|
214
|
+
/** The file shape, with `explicitAsk` deliberately absent — see listRules. */
|
|
215
|
+
function toFileShape(rules) {
|
|
216
|
+
const r = rules && typeof rules === 'object' ? rules : {};
|
|
217
|
+
const list = (v) => (Array.isArray(v) ? v.map(String) : []);
|
|
218
|
+
return {
|
|
219
|
+
defaultMode: r.defaultMode === 'allow' || r.defaultMode === 'deny' ? r.defaultMode : 'ask',
|
|
220
|
+
deny: list(r.deny),
|
|
221
|
+
allow: list(r.allow),
|
|
222
|
+
ask: list(r.ask),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Add one rule to one list.
|
|
228
|
+
*
|
|
229
|
+
* @param {string} list 'allow' | 'deny' | 'ask'
|
|
230
|
+
* @param {string} rule the pattern, e.g. `exec(npm test)`
|
|
231
|
+
* @returns {{ok: boolean, added?: boolean, rules?: object, reason?: string}}
|
|
232
|
+
*/
|
|
233
|
+
function addRule(list, rule, opts = {}) {
|
|
234
|
+
const key = String(list || '');
|
|
235
|
+
if (!LISTS.includes(key)) return { ok: false, reason: `unknown list "${key}"` };
|
|
236
|
+
|
|
237
|
+
const valid = validateRule(rule, opts);
|
|
238
|
+
if (!valid.ok) return { ok: false, reason: valid.reason };
|
|
239
|
+
const warning = valid.warning;
|
|
240
|
+
|
|
241
|
+
const store = resolveStore(opts);
|
|
242
|
+
if (!store.ok) return { ok: false, reason: store.reason };
|
|
243
|
+
|
|
244
|
+
const pattern = String(rule).trim();
|
|
245
|
+
try {
|
|
246
|
+
const current = toFileShape(store.config.loadPermissions());
|
|
247
|
+
const res = store.config.addPermissionRule(key, pattern, current);
|
|
248
|
+
const next = res && res.rules ? toFileShape(res.rules) : current;
|
|
249
|
+
if (res && res.added === false) {
|
|
250
|
+
return { ok: true, added: false, rules: next, ...(warning ? { warning } : {}) };
|
|
251
|
+
}
|
|
252
|
+
store.config.savePermissions(next);
|
|
253
|
+
return { ok: true, added: true, rules: next, ...(warning ? { warning } : {}) };
|
|
254
|
+
} catch (e) {
|
|
255
|
+
return { ok: false, reason: (e && e.message) || String(e) };
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Remove one rule from one list.
|
|
261
|
+
*
|
|
262
|
+
* Not validated: removing an unparseable rule is how a user repairs a file they
|
|
263
|
+
* (or an older build) got wrong, so a broken pattern must remain removable. The
|
|
264
|
+
* read-back is what proves it happened.
|
|
265
|
+
*
|
|
266
|
+
* @returns {{ok: boolean, removed?: boolean, rules?: object, reason?: string}}
|
|
267
|
+
*/
|
|
268
|
+
function removeRule(list, rule, opts = {}) {
|
|
269
|
+
const key = String(list || '');
|
|
270
|
+
if (!LISTS.includes(key)) return { ok: false, reason: `unknown list "${key}"` };
|
|
271
|
+
|
|
272
|
+
const store = resolveStore(opts);
|
|
273
|
+
if (!store.ok) return { ok: false, reason: store.reason };
|
|
274
|
+
|
|
275
|
+
const pattern = String(rule == null ? '' : rule).trim();
|
|
276
|
+
if (!pattern) return { ok: false, reason: 'no rule named' };
|
|
277
|
+
|
|
278
|
+
try {
|
|
279
|
+
const current = toFileShape(store.config.loadPermissions());
|
|
280
|
+
const res = store.config.removePermissionRule(key, pattern, current);
|
|
281
|
+
const next = res && res.rules ? toFileShape(res.rules) : current;
|
|
282
|
+
if (res && res.removed === false) return { ok: true, removed: false, rules: next };
|
|
283
|
+
store.config.savePermissions(next);
|
|
284
|
+
return { ok: true, removed: true, rules: next };
|
|
285
|
+
} catch (e) {
|
|
286
|
+
return { ok: false, reason: (e && e.message) || String(e) };
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
module.exports = {
|
|
291
|
+
LISTS,
|
|
292
|
+
KNOWN_TOOLS,
|
|
293
|
+
toFileShape,
|
|
294
|
+
validateRule,
|
|
295
|
+
listRules,
|
|
296
|
+
addRule,
|
|
297
|
+
removeRule,
|
|
298
|
+
};
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
|
|
2
|
+
// AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
|
|
3
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
4
|
+
//
|
|
5
|
+
// ── "Always allow this command" — persisting an exact rule ─────────────────
|
|
6
|
+
//
|
|
7
|
+
// §3 of ~/plan/agent-safety-plan.md. §2 taught the desktop gate to *read*
|
|
8
|
+
// `~/.aegiscode/permissions.json`. This module is the other direction: the card
|
|
9
|
+
// gains an answer that outlives the window.
|
|
10
|
+
//
|
|
11
|
+
// ## The two defects this closes
|
|
12
|
+
//
|
|
13
|
+
// The gate's only durable-ish answer was "Allow for this session", which is in
|
|
14
|
+
// memory and is keyed on the TOOL NAME (`sessionAllowlists`, engine.js). So:
|
|
15
|
+
//
|
|
16
|
+
// scope approving one `exec` allowed EVERY `exec` for the rest of the
|
|
17
|
+
// conversation — `npm test` was approved, and `rm -rf ~/work` was
|
|
18
|
+
// thereby also approved. The user answered about a command and the
|
|
19
|
+
// engine remembered a tool.
|
|
20
|
+
// lifetime "this session" is cleared by `newChat()` and never survives a
|
|
21
|
+
// restart, so the same safe `npm test` was re-asked every morning.
|
|
22
|
+
//
|
|
23
|
+
// Persisting an exact rule fixes both at once: the rule is the *command*, and
|
|
24
|
+
// it is in the shared store the CLI already reads, so one face's answer governs
|
|
25
|
+
// the other.
|
|
26
|
+
//
|
|
27
|
+
// ## Why the rule is composed, never globbed from the command
|
|
28
|
+
//
|
|
29
|
+
// The tempting implementation is `exec(${command})`. It is wrong, and the rule
|
|
30
|
+
// grammar is why: `globToRegex` (cli/src/permissions.js) maps `*` to `.*` and
|
|
31
|
+
// has NO escape syntax for a literal one. A command the user approved that
|
|
32
|
+
// happens to contain `*` — `ls *.js`, `find . -name '*.tmp' -delete` — would be
|
|
33
|
+
// persisted as a WILDCARD wider than what they clicked. So a subject containing
|
|
34
|
+
// `*`, `?` or a newline is refused rather than approximated: the card's other
|
|
35
|
+
// three answers remain, and this one declines with a reason. Widening a
|
|
36
|
+
// permission because the input could not be expressed honestly is the exact
|
|
37
|
+
// class of bug this whole plan exists to prevent.
|
|
38
|
+
//
|
|
39
|
+
// ## What a persisted rule may NOT be
|
|
40
|
+
//
|
|
41
|
+
// `exec` with no pattern matches every shell call, and an allow rule is
|
|
42
|
+
// consulted BEFORE the multi-`cd` rail (`evalPermission`: deny, allow,
|
|
43
|
+
// isMultiDirCommand, ask, explicitAsk). Persisting one would therefore disable
|
|
44
|
+
// that rail permanently — silently, with the gate still looking healthy. So an
|
|
45
|
+
// empty subject is refused: no subject, no rule.
|
|
46
|
+
//
|
|
47
|
+
// A multi-`cd` command is treated differently, and deliberately: the exact
|
|
48
|
+
// string is stored (`exec(cd a && cd b && ls)`), which suppresses the rail for
|
|
49
|
+
// THAT command only. The rail's purpose is per-invocation scrutiny of a command
|
|
50
|
+
// that changes directories twice; a rule that can only ever match the precise
|
|
51
|
+
// string the user was shown and clicked does not remove that scrutiny from any
|
|
52
|
+
// other command. A bare/wildcard form would, which is why those stay refused.
|
|
53
|
+
//
|
|
54
|
+
// ## The writer is the CLI's, deliberately
|
|
55
|
+
//
|
|
56
|
+
// Rules are written through `cli/src/config.js`'s `addPermissionRule` +
|
|
57
|
+
// `savePermissions` — the same pair `/permissions` uses — so there is one rule
|
|
58
|
+
// schema and one file format (D8 rule 1). This module composes a string and
|
|
59
|
+
// delegates; it never writes JSON itself. `explicitAsk` is stripped before
|
|
60
|
+
// saving because it is DERIVED from `defaultMode` at read time, and persisting
|
|
61
|
+
// it produces the disagreeing-file bug `saveRuleMode` documents.
|
|
62
|
+
|
|
63
|
+
'use strict';
|
|
64
|
+
|
|
65
|
+
const { loadShared } = require('./perm-rules.js');
|
|
66
|
+
|
|
67
|
+
// ── Composing the rule ────────────────────────────────────────────────────
|
|
68
|
+
|
|
69
|
+
/** Characters the rule grammar cannot express literally. `*` and `?` are its
|
|
70
|
+
* wildcards and `globToRegex` offers no escape; a newline does not match
|
|
71
|
+
* `RULE_RE`'s `.*` at all, so a multi-line command would half-match or not
|
|
72
|
+
* match while still being stored. */
|
|
73
|
+
const INEXPRESSIBLE = /[*?\n\r]/;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Build the exact rule for one approved call.
|
|
77
|
+
*
|
|
78
|
+
* @param {string} name tool name as the engine spells it (`exec`, `writeFile`)
|
|
79
|
+
* @param {object} args normalised args
|
|
80
|
+
* @param {object} [opts]
|
|
81
|
+
* @param {object} [opts.dir] override the module-layout search root (tests)
|
|
82
|
+
* @returns {{ok: boolean, rule?: string, subject?: string, reason?: string}}
|
|
83
|
+
* `ok: false` is a refusal, not an error — the caller keeps the card's other
|
|
84
|
+
* answers and shows `reason`.
|
|
85
|
+
*/
|
|
86
|
+
function composeRule(name, args, opts = {}) {
|
|
87
|
+
const tool = String(name || '').trim();
|
|
88
|
+
if (!tool) return { ok: false, reason: 'no tool name' };
|
|
89
|
+
|
|
90
|
+
const shared = loadShared(opts.dir);
|
|
91
|
+
if (!shared) return { ok: false, reason: 'one/src/permissions.js not found' };
|
|
92
|
+
|
|
93
|
+
let subjectFor;
|
|
94
|
+
try {
|
|
95
|
+
subjectFor = shared.permissions.loadEvaluator().subjectFor;
|
|
96
|
+
} catch (e) {
|
|
97
|
+
return { ok: false, reason: (e && e.message) || String(e) };
|
|
98
|
+
}
|
|
99
|
+
if (typeof subjectFor !== 'function') return { ok: false, reason: 'rule evaluator is incomplete' };
|
|
100
|
+
|
|
101
|
+
const subject = subjectFor(tool, args || {});
|
|
102
|
+
// An empty subject means the tool carries no addressable target (a bare
|
|
103
|
+
// `exec` with no command, say). Persisting that would be a tool-wide allow —
|
|
104
|
+
// see the header on the multi-`cd` rail.
|
|
105
|
+
if (!subject) return { ok: false, reason: 'this call has no command or path to scope a rule to' };
|
|
106
|
+
if (INEXPRESSIBLE.test(subject)) {
|
|
107
|
+
return {
|
|
108
|
+
ok: false,
|
|
109
|
+
reason: 'this command contains a wildcard character, which the rule language cannot match literally',
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// The rule grammar's own limit: `RULE_RE` is `^(\w+)(?:\((.*)\))?$` and `.*`
|
|
114
|
+
// is greedy, so a subject whose own parentheses balance is safe (the last `)`
|
|
115
|
+
// closes the rule) — but one that *ends* in `)` still parses correctly for the
|
|
116
|
+
// same reason. Verified in test/local-perm-write.test.mjs rather than assumed.
|
|
117
|
+
return { ok: true, rule: `${tool}(${subject})`, subject, tool };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** A human label for the card, so the button can say what it will store. */
|
|
121
|
+
function describeRule(name, args, opts = {}) {
|
|
122
|
+
const composed = composeRule(name, args, opts);
|
|
123
|
+
return composed.ok ? composed.rule : null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ── Persisting it ─────────────────────────────────────────────────────────
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Append an exact allow rule to the shared permission file.
|
|
130
|
+
*
|
|
131
|
+
* @param {string} rule from `composeRule`
|
|
132
|
+
* @param {object} [opts]
|
|
133
|
+
* @param {object} [opts.dir] layout override (tests)
|
|
134
|
+
* @param {object} [opts.config] injected config-store module (tests) — when
|
|
135
|
+
* present the real file is never read or written
|
|
136
|
+
* @returns {{ok: boolean, added?: boolean, rules?: object, source?: string, reason?: string}}
|
|
137
|
+
*/
|
|
138
|
+
function persistAllow(rule, opts = {}) {
|
|
139
|
+
if (!rule || typeof rule !== 'string') return { ok: false, reason: 'no rule to persist' };
|
|
140
|
+
|
|
141
|
+
let config = opts.config;
|
|
142
|
+
if (!config) {
|
|
143
|
+
const shared = loadShared(opts.dir);
|
|
144
|
+
if (!shared) return { ok: false, reason: 'one/src/permissions.js not found' };
|
|
145
|
+
try {
|
|
146
|
+
config = shared.permissions.loadConfigStore();
|
|
147
|
+
} catch (e) {
|
|
148
|
+
return { ok: false, reason: (e && e.message) || String(e) };
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (!config || typeof config.addPermissionRule !== 'function' || typeof config.savePermissions !== 'function') {
|
|
152
|
+
return { ok: false, reason: 'the permissions store is unavailable, so no rule was written' };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let current;
|
|
156
|
+
try {
|
|
157
|
+
current = typeof config.loadPermissions === 'function' ? config.loadPermissions() : {};
|
|
158
|
+
} catch (e) {
|
|
159
|
+
return { ok: false, reason: (e && e.message) || String(e) };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// `explicitAsk` is derived from `defaultMode` by loadPermissions() and is not
|
|
163
|
+
// a field of the file. Saving it back would leave a file that disagrees with
|
|
164
|
+
// itself — the bug cli/src/config.js's saveRuleMode documents at length.
|
|
165
|
+
const { explicitAsk, ...rest } = current || {};
|
|
166
|
+
const base = {
|
|
167
|
+
defaultMode: rest.defaultMode === 'allow' || rest.defaultMode === 'deny' ? rest.defaultMode : 'ask',
|
|
168
|
+
allow: Array.isArray(rest.allow) ? rest.allow : [],
|
|
169
|
+
deny: Array.isArray(rest.deny) ? rest.deny : [],
|
|
170
|
+
ask: Array.isArray(rest.ask) ? rest.ask : [],
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
let next;
|
|
174
|
+
try {
|
|
175
|
+
const res = config.addPermissionRule('allow', rule, base);
|
|
176
|
+
next = res && res.rules ? res.rules : base;
|
|
177
|
+
if (res && res.added === false) return { ok: true, added: false, rules: next, source: 'store' };
|
|
178
|
+
config.savePermissions(next);
|
|
179
|
+
} catch (e) {
|
|
180
|
+
return { ok: false, reason: (e && e.message) || String(e) };
|
|
181
|
+
}
|
|
182
|
+
return { ok: true, added: true, rules: next, source: 'store' };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* The whole answer: compose and persist in one call, in that order, so an
|
|
187
|
+
* inexpressible subject never reaches the writer.
|
|
188
|
+
*
|
|
189
|
+
* @returns {{ok: boolean, rule?: string, added?: boolean, reason?: string}}
|
|
190
|
+
*/
|
|
191
|
+
function alwaysAllow(name, args, opts = {}) {
|
|
192
|
+
const composed = composeRule(name, args, opts);
|
|
193
|
+
if (!composed.ok) return { ok: false, reason: composed.reason };
|
|
194
|
+
const saved = persistAllow(composed.rule, opts);
|
|
195
|
+
if (!saved.ok) return { ok: false, rule: composed.rule, reason: saved.reason };
|
|
196
|
+
return { ok: true, rule: composed.rule, added: saved.added !== false };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
module.exports = { composeRule, describeRule, persistAllow, alwaysAllow };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
// Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
3
|
+
"use strict";const fs=require("node:fs"),os=require("node:os"),path=require("node:path"),MAX_ENTRIES=200;function storePath(e=process.env){const t=e||{},r=process.env||{},n=t.AEGIS_PRO_SESSIONS_FILE||r.AEGIS_PRO_SESSIONS_FILE;if(n)return n;const s=t.AEGISCODE_HOME||t.AEGIS_HOME||r.AEGISCODE_HOME||r.AEGIS_HOME;return path.join(s?path.resolve(String(s).trim()):path.join(os.homedir(),".aegiscode"),"pro-sessions.json")}function read(e=process.env){try{const t=fs.readFileSync(storePath(e),"utf8"),r=JSON.parse(t);if(!r||"object"!=typeof r||Array.isArray(r))return{};const n={};for(const[e,t]of Object.entries(r))t&&"object"==typeof t&&"string"==typeof t.id&&t.id&&(n[e]={id:t.id,at:Number(t.at)||0});return n}catch{return{}}}function prune(e){const t=Object.keys(e);if(t.length<=MAX_ENTRIES)return e;t.sort(((t,r)=>(e[r].at||0)-(e[t].at||0)));const r={};for(const n of t.slice(0,MAX_ENTRIES))r[n]=e[n];return r}function write(e,t=process.env){try{const r=storePath(t);fs.mkdirSync(path.dirname(r),{recursive:!0});const n=`${r}.${process.pid}.tmp`;return fs.writeFileSync(n,JSON.stringify(prune(e),null,2),{mode:384}),fs.renameSync(n,r),!0}catch{return!1}}function get(e,t=process.env){if(!e)return null;const r=read(t);return r[e]?r[e].id:null}function set(e,t,r=process.env){if(!e||"string"!=typeof t||!t)return!1;const n=read(r);return n[e]={id:t,at:Date.now()},write(n,r)}function del(e,t=process.env){if(!e)return!1;const r=read(t);return!r[e]||(delete r[e],write(r,t))}module.exports={storePath:storePath,read:read,write:write,get:get,set:set,del:del,MAX_ENTRIES:MAX_ENTRIES};
|