bdy 1.25.39-stage → 1.25.41-dev

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.
@@ -0,0 +1,182 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.buildInjection = exports.parseVarFile = void 0;
7
+ const node_os_1 = require("node:os");
8
+ const node_path_1 = require("node:path");
9
+ const utils_1 = require("../../utils");
10
+ const texts_1 = require("../../texts");
11
+ const cliError_1 = __importDefault(require("../../cliError"));
12
+ // The API keeps these as base64 (they may be binary); the keys are text.
13
+ const BASE64_TYPES = [
14
+ utils_1.VARIABLE_TYPE.FILE,
15
+ utils_1.VARIABLE_TYPE.P12,
16
+ utils_1.VARIABLE_TYPE.IOS_KEYCHAIN,
17
+ utils_1.VARIABLE_TYPE.IOS_PROVISION_PROFILES,
18
+ ];
19
+ const MASKED = '!encrypted';
20
+ const MIN_MASKED_LINE = 8;
21
+ const MIN_MASKED_VALUE = 6;
22
+ // KEY:PATH, split on the first colon: a key has none, a Windows path may.
23
+ // `~` is expanded here - the shell leaves it alone in the middle of a word.
24
+ const parseVarFile = (spec) => {
25
+ const at = spec.indexOf(':');
26
+ const key = at < 0 ? '' : spec.slice(0, at);
27
+ let path = at < 0 ? '' : spec.slice(at + 1);
28
+ if (!key || !path) {
29
+ throw new cliError_1.default('usage_error', (0, texts_1.ERR_VARIABLE_INJECT_VAR_FILE_FORMAT)(spec));
30
+ }
31
+ if (path === '~' || path.startsWith('~/'))
32
+ path = (0, node_os_1.homedir)() + path.slice(1);
33
+ return { key, path: (0, node_path_1.resolve)(path) };
34
+ };
35
+ exports.parseVarFile = parseVarFile;
36
+ /**
37
+ * What goes into the command, from GET /variables?environment_id=: the
38
+ * environment's own variables first, then its bases nearest first, so the
39
+ * first variable of a key is the one that applies.
40
+ *
41
+ * VAR variables go into the process environment - all of them, or only the
42
+ * ones named by --var. File variables go only where --var-file puts them,
43
+ * with their password/passphrase as KEY_PASSWORD/KEY_PASSPHRASE.
44
+ *
45
+ * An encrypted value the API did not decrypt fails the whole run: injecting
46
+ * the masked form would hand the command a wrong secret without a word. The
47
+ * API does not say why it was not decrypted, so the reason is read from the
48
+ * response, in the order the API checks it: a bare marker is the missing
49
+ * manage permission, a marker with the ciphertext is the environment flag or,
50
+ * with the flag on, the token's scope.
51
+ */
52
+ const buildInjection = (variables, environment, vars, varFiles) => {
53
+ const byKey = new Map();
54
+ for (const variable of variables) {
55
+ if (!byKey.has(variable.key))
56
+ byKey.set(variable.key, variable);
57
+ }
58
+ const find = (key) => {
59
+ const variable = byKey.get(key);
60
+ if (!variable) {
61
+ throw new cliError_1.default('not_found', (0, texts_1.ERR_VARIABLE_INJECT_NOT_FOUND)(key), (0, texts_1.HINT_VARIABLE_INJECT_LIST)(environment.identifier ?? '<environment>'));
62
+ }
63
+ if (variable.disabled) {
64
+ throw new cliError_1.default('usage_error', (0, texts_1.ERR_VARIABLE_INJECT_DISABLED)(key));
65
+ }
66
+ return variable;
67
+ };
68
+ let plain;
69
+ if (vars.length) {
70
+ plain = [...new Set(vars)].map((key) => {
71
+ const variable = find(key);
72
+ const type = variable.type ?? utils_1.VARIABLE_TYPE.VAR;
73
+ if (type !== utils_1.VARIABLE_TYPE.VAR) {
74
+ throw new cliError_1.default('usage_error', (0, texts_1.ERR_VARIABLE_INJECT_NOT_VAR)(key, type));
75
+ }
76
+ return variable;
77
+ });
78
+ }
79
+ else {
80
+ plain = [...byKey.values()].filter((v) => (v.type ?? utils_1.VARIABLE_TYPE.VAR) === utils_1.VARIABLE_TYPE.VAR && !v.disabled);
81
+ }
82
+ const keys = new Set();
83
+ const paths = new Set();
84
+ const fileVars = varFiles.map(({ key, path }) => {
85
+ if (keys.has(key)) {
86
+ throw new cliError_1.default('usage_error', (0, texts_1.ERR_VARIABLE_INJECT_VAR_FILE_DUPLICATE_KEY)(key));
87
+ }
88
+ if (paths.has(path)) {
89
+ throw new cliError_1.default('usage_error', (0, texts_1.ERR_VARIABLE_INJECT_VAR_FILE_DUPLICATE_PATH)(path));
90
+ }
91
+ keys.add(key);
92
+ paths.add(path);
93
+ const variable = find(key);
94
+ if ((variable.type ?? utils_1.VARIABLE_TYPE.VAR) === utils_1.VARIABLE_TYPE.VAR) {
95
+ throw new cliError_1.default('usage_error', (0, texts_1.ERR_VARIABLE_INJECT_NOT_FILE)(key));
96
+ }
97
+ return { variable, path };
98
+ });
99
+ // every secret field that reaches the command, checked before anything runs
100
+ const masked = [];
101
+ const check = (key, value, secret) => {
102
+ if (secret && typeof value === 'string' && value.startsWith(MASKED)) {
103
+ masked.push({ key, value });
104
+ }
105
+ };
106
+ for (const v of plain)
107
+ check(v.key, v.value, !!v.encrypted);
108
+ for (const { variable: v } of fileVars) {
109
+ check(v.key, v.value, !!v.encrypted);
110
+ check(v.key, v.password, true);
111
+ check(v.key, v.passphrase, true);
112
+ }
113
+ if (masked.length) {
114
+ const failing = [...new Set(masked.map((m) => m.key))];
115
+ if (masked.some((m) => m.value === MASKED)) {
116
+ const noAccess = [
117
+ ...new Set(masked.filter((m) => m.value === MASKED).map((m) => m.key)),
118
+ ];
119
+ throw new cliError_1.default('forbidden', (0, texts_1.ERR_VARIABLE_INJECT_NO_ACCESS)(noAccess), texts_1.HINT_VARIABLE_INJECT_NO_ACCESS);
120
+ }
121
+ if (!environment.allow_decrypted_values) {
122
+ throw new cliError_1.default('forbidden', (0, texts_1.ERR_VARIABLE_INJECT_NOT_ALLOWED)(failing), texts_1.HINT_VARIABLE_INJECT_NOT_ALLOWED);
123
+ }
124
+ throw new cliError_1.default('forbidden', (0, texts_1.ERR_VARIABLE_INJECT_NO_SCOPE)(failing), texts_1.HINT_VARIABLE_INJECT_NO_SCOPE);
125
+ }
126
+ const injection = { env: {}, files: [], secrets: [], unmasked: [] };
127
+ // without the line ending a key or a file ends with, so printing one does
128
+ // not swallow the newline after it
129
+ const addSecret = (key, value) => {
130
+ let bytes = Buffer.isBuffer(value) ? value : Buffer.from(value);
131
+ let end = bytes.length;
132
+ while (end && (bytes[end - 1] === 0x0a || bytes[end - 1] === 0x0d))
133
+ end--;
134
+ bytes = bytes.subarray(0, end);
135
+ if (!bytes.length)
136
+ return;
137
+ if (bytes.length < MIN_MASKED_VALUE) {
138
+ if (!injection.unmasked.includes(key))
139
+ injection.unmasked.push(key);
140
+ return;
141
+ }
142
+ injection.secrets.push(bytes);
143
+ // a key or a file printed in part (`head -1`, a tool quoting one line of
144
+ // it) is masked line by line too; a short line is no secret on its own
145
+ // and masking it would garble unrelated output
146
+ if (bytes.includes(0x0a)) {
147
+ for (const line of bytes.toString('latin1').split(/\r?\n/)) {
148
+ if (line.length >= MIN_MASKED_LINE)
149
+ injection.secrets.push(Buffer.from(line, 'latin1'));
150
+ }
151
+ }
152
+ };
153
+ for (const v of plain) {
154
+ const value = v.value ?? '';
155
+ injection.env[v.key] = value;
156
+ if (v.encrypted)
157
+ addSecret(v.key, value);
158
+ }
159
+ const extras = [];
160
+ for (const { variable: v, path } of fileVars) {
161
+ const value = v.value ?? '';
162
+ const content = BASE64_TYPES.includes(v.type)
163
+ ? Buffer.from(value, 'base64')
164
+ : Buffer.from(value);
165
+ injection.files.push({ path, content });
166
+ if (v.encrypted)
167
+ addSecret(v.key, content);
168
+ if (v.password)
169
+ extras.push({ name: `${v.key}_PASSWORD`, key: v.key, value: v.password });
170
+ if (v.passphrase)
171
+ extras.push({ name: `${v.key}_PASSPHRASE`, key: v.key, value: v.passphrase });
172
+ }
173
+ for (const { name, key, value } of extras) {
174
+ if (name in injection.env) {
175
+ throw new cliError_1.default('usage_error', (0, texts_1.ERR_VARIABLE_INJECT_NAME_CONFLICT)(name, key));
176
+ }
177
+ injection.env[name] = value;
178
+ addSecret(name, value);
179
+ }
180
+ return injection;
181
+ };
182
+ exports.buildInjection = buildInjection;
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const node_stream_1 = require("node:stream");
4
+ const MASK = Buffer.from('***');
5
+ /**
6
+ * Replaces the secrets in a byte stream with ***. Works on bytes, not text,
7
+ * so binary output passes through intact and a multi-byte character split
8
+ * between chunks is not a concern.
9
+ *
10
+ * A secret can arrive split between two writes, so a tail of a chunk that is
11
+ * the start of a secret is held back until the next chunk shows whether it
12
+ * is one. Only such a tail is held - anything else goes out at once - and
13
+ * only for flushDelay: a prompt that happens to end like the start of a
14
+ * secret must not hang. A secret written in two parts further apart than
15
+ * that is not masked; programs print a value in one write.
16
+ */
17
+ class SecretMask extends node_stream_1.Transform {
18
+ flushDelay;
19
+ secrets;
20
+ longest;
21
+ pending = Buffer.alloc(0);
22
+ timer = null;
23
+ // bytes taken in so far, for telling a stream that still flows from one
24
+ // that went quiet
25
+ bytesIn = 0;
26
+ constructor(secrets, flushDelay = 50) {
27
+ super();
28
+ this.flushDelay = flushDelay;
29
+ // longest first, so a secret containing another is masked whole
30
+ this.secrets = secrets
31
+ .filter((s) => s.length)
32
+ .sort((a, b) => b.length - a.length);
33
+ this.longest = this.secrets[0]?.length ?? 0;
34
+ }
35
+ _transform(chunk, _encoding, callback) {
36
+ this.clearTimer();
37
+ this.bytesIn += chunk.length;
38
+ const data = this.pending.length ? Buffer.concat([this.pending, chunk]) : chunk;
39
+ const out = [];
40
+ let i = 0;
41
+ for (;;) {
42
+ let at = -1;
43
+ let match = null;
44
+ for (const secret of this.secrets) {
45
+ const index = data.indexOf(secret, i);
46
+ if (index >= 0 && (at < 0 || index < at)) {
47
+ at = index;
48
+ match = secret;
49
+ }
50
+ }
51
+ if (!match)
52
+ break;
53
+ out.push(data.subarray(i, at), MASK);
54
+ i = at + match.length;
55
+ }
56
+ const hold = this.heldTail(data, i);
57
+ out.push(data.subarray(i, hold));
58
+ this.pending = Buffer.from(data.subarray(hold));
59
+ const ready = Buffer.concat(out);
60
+ if (ready.length)
61
+ this.push(ready);
62
+ if (this.pending.length) {
63
+ this.timer = setTimeout(() => this.release(), this.flushDelay);
64
+ }
65
+ callback();
66
+ }
67
+ _flush(callback) {
68
+ this.release();
69
+ callback();
70
+ }
71
+ _destroy(err, callback) {
72
+ this.clearTimer();
73
+ callback(err);
74
+ }
75
+ // Earliest position from which the rest of data starts some secret.
76
+ heldTail(data, from) {
77
+ const start = Math.max(from, data.length - this.longest + 1);
78
+ for (let p = start; p < data.length; p++) {
79
+ const rest = data.subarray(p);
80
+ if (this.secrets.some((s) => s.length > rest.length && s.subarray(0, rest.length).equals(rest))) {
81
+ return p;
82
+ }
83
+ }
84
+ return data.length;
85
+ }
86
+ release() {
87
+ this.clearTimer();
88
+ if (this.pending.length) {
89
+ const pending = this.pending;
90
+ this.pending = Buffer.alloc(0);
91
+ this.push(pending);
92
+ }
93
+ }
94
+ clearTimer() {
95
+ if (this.timer)
96
+ clearTimeout(this.timer);
97
+ this.timer = null;
98
+ }
99
+ }
100
+ exports.default = SecretMask;
@@ -10,6 +10,7 @@ const get_1 = __importDefault(require("./variable/get"));
10
10
  const create_1 = __importDefault(require("./variable/create"));
