@infosupport/huddle-cli 0.0.0-experiment-66.40.1 → 0.0.0-experiment-98.43.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/dist/index.js CHANGED
@@ -15,8 +15,8 @@ const images_1 = require("./images");
15
15
  const self_update_1 = require("./self-update");
16
16
  const utils_1 = require("./utils");
17
17
  const experiment_1 = require("./experiment");
18
- const VALUE_FLAGS = new Set(['url', 'ide', 'name', 'image', 'workspace', 'container', 'status', 'runtime', 'experiment', 'path', 'out', 'ca-path', 'output']);
19
- const BOOLEAN_FLAGS = new Set(['help', 'h', 'empty', 'i', 'interactive', 'version', 'v', 'deny', 'replace', 'docker-socket', 'force']);
18
+ const VALUE_FLAGS = new Set(['url', 'ide', 'name', 'image', 'workspace', 'container', 'status', 'runtime', 'experiment', 'path', 'ca-path', 'output', 'out']);
19
+ const BOOLEAN_FLAGS = new Set(['help', 'h', 'empty', 'i', 'interactive', 'version', 'v', 'deny', 'docker-socket', 'force', 'replace']);
20
20
  const COMMANDS = new Set(['start', 'firewall', 'fw', 'init', 'restart', 'experiment', 'migrate', 'help', 'version']);
