@veda-hq/niyam 0.6.24 → 0.6.25
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/README.md +76 -28
- package/dist/agent.js +2 -1
- package/dist/board-app.js +254 -0
- package/dist/cloud-key.js +20 -55
- package/dist/cloud.js +32 -29
- package/dist/doctor.js +15 -12
- package/dist/error-detail.js +12 -0
- package/dist/layout.js +43 -0
- package/dist/onboard.js +63 -32
- package/dist/run.js +6 -1
- package/dist/secrets.js +73 -25
- package/dist/ssh-sign.js +84 -0
- package/dist/sshsig.js +218 -0
- package/dist/sys.js +25 -0
- package/package.json +1 -1
package/dist/sshsig.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { createHash, createPrivateKey, createPublicKey, generateKeyPairSync, randomBytes, sign, verify } from 'node:crypto';
|
|
2
|
+
import { errorDetail } from './error-detail.js';
|
|
3
|
+
/**
|
|
4
|
+
* SSH signing in Node, for the hosted Claude cloud image, which has no
|
|
5
|
+
* ssh-keygen (#425): the unencrypted ed25519 key an OpenSSH private key file
|
|
6
|
+
* holds, and SSHSIG signatures over it, as `ssh-keygen -Y sign` makes them
|
|
7
|
+
* and `ssh-keygen -Y verify` and GitHub check them (OpenSSH's PROTOCOL.sshsig
|
|
8
|
+
* and PROTOCOL.key).
|
|
9
|
+
*/
|
|
10
|
+
const PEM_BEGIN = '-----BEGIN OPENSSH PRIVATE KEY-----';
|
|
11
|
+
const PEM_END = '-----END OPENSSH PRIVATE KEY-----';
|
|
12
|
+
const SIG_BEGIN = '-----BEGIN SSH SIGNATURE-----';
|
|
13
|
+
const SIG_END = '-----END SSH SIGNATURE-----';
|
|
14
|
+
const KEY_MAGIC = 'openssh-key-v1\0';
|
|
15
|
+
const SIG_MAGIC = 'SSHSIG';
|
|
16
|
+
const ED25519 = 'ssh-ed25519';
|
|
17
|
+
const HASH = 'sha512';
|
|
18
|
+
/** Reads SSH wire-format fields from a buffer, throwing on a field that runs past its end. */
|
|
19
|
+
class Reader {
|
|
20
|
+
at = 0;
|
|
21
|
+
data;
|
|
22
|
+
constructor(data) {
|
|
23
|
+
this.data = data;
|
|
24
|
+
}
|
|
25
|
+
uint32() {
|
|
26
|
+
if (this.at + 4 > this.data.length)
|
|
27
|
+
throw new Error('it ends early');
|
|
28
|
+
const value = this.data.readUInt32BE(this.at);
|
|
29
|
+
this.at += 4;
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
bytes(length) {
|
|
33
|
+
if (this.at + length > this.data.length)
|
|
34
|
+
throw new Error('it ends early');
|
|
35
|
+
const value = this.data.subarray(this.at, this.at + length);
|
|
36
|
+
this.at += length;
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
string() {
|
|
40
|
+
return this.bytes(this.uint32());
|
|
41
|
+
}
|
|
42
|
+
text() {
|
|
43
|
+
return this.string().toString('latin1');
|
|
44
|
+
}
|
|
45
|
+
get done() {
|
|
46
|
+
return this.at === this.data.length;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function field(value) {
|
|
50
|
+
const bytes = typeof value === 'string' ? Buffer.from(value, 'latin1') : value;
|
|
51
|
+
const length = Buffer.alloc(4);
|
|
52
|
+
length.writeUInt32BE(bytes.length);
|
|
53
|
+
return Buffer.concat([length, bytes]);
|
|
54
|
+
}
|
|
55
|
+
/** The key's public blob, as SSH writes it: its type, then the 32 bytes. */
|
|
56
|
+
function publicBlob(publicKey) {
|
|
57
|
+
return Buffer.concat([field(ED25519), field(publicKey)]);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The ed25519 key in an OpenSSH private key file's text, or why it cannot be
|
|
61
|
+
* used: niyam signs with an unencrypted ed25519 key alone, which is what
|
|
62
|
+
* `niyam cloud-key` makes.
|
|
63
|
+
*/
|
|
64
|
+
export function readPrivateKey(text) {
|
|
65
|
+
const trimmed = text.trim();
|
|
66
|
+
if (!trimmed.startsWith(PEM_BEGIN) || !trimmed.endsWith(PEM_END))
|
|
67
|
+
return { problem: 'it is not an OpenSSH private key' };
|
|
68
|
+
const data = Buffer.from(trimmed.slice(PEM_BEGIN.length, -PEM_END.length).replace(/\s+/g, ''), 'base64');
|
|
69
|
+
try {
|
|
70
|
+
if (data.subarray(0, KEY_MAGIC.length).toString('latin1') !== KEY_MAGIC)
|
|
71
|
+
return { problem: 'it is not an OpenSSH private key' };
|
|
72
|
+
const reader = new Reader(data.subarray(KEY_MAGIC.length));
|
|
73
|
+
const cipher = reader.text();
|
|
74
|
+
const kdf = reader.text();
|
|
75
|
+
reader.string();
|
|
76
|
+
if (cipher !== 'none' || kdf !== 'none')
|
|
77
|
+
return { problem: 'it has a passphrase, and niyam signs with a key that has none' };
|
|
78
|
+
if (reader.uint32() !== 1)
|
|
79
|
+
return { problem: 'it holds more than one key' };
|
|
80
|
+
reader.string();
|
|
81
|
+
const secret = new Reader(reader.string());
|
|
82
|
+
if (secret.uint32() !== secret.uint32())
|
|
83
|
+
return { problem: 'its check numbers differ' };
|
|
84
|
+
const type = secret.text();
|
|
85
|
+
if (type !== ED25519)
|
|
86
|
+
return { problem: `it is a ${type} key, and niyam signs with ed25519 alone` };
|
|
87
|
+
const publicKey = secret.string();
|
|
88
|
+
const both = secret.string();
|
|
89
|
+
if (publicKey.length !== 32 || both.length !== 64 || !both.subarray(32).equals(publicKey))
|
|
90
|
+
return { problem: 'its ed25519 halves do not match' };
|
|
91
|
+
return { seed: Buffer.from(both.subarray(0, 32)), publicKey: Buffer.from(publicKey) };
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
// fail-open: a key niyam cannot read is a problem the caller reports, never a crash.
|
|
95
|
+
return { problem: `it cannot be read (${errorDetail(error)})` };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** The key's public half as a line of an authorized_keys file, with no comment. */
|
|
99
|
+
export function publicLine(key) {
|
|
100
|
+
return `${ED25519} ${publicBlob(key.publicKey).toString('base64')}`;
|
|
101
|
+
}
|
|
102
|
+
/** A new ed25519 key pair from Node's crypto, held in memory alone (#437). */
|
|
103
|
+
export function newKey() {
|
|
104
|
+
const { privateKey } = generateKeyPairSync('ed25519');
|
|
105
|
+
const { d, x } = privateKey.export({ format: 'jwk' });
|
|
106
|
+
return { seed: Buffer.from(d, 'base64url'), publicKey: Buffer.from(x, 'base64url') };
|
|
107
|
+
}
|
|
108
|
+
function uint32(value) {
|
|
109
|
+
const bytes = Buffer.alloc(4);
|
|
110
|
+
bytes.writeUInt32BE(value);
|
|
111
|
+
return bytes;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* `key` as an unencrypted OpenSSH private key file's text, as `ssh-keygen -N ''`
|
|
115
|
+
* writes one (PROTOCOL.key), with `comment`: what `readPrivateKey` and
|
|
116
|
+
* ssh-keygen read back.
|
|
117
|
+
*/
|
|
118
|
+
export function privateKeyText(key, comment) {
|
|
119
|
+
const check = randomBytes(4);
|
|
120
|
+
const secret = Buffer.concat([check, check, field(ED25519), field(key.publicKey), field(Buffer.concat([key.seed, key.publicKey])), field(Buffer.from(comment, 'utf8'))]);
|
|
121
|
+
// Padded to the cipher's block size, 8 with none, by the bytes 1, 2, 3, … as OpenSSH pads it.
|
|
122
|
+
const padding = Buffer.from(Array.from({ length: (8 - (secret.length % 8)) % 8 }, (_, i) => i + 1));
|
|
123
|
+
const body = Buffer.concat([Buffer.from(KEY_MAGIC, 'latin1'), field('none'), field('none'), field(''), uint32(1), field(publicBlob(key.publicKey)), field(Buffer.concat([secret, padding]))]).toString('base64');
|
|
124
|
+
return `${PEM_BEGIN}\n${body.match(/.{1,70}/g)?.join('\n') ?? ''}\n${PEM_END}\n`;
|
|
125
|
+
}
|
|
126
|
+
function base64url(bytes) {
|
|
127
|
+
return bytes.toString('base64url');
|
|
128
|
+
}
|
|
129
|
+
/** What SSHSIG signs: its preamble, the namespace, and the message's sha512. */
|
|
130
|
+
function signedData(message, namespace) {
|
|
131
|
+
return Buffer.concat([Buffer.from(SIG_MAGIC, 'latin1'), field(namespace), field(''), field(HASH), field(createHash(HASH).update(message).digest())]);
|
|
132
|
+
}
|
|
133
|
+
/** An armored SSHSIG signature of `message` by `key`, in `namespace`, as `ssh-keygen -Y sign` writes it. */
|
|
134
|
+
export function signMessage(key, message, namespace) {
|
|
135
|
+
const privateKey = createPrivateKey({ key: { kty: 'OKP', crv: 'Ed25519', d: base64url(key.seed), x: base64url(key.publicKey) }, format: 'jwk' });
|
|
136
|
+
const signature = sign(null, signedData(message, namespace), privateKey);
|
|
137
|
+
const version = Buffer.alloc(4);
|
|
138
|
+
version.writeUInt32BE(1);
|
|
139
|
+
const blob = Buffer.concat([
|
|
140
|
+
Buffer.from(SIG_MAGIC, 'latin1'),
|
|
141
|
+
version,
|
|
142
|
+
field(publicBlob(key.publicKey)),
|
|
143
|
+
field(namespace),
|
|
144
|
+
field(''),
|
|
145
|
+
field(HASH),
|
|
146
|
+
field(Buffer.concat([field(ED25519), field(signature)])),
|
|
147
|
+
]).toString('base64');
|
|
148
|
+
return `${SIG_BEGIN}\n${blob.match(/.{1,70}/g)?.join('\n') ?? ''}\n${SIG_END}\n`;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Whether `armored` is an SSHSIG signature of `message` in `namespace` by the
|
|
152
|
+
* key whose public line is `publicKeyLine`; undefined when it is, else why not.
|
|
153
|
+
*/
|
|
154
|
+
export function signatureProblem(armored, message, namespace, publicKeyLine) {
|
|
155
|
+
const trimmed = armored.trim();
|
|
156
|
+
if (!trimmed.startsWith(SIG_BEGIN) || !trimmed.endsWith(SIG_END))
|
|
157
|
+
return 'it is not an SSH signature';
|
|
158
|
+
try {
|
|
159
|
+
const data = Buffer.from(trimmed.slice(SIG_BEGIN.length, -SIG_END.length).replace(/\s+/g, ''), 'base64');
|
|
160
|
+
if (data.subarray(0, SIG_MAGIC.length).toString('latin1') !== SIG_MAGIC)
|
|
161
|
+
return 'it is not an SSH signature';
|
|
162
|
+
const reader = new Reader(data.subarray(SIG_MAGIC.length));
|
|
163
|
+
if (reader.uint32() !== 1)
|
|
164
|
+
return 'its version is not 1';
|
|
165
|
+
const signer = reader.string();
|
|
166
|
+
const signedNamespace = reader.text();
|
|
167
|
+
reader.string();
|
|
168
|
+
const hash = reader.text();
|
|
169
|
+
const signature = new Reader(reader.string());
|
|
170
|
+
if (!reader.done)
|
|
171
|
+
return 'it has bytes after the signature';
|
|
172
|
+
if (signedNamespace !== namespace)
|
|
173
|
+
return `it is for the namespace "${signedNamespace}", not "${namespace}"`;
|
|
174
|
+
if (hash !== HASH)
|
|
175
|
+
return `it hashes with ${hash}, not ${HASH}`;
|
|
176
|
+
if (`${ED25519} ${signer.toString('base64')}` !== publicKeyLine.trim().split(/\s+/).slice(0, 2).join(' '))
|
|
177
|
+
return 'another key made it';
|
|
178
|
+
if (signature.text() !== ED25519)
|
|
179
|
+
return 'it is not an ed25519 signature';
|
|
180
|
+
const bytes = signature.string();
|
|
181
|
+
const keyBytes = new Reader(signer);
|
|
182
|
+
keyBytes.string();
|
|
183
|
+
const publicKey = createPublicKey({ key: { kty: 'OKP', crv: 'Ed25519', x: base64url(keyBytes.string()) }, format: 'jwk' });
|
|
184
|
+
return verify(null, signedData(message, namespace), publicKey, bytes) ? undefined : 'the signature does not match';
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
// fail-open: a signature niyam cannot read is one that does not verify.
|
|
188
|
+
return `it cannot be read (${errorDetail(error)})`;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* A raw commit object split into what its SSH signature signs, the object
|
|
193
|
+
* without its gpgsig header, and that signature; none when it has no gpgsig.
|
|
194
|
+
*/
|
|
195
|
+
export function commitSignature(raw) {
|
|
196
|
+
const end = raw.indexOf('\n\n');
|
|
197
|
+
const headers = (end === -1 ? raw : raw.slice(0, end)).split('\n');
|
|
198
|
+
const rest = end === -1 ? '' : raw.slice(end);
|
|
199
|
+
const kept = [];
|
|
200
|
+
const signature = [];
|
|
201
|
+
let inSignature = false;
|
|
202
|
+
for (const line of headers) {
|
|
203
|
+
if (line.startsWith('gpgsig ')) {
|
|
204
|
+
inSignature = true;
|
|
205
|
+
signature.push(line.slice('gpgsig '.length));
|
|
206
|
+
}
|
|
207
|
+
else if (inSignature && line.startsWith(' ')) {
|
|
208
|
+
signature.push(line.slice(1));
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
inSignature = false;
|
|
212
|
+
kept.push(line);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (signature.length === 0)
|
|
216
|
+
return undefined;
|
|
217
|
+
return { payload: Buffer.from(`${kept.join('\n')}${rest}`, 'utf8'), signature: `${signature.join('\n')}\n` };
|
|
218
|
+
}
|
package/dist/sys.js
CHANGED
|
@@ -189,6 +189,31 @@ export function realSys() {
|
|
|
189
189
|
open(url) {
|
|
190
190
|
spawnSync('open', [url]);
|
|
191
191
|
},
|
|
192
|
+
serve(handle) {
|
|
193
|
+
const server = http.createServer((request, response) => {
|
|
194
|
+
const answer = (status, headers = {}, body = '') => {
|
|
195
|
+
response.writeHead(status, { 'cache-control': 'no-store', ...headers });
|
|
196
|
+
response.end(body);
|
|
197
|
+
};
|
|
198
|
+
if (request.method !== 'GET')
|
|
199
|
+
return answer(405, { allow: 'GET' });
|
|
200
|
+
const url = new URL(request.url ?? '/', 'http://127.0.0.1');
|
|
201
|
+
handle({ path: url.pathname, query: Object.fromEntries(url.searchParams) }).then(({ status, headers, body }) => answer(status, headers, body), () => answer(500));
|
|
202
|
+
});
|
|
203
|
+
return new Promise((done, fail) => {
|
|
204
|
+
server.once('error', fail);
|
|
205
|
+
server.listen(0, '127.0.0.1', () => {
|
|
206
|
+
const { port } = server.address();
|
|
207
|
+
done({
|
|
208
|
+
port,
|
|
209
|
+
close() {
|
|
210
|
+
server.closeAllConnections();
|
|
211
|
+
server.close();
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
},
|
|
192
217
|
onInterrupt(handler) {
|
|
193
218
|
process.on('SIGINT', handler);
|
|
194
219
|
return () => {
|