11
11
  const update_1 = __importDefault(require("./variable/update"));
12
12
  const delete_1 = __importDefault(require("./variable/delete"));
13
+ const inject_1 = __importDefault(require("./variable/inject"));
13
14
  const commandVariable = (0, utils_1.newCommand)('variable', texts_1.DESC_COMMAND_VARIABLE);
14
15
  commandVariable.alias('var');
15
16
  commandVariable.addCommand(list_1.default);
@@ -17,4 +18,5 @@ commandVariable.addCommand(get_1.default);
17
18
  commandVariable.addCommand(create_1.default);
18
19
  commandVariable.addCommand(update_1.default);
19
20
  commandVariable.addCommand(delete_1.default);
21
+ commandVariable.addCommand(inject_1.default);
20
22
  exports.default = commandVariable;
@@ -750,7 +750,31 @@ class Input {
750
750
  });
751
751
  return list;
752
752
  }
753
- static async routeTarget(client, workspace, project, targetList) {
753
+ // `line` is where the route's distribution lives: an environment (its
754
+ // identifier, with the project of a project environment), a project, or the
755
+ // workspace (both empty). Artifacts and sandboxes are looked up along it,
756
+ // closest scope first - for an environment: the environment, its project,
757
+ // the workspace, as the backend accepts them.
758
+ static async routeTarget(client, workspace, line, targetList) {
759
+ const { project, environment } = line;
760
+ const lookupLines = [];
761
+ if (environment) {
762
+ lookupLines.push({ project: project || undefined, environment });
763
+ }
764
+ if (project)
765
+ lookupLines.push({ project });
766
+ const lookup = async (params, found) => {
767
+ let result = null;
768
+ for (const l of [...lookupLines, {}]) {
769
+ result = await client.getResourceByIdentifier(workspace, {
770
+ ...l,
771
+ ...params,
772
+ });
773
+ if (!result.domain || result[found])
774
+ break;
775
+ }
776
+ return result;
777
+ };
754
778
  if (!targetList || !Array.isArray(targetList) || !targetList.length) {
755
779
  output_1.default.exitError(texts_1.ERR_COMMAND_ROUTE_NO_TARGET);
756
780
  }
@@ -840,11 +864,7 @@ class Input {
840
864
  identifier = artifact;
841
865
  version = utils_1.ARTIFACT_DEFAULT_VERSION;
842
866
  }
843
- let result = await client.getArtifactVersionByIdentifier(workspace, project, identifier, version);
844
- if (!result.artifact_id && project) {
845
- // try in workspace scope
846
- result = await client.getArtifactVersionByIdentifier(workspace, null, identifier, version);
847
- }
867
+ const result = await lookup({ artifact: identifier, artifact_version: version }, 'artifact_id');
848
868
  if (!result.domain) {
849
869
  output_1.default.exitError((0, texts_1.ERR_WRONG_ROUTE_TARGET)(str, texts_1.ERR_WORKSPACE_NOT_FOUND));
850
870
  }
@@ -872,10 +892,13 @@ class Input {
872
892
  if (!identifier || !endpoint) {
873
893
  output_1.default.exitError((0, texts_1.ERR_WRONG_ROUTE_TARGET)(str, 'sandbox wrong format (identifier:endpoint)'));
874
894
  }
875
- if (!project) {
876
- output_1.default.exitError((0, texts_1.ERR_WRONG_ROUTE_TARGET)(str, 'sandbox target can be used only in project'));
895
+ if (!project && !environment) {
896
+ output_1.default.exitError((0, texts_1.ERR_WRONG_ROUTE_TARGET)(str, 'sandbox target can be used only in project or environment'));
877
897
  }
878
- const result = await client.getSandboxByIdentifier(workspace, project, identifier);
898
+ // a project distribution routes to its project's sandboxes only
899
+ const result = environment
900
+ ? await lookup({ sandbox: identifier }, 'sandbox_id')
901
+ : await client.getSandboxByIdentifier(workspace, project, identifier);
879
902
  if (!result.domain) {
880
903
  output_1.default.exitError((0, texts_1.ERR_WRONG_ROUTE_TARGET)(str, texts_1.ERR_WORKSPACE_NOT_FOUND));
881
904
  }
@@ -1356,7 +1379,7 @@ class Input {
1356
1379
  output_1.default.exitError(texts_1.ERR_COMMAND_SCOPE_NO_ENVIRONMENT);
1357
1380
  return scope;
1358
1381
  }
1359
- return output_1.default.exitError(texts_1.ERR_COMMAND_SCOPE);
1382
+ return output_1.default.exitError(allowEnv ? texts_1.ERR_COMMAND_SCOPE_WITH_ENVIRONMENT : texts_1.ERR_COMMAND_SCOPE);
1360
1383
  }
1361
1384
  if (allowEnv && environment)
1362
1385
  return distro_1.SCOPE.ENVIRONMENT;