@infosupport/huddle-cli 0.0.0-experiment-160.107.1 → 0.0.0-experiment-160.109.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/pipe/sbx-links.js +147 -0
- package/dist/pipe/serve.js +3 -0
- package/package.json +1 -1
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ── Host pipe op `sbx.links`: Settings → Folder mappings for a Docker Sandbox ──
|
|
3
|
+
// sbx mounts only the folders it is given at create, at their host path. So each
|
|
4
|
+
// sandbox gets two extra folders, ~/.huddle/sbx/links/<name>/rw and /ro, with one
|
|
5
|
+
// link per folder mapping in them (a junction on Windows, which needs no admin
|
|
6
|
+
// rights or developer mode; a symlink elsewhere). The gateway passes them to
|
|
7
|
+
// `sbx create`, ro with `:ro`, and links each one to its container path inside.
|
|
8
|
+
//
|
|
9
|
+
// Tested against sbx v0.39.0 on Windows (sbx-folder-links-poc.mjs): sbx's file
|
|
10
|
+
// share follows these links on the host side, `:ro` on the ro folder holds for
|
|
11
|
+
// what is behind them too, and a link the sandbox makes in rw itself stays inside
|
|
12
|
+
// the sandbox (it never appears on the host, so it cannot reach other folders).
|
|
13
|
+
//
|
|
14
|
+
// The op only (re)builds those folders. An empty list removes the sandbox's
|
|
15
|
+
// folder. Links in it are removed, never followed.
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.SBX_LINKS_DIR = void 0;
|
|
21
|
+
exports.removeNoFollow = removeNoFollow;
|
|
22
|
+
exports.sbxLinks = sbxLinks;
|
|
23
|
+
const fs_1 = __importDefault(require("fs"));
|
|
24
|
+
const path_1 = __importDefault(require("path"));
|
|
25
|
+
const config_1 = require("../config");
|
|
26
|
+
const protocol_1 = require("./protocol");
|
|
27
|
+
const fs_list_1 = require("./fs-list");
|
|
28
|
+
exports.SBX_LINKS_DIR = path_1.default.join(config_1.CONFIG_DIR, 'sbx', 'links');
|
|
29
|
+
// Same rules as the gateway (environments/sbx.ts): the name is a folder name here.
|
|
30
|
+
const SANDBOX_NAME_RE = /^[a-z0-9][a-z0-9-]{0,62}$/;
|
|
31
|
+
const LINK_ID_RE = /^[A-Za-z0-9_-]{1,64}$/;
|
|
32
|
+
const MAX_LINKS = 64;
|
|
33
|
+
/** Removes `p` without following any link in it: a link is removed itself. */
|
|
34
|
+
function removeNoFollow(p) {
|
|
35
|
+
let st;
|
|
36
|
+
try {
|
|
37
|
+
st = fs_1.default.lstatSync(p);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (st.isSymbolicLink()) {
|
|
43
|
+
// A junction is a directory to unlink() on some Node versions.
|
|
44
|
+
try {
|
|
45
|
+
fs_1.default.unlinkSync(p);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
fs_1.default.rmdirSync(p);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else if (st.isDirectory()) {
|
|
52
|
+
for (const e of fs_1.default.readdirSync(p))
|
|
53
|
+
removeNoFollow(path_1.default.join(p, e));
|
|
54
|
+
fs_1.default.rmdirSync(p);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
fs_1.default.unlinkSync(p);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function inside(child, parent, platform) {
|
|
61
|
+
const norm = (s) => {
|
|
62
|
+
const h = (0, fs_list_1.toHuddlePath)(s, platform);
|
|
63
|
+
return platform === 'win32' ? h.toLowerCase() : h;
|
|
64
|
+
};
|
|
65
|
+
const c = norm(child);
|
|
66
|
+
const p = norm(parent);
|
|
67
|
+
return c === p || c.startsWith(p.endsWith('/') ? p : `${p}/`);
|
|
68
|
+
}
|
|
69
|
+
function parseLinks(raw, platform) {
|
|
70
|
+
if (!Array.isArray(raw))
|
|
71
|
+
throw new protocol_1.PipeError('bad_request', 'links must be an array');
|
|
72
|
+
if (raw.length > MAX_LINKS)
|
|
73
|
+
throw new protocol_1.PipeError('bad_request', `at most ${MAX_LINKS} links`);
|
|
74
|
+
const seen = new Set();
|
|
75
|
+
return raw.map((l) => {
|
|
76
|
+
const { id, hostPath, readOnly } = (l ?? {});
|
|
77
|
+
if (typeof id !== 'string' || !LINK_ID_RE.test(id))
|
|
78
|
+
throw new protocol_1.PipeError('bad_request', 'link id must be 1-64 letters, digits, _ or -');
|
|
79
|
+
if (seen.has(id))
|
|
80
|
+
throw new protocol_1.PipeError('bad_request', `duplicate link id ${id}`);
|
|
81
|
+
seen.add(id);
|
|
82
|
+
if (typeof hostPath !== 'string' || hostPath.includes('\0') || !(0, fs_list_1.isAbsoluteHostPath)(hostPath, platform)) {
|
|
83
|
+
throw new protocol_1.PipeError('bad_request', `link ${id}: hostPath must be an absolute path`);
|
|
84
|
+
}
|
|
85
|
+
return { id, hostPath, readOnly: readOnly === true };
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/** The `sbx.links` op. `root` and `huddleHome` are only moved in tests. */
|
|
89
|
+
function sbxLinks(args, root = exports.SBX_LINKS_DIR, huddleHome = config_1.CONFIG_DIR, platform = process.platform) {
|
|
90
|
+
const { name } = args;
|
|
91
|
+
if (typeof name !== 'string' || !SANDBOX_NAME_RE.test(name))
|
|
92
|
+
throw new protocol_1.PipeError('bad_request', 'invalid sandbox name');
|
|
93
|
+
const links = parseLinks(args.links, platform);
|
|
94
|
+
// Resolved first: a mapping that is itself a link counts as where it points.
|
|
95
|
+
const usable = [];
|
|
96
|
+
const skipped = [];
|
|
97
|
+
for (const l of links) {
|
|
98
|
+
let target;
|
|
99
|
+
try {
|
|
100
|
+
target = fs_1.default.realpathSync(l.hostPath);
|
|
101
|
+
if (!fs_1.default.statSync(target).isDirectory()) {
|
|
102
|
+
skipped.push({ id: l.id, reason: 'not a folder' });
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
skipped.push({ id: l.id, reason: 'does not exist' });
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
// Same rule as every mount Huddle makes (gateway host-path.ts): never
|
|
111
|
+
// ~/.huddle or inside it, and a folder around it only read-only.
|
|
112
|
+
if (inside(target, huddleHome, platform)) {
|
|
113
|
+
skipped.push({ id: l.id, reason: "is Huddle's own folder or inside it" });
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (!l.readOnly && inside(huddleHome, target, platform)) {
|
|
117
|
+
skipped.push({ id: l.id, reason: "contains Huddle's own folder, so only read-only" });
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
usable.push({ ...l, target });
|
|
121
|
+
}
|
|
122
|
+
const boxDir = path_1.default.join(root, name);
|
|
123
|
+
try {
|
|
124
|
+
removeNoFollow(boxDir);
|
|
125
|
+
if (usable.length === 0)
|
|
126
|
+
return { rw: null, ro: null, skipped };
|
|
127
|
+
(0, protocol_1.ensurePrivateDir)(path_1.default.dirname(root));
|
|
128
|
+
(0, protocol_1.ensurePrivateDir)(root);
|
|
129
|
+
(0, protocol_1.ensurePrivateDir)(boxDir);
|
|
130
|
+
const made = { rw: null, ro: null };
|
|
131
|
+
for (const l of usable) {
|
|
132
|
+
const kind = l.readOnly ? 'ro' : 'rw';
|
|
133
|
+
const dir = path_1.default.join(boxDir, kind);
|
|
134
|
+
if (!made[kind]) {
|
|
135
|
+
fs_1.default.mkdirSync(dir, { mode: 0o700 });
|
|
136
|
+
made[kind] = (0, fs_list_1.toHuddlePath)(dir, platform);
|
|
137
|
+
}
|
|
138
|
+
fs_1.default.symlinkSync(l.target, path_1.default.join(dir, l.id), platform === 'win32' ? 'junction' : 'dir');
|
|
139
|
+
}
|
|
140
|
+
return { ...made, skipped };
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
if (err instanceof protocol_1.PipeError)
|
|
144
|
+
throw err;
|
|
145
|
+
throw new protocol_1.PipeError('internal', `could not prepare the folder links: ${err.message ?? err}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
package/dist/pipe/serve.js
CHANGED
|
@@ -19,6 +19,7 @@ const self_update_1 = require("../self-update");
|
|
|
19
19
|
const protocol_1 = require("./protocol");
|
|
20
20
|
const fs_list_1 = require("./fs-list");
|
|
21
21
|
const tools_1 = require("./tools");
|
|
22
|
+
const sbx_links_1 = require("./sbx-links");
|
|
22
23
|
// Ids are generated by the gateway (32 hex); anything that does not look like
|
|
23
24
|
// an id is not ours and is left alone rather than turned into a reply path.
|
|
24
25
|
const REQUEST_RE = /^([A-Za-z0-9_-]{1,128})\.in$/;
|
|
@@ -37,6 +38,8 @@ async function dispatch(req, maxEntries) {
|
|
|
37
38
|
return (0, fs_list_1.fsList)(args, maxEntries);
|
|
38
39
|
case 'tool.run':
|
|
39
40
|
return (0, tools_1.toolRun)(args);
|
|
41
|
+
case 'sbx.links':
|
|
42
|
+
return (0, sbx_links_1.sbxLinks)(args);
|
|
40
43
|
default:
|
|
41
44
|
throw new protocol_1.PipeError('unknown_op', `unknown op '${req.op}'`);
|
|
42
45
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@infosupport/huddle-cli",
|
|
3
|
-
"version": "0.0.0-experiment-160.
|
|
3
|
+
"version": "0.0.0-experiment-160.109.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.",
|