21
21
  function parseArgs(argv) {
22
22
  const positional = [];
package/dist/migrate.js CHANGED
@@ -118,12 +118,9 @@ function findMarkedNetworks(compose) {
118
118
  * Throws on hard configuration errors (no/ambiguous marked network); collects
119
119
  * softer problems in `warnings`.
120
120
  */
121
- function buildOverride(compose, opts = {}) {
122
- const caPath = opts.caPath ?? exports.DEFAULT_CA_PATH;
123
- const internalNet = opts.internalNet ?? init_1.INTERNAL_NET;
124
- const socketDir = opts.socketDir ?? init_1.HOST_SOCKET_DIR;
125
- const warnings = [];
126
- const blockers = [];
121
+ // Exactly one network must carry the huddle.network label. Zero or more than one
122
+ // is a hard error the operator must resolve before a safe override can be built.
123
+ function resolveMarkedNetwork(compose) {
127
124
  const marked = findMarkedNetworks(compose);
128
125
  if (marked.length === 0) {
129
126
  throw new Error(`No network marked with \`${exports.HUDDLE_NETWORK_LABEL}: "true"\` was found. ` +
@@ -132,21 +129,65 @@ function buildOverride(compose, opts = {}) {
132
129
  if (marked.length > 1) {
133
130
  throw new Error(`Multiple networks marked with \`${exports.HUDDLE_NETWORK_LABEL}\`: ${marked.join(', ')}. Mark exactly one.`);
134
131
  }
135
- const markedNetwork = marked[0];
136
- // Pick a key for the injected egress network that collides with NOTHING: not
137
- // an existing network, and not the marked network itself (otherwise the two
138
- // keys in the service's `networks` map below collapse into one and the egress
139
- // net is silently never added). Keep suffixing until it is unique.
132
+ return marked[0];
133
+ }
134
+ // Pick a key for the injected egress network that collides with NOTHING: not an
135
+ // existing network, and not the marked network itself (otherwise the two keys in
136
+ // the service's `networks` map collapse into one and the egress net is silently
137
+ // never added). Keep suffixing until it is unique.
138
+ function pickHuddleNetKey(compose, markedNetwork, preferred) {
140
139
  const taken = new Set(Object.keys(compose.networks ?? {}));
141
140
  taken.add(markedNetwork);
142
- let huddleNetKey = opts.huddleNetKey ?? exports.HUDDLE_NET_KEY;
143
- if (taken.has(huddleNetKey)) {
141
+ let key = preferred;
142
+ if (taken.has(key)) {
144
143
  let n = 0;
145
144
  do {
146
- huddleNetKey = n === 0 ? `${exports.HUDDLE_NET_KEY}-egress` : `${exports.HUDDLE_NET_KEY}-egress-${n}`;
145
+ key = n === 0 ? `${exports.HUDDLE_NET_KEY}-egress` : `${exports.HUDDLE_NET_KEY}-egress-${n}`;
147
146
  n++;
148
- } while (taken.has(huddleNetKey));
147
+ } while (taken.has(key));
148
+ }
149
+ return key;
150
+ }
151
+ // Build the override entry for a single service: keep it on the marked network,
152
+ // add the Huddle egress network + proxy env, and (with --docker-socket) mount the
153
+ // filtered Docker socket. Any second, non-internal network is flagged as a
154
+ // blocker — it would be an unfiltered route out that bypasses Huddle.
155
+ function buildInjectedService(svc, name, compose, ctx, warnings, blockers) {
156
+ for (const netKey of serviceNetworkKeys(svc)) {
157
+ if (netKey === ctx.markedNetwork)
158
+ continue;
159
+ const def = (compose.networks?.[netKey] ?? {});
160
+ if (!isTruthy(def.internal)) {
161
+ blockers.push(`Service "${name}" is also on network "${netKey}", which is not internal. ` +
162
+ 'Remove it or make it internal — a second non-internal network bypasses Huddle.');
163
+ }
164
+ }
165
+ const injected = {
166
+ // Keep the service on its own network AND add the Huddle egress network.
167
+ // Map form is robust regardless of Compose's list merge semantics.
168
+ networks: { [ctx.markedNetwork]: null, [ctx.huddleNetKey]: null },
169
+ environment: huddleProxyEnv(ctx.caPath, ctx.dockerSocket),
170
+ };
171
+ if (ctx.dockerSocket) {
172
+ const cn = svc.container_name;
173
+ if (!cn) {
174
+ warnings.push(`Service "${name}" has no \`container_name\`, so the filtered Docker socket cannot be mounted ` +
175
+ 'at a stable path. Set a fixed container_name or drop --docker-socket for this service.');
176
+ }
177
+ else {
178
+ injected.volumes = [`${ctx.socketDir}/${cn}:/var/run/huddle`];
179
+ }
149
180
  }
181
+ return injected;
182
+ }
183
+ function buildOverride(compose, opts = {}) {
184
+ const caPath = opts.caPath ?? exports.DEFAULT_CA_PATH;
185
+ const internalNet = opts.internalNet ?? init_1.INTERNAL_NET;
186
+ const socketDir = opts.socketDir ?? init_1.HOST_SOCKET_DIR;
187
+ const warnings = [];
188
+ const blockers = [];
189
+ const markedNetwork = resolveMarkedNetwork(compose);
190
+ const huddleNetKey = pickHuddleNetKey(compose, markedNetwork, opts.huddleNetKey ?? exports.HUDDLE_NET_KEY);
150
191
  // Guardrail: the marked network must be internal, otherwise it already has a
151
192
  // route to the internet and would bypass the firewall/proxy.
152
193
  const markedDef = (compose.networks?.[markedNetwork] ?? {});
@@ -160,37 +201,11 @@ function buildOverride(compose, opts = {}) {
160
201
  if (services.length === 0) {
161
202
  warnings.push(`No service is attached to the marked network "${markedNetwork}"; the override wires nothing.`);
162
203
  }
204
+ const ctx = { markedNetwork, huddleNetKey, caPath, socketDir, dockerSocket: !!opts.dockerSocket };
163
205
  const overrideServices = {};
164
206
  for (const name of services) {
165
207
  const svc = (compose.services?.[name] ?? {});
166
- // Guardrail: warn if the service also sits on another network that is not
167
- // internal — that would be a second, unfiltered route out.
168
- for (const netKey of serviceNetworkKeys(svc)) {
169
- if (netKey === markedNetwork)
170
- continue;
171
- const def = (compose.networks?.[netKey] ?? {});
172
- if (!isTruthy(def.internal)) {
173
- blockers.push(`Service "${name}" is also on network "${netKey}", which is not internal. ` +
174
- 'Remove it or make it internal — a second non-internal network bypasses Huddle.');
175
- }
176
- }
177
- const injected = {
178
- // Keep the service on its own network AND add the Huddle egress network.
179
- // Map form is robust regardless of Compose's list merge semantics.
180
- networks: { [markedNetwork]: null, [huddleNetKey]: null },
181
- environment: huddleProxyEnv(caPath, !!opts.dockerSocket),
182
- };
183
- if (opts.dockerSocket) {
184
- const cn = svc.container_name;
185
- if (!cn) {
186
- warnings.push(`Service "${name}" has no \`container_name\`, so the filtered Docker socket cannot be mounted ` +
187
- 'at a stable path. Set a fixed container_name or drop --docker-socket for this service.');
188
- }
189
- else {
190
- injected.volumes = [`${socketDir}/${cn}:/var/run/huddle`];
191
- }
192
- }
193
- overrideServices[name] = injected;
208
+ overrideServices[name] = buildInjectedService(svc, name, compose, ctx, warnings, blockers);
194
209
  }
195
210
  const override = {
196
211
  services: overrideServices,
@@ -229,6 +244,10 @@ function stripComment(line) {
229
244
  }
230
245
  return line;
231
246
  }
247
+ // Shared depth cap for both the reader (parseNode ↔ parseMapping/parseSequence)
248
+ // and the writer (dumpYaml ↔ dumpInlineOrBlock). Real compose trees are only a
249
+ // few levels deep; a much larger cap still forecloses stack-exhaustion (CWE-674).
250
+ const MAX_YAML_DEPTH = 64;
232
251
  function unsupportedYaml(feature) {
233
252
  return new Error(`the compose file uses ${feature}, which \`huddle migrate\`'s built-in YAML reader does ` +
234
253
  'not support and would mis-parse (risking a dropped, un-filtered service). Inline or ' +
@@ -279,10 +298,9 @@ function parseYaml(input) {
279
298
  let pos = 0;
280
299
  // Cap the mutual recursion (parseNode ↔ parseMapping/parseSequence) so a deeply
281
300
  // nested (malformed or hostile) compose file cannot exhaust the stack (CWE-674).
282
- const MAX_DEPTH = 64;
283
301
  function parseNode(indent, depth) {
284
- if (depth > MAX_DEPTH) {
285
- throw new Error(`YAML nesting exceeds ${MAX_DEPTH} levels; refusing to parse this compose file.`);
302
+ if (depth > MAX_YAML_DEPTH) {
303
+ throw new Error(`YAML nesting exceeds ${MAX_YAML_DEPTH} levels; refusing to parse this compose file.`);
286
304
  }
287
305
  if (pos >= lines.length || lines[pos].indent < indent)
288
306
  return null;
@@ -365,8 +383,12 @@ function parseScalar(raw) {
365
383
  const map = {};
366
384
  for (const part of splitFlow(value.slice(1, -1))) {
367
385
  const colon = findKeyColon(part);
386
+ // Fail closed: a flow-map entry we cannot split into `key: value` (e.g. the
387
+ // compact `{networks:[dev, public]}` form, where the colon has no following
388
+ // space) must NOT be silently dropped — that would omit the service from the
389
+ // generated override and leave it on its original, un-filtered network.
368
390
  if (colon < 0)
369
- continue;
391
+ throw unsupportedYaml('a compact flow-mapping entry (`{key:value}` without a space after the colon)');
370
392
  map[unquote(part.slice(0, colon).trim())] = parseScalar(part.slice(colon + 1).trim());
371
393
  }
372
394
  return map;
@@ -425,6 +447,13 @@ function unquote(value) {
425
447
  }
426
448
  /** Emits an object as YAML using the subset the reader understands. */
427
449
  function dumpYaml(obj, indent = 0) {
450
+ // `indent` doubles as the recursion depth (dumpYaml ↔ dumpInlineOrBlock each
451
+ // step it by one). Cap it so a pathologically nested object cannot exhaust the
452
+ // stack (CWE-674) — mirrors the MAX_DEPTH guard on the parse side. Legitimate
453
+ // compose trees are only a few levels deep.
454
+ if (indent > MAX_YAML_DEPTH) {
455
+ throw new Error(`YAML nesting exceeds ${MAX_YAML_DEPTH} levels; refusing to emit this override.`);
456
+ }
428
457
  const pad = ' '.repeat(indent);
429
458
  if (obj === null || obj === undefined)
430
459
  return '';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infosupport/huddle-cli",
3
- "version": "0.0.0-experiment-66.40.1",
3
+ "version": "0.0.0-experiment-98.43.1",
4
4
  "description": "Huddle CLI — start devcontainers and manage firewall rules",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "author": "Info Support B.V.",