@commonlyai/cli 0.1.313 → 0.1.314
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/package.json +1 -1
- package/src/lib/git-readonly.js +135 -31
package/package.json
CHANGED
package/src/lib/git-readonly.js
CHANGED
|
@@ -56,8 +56,12 @@
|
|
|
56
56
|
* file each one changed. A line range (log and show `-L<range>:<file>`)
|
|
57
57
|
* takes no pathspec, so it is taken only as the first argument, in one
|
|
58
58
|
* piece, where git cannot read it as another option's value, and its file
|
|
59
|
-
* is name-checked.
|
|
60
|
-
*
|
|
59
|
+
* is name-checked. A blob has no name of its own, so `show` refuses any
|
|
60
|
+
* revision that names a blob (or a tag that peels to one) other than as
|
|
61
|
+
* `<rev>:<path>`, whose path is name-checked: git is asked each one's type
|
|
62
|
+
* before the call runs (blobNamedById). `show` also refuses the options
|
|
63
|
+
* that make git pick objects itself (SHOW_DENIED_OPTIONS), since it prints
|
|
64
|
+
* every blob they reach whatever the pathspec says.
|
|
61
65
|
*/
|
|
62
66
|
import { execFile } from 'child_process';
|
|
63
67
|
import { realpathSync } from 'fs';
|
|
@@ -83,8 +87,40 @@ export const DENIED_LONG_OPTIONS = Object.freeze({
|
|
|
83
87
|
'--show-signature': 'runs gpg to check a signature',
|
|
84
88
|
'--follow': 'needs exactly one path, and every log here also carries the .env excludes',
|
|
85
89
|
'--full-diff': 'shows every file a picked commit changed, .env files included',
|
|
90
|
+
'--end-of-options': 'makes git read every later argument as a revision, so an option-shaped one skips the checks a revision gets',
|
|
86
91
|
});
|
|
87
92
|
|
|
93
|
+
// Options refused for `show` only. Each makes git pick objects itself (every
|
|
94
|
+
// ref, every tag, the index), and `show` prints every blob it is handed in
|
|
95
|
+
// full: a pathspec doesn't filter it, so `show --indexed-objects` would print
|
|
96
|
+
// every file in the index, and `show --tags` a tag that points at a blob.
|
|
97
|
+
// log reads the same options, but prints only the commits they reach.
|
|
98
|
+
export const SHOW_DENIED_OPTIONS = Object.freeze({
|
|
99
|
+
'--all': 'shows every object the refs point at, blobs included',
|
|
100
|
+
'--branches': 'shows every object the branches point at',
|
|
101
|
+
'--tags': 'shows every object the tags point at, blobs included',
|
|
102
|
+
'--remotes': 'shows every object the remote refs point at',
|
|
103
|
+
'--glob': 'shows every object the matching refs point at, blobs included',
|
|
104
|
+
'--exclude': 'only qualifies a ref-selecting option, which show refuses',
|
|
105
|
+
'--exclude-hidden': 'only qualifies a ref-selecting option, which show refuses',
|
|
106
|
+
'--reflog': 'shows every object a reflog names',
|
|
107
|
+
'--indexed-objects': 'shows every file in the index, whatever its name',
|
|
108
|
+
'--alternate-refs': 'shows objects another repository\'s refs point at',
|
|
109
|
+
'--bisect': 'shows the objects the bisect refs point at',
|
|
110
|
+
'--stdin': 'reads revisions the name check never sees',
|
|
111
|
+
});
|
|
112
|
+
const checkShowOption = (arg) => {
|
|
113
|
+
const [name] = arg.split('=', 1);
|
|
114
|
+
if (Object.prototype.hasOwnProperty.call(SHOW_DENIED_OPTIONS, name)) {
|
|
115
|
+
return `${name} is refused for show: it ${SHOW_DENIED_OPTIONS[name]}; name the commit, tree or <rev>:<path> to show`;
|
|
116
|
+
}
|
|
117
|
+
if (name.length >= 4) {
|
|
118
|
+
const denied = Object.keys(SHOW_DENIED_OPTIONS).find((option) => option.startsWith(name));
|
|
119
|
+
if (denied) return `${name} is refused for show: git may read it as ${denied}, which ${SHOW_DENIED_OPTIONS[denied]}`;
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
};
|
|
123
|
+
|
|
88
124
|
// Real options that are also prefixes of a denied one. Git takes an exact name
|
|
89
125
|
// over an abbreviation, so these never mean the denied option.
|
|
90
126
|
const EXACT_NAMES_THAT_PREFIX_A_DENIED_ONE = new Set(['--text', '--ignore-rev']);
|
|
@@ -162,8 +198,8 @@ const checkFormat = (arg) => {
|
|
|
162
198
|
// (adapters/codex.js) also denies .npmrc and .yarnrc*. They are kept out of
|
|
163
199
|
// git's results too, since the objects git reads hold them whatever the
|
|
164
200
|
// sandbox says about the file.
|
|
165
|
-
const ENV_FILE_SEGMENT = /^\.env(\..*)
|
|
166
|
-
const OTHER_PROTECTED_SEGMENT = /^(?:\.npmrc|\.yarnrc.*|\.commonly|\.codex)
|
|
201
|
+
const ENV_FILE_SEGMENT = /^\.env(\..*)?$/i;
|
|
202
|
+
const OTHER_PROTECTED_SEGMENT = /^(?:\.npmrc|\.yarnrc.*|\.commonly|\.codex)$/i;
|
|
167
203
|
const segmentsOf = (arg) => arg.split(':').flatMap((part) => part.split('/'));
|
|
168
204
|
const namesEnvFile = (arg) => segmentsOf(arg).some((segment) => ENV_FILE_SEGMENT.test(segment));
|
|
169
205
|
const namesOtherProtected = (arg) => segmentsOf(arg).some((segment) => OTHER_PROTECTED_SEGMENT.test(segment));
|
|
@@ -175,9 +211,9 @@ const protectedNameReason = (arg) => {
|
|
|
175
211
|
return null;
|
|
176
212
|
};
|
|
177
213
|
const ENV_EXCLUDES = Object.freeze([
|
|
178
|
-
':(exclude,glob)**/.env', ':(exclude,glob)**/.env.*', ':(exclude,glob)**/.env/**', ':(exclude,glob)**/.env.*/**',
|
|
179
|
-
':(exclude,glob)**/.npmrc', ':(exclude,glob)**/.npmrc/**', ':(exclude,glob)**/.yarnrc*', ':(exclude,glob)**/.yarnrc*/**',
|
|
180
|
-
':(exclude,glob)**/.commonly', ':(exclude,glob)**/.commonly/**', ':(exclude,glob)**/.codex', ':(exclude,glob)**/.codex/**',
|
|
214
|
+
':(exclude,glob,icase)**/.env', ':(exclude,glob,icase)**/.env.*', ':(exclude,glob,icase)**/.env/**', ':(exclude,glob,icase)**/.env.*/**',
|
|
215
|
+
':(exclude,glob,icase)**/.npmrc', ':(exclude,glob,icase)**/.npmrc/**', ':(exclude,glob,icase)**/.yarnrc*', ':(exclude,glob,icase)**/.yarnrc*/**',
|
|
216
|
+
':(exclude,glob,icase)**/.commonly', ':(exclude,glob,icase)**/.commonly/**', ':(exclude,glob,icase)**/.codex', ':(exclude,glob,icase)**/.codex/**',
|
|
181
217
|
]);
|
|
182
218
|
// blame takes one literal file, so the name check above covers it. A
|
|
183
219
|
// blob-to-blob diff (`diff A:f B:f`) takes no pathspec and is refused by git
|
|
@@ -276,7 +312,9 @@ export const readonlyGitArgv = ({ subcommand, args = [], root }) => {
|
|
|
276
312
|
} else if (arg === '--') {
|
|
277
313
|
pathsOnly = true;
|
|
278
314
|
} else {
|
|
279
|
-
const reason = arg.startsWith('--')
|
|
315
|
+
const reason = arg.startsWith('--')
|
|
316
|
+
? checkLongOption(arg) || checkFormat(arg) || (subcommand === 'show' ? checkShowOption(arg) : null)
|
|
317
|
+
: checkShortOption(arg, subcommand);
|
|
280
318
|
if (reason) return refuse(reason);
|
|
281
319
|
if (arg.startsWith('-L')) {
|
|
282
320
|
const rangeReason = protectedNameReason(arg.slice(2));
|
|
@@ -320,6 +358,72 @@ export const readonlyGitEnv = ({ root, baseEnv = process.env }) => {
|
|
|
320
358
|
};
|
|
321
359
|
};
|
|
322
360
|
|
|
361
|
+
const execGit = (git, argv, { root, baseEnv, timeoutMs }) => new Promise((done) => {
|
|
362
|
+
execFile(git, argv, {
|
|
363
|
+
cwd: root,
|
|
364
|
+
env: readonlyGitEnv({ root, baseEnv }),
|
|
365
|
+
timeout: timeoutMs,
|
|
366
|
+
maxBuffer: MAX_BUFFER_BYTES,
|
|
367
|
+
encoding: 'utf8',
|
|
368
|
+
windowsHide: true,
|
|
369
|
+
}, (error, stdout, stderr) => done({ error, stdout: String(stdout || ''), stderr: String(stderr || '') }));
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
// Whether git reads `arg` as `<rev>:<path>`. git splits at the first `:`
|
|
373
|
+
// outside any `{...}` (get_oid_with_context_1 in object-name.c counts brace
|
|
374
|
+
// depth), so a `:` inside `@{...}` or `^{...}` belongs to the revision: a
|
|
375
|
+
// reflog date such as `ref@{2026-11-09 11:41:50}` names no path at all.
|
|
376
|
+
const namesAPath = (arg) => {
|
|
377
|
+
let depth = 0;
|
|
378
|
+
for (const ch of arg) {
|
|
379
|
+
if (ch === '{') depth += 1;
|
|
380
|
+
else if (ch === '}' && depth > 0) depth -= 1;
|
|
381
|
+
else if (ch === ':' && depth === 0) return true;
|
|
382
|
+
}
|
|
383
|
+
return false;
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
// The arguments `show` would read as revisions: before `--`, not an option
|
|
387
|
+
// (`--end-of-options` is refused, so a `-` argument is never a revision). One
|
|
388
|
+
// that names a path does so through `<rev>:<path>`, and readonlyGitArgv has
|
|
389
|
+
// name-checked that path; every other one is probed.
|
|
390
|
+
const unnamedRevisions = (args) => {
|
|
391
|
+
const revisions = [];
|
|
392
|
+
for (const arg of args) {
|
|
393
|
+
if (arg === '--') break;
|
|
394
|
+
if (!arg.startsWith('-') && !namesAPath(arg)) revisions.push(arg);
|
|
395
|
+
}
|
|
396
|
+
return revisions;
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* The first of `show`'s revisions that is a blob, or a tag that peels to one,
|
|
401
|
+
* or null. git prints a blob given by id whatever path it was committed at, so
|
|
402
|
+
* a blob is only shown through `<rev>:<path>`. An argument git can't resolve
|
|
403
|
+
* to one object (a range, a typo) is left to `show` itself, which shows no
|
|
404
|
+
* blob for it.
|
|
405
|
+
*/
|
|
406
|
+
export const blobNamedById = async ({
|
|
407
|
+
args, root, git = 'git', baseEnv = process.env, timeoutMs = DEFAULT_TIMEOUT_MS,
|
|
408
|
+
}) => {
|
|
409
|
+
// One budget for every check, so many arguments can't stretch the call.
|
|
410
|
+
const deadline = Date.now() + timeoutMs;
|
|
411
|
+
for (const revision of unnamedRevisions(args)) {
|
|
412
|
+
const left = deadline - Date.now();
|
|
413
|
+
if (left <= 0) return { revision, unchecked: true };
|
|
414
|
+
// A revision here never starts with `-`, so cat-file can't read it as an option.
|
|
415
|
+
const { error, stdout } = await execGit(
|
|
416
|
+
git,
|
|
417
|
+
[...TOP_LEVEL, 'cat-file', '-t', `${revision}^{}`],
|
|
418
|
+
{ root, baseEnv, timeoutMs: left },
|
|
419
|
+
);
|
|
420
|
+
// A check that was stopped proves nothing, so the call is refused.
|
|
421
|
+
if (error?.killed || error?.signal) return { revision, unchecked: true };
|
|
422
|
+
if (!error && stdout.trim() === 'blob') return { revision };
|
|
423
|
+
}
|
|
424
|
+
return null;
|
|
425
|
+
};
|
|
426
|
+
|
|
323
427
|
const truncate = (text) => (text.length <= MAX_OUTPUT_CHARS
|
|
324
428
|
? text
|
|
325
429
|
: `${text.slice(0, MAX_OUTPUT_CHARS)}\n[output truncated at ${MAX_OUTPUT_CHARS} characters; narrow it with -n, a path, or a revision range]`);
|
|
@@ -328,32 +432,32 @@ const truncate = (text) => (text.length <= MAX_OUTPUT_CHARS
|
|
|
328
432
|
* Run one read-only git call in `root`. Resolves `{ ok, text }`; never rejects.
|
|
329
433
|
* `git` is the binary to run (the adapter resolves it outside the sandbox).
|
|
330
434
|
*/
|
|
331
|
-
export const runReadonlyGit = ({
|
|
435
|
+
export const runReadonlyGit = async ({
|
|
332
436
|
subcommand, args, root, git = 'git', baseEnv = process.env, timeoutMs = DEFAULT_TIMEOUT_MS,
|
|
333
|
-
}) =>
|
|
437
|
+
}) => {
|
|
334
438
|
const checked = readonlyGitArgv({ subcommand, args, root });
|
|
335
|
-
if (!checked.ok) {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
timeout: timeoutMs,
|
|
343
|
-
maxBuffer: MAX_BUFFER_BYTES,
|
|
344
|
-
encoding: 'utf8',
|
|
345
|
-
windowsHide: true,
|
|
346
|
-
}, (error, stdout, stderr) => {
|
|
347
|
-
const out = String(stdout || '');
|
|
348
|
-
if (!error) {
|
|
349
|
-
done({ ok: true, text: truncate(out || '(no output)') });
|
|
350
|
-
return;
|
|
439
|
+
if (!checked.ok) return { ok: false, text: checked.reason };
|
|
440
|
+
if (subcommand === 'show') {
|
|
441
|
+
const blob = await blobNamedById({
|
|
442
|
+
args, root, git, baseEnv, timeoutMs,
|
|
443
|
+
});
|
|
444
|
+
if (blob?.unchecked) {
|
|
445
|
+
return { ok: false, text: `${blob.revision} is refused: git did not say in time whether it is a blob` };
|
|
351
446
|
}
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
}
|
|
447
|
+
if (blob) {
|
|
448
|
+
return {
|
|
449
|
+
ok: false,
|
|
450
|
+
text: `${blob.revision} is refused: it is a blob, and a blob is shown only as <rev>:<path>, so the file it came from can be checked`,
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
const { error, stdout: out, stderr } = await execGit(git, checked.argv, { root, baseEnv, timeoutMs });
|
|
455
|
+
if (!error) return { ok: true, text: truncate(out || '(no output)') };
|
|
456
|
+
const detail = stderr.trim() || error.message;
|
|
457
|
+
return { ok: false, text: truncate([out.trim(), detail].filter(Boolean).join('\n')) };
|
|
458
|
+
};
|
|
356
459
|
|
|
357
460
|
export default {
|
|
358
|
-
READONLY_GIT_SUBCOMMANDS, DENIED_LONG_OPTIONS, DENIED_SHORT_OPTIONS, readonlyGitArgv, readonlyGitEnv,
|
|
461
|
+
READONLY_GIT_SUBCOMMANDS, DENIED_LONG_OPTIONS, DENIED_SHORT_OPTIONS, blobNamedById, readonlyGitArgv, readonlyGitEnv,
|
|
462
|
+
runReadonlyGit,
|
|
359
463
|
};
|