@peakflo/agent-harness 0.2.19 → 0.2.20-rc.2
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 +78 -0
- package/dist/browser-broker/agent-actions.mjs +799 -0
- package/dist/browser-broker/browsers.mjs +1699 -0
- package/dist/browser-broker/credentials.mjs +211 -0
- package/dist/browser-broker/disk-guard.mjs +84 -0
- package/dist/browser-broker/egress.mjs +183 -0
- package/dist/browser-broker/input.mjs +270 -0
- package/dist/browser-broker/lease.mjs +547 -0
- package/dist/browser-broker/local-network.mjs +173 -0
- package/dist/browser-broker/local.mjs +158 -0
- package/dist/browser-broker/log.mjs +28 -0
- package/dist/browser-broker/network-policy.mjs +144 -0
- package/dist/browser-broker/os-users.mjs +493 -0
- package/dist/browser-broker/package.json +1 -0
- package/dist/browser-broker/protocol.mjs +321 -0
- package/dist/browser-broker/queue.mjs +37 -0
- package/dist/browser-broker/recorder-scripts.mjs +221 -0
- package/dist/browser-broker/recorder.mjs +438 -0
- package/dist/browser-broker/recording-store.mjs +493 -0
- package/dist/browser-broker/relay.mjs +399 -0
- package/dist/browser-broker/screencast.mjs +198 -0
- package/dist/browser-broker/state-store.mjs +112 -0
- package/dist/browser-broker/transfers.mjs +460 -0
- package/dist/control-plane.d.ts +6 -0
- package/dist/control-plane.js +10 -0
- package/dist/github-proxy.d.ts +1 -1
- package/dist/github-workspace.d.ts +2 -0
- package/dist/github-workspace.js +4 -0
- package/dist/harness.d.ts +1 -0
- package/dist/harness.js +27 -1
- package/dist/runtime/knowledge-tools.d.ts +1 -0
- package/dist/runtime/knowledge-tools.js +3 -0
- package/dist/runtime/local-browser.d.ts +33 -0
- package/dist/runtime/local-browser.js +135 -0
- package/package.json +5 -3
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent credentials (protocol v3): the broker's copy of the placeholder and
|
|
3
|
+
* field rules, plus the per-chat redaction set.
|
|
4
|
+
*
|
|
5
|
+
* SOURCE OF TRUTH: packages/core/src/services/coding-agents/agent-credentials.ts
|
|
6
|
+
* (placeholders, fields) and packages/core/src/utils/credential-urn.ts
|
|
7
|
+
* (hostMatchesPattern). The broker is plain Node in a root-owned image layer
|
|
8
|
+
* and cannot import core; test/credentials.test.mjs reads core as text and
|
|
9
|
+
* keeps the copies equal.
|
|
10
|
+
*
|
|
11
|
+
* WHY THE BROKER HOLDS VALUES AT ALL. The agent and the harness run as the
|
|
12
|
+
* same uid (1500), so anything the harness holds the agent can read from
|
|
13
|
+
* /proc. The root broker is the one in-sandbox process the agent cannot
|
|
14
|
+
* inspect, so it is where a value may exist: in memory, for as short as
|
|
15
|
+
* possible, and never in a log line, a file or a tool result.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** `pfcred_<32 hex>` or `pfcred_<32 hex>_<field>` (core CREDENTIAL_PLACEHOLDER_RE). */
|
|
19
|
+
export const CREDENTIAL_PLACEHOLDER_RE =
|
|
20
|
+
/pfcred_([0-9a-f]{32})(?:_([A-Za-z][A-Za-z0-9]{0,63}))?/g;
|
|
21
|
+
export const CREDENTIAL_PLACEHOLDER_PREFIX = 'pfcred_';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The field name the proxy sends (and expects back in `values`) for a bare
|
|
25
|
+
* `pfcred_<id>`: the credential's primary secret field, which only the API
|
|
26
|
+
* knows (login/basic_auth -> password, api_key -> key, bearer_token ->
|
|
27
|
+
* accessToken; custom has none and the API refuses).
|
|
28
|
+
*/
|
|
29
|
+
export const PRIMARY_FIELD = 'default';
|
|
30
|
+
|
|
31
|
+
/** Core NON_SECRET_CREDENTIAL_FIELDS. */
|
|
32
|
+
export const NON_SECRET_CREDENTIAL_FIELDS = Object.freeze([
|
|
33
|
+
'username',
|
|
34
|
+
'loginUrl',
|
|
35
|
+
'headerName',
|
|
36
|
+
'prefix',
|
|
37
|
+
'tokenType',
|
|
38
|
+
'expiresAt'
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
/** The current authenticator code: fill only, never a proxy field. */
|
|
42
|
+
export const TOTP_FIELD = 'totp';
|
|
43
|
+
|
|
44
|
+
export const CREDENTIAL_ID_RE =
|
|
45
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
46
|
+
export const FIELD_RE = /^[A-Za-z][A-Za-z0-9]{0,63}$/;
|
|
47
|
+
|
|
48
|
+
export function compactCredentialId(credentialId) {
|
|
49
|
+
return String(credentialId).replace(/-/g, '').toLowerCase();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function expandCredentialId(compact) {
|
|
53
|
+
if (!/^[0-9a-f]{32}$/.test(compact)) return null;
|
|
54
|
+
return `${compact.slice(0, 8)}-${compact.slice(8, 12)}-${compact.slice(
|
|
55
|
+
12,
|
|
56
|
+
16
|
|
57
|
+
)}-${compact.slice(16, 20)}-${compact.slice(20)}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function credentialPlaceholder(credentialId, field) {
|
|
61
|
+
const base = `${CREDENTIAL_PLACEHOLDER_PREFIX}${compactCredentialId(credentialId)}`;
|
|
62
|
+
return field && field !== PRIMARY_FIELD ? `${base}_${field}` : base;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Every placeholder in `text`: `{ placeholder, credentialId, field }` where a
|
|
67
|
+
* bare placeholder's field is PRIMARY_FIELD.
|
|
68
|
+
*/
|
|
69
|
+
export function findPlaceholders(text) {
|
|
70
|
+
const found = [];
|
|
71
|
+
if (typeof text !== 'string' || !text.includes(CREDENTIAL_PLACEHOLDER_PREFIX))
|
|
72
|
+
return found;
|
|
73
|
+
for (const match of text.matchAll(CREDENTIAL_PLACEHOLDER_RE)) {
|
|
74
|
+
const credentialId = expandCredentialId(match[1]);
|
|
75
|
+
if (!credentialId) continue;
|
|
76
|
+
found.push({
|
|
77
|
+
placeholder: match[0],
|
|
78
|
+
credentialId,
|
|
79
|
+
field: match[2] ?? PRIMARY_FIELD
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return found;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Core hostMatchesPattern, minus the bare `*`: an agent-eligible credential
|
|
87
|
+
* never has it (the API refuses the grant), so here it matches NOTHING.
|
|
88
|
+
* Fail closed: a `*` reaching the broker must not make every host a
|
|
89
|
+
* substitution target.
|
|
90
|
+
*/
|
|
91
|
+
export function hostMatchesPattern(host, pattern) {
|
|
92
|
+
if (!host || !pattern) return false;
|
|
93
|
+
const h = String(host).toLowerCase().split(':')[0];
|
|
94
|
+
const p = String(pattern).toLowerCase().trim();
|
|
95
|
+
if (p === '*' || p === '') return false;
|
|
96
|
+
if (p.startsWith('*.')) {
|
|
97
|
+
const suffix = p.slice(1);
|
|
98
|
+
return h.endsWith(suffix) && h.length > suffix.length;
|
|
99
|
+
}
|
|
100
|
+
return h === p;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/* -------------------------------------------------------------------------- */
|
|
104
|
+
/* fill_credential: which element may take which field */
|
|
105
|
+
/* -------------------------------------------------------------------------- */
|
|
106
|
+
|
|
107
|
+
const TEXTISH = new Set(['text', 'email', 'tel']);
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Contract step 2. `info` is what the page reports for the target:
|
|
111
|
+
* `{ tag, type, autocomplete, readOnly, disabled }` (`type` is the INPUT's
|
|
112
|
+
* `type` property, so a missing attribute reads 'text'). A wrong element is
|
|
113
|
+
* refused rather than "helpfully" typed into: a password must never land in
|
|
114
|
+
* a visible search box that some script posts elsewhere.
|
|
115
|
+
*/
|
|
116
|
+
export function elementAllowsField(field, info) {
|
|
117
|
+
if (!info || info.readOnly || info.disabled) return false;
|
|
118
|
+
const tag = String(info.tag || '').toUpperCase();
|
|
119
|
+
const type = String(info.type || '').toLowerCase();
|
|
120
|
+
const tokens = String(info.autocomplete || '')
|
|
121
|
+
.toLowerCase()
|
|
122
|
+
.split(/\s+/)
|
|
123
|
+
.filter(Boolean);
|
|
124
|
+
if (field === 'password') {
|
|
125
|
+
if (tag !== 'INPUT') return false;
|
|
126
|
+
if (type === 'password') return true;
|
|
127
|
+
return (
|
|
128
|
+
(type === 'text' || type === '') &&
|
|
129
|
+
(tokens.includes('current-password') || tokens.includes('new-password'))
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
if (field === 'username') {
|
|
133
|
+
return tag === 'INPUT' && (TEXTISH.has(type) || type === '');
|
|
134
|
+
}
|
|
135
|
+
if (field === TOTP_FIELD) {
|
|
136
|
+
if (tag !== 'INPUT') return false;
|
|
137
|
+
if (['text', 'tel', 'number', ''].includes(type)) return true;
|
|
138
|
+
return tokens.includes('one-time-code') && type === 'password';
|
|
139
|
+
}
|
|
140
|
+
if (tag === 'TEXTAREA') return true;
|
|
141
|
+
return (
|
|
142
|
+
tag === 'INPUT' && (TEXTISH.has(type) || type === 'password' || type === '')
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/* -------------------------------------------------------------------------- */
|
|
147
|
+
/* Per-chat redaction */
|
|
148
|
+
/* -------------------------------------------------------------------------- */
|
|
149
|
+
|
|
150
|
+
export const REDACTED = '[redacted]';
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The values a chat's browser was given by `fill_credential`, held ONLY in
|
|
154
|
+
* this root process and dropped when the browser closes. Everything the
|
|
155
|
+
* broker hands back to the agent or writes to a recording passes through
|
|
156
|
+
* `redact()` (snapshot values, `get` text, URLs, error messages, recorder
|
|
157
|
+
* steps and snapshots), so a page that echoes a value cannot turn it into
|
|
158
|
+
* tool output.
|
|
159
|
+
*/
|
|
160
|
+
export class RedactionSet {
|
|
161
|
+
#values = new Set();
|
|
162
|
+
|
|
163
|
+
get size() {
|
|
164
|
+
return this.#values.size;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
add(value) {
|
|
168
|
+
if (typeof value === 'string' && value.length > 0) {
|
|
169
|
+
this.#values.add(value);
|
|
170
|
+
// The same value as it appears URL-encoded (a GET form, a redirect).
|
|
171
|
+
const encoded = encodeURIComponent(value);
|
|
172
|
+
if (encoded !== value) this.#values.add(encoded);
|
|
173
|
+
const plus = encoded.replace(/%20/g, '+');
|
|
174
|
+
if (plus !== encoded) this.#values.add(plus);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
clear() {
|
|
179
|
+
this.#values.clear();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** A string with every held value replaced by [redacted]. */
|
|
183
|
+
text(value) {
|
|
184
|
+
if (typeof value !== 'string' || this.#values.size === 0) return value;
|
|
185
|
+
let out = value;
|
|
186
|
+
// Longest first, so a value that contains another is replaced whole.
|
|
187
|
+
for (const secret of [...this.#values].sort(
|
|
188
|
+
(a, b) => b.length - a.length
|
|
189
|
+
)) {
|
|
190
|
+
if (out.includes(secret)) out = out.split(secret).join(REDACTED);
|
|
191
|
+
}
|
|
192
|
+
return out;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** A deep copy of `value` with every string redacted (keys included). */
|
|
196
|
+
redact(value, skipKeys = null, depth = 0) {
|
|
197
|
+
if (this.#values.size === 0) return value;
|
|
198
|
+
if (typeof value === 'string') return this.text(value);
|
|
199
|
+
if (!value || typeof value !== 'object' || depth > 32) return value;
|
|
200
|
+
if (Buffer.isBuffer(value)) return value;
|
|
201
|
+
if (Array.isArray(value))
|
|
202
|
+
return value.map((item) => this.redact(item, skipKeys, depth + 1));
|
|
203
|
+
const out = {};
|
|
204
|
+
for (const [key, item] of Object.entries(value)) {
|
|
205
|
+
out[this.text(key)] = skipKeys?.has(key)
|
|
206
|
+
? item
|
|
207
|
+
: this.redact(item, skipKeys, depth + 1);
|
|
208
|
+
}
|
|
209
|
+
return out;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Free-disk guard shared by uploads, downloads and profile growth.
|
|
3
|
+
*
|
|
4
|
+
* The product puts no size cap on downloads (owner decision), so the only
|
|
5
|
+
* limit is the sandbox disk itself. The guard keeps a RESERVE free for the
|
|
6
|
+
* agent's own workspace and the sandbox's processes:
|
|
7
|
+
*
|
|
8
|
+
* reserve = max(1 GiB, 10% of the filesystem)
|
|
9
|
+
*
|
|
10
|
+
* uploads refused (storage_full) when the file would eat into the reserve;
|
|
11
|
+
* downloads cancelled while in progress once free space drops below the
|
|
12
|
+
* reserve (ChatBrowser polls every DOWNLOAD_POLL_MS), and a new
|
|
13
|
+
* download is refused while free space is below it;
|
|
14
|
+
* profiles each live browser checks every PROFILE_CHECK_MS and tells the
|
|
15
|
+
* people watching (notice) when the disk is nearly full. The
|
|
16
|
+
* browser is NOT killed.
|
|
17
|
+
*
|
|
18
|
+
* statfs is injectable for tests.
|
|
19
|
+
*/
|
|
20
|
+
import fs from 'node:fs';
|
|
21
|
+
import { BrokerError } from './protocol.mjs';
|
|
22
|
+
|
|
23
|
+
export const DISK_RESERVE_MIN_BYTES = 1024 ** 3;
|
|
24
|
+
export const DISK_RESERVE_FRACTION = 0.1;
|
|
25
|
+
export const DOWNLOAD_POLL_MS = 1000;
|
|
26
|
+
export const PROFILE_CHECK_MS = 30_000;
|
|
27
|
+
|
|
28
|
+
export const DISK_FULL_DOWNLOAD_MESSAGE =
|
|
29
|
+
'The download was stopped because the sandbox disk is nearly full.';
|
|
30
|
+
export const DISK_FULL_NEW_DOWNLOAD_MESSAGE =
|
|
31
|
+
'The download was not started because the sandbox disk is nearly full.';
|
|
32
|
+
export const DISK_LOW_MESSAGE =
|
|
33
|
+
'The sandbox disk is nearly full. New downloads are paused until space is freed.';
|
|
34
|
+
export const DISK_FULL_UPLOAD_MESSAGE =
|
|
35
|
+
'The sandbox does not have enough free disk for this file.';
|
|
36
|
+
|
|
37
|
+
/** max(1 GiB, 10% of the filesystem) for a statfs result. */
|
|
38
|
+
export function diskReserve(stats) {
|
|
39
|
+
const total = Number(stats?.blocks) * Number(stats?.bsize);
|
|
40
|
+
const tenth =
|
|
41
|
+
Number.isFinite(total) && total > 0
|
|
42
|
+
? Math.floor(total * DISK_RESERVE_FRACTION)
|
|
43
|
+
: 0;
|
|
44
|
+
return Math.max(DISK_RESERVE_MIN_BYTES, tenth);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class DiskGuard {
|
|
48
|
+
/** @param {{ path: string, statfs?: (p: string) => { bavail, bsize, blocks } }} options */
|
|
49
|
+
constructor({ path, statfs = fs.statfsSync }) {
|
|
50
|
+
this.path = path;
|
|
51
|
+
this.statfs = statfs;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** { free, reserve } in bytes. A statfs failure reads as "no space" (fail closed). */
|
|
55
|
+
measure() {
|
|
56
|
+
try {
|
|
57
|
+
const stats = this.statfs(this.path);
|
|
58
|
+
return {
|
|
59
|
+
free: Number(stats.bavail) * Number(stats.bsize),
|
|
60
|
+
reserve: diskReserve(stats)
|
|
61
|
+
};
|
|
62
|
+
} catch {
|
|
63
|
+
return { free: 0, reserve: DISK_RESERVE_MIN_BYTES };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
freeBytes() {
|
|
68
|
+
return this.measure().free;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Free space is below the reserve. */
|
|
72
|
+
low() {
|
|
73
|
+
const { free, reserve } = this.measure();
|
|
74
|
+
return free < reserve;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Throws storage_full unless `bytes` fit and the reserve stays free. */
|
|
78
|
+
requireSpace(bytes) {
|
|
79
|
+
const { free, reserve } = this.measure();
|
|
80
|
+
if (!(bytes >= 0) || bytes > free - reserve) {
|
|
81
|
+
throw new BrokerError('storage_full', DISK_FULL_UPLOAD_MESSAGE);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Restricted sandbox egress (Daytona "netleash").
|
|
3
|
+
*
|
|
4
|
+
* With per-sandbox egress restrictions on, Daytona routes the sandbox's
|
|
5
|
+
* outbound HTTPS through its egress proxy, which TERMINATES TLS and re-signs
|
|
6
|
+
* every server certificate with its own CA, /etc/daytona/netleash/ca.crt. The
|
|
7
|
+
* platform injects the trust/proxy variables into the sandbox environment
|
|
8
|
+
* (NODE_EXTRA_CA_CERTS / SSL_CERT_FILE = that CA, HTTPS_PROXY = the proxy).
|
|
9
|
+
*
|
|
10
|
+
* The interception is TRANSPARENT: a plain, proxy-unaware TLS connect to the
|
|
11
|
+
* control plane is intercepted and answered with a netleash-signed leaf. That
|
|
12
|
+
* is exactly what broke the broker (`unable to verify the first certificate`
|
|
13
|
+
* from a direct `ws` connect, which never reads HTTPS_PROXY), and it is why
|
|
14
|
+
* the harness's own direct `fetch()` calls (also proxy-unaware) work there:
|
|
15
|
+
* they only need the CA. So the broker needs the CA, not a proxy client.
|
|
16
|
+
*
|
|
17
|
+
* Two consumers:
|
|
18
|
+
* - daemonEnv(): the environment of the detached broker daemon. An
|
|
19
|
+
* ALLOWLIST: PATH/NODE_ENV/TZ as before, plus the platform's TLS/proxy
|
|
20
|
+
* variables when present, plus NODE_EXTRA_CA_CERTS = the netleash CA when
|
|
21
|
+
* the platform did not set it but the file exists. Nothing else: no token
|
|
22
|
+
* (the daemon reads the 0600 file), no other sandbox env.
|
|
23
|
+
* - trustEgressCa(): each chat's Chromium uses NSS for certificate trust,
|
|
24
|
+
* not these variables, so the netleash CA is imported into the chat
|
|
25
|
+
* user's own NSS database ($HOME/.pki/nssdb) before every launch. Proper
|
|
26
|
+
* CA trust: certificate errors are NOT ignored.
|
|
27
|
+
*/
|
|
28
|
+
import fs from 'node:fs';
|
|
29
|
+
import { Readable } from 'node:stream';
|
|
30
|
+
import { runAsUser } from './os-users.mjs';
|
|
31
|
+
import { log, errorText } from './log.mjs';
|
|
32
|
+
|
|
33
|
+
export const NETLEASH_CA_FILE = '/etc/daytona/netleash/ca.crt';
|
|
34
|
+
export const DAEMON_PATH =
|
|
35
|
+
'/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Platform-injected TLS/proxy variables the daemon inherits, when present.
|
|
39
|
+
* Nothing in the daemon reads the proxy ones today (`ws` dials directly and
|
|
40
|
+
* Node ignores them without NODE_USE_ENV_PROXY, which is NOT passed); they are
|
|
41
|
+
* kept so a future proxy-aware client sees the platform's own NO_PROXY too.
|
|
42
|
+
*/
|
|
43
|
+
export const DAEMON_PASSTHROUGH_ENV = Object.freeze([
|
|
44
|
+
'NODE_EXTRA_CA_CERTS',
|
|
45
|
+
'SSL_CERT_FILE',
|
|
46
|
+
'HTTPS_PROXY',
|
|
47
|
+
'https_proxy',
|
|
48
|
+
'HTTP_PROXY',
|
|
49
|
+
'http_proxy',
|
|
50
|
+
'NO_PROXY',
|
|
51
|
+
'no_proxy'
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
const MAX_ENV_VALUE = 4096;
|
|
55
|
+
|
|
56
|
+
function usable(value) {
|
|
57
|
+
return (
|
|
58
|
+
typeof value === 'string' &&
|
|
59
|
+
value.length > 0 &&
|
|
60
|
+
value.length <= MAX_ENV_VALUE &&
|
|
61
|
+
!/[\0\r\n]/.test(value)
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The detached daemon's environment.
|
|
67
|
+
* @param {Record<string, string|undefined>} source the launcher's env
|
|
68
|
+
* @param {{ exists?: (file: string) => boolean, caFile?: string }} [options]
|
|
69
|
+
*/
|
|
70
|
+
export function daemonEnv(source = process.env, options = {}) {
|
|
71
|
+
const exists = options.exists ?? fs.existsSync;
|
|
72
|
+
const caFile = options.caFile ?? NETLEASH_CA_FILE;
|
|
73
|
+
const env = {
|
|
74
|
+
PATH: DAEMON_PATH,
|
|
75
|
+
NODE_ENV: 'production',
|
|
76
|
+
TZ: source.TZ || 'UTC'
|
|
77
|
+
};
|
|
78
|
+
for (const key of DAEMON_PASSTHROUGH_ENV) {
|
|
79
|
+
if (usable(source[key])) env[key] = source[key];
|
|
80
|
+
}
|
|
81
|
+
if (!env.NODE_EXTRA_CA_CERTS && exists(caFile)) {
|
|
82
|
+
env.NODE_EXTRA_CA_CERTS = caFile;
|
|
83
|
+
}
|
|
84
|
+
return env;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/* -------------------------------------------------------------------------- */
|
|
88
|
+
/* Chromium: the netleash CA in the chat user's NSS database */
|
|
89
|
+
/* -------------------------------------------------------------------------- */
|
|
90
|
+
|
|
91
|
+
export const CERTUTIL = '/usr/bin/certutil';
|
|
92
|
+
export const NSS_NICKNAME_PREFIX = 'pf-sandbox-egress-ca';
|
|
93
|
+
const MAX_CA_FILE_BYTES = 256 * 1024;
|
|
94
|
+
const MAX_CA_CERTS = 16;
|
|
95
|
+
const PEM_CERT =
|
|
96
|
+
/-----BEGIN CERTIFICATE-----\r?\n[A-Za-z0-9+/=\r\n]+?-----END CERTIFICATE-----/g;
|
|
97
|
+
|
|
98
|
+
/** The PEM certificates in `text` (at most MAX_CA_CERTS). */
|
|
99
|
+
export function pemCertificates(text) {
|
|
100
|
+
return (String(text).match(PEM_CERT) ?? [])
|
|
101
|
+
.slice(0, MAX_CA_CERTS)
|
|
102
|
+
.map((pem) => `${pem.replace(/\r/g, '')}\n`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The netleash CA certificates, or [] when this sandbox has no egress CA. */
|
|
106
|
+
export function egressCaCertificates(caFile = NETLEASH_CA_FILE) {
|
|
107
|
+
let stat;
|
|
108
|
+
try {
|
|
109
|
+
stat = fs.statSync(caFile);
|
|
110
|
+
} catch {
|
|
111
|
+
return [];
|
|
112
|
+
}
|
|
113
|
+
if (!stat.isFile() || stat.size === 0 || stat.size > MAX_CA_FILE_BYTES)
|
|
114
|
+
return [];
|
|
115
|
+
return pemCertificates(fs.readFileSync(caFile, 'utf8'));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Make `user`'s Chromium trust the egress CA. Idempotent and safe next to a
|
|
120
|
+
* running Chromium: the user's NSS database is created only when absent, and
|
|
121
|
+
* only OUR nicknames are removed and re-added on each launch, so a rotated CA
|
|
122
|
+
* replaces the old one. Runs as the chat user; the CA bytes are piped in by
|
|
123
|
+
* the root broker, so the user needs no read access to /etc/daytona. (The db
|
|
124
|
+
* is the chat user's own, so code in that browser could add CAs to it; that
|
|
125
|
+
* affects only that user's browser.)
|
|
126
|
+
*
|
|
127
|
+
* Best effort: a failure is logged and the browser still starts (sites the
|
|
128
|
+
* proxy does not intercept keep working; intercepted ones show a certificate
|
|
129
|
+
* error, never a silent bypass).
|
|
130
|
+
*
|
|
131
|
+
* @returns {Promise<number>} certificates imported
|
|
132
|
+
*/
|
|
133
|
+
export async function trustEgressCa(user, options = {}) {
|
|
134
|
+
const certs = options.certificates ?? egressCaCertificates(options.caFile);
|
|
135
|
+
const run = options.run ?? runAsUser;
|
|
136
|
+
if (certs.length === 0) return 0;
|
|
137
|
+
const db = `sql:${user.home}/.pki/nssdb`;
|
|
138
|
+
try {
|
|
139
|
+
await run(user, [
|
|
140
|
+
'/bin/sh',
|
|
141
|
+
'-c',
|
|
142
|
+
'umask 077 && d="sql:$1/.pki/nssdb" && mkdir -p "$1/.pki/nssdb" && ' +
|
|
143
|
+
'{ [ -f "$1/.pki/nssdb/cert9.db" ] || "$2" -N -d "$d" --empty-password; } && ' +
|
|
144
|
+
'i=0; while [ "$i" -lt "$4" ]; do ' +
|
|
145
|
+
'j=0; while [ "$j" -lt 8 ] && "$2" -D -d "$d" -n "$3-$i" 2>/dev/null; ' +
|
|
146
|
+
'do j=$((j+1)); done; i=$((i+1)); done',
|
|
147
|
+
'sh',
|
|
148
|
+
user.home,
|
|
149
|
+
CERTUTIL,
|
|
150
|
+
NSS_NICKNAME_PREFIX,
|
|
151
|
+
String(MAX_CA_CERTS)
|
|
152
|
+
]);
|
|
153
|
+
for (const [index, pem] of certs.entries()) {
|
|
154
|
+
// No -i: certutil -A reads the certificate from stdin.
|
|
155
|
+
await run(
|
|
156
|
+
user,
|
|
157
|
+
[
|
|
158
|
+
CERTUTIL,
|
|
159
|
+
'-A',
|
|
160
|
+
'-d',
|
|
161
|
+
db,
|
|
162
|
+
'-n',
|
|
163
|
+
`${NSS_NICKNAME_PREFIX}-${index}`,
|
|
164
|
+
'-t',
|
|
165
|
+
'C,,',
|
|
166
|
+
'-a'
|
|
167
|
+
],
|
|
168
|
+
{ stdin: Readable.from([pem]) }
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
log.info('egress CA trusted by the browser', {
|
|
172
|
+
user: user.name,
|
|
173
|
+
count: certs.length
|
|
174
|
+
});
|
|
175
|
+
return certs.length;
|
|
176
|
+
} catch (error) {
|
|
177
|
+
log.warn('egress CA could not be added to the browser', {
|
|
178
|
+
user: user.name,
|
|
179
|
+
error: errorText(error)
|
|
180
|
+
});
|
|
181
|
+
return 0;
|
|
182
|
+
}
|
|
183
|
+
}
|