@infosupport/huddle-cli 0.0.0-experiment-160.153.1 → 0.0.0-experiment-160.154.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.
Files changed (2) hide show
  1. package/dist/pipe/tools.js +43 -17
  2. package/package.json +1 -1
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.TOOLS = void 0;
13
13
  exports.resolveTool = resolveTool;
14
14
  exports.execTool = execTool;
15
+ exports.checkToolCwd = checkToolCwd;
15
16
  exports.toolRun = toolRun;
16
17
  exports.toolVersion = toolVersion;
17
18
  exports.toolsInfo = toolsInfo;
@@ -21,6 +22,7 @@ const os_1 = __importDefault(require("os"));
21
22
  const path_1 = __importDefault(require("path"));
22
23
  const child_process_1 = require("child_process");
23
24
  const protocol_1 = require("./protocol");
25
+ const config_1 = require("../config");
24
26
  exports.TOOLS = {
25
27
  sbx: { bins: ['sbx', 'sbx.exe'], env: 'HUDDLE_SBX_BIN' },
26
28
  // Firebrick (https://firebrick-sandbox.dev): one microVM per project folder.
@@ -169,6 +171,44 @@ function execTool(file, args, timeoutMs, maxBytes = protocol_1.MAX_OUTPUT_BYTES,
169
171
  });
170
172
  }
171
173
  /** The `tool.run` op. */
174
+ /** `child` is `parent` or inside it (case-insensitive on Windows). */
175
+ function isInside(child, parent) {
176
+ const norm = (s) => {
177
+ const n = path_1.default.resolve(s);
178
+ return process.platform === 'win32' ? n.toLowerCase() : n;
179
+ };
180
+ const c = norm(child);
181
+ const p = norm(parent);
182
+ return c === p || c.startsWith(p.endsWith(path_1.default.sep) ? p : p + path_1.default.sep);
183
+ }
184
+ /**
185
+ * The folder a tool may run in: an existing directory, with links followed,
186
+ * that is not ~/.huddle or inside it (the pipe, the operator token, the CA
187
+ * key live there). The gateway checks this too; this is the host's own say.
188
+ */
189
+ function checkToolCwd(cwd, huddleHome = config_1.CONFIG_DIR) {
190
+ if (typeof cwd !== 'string' || !cwd || cwd.includes('\0') || !path_1.default.isAbsolute(cwd)) {
191
+ throw new protocol_1.PipeError('bad_request', 'cwd must be an absolute path');
192
+ }
193
+ let real;
194
+ try {
195
+ real = fs_1.default.realpathSync(cwd);
196
+ if (!fs_1.default.statSync(real).isDirectory())
197
+ throw new Error('not a directory');
198
+ }
199
+ catch {
200
+ throw new protocol_1.PipeError('bad_request', `cwd is not a directory on the host: ${cwd}`);
201
+ }
202
+ let home = huddleHome;
203
+ try {
204
+ home = fs_1.default.realpathSync(huddleHome);
205
+ }
206
+ catch { /* not there yet: compare by name */ }
207
+ if (isInside(real, home) || isInside(real, huddleHome)) {
208
+ throw new protocol_1.PipeError('bad_request', `cwd must not be Huddle's own folder or inside it: ${cwd}`);
209
+ }
210
+ return real;
211
+ }
172
212
  async function toolRun(args) {
173
213
  const { tool, args: argv, timeoutMs, cwd } = args;
174
214
  if (typeof tool !== 'string' || !tool)
@@ -182,28 +222,14 @@ async function toolRun(args) {
182
222
  throw new protocol_1.PipeError('bad_request', 'timeoutMs must be a positive number');
183
223
  }
184
224
  // Optional working directory: Firebrick derives the sandbox from the folder it
185
- // runs in. Only an absolute path to an existing directory; the gateway has
186
- // already held it to the mount rules (no ~/.huddle and the like).
187
- if (cwd !== undefined) {
188
- if (typeof cwd !== 'string' || !cwd || cwd.includes('\0') || !path_1.default.isAbsolute(cwd)) {
189
- throw new protocol_1.PipeError('bad_request', 'cwd must be an absolute path');
190
- }
191
- let isDir = false;
192
- try {
193
- isDir = fs_1.default.statSync(cwd).isDirectory();
194
- }
195
- catch {
196
- isDir = false;
197
- }
198
- if (!isDir)
199
- throw new protocol_1.PipeError('bad_request', `cwd is not a directory on the host: ${cwd}`);
200
- }
225
+ // runs in. Run in the resolved folder, so a link cannot change between check and use.
226
+ const runIn = cwd === undefined ? undefined : checkToolCwd(cwd);
201
227
  const resolved = resolveTool(tool);
202
228
  if ('error' in resolved)
203
229
  throw resolved.error;
204
230
  const timeout = Math.min(Math.round(timeoutMs ?? protocol_1.DEFAULT_TOOL_TIMEOUT_MS), protocol_1.MAX_TOOL_TIMEOUT_MS);
205
231
  try {
206
- return await execTool(resolved.path, argv, timeout, protocol_1.MAX_OUTPUT_BYTES, cwd);
232
+ return await execTool(resolved.path, argv, timeout, protocol_1.MAX_OUTPUT_BYTES, runIn);
207
233
  }
208
234
  catch (err) {
209
235
  const e = err;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infosupport/huddle-cli",
3
- "version": "0.0.0-experiment-160.153.1",
3
+ "version": "0.0.0-experiment-160.154.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.",