aegiscode 7.0.24 → 7.0.26
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/package.json +7 -3
- package/scripts/check.mjs +52 -1
- package/scripts/demo.mjs +94 -1
- package/scripts/predist.mjs +75 -1
- package/scripts/runtests.mjs +56 -1
- package/scripts/seal-gate.mjs +74 -1
- package/src/agents.js +139 -1
- package/src/app.js +2957 -1
- package/src/art.js +289 -1
- package/src/chatflow.js +3023 -1
- package/src/checkpoint.js +90 -1
- package/src/clipboard.js +424 -1
- package/src/cloudsync.js +528 -1
- package/src/commands.js +4089 -1
- package/src/config.js +581 -1
- package/src/connect.js +349 -1
- package/src/credentials.js +33 -1
- package/src/deps.js +232 -1
- package/src/devrun.js +187 -1
- package/src/diff.js +333 -1
- package/src/diffview.js +296 -1
- package/src/engine.js +297 -1
- package/src/entitlement.js +355 -1
- package/src/events.js +707 -1
- package/src/export.js +67 -1
- package/src/filepreview.js +153 -1
- package/src/fingerprint.js +592 -1
- package/src/format.js +259 -1
- package/src/fuzzy.js +119 -1
- package/src/history.js +423 -1
- package/src/init.js +175 -1
- package/src/input.js +139 -1
- package/src/keys.js +183 -1
- package/src/markdown.js +306 -1
- package/src/models.js +507 -1
- package/src/naming.js +57 -1
- package/src/overlays.js +462 -1
- package/src/panels.js +1418 -1
- package/src/ping.js +226 -1
- package/src/presence.js +238 -1
- package/src/render.js +516 -1
- package/src/screen.js +518 -1
- package/src/screens.js +896 -1
- package/src/secret.js +59 -1
- package/src/securefs.js +359 -1
- package/src/settings.js +155 -1
- package/src/shared.js +52 -1
- package/src/sharedpaths.js +53 -1
- package/src/signup.js +156 -1
- package/src/summarize.js +95 -1
- package/src/sysdiag.js +585 -1
- package/src/system.js +40 -1
- package/src/theme.js +570 -1
- package/src/title.js +220 -1
- package/src/todos.js +115 -1
- package/src/tokens.js +263 -1
- package/src/topup.js +210 -1
- package/src/update.js +191 -1
- package/vendor/client/aegis.js +2441 -1
- package/vendor/client/attest.js +518 -1
- package/vendor/client/authorship-key.js +410 -1
- package/vendor/client/auto-provision.js +134 -1
- package/vendor/client/brain-catalog.js +268 -1
- package/vendor/client/byok-catalog.js +199 -1
- package/vendor/client/catalog-client.js +487 -1
- package/vendor/client/conversation-export.js +177 -1
- package/vendor/client/credentials.js +494 -1
- package/vendor/client/device-fingerprint.js +141 -1
- package/vendor/client/drive-connect.js +110 -1
- package/vendor/client/drive-upload.js +234 -1
- package/vendor/client/env-file.js +1273 -1
- package/vendor/client/fingerprint.js +1064 -1
- package/vendor/client/foreign-memory.js +669 -1
- package/vendor/client/google-signin.js +133 -1
- package/vendor/client/hooks.js +430 -1
- package/vendor/client/model-discovery.js +373 -1
- package/vendor/client/naming.js +283 -1
- package/vendor/client/platform.js +937 -1
- package/vendor/client/prefs.js +351 -1
- package/vendor/client/presence-constants.js +107 -1
- package/vendor/client/presence.js +438 -1
- package/vendor/client/record-vault.js +1055 -3
- package/vendor/client/session-store.js +575 -1
- package/vendor/client/topup.js +297 -1
- package/vendor/client/touch.js +458 -1
- package/vendor/client/update.js +763 -1
- package/vendor/client/user-models.js +381 -1
- package/vendor/desktop/lib/local/agents.js +105 -1
- package/vendor/desktop/lib/local/attachments.js +276 -1
- package/vendor/desktop/lib/local/autonomous.js +1045 -1
- package/vendor/desktop/lib/local/claude-pro.js +1028 -1
- package/vendor/desktop/lib/local/context.js +125 -1
- package/vendor/desktop/lib/local/engine.js +3666 -1
- package/vendor/desktop/lib/local/entitlement.js +660 -1
- package/vendor/desktop/lib/local/git-scope.js +228 -1
- package/vendor/desktop/lib/local/local.js +2145 -1
- package/vendor/desktop/lib/local/prompt.js +280 -1
- package/vendor/desktop/lib/local/queue.js +560 -1
- package/vendor/desktop/lib/local/session-rounds.js +238 -1
- package/vendor/desktop/lib/local/spill.js +75 -1
- package/vendor/desktop/lib/local/turn-guard.js +300 -1
- package/vendor/desktop/lib/local/worktree-lock.js +235 -1
- package/vendor/desktop/lib/settings.js +890 -1
- package/vendor/desktop/renderer/usage.js +1239 -1
- package/vendor/hosts/install.js +638 -1
- package/vendor/hosts/openai-shim.js +515 -1
- package/vendor/hosts/spec.js +164 -1
- package/vendor/hosts/targets.js +346 -1
- package/vendor/mcp/server.js +221 -1
- package/vendor/mcp/tools.js +408 -1
- package/vendor/one/src/child.js +285 -1
- package/vendor/one/src/permissions.js +383 -1
- package/SOURCE-OFFER.txt +0 -9
package/src/checkpoint.js
CHANGED
|
@@ -1,3 +1,92 @@
|
|
|
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
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Transcript checkpoints for /rewind. The session loop snapshots the
|
|
10
|
+
* transcript after every exchange into ~/.aegiscode/checkpoints/<session>.jsonl
|
|
11
|
+
* (bounded to the last CHECKPOINT_LIMIT), and /rewind lists or restores them.
|
|
12
|
+
* Checkpoint 0 is the session start (empty transcript).
|
|
13
|
+
*
|
|
14
|
+
* Ported from the upstream engine reference (ESM → CommonJS). The data dir
|
|
15
|
+
* now comes from config.js's shared `aegisDir()` helper.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const fs = require('node:fs');
|
|
19
|
+
const path = require('node:path');
|
|
20
|
+
const { aegisDir } = require('./config.js');
|
|
21
|
+
const { ensurePrivateDir, writePrivateFile } = require('./securefs.js');
|
|
22
|
+
|
|
23
|
+
const CHECKPOINT_LIMIT = 8;
|
|
24
|
+
|
|
25
|
+
function checkpointsPath(sessionId) {
|
|
26
|
+
return path.join(aegisDir(), 'checkpoints', `${sessionId}.jsonl`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Snapshot the current transcript. Returns the new checkpoint index.
|
|
31
|
+
* Cheap: transcripts are small arrays of messages.
|
|
32
|
+
*/
|
|
33
|
+
function snapshotCheckpoint(sessionId, transcript) {
|
|
34
|
+
try {
|
|
35
|
+
ensurePrivateDir(path.dirname(checkpointsPath(sessionId)));
|
|
36
|
+
const entry = {
|
|
37
|
+
ts: new Date().toISOString(),
|
|
38
|
+
depth: transcript.length,
|
|
39
|
+
words: transcript.reduce((a, m) => a + (m.text || '').split(/\s+/).length, 0),
|
|
40
|
+
transcript,
|
|
41
|
+
};
|
|
42
|
+
const p = checkpointsPath(sessionId);
|
|
43
|
+
const prev = fs.existsSync(p) ? fs.readFileSync(p, 'utf8').split('\n').filter(Boolean) : [];
|
|
44
|
+
const lines = [...prev, JSON.stringify(entry)];
|
|
45
|
+
const trimmed = lines.slice(Math.max(0, lines.length - CHECKPOINT_LIMIT));
|
|
46
|
+
// 0600: a checkpoint is the full transcript at a point in time.
|
|
47
|
+
writePrivateFile(p, trimmed.join('\n') + '\n');
|
|
48
|
+
return lines.length - 1; // index into the trimmed list
|
|
49
|
+
} catch {
|
|
50
|
+
return -1;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Newest-first checkpoint list: [{ idx, ts, depth, words }]. */
|
|
55
|
+
function listCheckpoints(sessionId) {
|
|
56
|
+
try {
|
|
57
|
+
const raw = fs.readFileSync(checkpointsPath(sessionId), 'utf8');
|
|
58
|
+
return raw
|
|
59
|
+
.split('\n')
|
|
60
|
+
.filter(Boolean)
|
|
61
|
+
.map((l, idx) => {
|
|
62
|
+
try {
|
|
63
|
+
const e = JSON.parse(l);
|
|
64
|
+
return { idx, ts: e.ts, depth: e.depth || 0, words: e.words || 0 };
|
|
65
|
+
} catch { return null; }
|
|
66
|
+
})
|
|
67
|
+
.filter(Boolean)
|
|
68
|
+
.reverse();
|
|
69
|
+
} catch {
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Restore a checkpoint by its stored index. Returns the transcript or null. */
|
|
75
|
+
function loadCheckpoint(sessionId, idx) {
|
|
76
|
+
try {
|
|
77
|
+
const raw = fs.readFileSync(checkpointsPath(sessionId), 'utf8').split('\n').filter(Boolean);
|
|
78
|
+
const e = JSON.parse(raw[idx]);
|
|
79
|
+
if (!e || !Array.isArray(e.transcript)) return null;
|
|
80
|
+
return e.transcript;
|
|
81
|
+
} catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
module.exports = {
|
|
87
|
+
CHECKPOINT_LIMIT,
|
|
88
|
+
checkpointsPath,
|
|
89
|
+
snapshotCheckpoint,
|
|
90
|
+
listCheckpoints,
|
|
91
|
+
loadCheckpoint,
|
|
92
|
+
};
|
package/src/clipboard.js
CHANGED
|
@@ -1,3 +1,426 @@
|
|
|
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
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Cross-platform clipboard access, both directions — the CLI's half of the
|
|
10
|
+
* gesture the user actually performs: mark text, copy it, paste it back.
|
|
11
|
+
*
|
|
12
|
+
* WRITE (copy): a real clipboard tool first (pbcopy / clip.exe / wl-copy /
|
|
13
|
+
* xclip / xsel), then **OSC 52** — the terminal's own "put this in the
|
|
14
|
+
* clipboard" escape, which needs no binary at all and works over SSH and inside
|
|
15
|
+
* tmux / screen — then a file in the data dir as the last resort. The CLI never
|
|
16
|
+
* errors out over clipboard availability.
|
|
17
|
+
*
|
|
18
|
+
* The OSC 52 rung is what makes copy work on a host with no clipboard binary
|
|
19
|
+
* installed. Measured on this host (`XDG_SESSION_TYPE=x11`, `DISPLAY=:0`, none
|
|
20
|
+
* of wl-copy / xclip / xsel present): every marking drag ended in
|
|
21
|
+
* "Marked N lines → ~/.aegiscode/clipboard.txt" — the text did NOT reach any
|
|
22
|
+
* clipboard, the user had to go find a file. The upstream engine fixed exactly
|
|
23
|
+
* this with the OSC 52 rung (`aegiscodex-dev/src/clipboard.js`); this module
|
|
24
|
+
* had the tool ladder and the file fallback only.
|
|
25
|
+
*
|
|
26
|
+
* READ (paste): the same tools in reverse — pbpaste / Get-Clipboard / xclip -o
|
|
27
|
+
* / wl-paste. Ctrl+V pastes an image from the clipboard (the file path lands in
|
|
28
|
+
* the prompt) or, when the clipboard holds no image, its text. Without this the
|
|
29
|
+
* CLI had NO clipboard paste at all: it only received what the terminal
|
|
30
|
+
* bracketed-paste frame delivered, so Ctrl+V did nothing on every platform.
|
|
31
|
+
*
|
|
32
|
+
* Two host-specific details this module has to get right, both of them
|
|
33
|
+
* previously wrong here:
|
|
34
|
+
*
|
|
35
|
+
* · **Windows has no mode bits.** The PATH hardening below (a directory is
|
|
36
|
+
* trusted only when it is not group/world-writable and is owned by root or
|
|
37
|
+
* by us) is a POSIX idea. `fs.statSync` on Windows reports 0o666-ish modes
|
|
38
|
+
* for every file, so `mode & 0o022` was true for *everything* — every
|
|
39
|
+
* candidate failed, `resolveTrustedTool()` returned null, and copy fell to
|
|
40
|
+
* the file rung on every Windows host even with clip.exe present. The
|
|
41
|
+
* ownership test is therefore skipped when `platform === 'win32'` (where the
|
|
42
|
+
* real guarantee is that PATH is user-owned), and Windows resolution adds
|
|
43
|
+
* the executable suffixes (`.exe`, `.cmd`, `.bat`) that a bare `clip` never
|
|
44
|
+
* matches.
|
|
45
|
+
* · **A tool that is not there is not a copy.** `via: 'file'` means the text
|
|
46
|
+
* reached no clipboard; `describeCopy()` says so instead of claiming a copy.
|
|
47
|
+
*
|
|
48
|
+
* Ported from the upstream engine reference (ESM → CommonJS). The data dir
|
|
49
|
+
* comes from the shared `aegisDir()` helper and the env vars are the CLI's own
|
|
50
|
+
* (AEGISCODE_*), with AEGISCODE_TRUST_PATH=0 and AEGISCODE_NO_OSC52=1 as the
|
|
51
|
+
* documented hatches.
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
const { spawnSync } = require('node:child_process');
|
|
55
|
+
const fs = require('node:fs');
|
|
56
|
+
const path = require('node:path');
|
|
57
|
+
const { aegisDir } = require('./config.js');
|
|
58
|
+
const { writePrivateFile } = require('./securefs.js');
|
|
59
|
+
|
|
60
|
+
const ESC = '\u001b';
|
|
61
|
+
const realPlatform = () => process.platform;
|
|
62
|
+
|
|
63
|
+
// ── tool discovery ─────────────────────────────────────────────────────────
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The clipboard tools this host could use, best-first.
|
|
67
|
+
*
|
|
68
|
+
* Platform gates first: pbcopy/pbpaste/pngpaste are macOS, clip.exe and
|
|
69
|
+
* PowerShell are Windows, the rest are X11/Wayland. Then env fit: on Linux a
|
|
70
|
+
* Wayland session lists wl-copy first and an X11 session lists xclip first, so
|
|
71
|
+
* a Wayland terminal with XWayland present doesn't pay a spawn + 4s timeout
|
|
72
|
+
* failing wl-copy. `platform` and `env` are injectable so every branch is
|
|
73
|
+
* exercisable on this Linux host (the repo's Phase 30 rule: no platform branch
|
|
74
|
+
* reachable only on the host it is written for).
|
|
75
|
+
*/
|
|
76
|
+
function clipboardCandidates(kind, { platform = realPlatform(), env = process.env } = {}) {
|
|
77
|
+
const isLinux = platform === 'linux';
|
|
78
|
+
const isMac = platform === 'darwin';
|
|
79
|
+
const isWin = platform === 'win32';
|
|
80
|
+
const e = env || {};
|
|
81
|
+
const hasWayland = !!e.WAYLAND_DISPLAY;
|
|
82
|
+
const linuxWrite = hasWayland
|
|
83
|
+
? [
|
|
84
|
+
{ name: 'wl-copy', cmd: 'wl-copy', args: [], env: 'wayland' },
|
|
85
|
+
{ name: 'xclip', cmd: 'xclip', args: ['-selection', 'clipboard'], env: 'x11' },
|
|
86
|
+
{ name: 'xsel', cmd: 'xsel', args: ['--clipboard', '--input'], env: 'x11' },
|
|
87
|
+
]
|
|
88
|
+
: [
|
|
89
|
+
{ name: 'xclip', cmd: 'xclip', args: ['-selection', 'clipboard'], env: 'x11' },
|
|
90
|
+
{ name: 'xsel', cmd: 'xsel', args: ['--clipboard', '--input'], env: 'x11' },
|
|
91
|
+
{ name: 'wl-copy', cmd: 'wl-copy', args: [], env: 'wayland' },
|
|
92
|
+
];
|
|
93
|
+
if (kind === 'write') {
|
|
94
|
+
return [
|
|
95
|
+
isMac && { name: 'pbcopy', cmd: 'pbcopy', args: [], env: 'any' },
|
|
96
|
+
isWin && { name: 'clip', cmd: 'clip', args: [], env: 'any' },
|
|
97
|
+
...(isLinux ? linuxWrite : []),
|
|
98
|
+
].filter(Boolean);
|
|
99
|
+
}
|
|
100
|
+
if (kind === 'text') {
|
|
101
|
+
return [
|
|
102
|
+
isMac && { name: 'pbpaste', cmd: 'pbpaste', args: [], env: 'any' },
|
|
103
|
+
isWin && {
|
|
104
|
+
name: 'powershell',
|
|
105
|
+
cmd: 'powershell',
|
|
106
|
+
args: ['-NoProfile', '-Command', 'Get-Clipboard'],
|
|
107
|
+
env: 'any',
|
|
108
|
+
winNewline: true,
|
|
109
|
+
},
|
|
110
|
+
...(isLinux ? [
|
|
111
|
+
{ name: 'wl-paste', cmd: 'wl-paste', args: ['--no-newline'], env: 'wayland' },
|
|
112
|
+
{ name: 'xclip', cmd: 'xclip', args: ['-selection', 'clipboard', '-o'], env: 'x11' },
|
|
113
|
+
{ name: 'xsel', cmd: 'xsel', args: ['--clipboard', '--output'], env: 'x11' },
|
|
114
|
+
] : []),
|
|
115
|
+
].filter(Boolean);
|
|
116
|
+
}
|
|
117
|
+
// image
|
|
118
|
+
return [
|
|
119
|
+
isMac && { name: 'pngpaste', cmd: 'pngpaste', args: ['-'], env: 'any' },
|
|
120
|
+
...(isLinux ? [
|
|
121
|
+
{ name: 'wl-paste', cmd: 'wl-paste', args: ['--type', 'image/png'], env: 'wayland', mime: 'image/png' },
|
|
122
|
+
{ name: 'wl-paste', cmd: 'wl-paste', args: ['--type', 'image/jpeg'], env: 'wayland', mime: 'image/jpeg' },
|
|
123
|
+
{ name: 'xclip', cmd: 'xclip', args: ['-selection', 'clipboard', '-t', 'image/png', '-o'], env: 'x11', mime: 'image/png' },
|
|
124
|
+
] : []),
|
|
125
|
+
].filter(Boolean);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// ── PATH hardening (POSIX) and PATH resolution (Windows) ────────────────────
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Is `dir` a directory a second local account could write into?
|
|
132
|
+
*
|
|
133
|
+
* A clipboard tool is handed the complete text of whatever the user copied —
|
|
134
|
+
* which in this CLI is usually model output, sometimes containing code, paths,
|
|
135
|
+
* or a secret the user was about to paste into a browser. `spawnSync('wl-copy',
|
|
136
|
+
* …)` resolves that name against `$PATH`, so whoever owns an earlier PATH entry
|
|
137
|
+
* than the real binary owns every byte the user copies: drop a `wl-copy` in a
|
|
138
|
+
* group-writable `~/.local/bin`, or rely on `/tmp` or `.` being in PATH, and the
|
|
139
|
+
* text arrives at the attacker instead of the clipboard.
|
|
140
|
+
*
|
|
141
|
+
* A directory is trusted only when it is not group- or world-writable AND is
|
|
142
|
+
* owned by root or by us. `/tmp` fails on the first test; `~/bin` that the user
|
|
143
|
+
* owns and left at 0755 passes.
|
|
144
|
+
*
|
|
145
|
+
* On Windows neither test means anything: `stat` reports mode bits that do not
|
|
146
|
+
* describe the ACL, so `mode & 0o022` is true for every path and the check
|
|
147
|
+
* rejected the whole PATH (every copy fell through to the file rung). There the
|
|
148
|
+
* question "can a stranger swap this binary?" is answered by the ACL, not by
|
|
149
|
+
* `stat`, and the file this CLI would run is `clip.exe` out of
|
|
150
|
+
* `%SystemRoot%\system32` — so the ownership test is skipped and resolution is
|
|
151
|
+
* limited to directories on PATH plus the executable suffixes, which is what
|
|
152
|
+
* keeps `clip` from being confused with a `clip.cmd` sitting in the cwd.
|
|
153
|
+
*/
|
|
154
|
+
function dirIsTrusted(dir, { platform = realPlatform() } = {}) {
|
|
155
|
+
try {
|
|
156
|
+
const st = fs.statSync(dir);
|
|
157
|
+
if (!st.isDirectory()) return false;
|
|
158
|
+
if (platform === 'win32') return true; // ACL-based; mode bits are not evidence
|
|
159
|
+
if (st.mode & 0o022) return false;
|
|
160
|
+
if (typeof process.getuid === 'function' && st.uid !== 0 && st.uid !== process.getuid()) return false;
|
|
161
|
+
return true;
|
|
162
|
+
} catch {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const WIN_EXTS = ['', '.exe', '.cmd', '.bat', '.com'];
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Join a PATH entry and a file name the way the *entry* is spelled.
|
|
171
|
+
*
|
|
172
|
+
* `path.win32.join('C:\\Windows', 'clip.exe')` is what Windows needs, but a
|
|
173
|
+
* win32-style join applied to a POSIX-style entry produces
|
|
174
|
+
* `\\tmp\\x\\clip.exe`, which no filesystem resolves — and the whole point of
|
|
175
|
+
* taking `platform` as an option is that the win32 branch is exercisable on
|
|
176
|
+
* this Linux host with a real temp directory. Windows accepts both separators,
|
|
177
|
+
* so choosing by the entry's own spelling is correct on both sides.
|
|
178
|
+
*/
|
|
179
|
+
function joinFor(dir, name, isWin) {
|
|
180
|
+
if (!isWin) return path.join(dir, name);
|
|
181
|
+
const winStyle = /\\/.test(dir) || /^[A-Za-z]:/.test(dir);
|
|
182
|
+
return winStyle ? path.win32.join(dir, name) : path.join(dir, name);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Resolve `cmd` to an absolute path we are willing to execute, or null.
|
|
187
|
+
*
|
|
188
|
+
* Replaces the old `sh -c 'command -v <cmd>'` availability probe: that check
|
|
189
|
+
* answered "is this name on PATH?" when the question that matters is "is the
|
|
190
|
+
* binary PATH would pick one I trust?". Answering the second subsumes the
|
|
191
|
+
* first, and does it without a shell in the loop.
|
|
192
|
+
*
|
|
193
|
+
* Every candidate must survive: a PATH entry that is absolute, trusted, and not
|
|
194
|
+
* the empty string or `.` (both of which mean the current directory); a real
|
|
195
|
+
* file that is executable (POSIX), not group/world-writable, and owned by root
|
|
196
|
+
* or us; and a realpath whose own directory is trusted too, so a link in a good
|
|
197
|
+
* directory cannot redirect execution into a bad one. On Windows the mode/owner
|
|
198
|
+
* tests are skipped (see `dirIsTrusted`) and each executable suffix is tried,
|
|
199
|
+
* because a bare `clip` is `clip.exe` on disk.
|
|
200
|
+
*/
|
|
201
|
+
function resolveTrustedTool(cmd, { platform = realPlatform(), env = process.env } = {}) {
|
|
202
|
+
const e = env || {};
|
|
203
|
+
const trustPath = e.AEGISCODE_TRUST_PATH !== '0';
|
|
204
|
+
const isWin = platform === 'win32';
|
|
205
|
+
const p = isWin ? path.win32 : path;
|
|
206
|
+
const entries = String((e.PATH != null ? e.PATH : process.env.PATH) || '').split(isWin ? ';' : path.delimiter);
|
|
207
|
+
// AEGISCODE_TRUST_PATH=0 restores the old "whatever PATH resolves" behaviour
|
|
208
|
+
// for a user whose tool legitimately lives in a group-writable prefix (an
|
|
209
|
+
// unusual but real layout: a Nix/Guix profile, a CI image). Secure by
|
|
210
|
+
// default; the hatch is documented in the module header rather than being the
|
|
211
|
+
// default. It stays on for Windows, where the checks are ACL-based and
|
|
212
|
+
// `dirIsTrusted` has nothing to verify anyway.
|
|
213
|
+
const harden = trustPath || isWin;
|
|
214
|
+
for (const raw of entries) {
|
|
215
|
+
const dir = String(raw).trim().replace(/^"|"$/g, '');
|
|
216
|
+
if (!dir) continue;
|
|
217
|
+
if (!isWin && !p.isAbsolute(dir)) continue; // '' and '.' mean "the cwd"
|
|
218
|
+
if (harden && !dirIsTrusted(dir, { platform })) continue;
|
|
219
|
+
|
|
220
|
+
for (const ext of isWin ? WIN_EXTS : ['']) {
|
|
221
|
+
const full = joinFor(dir, cmd + ext, isWin);
|
|
222
|
+
let real;
|
|
223
|
+
try {
|
|
224
|
+
real = fs.realpathSync(full); // throws on ENOENT and on a symlink loop
|
|
225
|
+
} catch {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
let st;
|
|
229
|
+
try {
|
|
230
|
+
st = fs.statSync(real);
|
|
231
|
+
} catch {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
if (!st.isFile()) continue;
|
|
235
|
+
if (!isWin) {
|
|
236
|
+
if (!(st.mode & 0o111)) continue; // not executable — always required
|
|
237
|
+
if (harden) {
|
|
238
|
+
if (st.mode & 0o022) continue; // group/world-writable binary
|
|
239
|
+
if (typeof process.getuid === 'function' && st.uid !== 0 && st.uid !== process.getuid()) continue;
|
|
240
|
+
if (!dirIsTrusted(path.dirname(real), { platform })) continue; // the realpath may leave `dir`
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return real;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// ── OSC 52 — the terminal's own clipboard ──────────────────────────────────
|
|
250
|
+
// "ESC ] 52 ; c ; <base64> BEL" sets the system clipboard. Inside tmux or
|
|
251
|
+
// screen the sequence has to be wrapped in a DCS passthrough or the
|
|
252
|
+
// multiplexer eats it (tmux forwards it with `set-clipboard` at its default).
|
|
253
|
+
// The inner ESC bytes are doubled for tmux, per its docs.
|
|
254
|
+
const base64 = (s) => Buffer.from(String(s), 'utf8').toString('base64');
|
|
255
|
+
/** Terminals that refuse OSC 52 for size need chunks, but the payloads a
|
|
256
|
+
* marking produces are small; a fixed cap keeps a giant copy from wedging the
|
|
257
|
+
* terminal, and the file rung already covers that case. */
|
|
258
|
+
const OSC52_MAX = 100_000;
|
|
259
|
+
|
|
260
|
+
function osc52Sequence(text, env = process.env) {
|
|
261
|
+
const body = `${ESC}]52;c;${base64(text)}\u0007`;
|
|
262
|
+
const e = env || {};
|
|
263
|
+
if (e.TMUX) return `${ESC}Ptmux;${ESC}${body.split(ESC).join(ESC + ESC)}${ESC}\\`;
|
|
264
|
+
if (e.STY) return `${ESC}P${body}${ESC}\\`;
|
|
265
|
+
return body;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Send `text` to the terminal's clipboard. Only meaningful on a TTY — with
|
|
270
|
+
* stdout redirected there is no terminal to hand it to, and the bytes would
|
|
271
|
+
* land in the redirected file (which is why `isTTY` is part of the contract and
|
|
272
|
+
* why the tests inject a stub stream). Returns true when the sequence was
|
|
273
|
+
* written.
|
|
274
|
+
*/
|
|
275
|
+
function writeOsc52(text, { stdout = process.stdout, env = process.env } = {}) {
|
|
276
|
+
const e = env || {};
|
|
277
|
+
if (e.AEGISCODE_NO_OSC52 === '1') return false;
|
|
278
|
+
if (!stdout || !stdout.isTTY || typeof stdout.write !== 'function') return false;
|
|
279
|
+
const payload = String(text);
|
|
280
|
+
if (!payload || payload.length > OSC52_MAX) return false;
|
|
281
|
+
try {
|
|
282
|
+
stdout.write(osc52Sequence(payload, e));
|
|
283
|
+
return true;
|
|
284
|
+
} catch {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// ── copy ───────────────────────────────────────────────────────────────────
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Copy text to the system clipboard.
|
|
293
|
+
* Returns { ok, via, path? } — via is 'tool:<name>', 'osc52:<where>' or 'file'.
|
|
294
|
+
* `via: 'file'` means the text did NOT reach any clipboard: the caller is
|
|
295
|
+
* expected to say so and hand over the path, not to claim a copy.
|
|
296
|
+
*/
|
|
297
|
+
function copyToClipboard(text, opts = {}) {
|
|
298
|
+
const {
|
|
299
|
+
stdout = process.stdout,
|
|
300
|
+
env = process.env,
|
|
301
|
+
platform = realPlatform(),
|
|
302
|
+
spawn = spawnSync,
|
|
303
|
+
} = opts;
|
|
304
|
+
const e = env || {};
|
|
305
|
+
const forced = e.AEGISCODE_NO_CLIPBOARD === '1';
|
|
306
|
+
if (!forced) {
|
|
307
|
+
for (const t of clipboardCandidates('write', { platform, env: e })) {
|
|
308
|
+
const exe = resolveTrustedTool(t.cmd, { platform, env: e });
|
|
309
|
+
if (!exe) continue; // not installed, or installed somewhere untrusted
|
|
310
|
+
const r = spawn(exe, t.args, { input: String(text), encoding: 'utf8', timeout: 4000, windowsHide: true });
|
|
311
|
+
if (!r.error && r.status === 0) return { ok: true, via: `tool:${t.name}` };
|
|
312
|
+
}
|
|
313
|
+
if (writeOsc52(text, { stdout, env: e })) {
|
|
314
|
+
const where = e.TMUX ? 'tmux' : e.STY ? 'screen' : 'terminal';
|
|
315
|
+
return { ok: true, via: `osc52:${where}` };
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
// Fallback: write to the data dir and report the path. 0600 + O_NOFOLLOW via
|
|
319
|
+
// securefs — this file holds whatever the user last marked, and neither the
|
|
320
|
+
// mode nor a planted symlink may let it double as a way to read or overwrite
|
|
321
|
+
// something else.
|
|
322
|
+
try {
|
|
323
|
+
const p = path.join(aegisDir(), 'clipboard.txt');
|
|
324
|
+
writePrivateFile(p, String(text), { atomic: false });
|
|
325
|
+
return { ok: true, via: 'file', path: p };
|
|
326
|
+
} catch {
|
|
327
|
+
return { ok: false, via: 'none' };
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** Human sentence for a copy result — one place, so every caller matches. */
|
|
332
|
+
function describeCopy(res, subject = 'Copied') {
|
|
333
|
+
if (!res || !res.ok) return 'Clipboard unavailable; nothing copied.';
|
|
334
|
+
if (res.via === 'file') {
|
|
335
|
+
// Not a copy: nothing reached any clipboard, so the sentence must not say
|
|
336
|
+
// it did — it names the file the text actually went to instead.
|
|
337
|
+
return `${subject} — no clipboard tool on this host; the text is in ${res.path}`;
|
|
338
|
+
}
|
|
339
|
+
const [kind, detail] = String(res.via).split(':');
|
|
340
|
+
if (kind === 'osc52') {
|
|
341
|
+
return detail === 'terminal'
|
|
342
|
+
? `${subject} to the clipboard via the terminal.`
|
|
343
|
+
: `${subject} to the clipboard (through ${detail}).`;
|
|
344
|
+
}
|
|
345
|
+
return `${subject} to the clipboard (${detail}).`;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// ── read (paste) ───────────────────────────────────────────────────────────
|
|
349
|
+
|
|
350
|
+
const PNG_MAGIC = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
|
351
|
+
const JPEG_MAGIC = Buffer.from([0xff, 0xd8, 0xff]);
|
|
352
|
+
|
|
353
|
+
/** Sniff a Buffer's image type from its magic bytes, or null when not an image. */
|
|
354
|
+
function sniffImage(buf) {
|
|
355
|
+
if (!buf || buf.length < 8) return null;
|
|
356
|
+
if (buf.subarray(0, 8).equals(PNG_MAGIC)) return 'image/png';
|
|
357
|
+
if (buf.subarray(0, 3).equals(JPEG_MAGIC)) return 'image/jpeg';
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Read the clipboard's text, if any tool can. Returns { ok, text, via, error? }
|
|
363
|
+
* — `ok:false` means no tool answered, never "the clipboard was empty" (an
|
|
364
|
+
* empty clipboard returns ok:true and text:''). The caller's message differs for
|
|
365
|
+
* the two, which is the whole reason the distinction is in the return value.
|
|
366
|
+
*/
|
|
367
|
+
function readClipboardText(opts = {}) {
|
|
368
|
+
const { env = process.env, platform = realPlatform(), spawn = spawnSync } = opts;
|
|
369
|
+
const e = env || {};
|
|
370
|
+
if (e.AEGISCODE_NO_CLIPBOARD === '1') return { ok: false, via: 'none', error: 'clipboard disabled' };
|
|
371
|
+
for (const t of clipboardCandidates('text', { platform, env: e })) {
|
|
372
|
+
const exe = resolveTrustedTool(t.cmd, { platform, env: e });
|
|
373
|
+
if (!exe) continue;
|
|
374
|
+
const r = spawn(exe, t.args, { encoding: 'utf8', timeout: 4000, maxBuffer: 16 * 1024 * 1024, windowsHide: true });
|
|
375
|
+
if (r.error || r.status !== 0 || r.stdout == null) continue;
|
|
376
|
+
// PowerShell's Get-Clipboard appends its own CRLF per line.
|
|
377
|
+
const text = t.winNewline
|
|
378
|
+
? String(r.stdout).replace(/\r\n/g, '\n').replace(/\n$/, '')
|
|
379
|
+
: String(r.stdout);
|
|
380
|
+
return { ok: true, text, via: `tool:${t.name}` };
|
|
381
|
+
}
|
|
382
|
+
return { ok: false, via: 'none', error: 'no clipboard reader available' };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Read an image off the clipboard. Returns { ok, data (Buffer), mime, via,
|
|
387
|
+
* error? }. The bytes are magic-checked: a tool that answers with text (an
|
|
388
|
+
* empty selection, a type the clipboard does not carry) is not an image, and
|
|
389
|
+
* the caller must not write it out as one.
|
|
390
|
+
*/
|
|
391
|
+
function readClipboardImage(opts = {}) {
|
|
392
|
+
const { env = process.env, platform = realPlatform(), spawn = spawnSync } = opts;
|
|
393
|
+
const e = env || {};
|
|
394
|
+
if (e.AEGISCODE_NO_CLIPBOARD === '1') return { ok: false, via: 'none', error: 'clipboard disabled' };
|
|
395
|
+
for (const t of clipboardCandidates('image', { platform, env: e })) {
|
|
396
|
+
const exe = resolveTrustedTool(t.cmd, { platform, env: e });
|
|
397
|
+
if (!exe) continue;
|
|
398
|
+
const r = spawn(exe, t.args, { timeout: 5000, maxBuffer: 64 * 1024 * 1024, windowsHide: true });
|
|
399
|
+
if (r.error || r.status !== 0 || !r.stdout) continue;
|
|
400
|
+
const data = Buffer.isBuffer(r.stdout) ? r.stdout : Buffer.from(String(r.stdout), 'utf8');
|
|
401
|
+
const mime = sniffImage(data);
|
|
402
|
+
if (mime) return { ok: true, data, mime, via: `tool:${t.name}` };
|
|
403
|
+
}
|
|
404
|
+
return { ok: false, via: 'none', error: 'no image on the clipboard' };
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** Which clipboard facilities this host actually has — for honest UI hints. */
|
|
408
|
+
function clipboardCapabilities({ platform = realPlatform(), env = process.env } = {}) {
|
|
409
|
+
const any = (kind) =>
|
|
410
|
+
clipboardCandidates(kind, { platform, env }).some((t) => !!resolveTrustedTool(t.cmd, { platform, env }));
|
|
411
|
+
return { canWrite: any('write'), canReadText: any('text'), canReadImage: any('image') };
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
module.exports = {
|
|
415
|
+
copyToClipboard,
|
|
416
|
+
describeCopy,
|
|
417
|
+
readClipboardText,
|
|
418
|
+
readClipboardImage,
|
|
419
|
+
sniffImage,
|
|
420
|
+
clipboardCandidates,
|
|
421
|
+
clipboardCapabilities,
|
|
422
|
+
osc52Sequence,
|
|
423
|
+
writeOsc52,
|
|
424
|
+
resolveTrustedTool,
|
|
425
|
+
dirIsTrusted,
|
|
426
|
+
};
|