@moxuli/moremote 0.0.0-stage → 0.1.1
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/LICENSE +202 -0
- package/NOTICE +31 -0
- package/README.md +73 -2
- package/bin/moremote-cli.js +34 -0
- package/bin/moremote-service.js +7 -0
- package/dist/bindings.d.ts +6 -0
- package/dist/bindings.js +4 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +325 -0
- package/dist/cloud-client.d.ts +43 -0
- package/dist/cloud-client.js +128 -0
- package/dist/config.d.ts +36 -0
- package/dist/config.js +68 -0
- package/dist/control-transport.d.ts +4 -0
- package/dist/control-transport.js +68 -0
- package/dist/control.d.ts +3 -0
- package/dist/control.js +297 -0
- package/dist/controls.d.ts +15 -0
- package/dist/controls.js +36 -0
- package/dist/crypto.d.ts +23 -0
- package/dist/crypto.js +68 -0
- package/dist/evidence.d.ts +19 -0
- package/dist/evidence.js +4 -0
- package/dist/journal.d.ts +104 -0
- package/dist/journal.js +132 -0
- package/dist/limits.d.ts +21 -0
- package/dist/limits.js +8 -0
- package/dist/official-client.d.ts +120 -0
- package/dist/official-client.js +162 -0
- package/dist/operations.d.ts +71 -0
- package/dist/operations.js +210 -0
- package/dist/pairing.d.ts +19 -0
- package/dist/pairing.js +68 -0
- package/dist/permission.d.ts +9 -0
- package/dist/permission.js +66 -0
- package/dist/projection.d.ts +24 -0
- package/dist/projection.js +35 -0
- package/dist/resources.d.ts +76 -0
- package/dist/resources.js +299 -0
- package/dist/runtime.d.ts +8 -0
- package/dist/runtime.js +375 -0
- package/dist/service.d.ts +21 -0
- package/dist/service.js +310 -0
- package/dist/supervisor.d.ts +15 -0
- package/dist/supervisor.js +175 -0
- package/dist/update-state.d.ts +30 -0
- package/dist/update-state.js +158 -0
- package/dist/updater.d.ts +9 -0
- package/dist/updater.js +152 -0
- package/docs/README.md +23 -0
- package/docs/STATE.md +155 -0
- package/docs/configuration.md +37 -0
- package/docs/conventions.md +9 -0
- package/docs/decisions/0001-managed-updates.md +19 -0
- package/docs/plans/2026-11-10-managed-updates.md +26 -0
- package/docs/plans/2026-11-10-notification-delivery.md +118 -0
- package/docs/releasing.md +34 -0
- package/docs/specs/01-architecture.md +102 -0
- package/docs/specs/02-encrypted-delivery.md +194 -0
- package/docs/specs/03-api-contract.md +397 -0
- package/docs/specs/04-control-and-receipt-lifecycle.md +175 -0
- package/docs/specs/05-endpoint-and-lifecycle.md +57 -0
- package/docs/specs/06-managed-updates.md +55 -0
- package/licenses/Paseo-0.15.3.txt +211 -0
- package/package.json +37 -4
- package/vectors/bearer-v1.json +6 -0
- package/vectors/envelope-v1.json +24 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { constants, lstatSync, openSync, closeSync, fstatSync, readSync, writeFileSync, fsyncSync, unlinkSync, renameSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { dirname, basename, join } from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
5
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
6
|
+
export const JOURNAL_APPLICATION_ID = 0x4d524331;
|
|
7
|
+
export const RUNTIME_OWNER = 'MoRemote CLI runtime';
|
|
8
|
+
export function hasCode(error, code) { return error instanceof Error && 'code' in error && error.code === code; }
|
|
9
|
+
export function sameResource(left, right) { return left.dev === right.dev && left.ino === right.ino; }
|
|
10
|
+
export function inspectResource(path, kind, optional = false, privateMode = true) {
|
|
11
|
+
let stat;
|
|
12
|
+
try {
|
|
13
|
+
stat = lstatSync(path);
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
if (optional && hasCode(error, 'ENOENT'))
|
|
17
|
+
return null;
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
if (stat.isSymbolicLink() || !(kind === 'file' ? stat.isFile() : kind === 'directory' ? stat.isDirectory() : stat.isSocket()))
|
|
21
|
+
throw Error('FOREIGN_RESOURCE_KIND');
|
|
22
|
+
if (kind !== 'directory' && stat.nlink !== 1)
|
|
23
|
+
throw Error('FOREIGN_RESOURCE_LINKS');
|
|
24
|
+
if (typeof process.getuid === 'function' && stat.uid !== process.getuid())
|
|
25
|
+
throw Error('FOREIGN_RESOURCE_USER');
|
|
26
|
+
if (privateMode && process.platform !== 'win32' && ((stat.mode & 0o7077) !== 0 || (stat.mode & (kind === 'directory' ? 0o700 : 0o600)) !== (kind === 'directory' ? 0o700 : 0o600)))
|
|
27
|
+
throw Error('FOREIGN_RESOURCE_MODE');
|
|
28
|
+
return stat;
|
|
29
|
+
}
|
|
30
|
+
export function readPrivateText(path, limit, encoding = 'utf8') {
|
|
31
|
+
const before = inspectResource(path, 'file');
|
|
32
|
+
const fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
33
|
+
try {
|
|
34
|
+
const opened = fstatSync(fd);
|
|
35
|
+
if (!before || !sameResource(before, opened) || !opened.isFile() || opened.nlink !== 1 || opened.size > limit)
|
|
36
|
+
throw Error('FOREIGN_RESOURCE_LIMIT');
|
|
37
|
+
const bytes = Buffer.alloc(limit + 1);
|
|
38
|
+
let size = 0;
|
|
39
|
+
while (size < bytes.length) {
|
|
40
|
+
const count = readSync(fd, bytes, size, bytes.length - size, null);
|
|
41
|
+
if (count === 0)
|
|
42
|
+
break;
|
|
43
|
+
size += count;
|
|
44
|
+
}
|
|
45
|
+
const after = inspectResource(path, 'file');
|
|
46
|
+
if (size > limit || !after || !sameResource(opened, after) || opened.size !== after.size || opened.mtimeMs !== after.mtimeMs)
|
|
47
|
+
throw Error('FOREIGN_RESOURCE_CHANGED');
|
|
48
|
+
return { text: bytes.toString(encoding, 0, size), identity: { dev: opened.dev, ino: opened.ino } };
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
closeSync(fd);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export function assertStateRoot(dir) {
|
|
55
|
+
inspectResource(dir, 'directory');
|
|
56
|
+
if (readPrivateText(join(dir, '.moremote-cli-owner'), 128).text !== 'MoRemote CLI state v1\n')
|
|
57
|
+
throw Error('FOREIGN_DATA_DIRECTORY');
|
|
58
|
+
}
|
|
59
|
+
function object(value) { return typeof value === 'object' && value !== null && !Array.isArray(value); }
|
|
60
|
+
function integer(value, min, max = Number.MAX_SAFE_INTEGER) { return typeof value === 'number' && Number.isSafeInteger(value) && value >= min && value <= max; }
|
|
61
|
+
function resourceIdentity(value) { return object(value) && Object.keys(value).length === 2 && integer(value.dev, 0) && integer(value.ino, 0); }
|
|
62
|
+
function runtimeOwner(value) {
|
|
63
|
+
return object(value) && value.owner === RUNTIME_OWNER && value.version === 1 && typeof value.instanceId === 'string' && /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(value.instanceId) && integer(value.pid, 1, 2147483647) && integer(value.startedAt, 1) && typeof value.token === 'string' && /^[A-Za-z0-9_-]{43}$/.test(value.token) && Buffer.from(value.token, 'base64url').toString('base64url') === value.token;
|
|
64
|
+
}
|
|
65
|
+
export function readRuntimeOwner(path) {
|
|
66
|
+
const file = readPrivateText(path, 2048);
|
|
67
|
+
const value = JSON.parse(file.text);
|
|
68
|
+
if (!runtimeOwner(value))
|
|
69
|
+
throw Error('INVALID_RUNTIME_LOCK');
|
|
70
|
+
if (Object.keys(value).length === 6)
|
|
71
|
+
return { value, identity: file.identity };
|
|
72
|
+
if (Object.keys(value).length === 7 && 'staging' in value && resourceIdentity(value.staging))
|
|
73
|
+
return { value: { ...value, staging: value.staging }, identity: file.identity };
|
|
74
|
+
throw Error('INVALID_RUNTIME_LOCK');
|
|
75
|
+
}
|
|
76
|
+
export function readRuntimeRecord(path) {
|
|
77
|
+
const file = readPrivateText(path, 2048);
|
|
78
|
+
const value = JSON.parse(file.text);
|
|
79
|
+
if (!runtimeOwner(value) || !('socket' in value) || Object.keys(value).length !== 7 || !(value.socket === null || resourceIdentity(value.socket)))
|
|
80
|
+
throw Error('INVALID_RUNTIME_RECORD');
|
|
81
|
+
return { value: { ...value, socket: value.socket }, identity: file.identity };
|
|
82
|
+
}
|
|
83
|
+
export function runtimeMatches(lock, record) {
|
|
84
|
+
return lock.instanceId === record.instanceId && lock.pid === record.pid && lock.startedAt === record.startedAt && lock.token === record.token;
|
|
85
|
+
}
|
|
86
|
+
export function inspectRuntime(dir) {
|
|
87
|
+
const lockPath = join(dir, 'runtime.lock'), recordPath = join(dir, 'runtime.json');
|
|
88
|
+
const lock = inspectResource(lockPath, 'file', true) ? readRuntimeOwner(lockPath) : null;
|
|
89
|
+
const record = inspectResource(recordPath, 'file', true) ? readRuntimeRecord(recordPath) : null;
|
|
90
|
+
const socket = process.platform === 'win32' ? null : inspectResource(join(dir, 'runtime.sock'), 'socket', true);
|
|
91
|
+
const stageNames = readdirSync(dir).filter(name => /^s\.[0-9a-f]{8}$/.test(name));
|
|
92
|
+
if (stageNames.some(name => !lock || name !== 's.' + lock.value.instanceId.slice(0, 8)))
|
|
93
|
+
throw Error('RUNTIME_UNPROVEN_STAGE');
|
|
94
|
+
let staged = null;
|
|
95
|
+
if (lock && process.platform !== 'win32') {
|
|
96
|
+
const path = join(dir, 's.' + lock.value.instanceId.slice(0, 8));
|
|
97
|
+
const stat = inspectResource(path, 'socket', true, false);
|
|
98
|
+
if (stat) {
|
|
99
|
+
// A pre-chmod crash is recoverable only with an inode durably bound to
|
|
100
|
+
// the marked lock. A filename or private mode alone is not ownership.
|
|
101
|
+
if (!lock.value.staging || !sameResource(stat, lock.value.staging) || (stat.mode & 0o7000) !== 0)
|
|
102
|
+
throw Error('RUNTIME_UNPROVEN_STAGE');
|
|
103
|
+
staged = { path, identity: { dev: stat.dev, ino: stat.ino } };
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (record && (!lock || !runtimeMatches(lock.value, record.value)))
|
|
107
|
+
throw Error('RUNTIME_RESOURCE_COLLISION');
|
|
108
|
+
if (socket && !(record?.value.socket && sameResource(socket, record.value.socket)) && !(lock?.value.staging && sameResource(socket, lock.value.staging)))
|
|
109
|
+
throw Error('RUNTIME_RESOURCE_COLLISION');
|
|
110
|
+
if (record && process.platform !== 'win32' && record.value.socket === null)
|
|
111
|
+
throw Error('RUNTIME_RESOURCE_COLLISION');
|
|
112
|
+
if (record && process.platform === 'win32' && record.value.socket !== null)
|
|
113
|
+
throw Error('RUNTIME_RESOURCE_COLLISION');
|
|
114
|
+
return { lock, record, socket, staged };
|
|
115
|
+
}
|
|
116
|
+
export function validateJournalPath(path) {
|
|
117
|
+
inspectResource(dirname(path), 'directory');
|
|
118
|
+
const existing = inspectResource(path, 'file', true);
|
|
119
|
+
const sidecars = ['-wal', '-shm', '-journal'].map(suffix => inspectResource(path + suffix, 'file', true));
|
|
120
|
+
if (!existing) {
|
|
121
|
+
if (sidecars.some(Boolean))
|
|
122
|
+
throw Error('FOREIGN_JOURNAL_SIDECAR');
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
const fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
126
|
+
try {
|
|
127
|
+
if (!sameResource(existing, fstatSync(fd)))
|
|
128
|
+
throw Error('FOREIGN_RESOURCE_CHANGED');
|
|
129
|
+
// immutable avoids SQLite creating or updating WAL/SHM while probing ownership.
|
|
130
|
+
const probe = new DatabaseSync(pathToFileURL(path).href + '?mode=ro&immutable=1', { readOnly: true, enableForeignKeyConstraints: false });
|
|
131
|
+
try {
|
|
132
|
+
if (probe.prepare('PRAGMA application_id').get()?.application_id !== JOURNAL_APPLICATION_ID)
|
|
133
|
+
throw Error('FOREIGN_JOURNAL');
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
probe.close();
|
|
137
|
+
}
|
|
138
|
+
const after = inspectResource(path, 'file');
|
|
139
|
+
if (!after || !sameResource(existing, after))
|
|
140
|
+
throw Error('FOREIGN_RESOURCE_CHANGED');
|
|
141
|
+
return { dev: existing.dev, ino: existing.ino };
|
|
142
|
+
}
|
|
143
|
+
finally {
|
|
144
|
+
closeSync(fd);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
export function removeOwnedResource(path, identity, kind = 'file', privateMode = true) {
|
|
148
|
+
let current;
|
|
149
|
+
try {
|
|
150
|
+
current = inspectResource(path, kind, true, privateMode);
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
if (!current || !sameResource(current, identity))
|
|
156
|
+
return false;
|
|
157
|
+
unlinkSync(path);
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
export function createPrivateFile(path, text) {
|
|
161
|
+
const fd = openSync(path, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
|
162
|
+
const identity = fstatSync(fd);
|
|
163
|
+
try {
|
|
164
|
+
writeFileSync(fd, text);
|
|
165
|
+
fsyncSync(fd);
|
|
166
|
+
return { dev: identity.dev, ino: identity.ino };
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
removeOwnedResource(path, identity);
|
|
170
|
+
throw error;
|
|
171
|
+
}
|
|
172
|
+
finally {
|
|
173
|
+
closeSync(fd);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
export function openOwnedJournal(path) {
|
|
177
|
+
const existing = validateJournalPath(path);
|
|
178
|
+
const identity = existing ?? createPrivateFile(path, '');
|
|
179
|
+
const current = inspectResource(path, 'file');
|
|
180
|
+
if (!current || !sameResource(current, identity))
|
|
181
|
+
throw Error('FOREIGN_RESOURCE_CHANGED');
|
|
182
|
+
let db;
|
|
183
|
+
try {
|
|
184
|
+
db = new DatabaseSync(path);
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
if (!existing)
|
|
188
|
+
removeOwnedResource(path, identity);
|
|
189
|
+
throw error;
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
192
|
+
if (!existing)
|
|
193
|
+
db.exec(`PRAGMA application_id=${JOURNAL_APPLICATION_ID}`);
|
|
194
|
+
if (db.prepare('PRAGMA application_id').get()?.application_id !== JOURNAL_APPLICATION_ID)
|
|
195
|
+
throw Error('FOREIGN_JOURNAL');
|
|
196
|
+
const after = inspectResource(path, 'file');
|
|
197
|
+
if (!after || !sameResource(after, identity))
|
|
198
|
+
throw Error('FOREIGN_RESOURCE_CHANGED');
|
|
199
|
+
return db;
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
db.close();
|
|
203
|
+
if (!existing)
|
|
204
|
+
removeOwnedResource(path, identity);
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
export function writePrivateAtomic(path, value) {
|
|
209
|
+
const dir = dirname(path), name = basename(path);
|
|
210
|
+
assertStateRoot(dir);
|
|
211
|
+
if (name !== 'config.json' && name !== 'runtime.json' && name !== 'runtime.lock')
|
|
212
|
+
throw Error('FOREIGN_RESOURCE_NAME');
|
|
213
|
+
const before = inspectResource(path, 'file', true);
|
|
214
|
+
if (before) {
|
|
215
|
+
if (name === 'runtime.lock')
|
|
216
|
+
readRuntimeOwner(path);
|
|
217
|
+
else
|
|
218
|
+
assertStateResource(dir, name);
|
|
219
|
+
}
|
|
220
|
+
const temp = path + '.' + randomUUID() + '.tmp';
|
|
221
|
+
const identity = createPrivateFile(temp, JSON.stringify(value) + '\n');
|
|
222
|
+
try {
|
|
223
|
+
const current = inspectResource(path, 'file', true);
|
|
224
|
+
if (before ? (!current || !sameResource(before, current)) : current !== null)
|
|
225
|
+
throw Error('FOREIGN_RESOURCE_CHANGED');
|
|
226
|
+
renameSync(temp, path);
|
|
227
|
+
if (process.platform !== 'win32') {
|
|
228
|
+
const fd = openSync(dir, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
|
|
229
|
+
try {
|
|
230
|
+
fsyncSync(fd);
|
|
231
|
+
}
|
|
232
|
+
finally {
|
|
233
|
+
closeSync(fd);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return identity;
|
|
237
|
+
}
|
|
238
|
+
finally {
|
|
239
|
+
removeOwnedResource(temp, identity);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
export function assertTaskResource(dir, platform = process.platform) {
|
|
243
|
+
if (platform !== 'win32')
|
|
244
|
+
throw Error('FOREIGN_TASK_RESOURCE');
|
|
245
|
+
const text = readPrivateText(join(dir, 'moremote-cli-task.xml'), 131072, 'utf16le').text;
|
|
246
|
+
const marker = 'Managed by moremote-cli; per-user ownership v1; owner=' + createHash('sha256').update(dir).digest('hex');
|
|
247
|
+
if (!text.startsWith('<?xml version="1.0" encoding="UTF-16"?>') || !text.includes('<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">') || !text.includes('<Description>' + marker + '</Description>') || !text.trimEnd().endsWith('</Task>'))
|
|
248
|
+
throw Error('FOREIGN_TASK_RESOURCE');
|
|
249
|
+
}
|
|
250
|
+
export function assertStateResource(dir, name) {
|
|
251
|
+
const path = join(dir, name);
|
|
252
|
+
if (name === '.moremote-cli-owner') {
|
|
253
|
+
assertStateRoot(dir);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (name === 'journal.sqlite') {
|
|
257
|
+
validateJournalPath(path);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
if (/^journal\.sqlite-(wal|shm|journal)$/.test(name)) {
|
|
261
|
+
validateJournalPath(join(dir, 'journal.sqlite'));
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
if (name === 'runtime.lock' || name === 'runtime.json' || name === 'runtime.sock') {
|
|
265
|
+
inspectRuntime(dir);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (name === 'managed') {
|
|
269
|
+
inspectResource(path, 'directory');
|
|
270
|
+
if (readPrivateText(join(path, '.moremote-cli-managed-owner'), 128).text !== 'MoRemote CLI managed package v1\n')
|
|
271
|
+
throw Error('FOREIGN_MANAGED_DIRECTORY');
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (/^s\.[0-9a-f]{8}$/.test(name)) {
|
|
275
|
+
const runtime = inspectRuntime(dir);
|
|
276
|
+
if (!runtime.staged || runtime.staged.path !== path)
|
|
277
|
+
throw Error('RUNTIME_UNPROVEN_STAGE');
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (name === 'config.json' || /^config\.json\.[0-9a-f-]{36}\.tmp$/.test(name)) {
|
|
281
|
+
const value = JSON.parse(readPrivateText(path, 131072).text);
|
|
282
|
+
if (!object(value) || value.version !== 1 || typeof value.clientId !== 'string' || !value.clientId.startsWith('moremote-cli-') || value.clientId.length > 256)
|
|
283
|
+
throw Error('FOREIGN_CONFIG');
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (/^runtime\.json\.[0-9a-f-]{36}\.tmp$/.test(name)) {
|
|
287
|
+
readRuntimeRecord(path);
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if (/^runtime\.lock\.[0-9a-f-]{36}\.tmp$/.test(name)) {
|
|
291
|
+
readRuntimeOwner(path);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
if (name === 'moremote-cli-task.xml') {
|
|
295
|
+
assertTaskResource(dir);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
throw Error('FOREIGN_DATA_DIRECTORY');
|
|
299
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Pair } from './config.js';
|
|
2
|
+
import { type Attention } from './official-client.js';
|
|
3
|
+
import { OperationJournal, type Occurrence } from './journal.js';
|
|
4
|
+
export declare function queueAttention(journal: OperationJournal, pair: Pair, event: Attention & {
|
|
5
|
+
sourceFingerprint?: string;
|
|
6
|
+
occurrence?: Occurrence;
|
|
7
|
+
}, now?: number): Promise<void>;
|
|
8
|
+
export declare function runRuntime(dir: string, version?: string): Promise<void>;
|