aegis-desktop 1.0.18 → 1.0.20
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/lib/local/exec-plan.js +269 -1
- package/lib/local/integrity.js +165 -0
- package/lib/local/shell.js +712 -1
- package/lib/local/tools.js +1328 -1
- package/main.js +4812 -3
- package/package.json +1 -1
package/lib/local/exec-plan.js
CHANGED
|
@@ -1,3 +1,271 @@
|
|
|
1
1
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
2
|
// Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
|
|
3
|
-
|
|
3
|
+
// AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
|
|
4
|
+
// Conveying a modified copy, or running one as a network service, owes the
|
|
5
|
+
// corresponding source under this licence (§5/§13) — see LICENSING.md.
|
|
6
|
+
/**
|
|
7
|
+
* Which `exec` calls are safe to run WITHOUT the turn's persistent shell.
|
|
8
|
+
*
|
|
9
|
+
* WHY THIS EXISTS. `engine.js` batches the read-only calls in a round into one
|
|
10
|
+
* `Promise.all`, but `exec` was excluded from that batching because it is in
|
|
11
|
+
* `MUTATING_TOOLS` — one persistent shell per turn means two concurrent
|
|
12
|
+
* commands would interleave their streams and the `__AEGIS_SH_…EXIT` sentinel
|
|
13
|
+
* each one parses would resolve the wrong call. That exclusion is correct, but
|
|
14
|
+
* it is also where the wall-clock went: across the measured SWE-bench runs
|
|
15
|
+
* 81.7% of tool calls are `exec` (438/536) and only 10.4% are readFile/grep/
|
|
16
|
+
* listDir/glob, so the parallel window covered a tenth of the work. Total time
|
|
17
|
+
* was 2223s against another harness's 1322s for the SAME patches while the
|
|
18
|
+
* medians matched (120s vs 126s) — a serial dispatch shows up as tail, not as
|
|
19
|
+
* median.
|
|
20
|
+
*
|
|
21
|
+
* THE OPENING. Most of those exec calls are not mutations at all. They are
|
|
22
|
+
* reads dressed as shell — `grep`, `sed -n`, `git log`, `ls`, `cat` — and
|
|
23
|
+
* 378 of 438 chain an absolute `cd` first, which is the habit the `exec` schema
|
|
24
|
+
* teaches ("state does not persist"? — it does, but the model writes `cd` anyway
|
|
25
|
+
* because every one-shot example it has seen does). A leading `cd <abs> &&` is
|
|
26
|
+
* therefore not state TO the command, it is an address the command carries with
|
|
27
|
+
* it: run that command with `cwd` set to that directory and the shell it does
|
|
28
|
+
* not touch is irrelevant.
|
|
29
|
+
*
|
|
30
|
+
* SO THE TEST IS: could this exact command run in a FRESH process, with `cwd`
|
|
31
|
+
* set as the lead-in says, and produce the same output? Everything below is an
|
|
32
|
+
* attempt to answer that conservatively. The bias is deliberate and one-way —
|
|
33
|
+
* a false negative costs a round of serial execution, a false positive corrupts
|
|
34
|
+
* a user's repository, so every uncertainty resolves to "keep it sequential".
|
|
35
|
+
*
|
|
36
|
+
* WHAT IS REJECTED, and why each one is not merely cautious:
|
|
37
|
+
* - a write verb anywhere in the command (`rm`, `mv`, `tee`, `>`, `>>`, `sed -i`,
|
|
38
|
+
* `pip install`, `patch`, `git commit`, …) — obviously not read-only;
|
|
39
|
+
* - `cd` that is NOT the sole leading `cd <path> &&` — a mid-command `cd`
|
|
40
|
+
* changes what a LATER command in the same string sees;
|
|
41
|
+
* - shell variables (`$FOO`, `${FOO}`), bare `export`, `source`, `.` — these
|
|
42
|
+
* only mean anything if session state exists, and with a fresh process they
|
|
43
|
+
* would expand to empty strings and silently produce a DIFFERENT result
|
|
44
|
+
* rather than an error;
|
|
45
|
+
* - job control / backgrounding (`&`, `nohup`), pipelines into a pager;
|
|
46
|
+
* - here-docs and multi-line commands — the exit sentinel logic is per-line
|
|
47
|
+
* and a multi-line body is the shape most likely to hide a write;
|
|
48
|
+
* - any command whose leading `cd` target is relative or `~`-relative beyond
|
|
49
|
+
* the first segment, because it would resolve against the SESSION's cwd,
|
|
50
|
+
* not this process's.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
// Read-shaped verbs that are safe as the FIRST word of a pipeline segment.
|
|
54
|
+
// Deliberately a small allow-list rather than a deny-list of write verbs: a
|
|
55
|
+
// deny-list fails open the moment someone invents a write verb we did not
|
|
56
|
+
// imagine, and the cost of failing open here is a corrupted tree.
|
|
57
|
+
//
|
|
58
|
+
// NOT in this set, on purpose: `python`, `python3`, `node`, `sh`, `bash`.
|
|
59
|
+
// They are arbitrary programs, and the contract this classifier has to prove is
|
|
60
|
+
// not "the command is a read the USER would call a read" but "the command cannot
|
|
61
|
+
// produce a filesystem mutation a CONCURRENT peer could observe". No text scan
|
|
62
|
+
// can prove that about an interpreter — `python -c "open(p,'w')"` is one line,
|
|
63
|
+
// and chasing every spelling of a write (`write`, `os.remove`, `shutil`,
|
|
64
|
+
// `subprocess`, `-m pip`) is an unbounded guess. So interpreter invocations stay
|
|
65
|
+
// on the sequential path. That is a real limit on how much of the measured
|
|
66
|
+
// exec traffic this opens up (most SWE-bench commands are `python -c`), and it
|
|
67
|
+
// is the reason the wall-clock gain here is bounded rather than dramatic.
|
|
68
|
+
const READ_VERBS = new Set([
|
|
69
|
+
'cat', 'head', 'tail', 'less', 'more', 'wc', 'grep', 'egrep', 'fgrep', 'rg',
|
|
70
|
+
'sed', 'awk', 'cut', 'sort', 'uniq', 'tr', 'diff', 'comm', 'join', 'nl',
|
|
71
|
+
'ls', 'dir', 'tree', 'stat', 'file', 'du', 'df', 'pwd', 'which', 'type',
|
|
72
|
+
'basename', 'dirname', 'realpath', 'readlink', 'find', 'locate',
|
|
73
|
+
'echo', 'printf', 'date', 'uname', 'whoami', 'id', 'env', 'printenv',
|
|
74
|
+
'true', 'test',
|
|
75
|
+
]);
|
|
76
|
+
|
|
77
|
+
// `git` subcommands that only read. `git diff/status/log/show/blame/grep` are
|
|
78
|
+
// the ones that actually appear in agent transcripts; anything else (`git
|
|
79
|
+
// checkout`, `git apply`, `git stash`, `git reset`) is not on this list on
|
|
80
|
+
// purpose.
|
|
81
|
+
const GIT_READ_SUBCOMMANDS = new Set([
|
|
82
|
+
'diff', 'status', 'log', 'show', 'blame', 'grep', 'ls-files', 'rev-parse',
|
|
83
|
+
'describe', 'cat-file', 'shortlog', 'whatchanged', 'reflog', 'show-ref',
|
|
84
|
+
'ls-tree', 'diff-tree', 'diff-files', 'diff-index', 'merge-base',
|
|
85
|
+
]);
|
|
86
|
+
|
|
87
|
+
// Absolute-write shapes. Any hit anywhere (not just at a segment start) makes
|
|
88
|
+
// the whole command sequential — `find . -delete`, `sed -i`, `python -c
|
|
89
|
+
// "open(p,'w')"` all hide their write behind a read-shaped verb.
|
|
90
|
+
const WRITE_PATTERNS = [
|
|
91
|
+
/(^|[\s;&|(])rm(\s|$)/,
|
|
92
|
+
/(^|[\s;&|(])rmdir(\s|$)/,
|
|
93
|
+
/(^|[\s;&|(])mv(\s|$)/,
|
|
94
|
+
/(^|[\s;&|(])cp(\s|$)/,
|
|
95
|
+
/(^|[\s;&|(])ln(\s|$)/,
|
|
96
|
+
/(^|[\s;&|(])mkdir(\s|$)/,
|
|
97
|
+
/(^|[\s;&|(])touch(\s|$)/,
|
|
98
|
+
/(^|[\s;&|(])chmod(\s|$)/,
|
|
99
|
+
/(^|[\s;&|(])chown(\s|$)/,
|
|
100
|
+
/(^|[\s;&|(])truncate(\s|$)/,
|
|
101
|
+
/(^|[\s;&|(])dd(\s|$)/,
|
|
102
|
+
/(^|[\s;&|(])tee(\s|$)/,
|
|
103
|
+
/(^|[\s;&|(])install(\s|$)/,
|
|
104
|
+
/(^|[\s;&|(])patch(\s|$)/,
|
|
105
|
+
/(^|[\s;&|(])git\s+(commit|add|rm|mv|checkout|switch|reset|apply|revert|merge|rebase|stash|clean|init|clone|pull|push|fetch|tag|branch|config)\b/,
|
|
106
|
+
/(^|[\s;&|(])sed\s+(-[a-zA-Z]*i|--in-place)/,
|
|
107
|
+
// `sort -o f`, `uniq -o`? — only a few read verbs take a write flag, and it
|
|
108
|
+
// is easy to miss because the verb is allow-listed. `grep -o` is NOT this
|
|
109
|
+
// (it only changes the OUTPUT shape), so the match is verb-qualified.
|
|
110
|
+
/(^|[\s;&|(])sort\b[^|;]*\s-o(\s|$)/,
|
|
111
|
+
/(^|[\s;&|(])sort\b[^|;]*--output=/,
|
|
112
|
+
/(^|[\s;&|(])dd\b[^|;]*\bof=/,
|
|
113
|
+
/(^|[\s;&|(])find\b[^|;]*\s-(fprint|fprint0|fls)\b/,
|
|
114
|
+
/(^|[\s;&|(])find\b[^|]*\s-(delete|exec|execdir|ok|okdir)\b/,
|
|
115
|
+
/(^|[\s;&|(])(pip|pip3)\s+(install|uninstall|download)\b/,
|
|
116
|
+
/(^|[\s;&|(])npm\s+(install|i|ci|uninstall|publish|run)\b/,
|
|
117
|
+
/(^|[\s;&|(])yarn\s+(add|install|remove)\b/,
|
|
118
|
+
/(^|[\s;&|(])apt(-get)?\s+(install|remove|purge|update|upgrade)\b/,
|
|
119
|
+
/(^|[\s;&|(])(python|python3)\s+-m\s+pip\s+install\b/,
|
|
120
|
+
/(^|[\s;&|(])pytest\b[^|]*--(cov|junitxml)/,
|
|
121
|
+
// Shell-level writes. `>` redirect is matched conservatively: `2>&1` and
|
|
122
|
+
// `>&-` are not writes, so the digit/`&` cases are excluded explicitly.
|
|
123
|
+
/(?<![0-9&])>>?(?![&-])/,
|
|
124
|
+
/\bexport\s+[A-Za-z_]/,
|
|
125
|
+
/(^|[\s;&|(])source(\s|$)/,
|
|
126
|
+
];
|
|
127
|
+
|
|
128
|
+
// Commands that only make sense against session state.
|
|
129
|
+
const STATEFUL_PATTERNS = [
|
|
130
|
+
/\$\{?[A-Za-z_][A-Za-z0-9_]*\}?/,
|
|
131
|
+
/(^|[\s;&|(])pushd(\s|$)/,
|
|
132
|
+
/(^|[\s;&|(])popd(\s|$)/,
|
|
133
|
+
/(^|[\s;&|(])nohup(\s|$)/,
|
|
134
|
+
/(^|[\s;&|(])disown(\s|$)/,
|
|
135
|
+
/<<-?\s*['"]?[A-Za-z_]/,
|
|
136
|
+
/(^|[\s;&|(])\&$/,
|
|
137
|
+
/(^|[\s;&|(])exec(\s|$)/,
|
|
138
|
+
/(^|[\s;&|(])history(\s|$)/,
|
|
139
|
+
/(^|[\s;&|(])jobs(\s|$)/,
|
|
140
|
+
];
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Strip leading wrapper programs (`timeout 300`, `time`, `command`, `env -i`)
|
|
144
|
+
* down to the verb that actually runs. A wrapper must NOT be trusted on its
|
|
145
|
+
* own: `timeout 550 python x.py` starts with `timeout`, but the thing being
|
|
146
|
+
* run is an interpreter, and the allow-list has to judge THAT.
|
|
147
|
+
* Returns null when the wrapper chain hides something we cannot name.
|
|
148
|
+
*/
|
|
149
|
+
function effectiveVerb(words) {
|
|
150
|
+
let i = 0;
|
|
151
|
+
// Bound the walk: a pathological chain of wrappers is not worth reasoning
|
|
152
|
+
// about, and stopping early only costs a miss (stays sequential).
|
|
153
|
+
for (let guard = 0; guard < 4 && i < words.length; guard++) {
|
|
154
|
+
const w = words[i];
|
|
155
|
+
if (w === 'time' || w === 'command') { i += 1; continue; }
|
|
156
|
+
if (w === 'env') {
|
|
157
|
+
i += 1;
|
|
158
|
+
// `env -i`, `env -u X`, and `env FOO=1` prefixes are all local to this
|
|
159
|
+
// invocation (they are not session state), so skip them.
|
|
160
|
+
while (i < words.length && (words[i].startsWith('-') || /^[A-Za-z_][A-Za-z0-9_]*=/.test(words[i]))) i += 1;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (w === 'timeout') {
|
|
164
|
+
i += 1;
|
|
165
|
+
while (i < words.length && words[i].startsWith('-')) i += 1;
|
|
166
|
+
// the duration argument
|
|
167
|
+
if (i < words.length && /^[0-9]+(\.[0-9]+)?[smhd]?$/.test(words[i])) i += 1;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (w === 'nice') {
|
|
171
|
+
i += 1;
|
|
172
|
+
if (i < words.length && words[i] === '-n') i += 2;
|
|
173
|
+
else if (i < words.length && /^-?\d+$/.test(words[i])) i += 1;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
return i < words.length ? { verb: words[i], rest: words.slice(i) } : null;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Split on `;`/`&&`/`||`/`|` into [ {sep, text} ] keeping the separators. */
|
|
182
|
+
function segments(command) {
|
|
183
|
+
const out = [];
|
|
184
|
+
const re = /(\|\||&&|[;|])/g;
|
|
185
|
+
let last = 0;
|
|
186
|
+
let m;
|
|
187
|
+
while ((m = re.exec(command))) {
|
|
188
|
+
out.push({ sep: null, text: command.slice(last, m.index) });
|
|
189
|
+
out.push({ sep: m[1] });
|
|
190
|
+
last = m.index + m[1].length;
|
|
191
|
+
}
|
|
192
|
+
out.push({ sep: null, text: command.slice(last) });
|
|
193
|
+
return out;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Pull a single leading `cd <absolute path> &&` off the front.
|
|
198
|
+
* Returns { cwd, rest } — `cwd` is null when there is no such lead-in.
|
|
199
|
+
* Only an ABSOLUTE path qualifies: a relative one would resolve against the
|
|
200
|
+
* session's directory, which a one-shot process does not share.
|
|
201
|
+
*/
|
|
202
|
+
function leadingCd(command) {
|
|
203
|
+
const m = /^\s*cd\s+(?:--\s+)?(?:"([^"]+)"|'([^']+)'|([^\s;&|]+))\s*&&\s*([\s\S]+)$/.exec(command);
|
|
204
|
+
if (!m) return { cwd: null, rest: command };
|
|
205
|
+
const target = (m[1] || m[2] || m[3] || '').trim();
|
|
206
|
+
if (!target.startsWith('/')) return { cwd: null, rest: command };
|
|
207
|
+
// `cd /a && cd /b && …` — the second cd is not the lead-in, so the rest must
|
|
208
|
+
// be judged on its own (it will then be rejected as a non-leading cd).
|
|
209
|
+
return { cwd: target, rest: m[4] };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Decide whether `command` may run as a one-shot process with an explicit cwd.
|
|
214
|
+
*
|
|
215
|
+
* @param {string} command raw command the model asked `exec` to run
|
|
216
|
+
* @param {object} [opts]
|
|
217
|
+
* @param {string} [opts.baseCwd] the turn's cwd, used when there is no lead-in
|
|
218
|
+
* @returns {{ oneShot: boolean, cwd: string|null, reason: string }}
|
|
219
|
+
* `reason` is for tests and diagnostics; it is never shown to the model.
|
|
220
|
+
*/
|
|
221
|
+
function planExec(command, opts = {}) {
|
|
222
|
+
const baseCwd = opts.baseCwd || null;
|
|
223
|
+
const raw = typeof command === 'string' ? command : '';
|
|
224
|
+
const no = (reason) => ({ oneShot: false, cwd: null, reason });
|
|
225
|
+
|
|
226
|
+
if (!raw.trim()) return no('empty command');
|
|
227
|
+
// A newline means a multi-command body. The session's exit sentinel is
|
|
228
|
+
// appended per command and a multi-line body is where a write hides; not
|
|
229
|
+
// worth the risk for the handful of these a turn contains.
|
|
230
|
+
if (raw.includes('\n')) return no('multi-line command');
|
|
231
|
+
|
|
232
|
+
for (const re of STATEFUL_PATTERNS) {
|
|
233
|
+
if (re.test(raw)) return no('depends on session state');
|
|
234
|
+
}
|
|
235
|
+
for (const re of WRITE_PATTERNS) {
|
|
236
|
+
if (re.test(raw)) return no('contains a write');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const { cwd, rest } = leadingCd(raw);
|
|
240
|
+
const restText = rest.trim();
|
|
241
|
+
if (!restText) return no('nothing after the cd');
|
|
242
|
+
|
|
243
|
+
// Every segment must start with a read-shaped verb. `git` is special-cased
|
|
244
|
+
// on its subcommand; a bare `git` with no subcommand reads the status, which
|
|
245
|
+
// is read-only, and `git --version` likewise.
|
|
246
|
+
const segs = segments(restText).filter((s) => !s.sep);
|
|
247
|
+
if (!segs.length) return no('no command');
|
|
248
|
+
for (const seg of segs) {
|
|
249
|
+
const words = seg.text.trim().replace(/^\(+\s*/, '').split(/\s+/).filter(Boolean);
|
|
250
|
+
if (!words.length) continue;
|
|
251
|
+
const eff = effectiveVerb(words);
|
|
252
|
+
if (!eff) return no('empty command after wrappers');
|
|
253
|
+
const verb = eff.verb;
|
|
254
|
+
if (verb === 'git') {
|
|
255
|
+
const sub = eff.rest[1];
|
|
256
|
+
if (!sub || sub.startsWith('-')) continue; // `git`, `git --version`
|
|
257
|
+
if (!GIT_READ_SUBCOMMANDS.has(sub)) return no(`git ${sub} is not read-only`);
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (!READ_VERBS.has(verb)) return no(`${verb} is not a known read verb`);
|
|
261
|
+
// `sed -i` is already caught by WRITE_PATTERNS above, but `sed … -w file`
|
|
262
|
+
// and `awk … > file` write without an `-i`.
|
|
263
|
+
if ((verb === 'sed' || verb === 'awk') && /(^|\s)-[a-zA-Z]*w\s/.test(eff.rest.join(' '))) {
|
|
264
|
+
return no(`${verb} writes a file`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return { oneShot: true, cwd: cwd || baseCwd, reason: 'read-only' };
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
module.exports = { planExec, READ_VERBS, GIT_READ_SUBCOMMANDS, WRITE_PATTERNS, STATEFUL_PATTERNS };
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
|
|
3
|
+
// AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
|
|
4
|
+
// Conveying a modified copy, or running one as a network service, owes the
|
|
5
|
+
// corresponding source under this licence (§5/§13) — see LICENSING.md.
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* integrity — the desktop's copy of the second gate.
|
|
10
|
+
*
|
|
11
|
+
* `cli/src/limits.js` guards the CLI and says why it is shaped this way; read
|
|
12
|
+
* that header first, the reasoning is the same and is not repeated here. This
|
|
13
|
+
* file is the same question asked of the DESKTOP, which until now had no
|
|
14
|
+
* second gate at all: `aegis-desktop` shipped no verifier, so a build whose
|
|
15
|
+
* own bytes had been rewritten ran normally and only `provenance check` on the
|
|
16
|
+
* CLI face would ever have said otherwise.
|
|
17
|
+
*
|
|
18
|
+
* What is covered is the set of files that OWN a privilege rather than the
|
|
19
|
+
* whole tree — the same doctrine as the CLI's `UNITS`, because the point is to
|
|
20
|
+
* make removing the question expensive, not to make it exhaustive:
|
|
21
|
+
*
|
|
22
|
+
* - `main.js` the Electron bootstrap: IPC registration, the spawn
|
|
23
|
+
* sites, and the window that serves the renderer;
|
|
24
|
+
* - `lib/local/shell.js` the process that actually executes `exec`;
|
|
25
|
+
* - `lib/local/tools.js` the tool surface handed to the model;
|
|
26
|
+
* - `lib/local/exec-plan.js` the rule that decides what may run batched.
|
|
27
|
+
*
|
|
28
|
+
* A digest is taken over those files *as one closed set*, so changing any one
|
|
29
|
+
* changes the whole, and compared to a value sealed into this file. On a
|
|
30
|
+
* mismatch:
|
|
31
|
+
*
|
|
32
|
+
* - one bland line on stderr naming no file, no category, no remedy;
|
|
33
|
+
* - `AEGIS_REDUCED_MODE=1` in the environment for modules that opt in; and
|
|
34
|
+
* - `armed()` returns true, which every owner above is expected to consult
|
|
35
|
+
* before exercising its privilege (`main.js` refuses to create a window).
|
|
36
|
+
*
|
|
37
|
+
* The paths are base64 and the sealed digest is XORed with a derived pad. As
|
|
38
|
+
* with the CLI's interlock this is obfuscation and not cryptography: whoever
|
|
39
|
+
* holds the bytes can read it. The cost raised is the cost of *noticing* —
|
|
40
|
+
* there is no runtime switch, no manifest naming what is covered, and no
|
|
41
|
+
* self-describing failure to imitate.
|
|
42
|
+
*
|
|
43
|
+
* THIS FILE IS NOT IN ITS OWN SET. The seal lives here, so including it would
|
|
44
|
+
* make the digest self-referential and unstable — it is covered by the pack
|
|
45
|
+
* audit's manifest instead.
|
|
46
|
+
*
|
|
47
|
+
* A tree that is mid-edit trips this on purpose. Resealing happens during the
|
|
48
|
+
* build, after obfuscation, via `tools/obfuscate-faces.mjs` — the source tree's
|
|
49
|
+
* seal is written by `node tools/desktop-seal.mjs --write`. There is no
|
|
50
|
+
* environment variable that disables the check, because a gate with a
|
|
51
|
+
* documented off switch is a gate the manipulator reads about in this file.
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
const { createHash } = require('node:crypto');
|
|
55
|
+
const { existsSync, readFileSync } = require('node:fs');
|
|
56
|
+
const path = require('node:path');
|
|
57
|
+
|
|
58
|
+
/** The desktop package root: this file is `lib/local/`, so up two. */
|
|
59
|
+
const PACKAGE_ROOT = path.join(__dirname, '..', '..');
|
|
60
|
+
|
|
61
|
+
/** Domain separator, so this digest cannot be confused with any other. */
|
|
62
|
+
const SALT = 'aegis-desktop-interlock-v1';
|
|
63
|
+
/** Pad derivation, so the sealed value is not a plain digest of the set. */
|
|
64
|
+
const PAD = 'aegis-desktop-pad-v1';
|
|
65
|
+
|
|
66
|
+
const sha256 = (data) => createHash('sha256').update(data).digest('hex');
|
|
67
|
+
const PAD_HEX = sha256(PAD);
|
|
68
|
+
|
|
69
|
+
/** Nibble-wise XOR against the derived pad; self-inverse. */
|
|
70
|
+
function unmask(sealed) {
|
|
71
|
+
let out = '';
|
|
72
|
+
for (let i = 0; i < sealed.length; i++) {
|
|
73
|
+
const a = parseInt(sealed[i], 16);
|
|
74
|
+
const b = parseInt(PAD_HEX[i % PAD_HEX.length], 16);
|
|
75
|
+
if (Number.isNaN(a) || Number.isNaN(b)) return null;
|
|
76
|
+
out += (a ^ b).toString(16);
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The sealed set, base64 so a `cat` of this file is not a table of contents.
|
|
83
|
+
* Order is part of the digest: reordering is as detectable as editing.
|
|
84
|
+
*/
|
|
85
|
+
const UNITS = Object.freeze([
|
|
86
|
+
{ id: 'd1', rel: Buffer.from('bWFpbi5qcw==', 'base64').toString('utf8') },
|
|
87
|
+
{ id: 'd2', rel: Buffer.from('bGliL2xvY2FsL3NoZWxsLmpz', 'base64').toString('utf8') },
|
|
88
|
+
{ id: 'd3', rel: Buffer.from('bGliL2xvY2FsL3Rvb2xzLmpz', 'base64').toString('utf8') },
|
|
89
|
+
{ id: 'd4', rel: Buffer.from('bGliL2xvY2FsL2V4ZWMtcGxhbi5qcw==', 'base64').toString('utf8') },
|
|
90
|
+
]);
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The masked digest of the sealed set. Rewritten ONLY by the seal writer
|
|
94
|
+
* (`tools/desktop-seal.mjs` in the source tree, `tools/obfuscate-faces.mjs`
|
|
95
|
+
* after obfuscation), never by hand.
|
|
96
|
+
*/
|
|
97
|
+
const SEALED = '1a861aa90f27252a86d2082280d26e4104832ee92c0cf0768a88ec52dac62347';
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Ask the question. Never throws — a check that can break the program it is
|
|
101
|
+
* checking is a check an attacker would rather have present.
|
|
102
|
+
*
|
|
103
|
+
* @returns {{ tripped: boolean, code: string|null, units: number }}
|
|
104
|
+
*/
|
|
105
|
+
function check() {
|
|
106
|
+
const parts = [];
|
|
107
|
+
for (const unit of UNITS) {
|
|
108
|
+
const abs = path.join(PACKAGE_ROOT, unit.rel);
|
|
109
|
+
if (!existsSync(abs)) return { tripped: true, code: 'd1', units: UNITS.length };
|
|
110
|
+
try {
|
|
111
|
+
parts.push(sha256(readFileSync(abs)));
|
|
112
|
+
} catch {
|
|
113
|
+
return { tripped: true, code: 'd1', units: UNITS.length };
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const combined = sha256(`${SALT}\n${parts.join('\n')}`);
|
|
117
|
+
const sealed = typeof SEALED === 'string' ? SEALED.trim() : '';
|
|
118
|
+
if (sealed.length !== 64) return { tripped: true, code: 'd2', units: UNITS.length };
|
|
119
|
+
const expected = unmask(sealed);
|
|
120
|
+
if (expected !== combined) return { tripped: true, code: 'd3', units: UNITS.length };
|
|
121
|
+
return { tripped: false, code: null, units: UNITS.length };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The only sentence a failing build says about itself. Blunt, not a manual. */
|
|
125
|
+
function line(state) {
|
|
126
|
+
if (!state || !state.tripped) return null;
|
|
127
|
+
return `aegis-desktop: reduced mode (${state.code}) — this build is not the one that was published.\n`;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Ask once, remember the answer, and print the one bland line on the way.
|
|
132
|
+
*
|
|
133
|
+
* Memoised for one process, and only so that several callers produce one
|
|
134
|
+
* sentence instead of several. It is NOT a cache an attacker can prime: the
|
|
135
|
+
* first caller computes it from the bytes on disk and every later caller gets
|
|
136
|
+
* that verdict. Cannot throw, for the same reason `check` cannot.
|
|
137
|
+
*
|
|
138
|
+
* @returns {boolean} true when this build is not the published one.
|
|
139
|
+
*/
|
|
140
|
+
let ARMED = null;
|
|
141
|
+
function armed() {
|
|
142
|
+
if (ARMED !== null) return ARMED;
|
|
143
|
+
let state;
|
|
144
|
+
try {
|
|
145
|
+
state = check();
|
|
146
|
+
} catch {
|
|
147
|
+
state = { tripped: true, code: 'd2', units: UNITS.length };
|
|
148
|
+
}
|
|
149
|
+
ARMED = Boolean(state.tripped);
|
|
150
|
+
if (ARMED) {
|
|
151
|
+
// The mark is the mechanism; the line is only its announcement.
|
|
152
|
+
process.env.AEGIS_REDUCED_MODE = '1';
|
|
153
|
+
const said = line(state);
|
|
154
|
+
if (said) process.stderr.write(said);
|
|
155
|
+
}
|
|
156
|
+
return ARMED;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The seal writer's half of the contract; the tool owns the mapping. */
|
|
160
|
+
function sealOf(files) {
|
|
161
|
+
const parts = files.map((abs) => sha256(readFileSync(abs)));
|
|
162
|
+
return sha256(`${SALT}\n${parts.join('\n')}`);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
module.exports = { UNITS, SALT, PAD, check, armed, line, sealOf, mask: unmask };
|