talentos 1.4.7 → 1.4.9
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/agent/adapters.js +72 -6
- package/dist/agent/codex-policy.js +8 -2
- package/dist/agent/codex-probe.js +46 -8
- package/dist/agent/native/config.js +15 -0
- package/dist/agent/native/disqualify.js +43 -3
- package/dist/agent/native/gate.js +78 -8
- package/dist/agent/native/native-issue.js +4 -0
- package/dist/agent/native/paths.js +11 -5
- package/dist/agent/native/policy.js +451 -47
- package/dist/agent/room-page.js +254 -0
- package/dist/agent/room-web-mcp.js +59 -0
- package/dist/agent/serve.js +15 -3
- package/dist/agent/web-guard.js +99 -0
- package/dist/commands/agent-native.js +6 -1
- package/dist/commands/agent-serve.js +2 -1
- package/dist/commands/agent.js +23 -2
- package/dist/gateway-install.js +7 -2
- package/package.json +2 -2
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, realpathSync, statSync, } from "node:fs";
|
|
2
|
+
import { basename, dirname, join, sep } from "node:path";
|
|
1
3
|
import { ROOM_LOCAL_TOOLS, ROOM_TOOLS } from "../codex-policy.js";
|
|
2
|
-
import { canonical, isInside, protectedPaths, protectedReason, reachesProtected, } from "./paths.js";
|
|
4
|
+
import { canonical, inGitDir, isInside, protectedPaths, protectedReason, reachesProtected, WRITE_PROTECTED_SEGMENTS, } from "./paths.js";
|
|
3
5
|
import { parseShell } from "./shell.js";
|
|
4
6
|
const NON_ALNUM = /[^A-Za-z\d]+/u;
|
|
5
7
|
const GREP_R_DIFF = /^-\w*r|^--recursive/u;
|
|
@@ -498,7 +500,7 @@ const GIT_GLOBAL_OK = new Set([
|
|
|
498
500
|
"--no-pager",
|
|
499
501
|
"-P",
|
|
500
502
|
]);
|
|
501
|
-
const GIT_DIR_FLAG =
|
|
503
|
+
const GIT_DIR_FLAG = /^(--(?:git-dir|work-tree))=(.+)$/u;
|
|
502
504
|
// `git [globals] <sub> ...`: only the globals above are understood.
|
|
503
505
|
function splitGit(args) {
|
|
504
506
|
const dirs = [];
|
|
@@ -510,14 +512,17 @@ function splitGit(args) {
|
|
|
510
512
|
if (GIT_GLOBAL_OK.has(word.text)) {
|
|
511
513
|
index += 1;
|
|
512
514
|
}
|
|
513
|
-
else if (attached?.[1]) {
|
|
514
|
-
dirs.push({
|
|
515
|
+
else if (attached?.[1] && attached[2]) {
|
|
516
|
+
dirs.push({
|
|
517
|
+
flag: attached[1],
|
|
518
|
+
word: { glob: word.glob, text: attached[2] },
|
|
519
|
+
});
|
|
515
520
|
index += 1;
|
|
516
521
|
}
|
|
517
522
|
else if (["-C", "--git-dir", "--work-tree"].includes(word.text) &&
|
|
518
523
|
next &&
|
|
519
524
|
!isFlag(next)) {
|
|
520
|
-
dirs.push({
|
|
525
|
+
dirs.push({ flag: word.text, word: next });
|
|
521
526
|
index += 2;
|
|
522
527
|
}
|
|
523
528
|
else {
|
|
@@ -569,15 +574,18 @@ function classifyGit(args, ctx) {
|
|
|
569
574
|
// `-C dir` and friends read that path: the same check as `cat dir/x`. Later
|
|
570
575
|
// paths resolve against the directory `-C` moved into.
|
|
571
576
|
let local = ctx;
|
|
572
|
-
for (const {
|
|
577
|
+
for (const { flag, word } of git.dirs) {
|
|
573
578
|
const refused = pathCheck(word, local, false);
|
|
574
579
|
if (refused) {
|
|
575
580
|
return refused;
|
|
576
581
|
}
|
|
577
|
-
if (
|
|
582
|
+
if (flag === "-C") {
|
|
578
583
|
local = { ...local, cwd: canonical(word.text, local.cwd, local.home) };
|
|
579
584
|
}
|
|
580
585
|
}
|
|
586
|
+
if (foreignRepo(gitRepoOf(git, local.cwd, ctx.home), ctx)) {
|
|
587
|
+
return deny(2);
|
|
588
|
+
}
|
|
581
589
|
for (const word of rest.filter((word) => !isFlag(word))) {
|
|
582
590
|
const refused = pathCheck(word, local, false);
|
|
583
591
|
if (refused) {
|
|
@@ -602,30 +610,332 @@ const GIT_TIER2 = new Set([
|
|
|
602
610
|
"switch",
|
|
603
611
|
]);
|
|
604
612
|
const PROTECTED_BRANCH = /^(?:main|master|trunk|develop|dev|release|production|prod|stable)$|^release[/-]/u;
|
|
605
|
-
|
|
613
|
+
// Auto answers tier 2 on its own (#12926), so each tier 2 form must be one that
|
|
614
|
+
// keeps work: a push of talentos/ branches to origin, a checkout or switch that
|
|
615
|
+
// leaves uncommitted changes alone. A force, a URL or other remote, tags, mirror,
|
|
616
|
+
// delete, a second refspec onto main, or a discard is tier 1.
|
|
617
|
+
const PUSH_SAFE_FLAG = /^(?:-[quv]+|--set-upstream|--quiet|--verbose|--progress|--no-progress)$/u;
|
|
606
618
|
const HEADS_PREFIX = /^refs\/heads\//u;
|
|
619
|
+
// Refspec and pathspec wildcards (a backslash too); `@{1}` is a plain revision.
|
|
620
|
+
const GIT_GLOB = /[*?[\\]/u;
|
|
621
|
+
const CHECKOUT_SAFE_FLAG = /^(?:-b|-t|--track|--no-track|-q|--quiet|--detach)$/u;
|
|
622
|
+
// `--create` asks like `-c`, which GIT_BAD_FLAG already catches.
|
|
623
|
+
const SWITCH_SAFE_FLAG = /^(?:-d|--detach|-t|--track|--no-track|-q|--quiet|--guess|--no-guess)$/u;
|
|
624
|
+
// Clone options that set config or hooks, or run a program.
|
|
625
|
+
const CLONE_BAD_FLAG = /^(?:-\w*[cu]|--config|--template|--upload-pack|--separate-git-dir)/u;
|
|
626
|
+
const GIT_DIR_PARTS = ["HEAD", "objects", "refs"];
|
|
627
|
+
// Git work that touches only `.git` and the network, never the folders around it.
|
|
628
|
+
const TRAILING_SLASHES = /[/\\]+$/u;
|
|
629
|
+
const URL_PATH_SEPARATOR = /[/:]/u;
|
|
630
|
+
const GIT_SUFFIX = /\.git$/u;
|
|
631
|
+
const GIT_KEEPS_TREE = new Set(["add", "commit", "fetch", "push"]);
|
|
632
|
+
const RESTORE_SAFE_FLAG = /^(?:-S|--staged|-q|--quiet|--)$/u;
|
|
633
|
+
const STASH_KEEPS = new Set([
|
|
634
|
+
"apply",
|
|
635
|
+
"branch",
|
|
636
|
+
"create",
|
|
637
|
+
"list",
|
|
638
|
+
"pop",
|
|
639
|
+
"push",
|
|
640
|
+
"save",
|
|
641
|
+
"show",
|
|
642
|
+
"store",
|
|
643
|
+
]);
|
|
607
644
|
const FILE_UTILS = new Set(["cp", "mkdir", "mv", "touch"]);
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
return 1;
|
|
645
|
+
const TARGET_DIR_FLAG = /^-\w*[tT]|^--(?:no-)?target-directory/u;
|
|
646
|
+
function pushesToThread(refspec) {
|
|
647
|
+
if (refspec.glob || GIT_GLOB.test(refspec.text)) {
|
|
648
|
+
return false;
|
|
613
649
|
}
|
|
614
|
-
const
|
|
615
|
-
.filter((word) => !isFlag(word))
|
|
616
|
-
.map((word) => word.text);
|
|
617
|
-
const refspec = positional[1];
|
|
618
|
-
if (!refspec || refspec.startsWith("+") || refspec.startsWith(":")) {
|
|
619
|
-
return 1;
|
|
620
|
-
}
|
|
621
|
-
const target = refspec.includes(":")
|
|
622
|
-
? (refspec.split(":")[1] ?? "")
|
|
623
|
-
: refspec;
|
|
650
|
+
const [source = "", target = source, ...extra] = refspec.text.split(":");
|
|
624
651
|
const branch = target.replace(HEADS_PREFIX, "");
|
|
625
|
-
return
|
|
652
|
+
return (source !== "" &&
|
|
653
|
+
!source.startsWith("+") &&
|
|
654
|
+
extra.length === 0 &&
|
|
655
|
+
branch.startsWith("talentos/") &&
|
|
656
|
+
!PROTECTED_BRANCH.test(branch));
|
|
657
|
+
}
|
|
658
|
+
function gitPushTier(rest) {
|
|
659
|
+
const [remote, ...refspecs] = rest.filter((word) => !isFlag(word));
|
|
660
|
+
// Only `origin` is the owner's own remote; a URL, a path or another name
|
|
661
|
+
// could be anyone's.
|
|
662
|
+
return rest.filter(isFlag).every((flag) => PUSH_SAFE_FLAG.test(flag.text)) &&
|
|
663
|
+
remote?.text === "origin" &&
|
|
664
|
+
refspecs.length > 0 &&
|
|
665
|
+
refspecs.every(pushesToThread)
|
|
626
666
|
? 2
|
|
627
667
|
: 1;
|
|
628
668
|
}
|
|
669
|
+
// `git checkout src/a.ts` and `git checkout talentos/a` read the same; a word
|
|
670
|
+
// that names a file, a pathspec or a glob is the discard form.
|
|
671
|
+
function namesPath(word, cwd, home) {
|
|
672
|
+
return (word.glob ||
|
|
673
|
+
GIT_GLOB.test(word.text) ||
|
|
674
|
+
word.text.startsWith(":") ||
|
|
675
|
+
existsSync(canonical(word.text, cwd, home)));
|
|
676
|
+
}
|
|
677
|
+
function gitKeepsWork(sub, rest, cwd, home) {
|
|
678
|
+
const flags = rest.filter(isFlag).map((word) => word.text);
|
|
679
|
+
switch (sub) {
|
|
680
|
+
case "checkout":
|
|
681
|
+
return rest.every((word) => isFlag(word)
|
|
682
|
+
? CHECKOUT_SAFE_FLAG.test(word.text)
|
|
683
|
+
: !namesPath(word, cwd, home));
|
|
684
|
+
case "switch":
|
|
685
|
+
return flags.every((flag) => SWITCH_SAFE_FLAG.test(flag));
|
|
686
|
+
case "clone":
|
|
687
|
+
return !flags.some((flag) => CLONE_BAD_FLAG.test(flag));
|
|
688
|
+
case "restore":
|
|
689
|
+
// Without --staged, restore overwrites the working tree.
|
|
690
|
+
return (flags.every((flag) => RESTORE_SAFE_FLAG.test(flag)) &&
|
|
691
|
+
flags.some((flag) => flag === "-S" || flag === "--staged"));
|
|
692
|
+
case "stash": {
|
|
693
|
+
// A leading flag is the implicit `push`; drop and clear lose entries.
|
|
694
|
+
const [first] = rest;
|
|
695
|
+
return !first || isFlag(first) || STASH_KEEPS.has(first.text);
|
|
696
|
+
}
|
|
697
|
+
default:
|
|
698
|
+
return true;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
// The repository git finds from `dir`: the nearest `.git` file or folder, or a
|
|
702
|
+
// folder that is itself a git directory (git opens it as a bare repository).
|
|
703
|
+
function gitRepoAt(dir) {
|
|
704
|
+
for (let at = dir;; at = dirname(at)) {
|
|
705
|
+
if (existsSync(join(at, ".git"))) {
|
|
706
|
+
return join(at, ".git");
|
|
707
|
+
}
|
|
708
|
+
if (GIT_DIR_PARTS.every((part) => existsSync(join(at, part)))) {
|
|
709
|
+
return at;
|
|
710
|
+
}
|
|
711
|
+
if (dirname(at) === at) {
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
// Where git runs: each `-C` resolves from the one before.
|
|
717
|
+
function gitCwd(git, cwd, home) {
|
|
718
|
+
return git.dirs.reduce((dir, { flag, word }) => flag === "-C" ? canonical(word.text, dir, home) : dir, cwd);
|
|
719
|
+
}
|
|
720
|
+
// Every folder a clone could create: each positional word (a flag's value
|
|
721
|
+
// reads as one too) and the folder git names after the URL.
|
|
722
|
+
function cloneTargets(git, ctx) {
|
|
723
|
+
const words = git.rest.filter((word) => !isFlag(word));
|
|
724
|
+
const named = words[0]?.text
|
|
725
|
+
.replace(TRAILING_SLASHES, "")
|
|
726
|
+
.split(URL_PATH_SEPARATOR)
|
|
727
|
+
.pop()
|
|
728
|
+
?.replace(GIT_SUFFIX, "");
|
|
729
|
+
const cwd = gitCwd(git, ctx.cwd, ctx.home);
|
|
730
|
+
return [...words, ...(named ? [{ glob: false, text: named }] : [])].map((word) => ({ ...word, text: canonical(word.text, cwd, ctx.home) }));
|
|
731
|
+
}
|
|
732
|
+
// A clone writes its folder like `cp -r`: the same protected targets.
|
|
733
|
+
function cloneIntoProtected(args, ctx) {
|
|
734
|
+
const git = splitGit(args);
|
|
735
|
+
return (git.sub?.text === "clone" &&
|
|
736
|
+
cloneTargets(git, ctx).some(({ glob, text }) => !glob &&
|
|
737
|
+
(writeProtected(text, ctx) ||
|
|
738
|
+
gitControlFile(text, ctx) ||
|
|
739
|
+
inGitDir(text))));
|
|
740
|
+
}
|
|
741
|
+
function gitRepoOf(git, cwd, home) {
|
|
742
|
+
const gitDir = git.dirs.filter(({ flag }) => flag === "--git-dir").pop();
|
|
743
|
+
return gitDir ? canonical(gitDir.word.text, cwd, home) : gitRepoAt(cwd);
|
|
744
|
+
}
|
|
745
|
+
// A repository inside the workspace that is not the workspace's own: one the
|
|
746
|
+
// model could have built, cloned or pointed at, so its config and hooks pick the
|
|
747
|
+
// programs git runs and the URL a push reaches (#12926).
|
|
748
|
+
function foreignRepo(repo, ctx) {
|
|
749
|
+
return (repo !== undefined &&
|
|
750
|
+
ctx.writeRoots.some((root) => isInside(repo, root)) &&
|
|
751
|
+
!ctx.writeRoots.some((root) => gitRepoAt(root) === repo));
|
|
752
|
+
}
|
|
753
|
+
// Files that pick what `git commit` runs (#13110): hook managers' configs, the
|
|
754
|
+
// `package.json` keys they read, and the repo's `core.hooksPath`. A change to
|
|
755
|
+
// one asks every time, so a later commit cannot run model-written hooks with
|
|
756
|
+
// no card. `.husky` and `.githooks` are tier 0 already.
|
|
757
|
+
const HOOK_CONFIG_NAME = /^(?:\.?lefthook(?:-local)?\.(?:ya?ml|json|jsonc|toml)|\.pre-commit-config\.ya?ml|\.lintstagedrc(?:\..+)?|lint-staged\.config\..+|\.?simple-git-hooks(?:\..+)?|\.huskyrc(?:\..+)?|husky\.config\..+|\.overcommit\.ya?ml|\.commitlintrc(?:\..+)?|commitlint\.config\..+)$/iu;
|
|
758
|
+
// A hook manager's config in `package.json` is an object or a list; a version
|
|
759
|
+
// string is only a dependency.
|
|
760
|
+
const PACKAGE_HOOK_KEY = /"(?:husky|simple-git-hooks|lint-staged|pre-commit|commitlint|ghooks|git-hooks)"\s*:\s*[[{]/u;
|
|
761
|
+
// lefthook's script folders; matched without case, like HOOK_CONFIG_NAME.
|
|
762
|
+
const HOOK_DIR_NAME = /^\.lefthook(?:-local)?$/iu;
|
|
763
|
+
const HOOKS_PATH = /^\s*hookspath\s*=\s*"?([^"\n]+?)"?\s*$/gimu;
|
|
764
|
+
const CONFIG_SECTION = /^\s*\[([^\]]+)\]/u;
|
|
765
|
+
const INCLUDE_SECTION = /^include(?:if\s+".*")?$/iu;
|
|
766
|
+
const INCLUDE_PATH = /^\s*path\s*=\s*"?([^"\n]+?)"?\s*$/iu;
|
|
767
|
+
// Git stops following includes at this depth too.
|
|
768
|
+
const MAX_INCLUDE_DEPTH = 10;
|
|
769
|
+
const GITDIR_LINE = /^gitdir:\s*(.+?)\s*$/mu;
|
|
770
|
+
function readText(path) {
|
|
771
|
+
try {
|
|
772
|
+
return readFileSync(path, "utf8");
|
|
773
|
+
}
|
|
774
|
+
catch {
|
|
775
|
+
return "";
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
function listDir(path) {
|
|
779
|
+
try {
|
|
780
|
+
return readdirSync(path);
|
|
781
|
+
}
|
|
782
|
+
catch {
|
|
783
|
+
return [];
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
// A git config file with every `[include]`/`[includeIf]` file it names
|
|
787
|
+
// appended (each condition counts as met). Paths resolve from the file's folder.
|
|
788
|
+
function configText(path, home, depth = 0) {
|
|
789
|
+
const text = readText(path);
|
|
790
|
+
if (depth >= MAX_INCLUDE_DEPTH) {
|
|
791
|
+
return text;
|
|
792
|
+
}
|
|
793
|
+
const parts = [text];
|
|
794
|
+
let including = false;
|
|
795
|
+
for (const line of text.split("\n")) {
|
|
796
|
+
const section = CONFIG_SECTION.exec(line)?.[1];
|
|
797
|
+
if (section !== undefined) {
|
|
798
|
+
including = INCLUDE_SECTION.test(section.trim());
|
|
799
|
+
continue;
|
|
800
|
+
}
|
|
801
|
+
const included = including ? INCLUDE_PATH.exec(line)?.[1] : undefined;
|
|
802
|
+
if (included) {
|
|
803
|
+
parts.push(configText(canonical(included, dirname(path), home), home, depth + 1));
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
return parts.join("\n");
|
|
807
|
+
}
|
|
808
|
+
function realTarget(path) {
|
|
809
|
+
try {
|
|
810
|
+
return realpathSync.native(path);
|
|
811
|
+
}
|
|
812
|
+
catch {
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
// What a hook config or folder at the top of `dir` resolves to: when it is a
|
|
817
|
+
// link, its target is what the hook manager reads.
|
|
818
|
+
function linkedHookTargets(dir) {
|
|
819
|
+
return [dir, join(dir, ".config")].flatMap((folder) => listDir(folder)
|
|
820
|
+
.filter((name) => HOOK_CONFIG_NAME.test(name) || HOOK_DIR_NAME.test(name))
|
|
821
|
+
.flatMap((name) => realTarget(join(folder, name)) ?? []));
|
|
822
|
+
}
|
|
823
|
+
// Each write root and the top of its repository: where the provider, hook and
|
|
824
|
+
// hook-manager folders sit.
|
|
825
|
+
function workspaceTops(ctx) {
|
|
826
|
+
const tops = new Set();
|
|
827
|
+
for (const root of ctx.writeRoots) {
|
|
828
|
+
tops.add(root);
|
|
829
|
+
const repo = gitRepoAt(root);
|
|
830
|
+
if (repo && basename(repo) === ".git") {
|
|
831
|
+
tops.add(dirname(repo));
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
return [...tops];
|
|
835
|
+
}
|
|
836
|
+
// The tier 0 write rule, also through a link: `.husky -> tools/husky` makes
|
|
837
|
+
// `tools/husky` as protected as `.husky` (#13118).
|
|
838
|
+
function writeProtected(real, ctx) {
|
|
839
|
+
return (Boolean(protectedReason(real, "write", ctx.home, ctx.guard)) ||
|
|
840
|
+
workspaceTops(ctx).some((top) => WRITE_PROTECTED_SEGMENTS.some((name) => {
|
|
841
|
+
const target = realTarget(join(top, name));
|
|
842
|
+
return target !== undefined && isInside(real, target);
|
|
843
|
+
})));
|
|
844
|
+
}
|
|
845
|
+
// What a commit in each write root runs: `core.hooksPath` from its repository's
|
|
846
|
+
// config (a worktree reads the main repository's) or the owner's global config,
|
|
847
|
+
// and any hook installed in the repository's `hooks` folder.
|
|
848
|
+
function hookSetup(ctx) {
|
|
849
|
+
const setup = { dirs: [], installed: false, linked: [] };
|
|
850
|
+
for (const root of ctx.writeRoots) {
|
|
851
|
+
const repo = gitRepoAt(root);
|
|
852
|
+
if (!repo) {
|
|
853
|
+
continue;
|
|
854
|
+
}
|
|
855
|
+
const top = basename(repo) === ".git" ? dirname(repo) : root;
|
|
856
|
+
const pointer = GITDIR_LINE.exec(readText(repo))?.[1];
|
|
857
|
+
const gitDir = pointer ? canonical(pointer, top, ctx.home) : repo;
|
|
858
|
+
const commonDir = readText(join(gitDir, "commondir")).trim();
|
|
859
|
+
const common = commonDir ? canonical(commonDir, gitDir, ctx.home) : gitDir;
|
|
860
|
+
const folders = [join(common, "hooks")];
|
|
861
|
+
for (const config of [
|
|
862
|
+
join(common, "config"),
|
|
863
|
+
join(gitDir, "config.worktree"),
|
|
864
|
+
join(ctx.home, ".gitconfig"),
|
|
865
|
+
join(ctx.home, ".config", "git", "config"),
|
|
866
|
+
]) {
|
|
867
|
+
for (const [, value] of configText(config, ctx.home).matchAll(HOOKS_PATH)) {
|
|
868
|
+
if (value) {
|
|
869
|
+
folders.push(canonical(value, top, ctx.home));
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
setup.dirs.push(...folders.slice(1));
|
|
874
|
+
for (const dir of new Set([top, root])) {
|
|
875
|
+
setup.linked.push(...linkedHookTargets(dir));
|
|
876
|
+
}
|
|
877
|
+
// A hooks folder, or a hook in it, may link to a tracked file or folder:
|
|
878
|
+
// git runs the target.
|
|
879
|
+
for (const folder of folders) {
|
|
880
|
+
for (const path of [
|
|
881
|
+
folder,
|
|
882
|
+
...listDir(folder)
|
|
883
|
+
.filter((name) => !name.endsWith(".sample"))
|
|
884
|
+
.map((name) => join(folder, name)),
|
|
885
|
+
]) {
|
|
886
|
+
const target = realTarget(path);
|
|
887
|
+
if (target) {
|
|
888
|
+
setup.linked.push(target);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
setup.installed ||= listDir(join(common, "hooks")).some((name) => !name.endsWith(".sample"));
|
|
893
|
+
}
|
|
894
|
+
return setup;
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* True when writing `real` changes what a commit runs. While hooks are set up,
|
|
898
|
+
* any `package.json` change counts: a hook may run any npm script. `content`
|
|
899
|
+
* is the new text when known; a shell write is unknown, so it counts.
|
|
900
|
+
*/
|
|
901
|
+
function hookConfig(real, ctx, content) {
|
|
902
|
+
const name = basename(real);
|
|
903
|
+
if (HOOK_CONFIG_NAME.test(name) ||
|
|
904
|
+
real.split(sep).some((segment) => HOOK_DIR_NAME.test(segment))) {
|
|
905
|
+
return true;
|
|
906
|
+
}
|
|
907
|
+
const setup = hookSetup(ctx);
|
|
908
|
+
if ([...setup.dirs, ...setup.linked].some((dir) => isInside(real, dir))) {
|
|
909
|
+
return true;
|
|
910
|
+
}
|
|
911
|
+
return (name === "package.json" &&
|
|
912
|
+
(content === undefined ||
|
|
913
|
+
setup.dirs.length > 0 ||
|
|
914
|
+
setup.installed ||
|
|
915
|
+
PACKAGE_HOOK_KEY.test(content) ||
|
|
916
|
+
PACKAGE_HOOK_KEY.test(readText(real))));
|
|
917
|
+
}
|
|
918
|
+
// A clone into a folder that holds a hooks folder or a hook config's target
|
|
919
|
+
// fills it from the clone (#13118).
|
|
920
|
+
function holdsHooks(target, ctx) {
|
|
921
|
+
const setup = hookSetup(ctx);
|
|
922
|
+
return [...setup.dirs, ...setup.linked].some((dir) => isInside(dir, target));
|
|
923
|
+
}
|
|
924
|
+
// `mv`/`cp` onto an existing file replaces it: the old content is gone.
|
|
925
|
+
function overwrites(args, ctx) {
|
|
926
|
+
if (args.some((word) => isFlag(word) && TARGET_DIR_FLAG.test(word.text))) {
|
|
927
|
+
return true;
|
|
928
|
+
}
|
|
929
|
+
const paths = args
|
|
930
|
+
.filter((word) => !isFlag(word))
|
|
931
|
+
.map((word) => canonical(word.text, ctx.cwd, ctx.home));
|
|
932
|
+
const target = paths.pop();
|
|
933
|
+
if (!(target && paths.length > 0 && existsSync(target))) {
|
|
934
|
+
return false;
|
|
935
|
+
}
|
|
936
|
+
return (!statSync(target).isDirectory() ||
|
|
937
|
+
paths.some((path) => existsSync(join(target, basename(path)))));
|
|
938
|
+
}
|
|
629
939
|
function shellAsk(command, ctx) {
|
|
630
940
|
const [programWord, ...args] = command.argv;
|
|
631
941
|
const program = programWord?.text ?? "?";
|
|
@@ -634,6 +944,10 @@ function shellAsk(command, ctx) {
|
|
|
634
944
|
!ctx.writeRoots.some((root) => isInside(canonical(redirect.target.text, ctx.cwd, ctx.home), root)))) {
|
|
635
945
|
return { kind: "shell:redirect-outside", tier: 1 };
|
|
636
946
|
}
|
|
947
|
+
if (command.redirects.some((redirect) => redirect.mode === "write" &&
|
|
948
|
+
hookConfig(canonical(redirect.target.text, ctx.cwd, ctx.home), ctx, undefined))) {
|
|
949
|
+
return { kind: "shell:hook-config", tier: 1 };
|
|
950
|
+
}
|
|
637
951
|
if (program.includes("/")) {
|
|
638
952
|
return { kind: "shell:script", tier: 1 };
|
|
639
953
|
}
|
|
@@ -644,21 +958,44 @@ function shellAsk(command, ctx) {
|
|
|
644
958
|
// A change to another repository is not the workspace's own work.
|
|
645
959
|
const elsewhere = git.dirs.some(({ word }) => word.glob ||
|
|
646
960
|
!ctx.writeRoots.some((root) => isInside(canonical(word.text, ctx.cwd, ctx.home), root)));
|
|
647
|
-
|
|
961
|
+
// Paths after `-C` resolve from there, as in classifyGit.
|
|
962
|
+
const cwd = gitCwd(git, ctx.cwd, ctx.home);
|
|
963
|
+
// `--git-dir`/`--work-tree` or a repository the model made pick another
|
|
964
|
+
// config and hooks.
|
|
965
|
+
if (!(git.ok && GIT_TIER2.has(sub)) ||
|
|
966
|
+
badFlag ||
|
|
967
|
+
elsewhere ||
|
|
968
|
+
git.dirs.some(({ flag }) => flag !== "-C") ||
|
|
969
|
+
foreignRepo(gitRepoOf(git, cwd, ctx.home), ctx)) {
|
|
648
970
|
return { kind: `git:${sub || "?"}`, tier: 1 };
|
|
649
971
|
}
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
tier
|
|
653
|
-
}
|
|
972
|
+
let tier = 2;
|
|
973
|
+
if (sub === "push") {
|
|
974
|
+
tier = gitPushTier(git.rest);
|
|
975
|
+
}
|
|
976
|
+
else if (!gitKeepsWork(sub, git.rest, cwd, ctx.home) ||
|
|
977
|
+
(sub === "clone" &&
|
|
978
|
+
cloneTargets(git, ctx).some((word) => word.glob ||
|
|
979
|
+
!ctx.writeRoots.some((root) => isInside(word.text, root)) ||
|
|
980
|
+
hookConfig(word.text, ctx, "") ||
|
|
981
|
+
holdsHooks(word.text, ctx)))) {
|
|
982
|
+
tier = 1;
|
|
983
|
+
}
|
|
984
|
+
return { kind: `git:${sub}`, tier };
|
|
654
985
|
}
|
|
655
986
|
if (FILE_UTILS.has(program)) {
|
|
656
987
|
const inside = args
|
|
657
988
|
.filter((word) => !isFlag(word))
|
|
658
989
|
.every((word) => !word.glob &&
|
|
659
990
|
ctx.writeRoots.some((root) => isInside(canonical(word.text, ctx.cwd, ctx.home), root)) &&
|
|
660
|
-
!
|
|
661
|
-
|
|
991
|
+
!writeProtected(canonical(word.text, ctx.cwd, ctx.home), ctx));
|
|
992
|
+
const replaces = (program === "mv" || program === "cp") && overwrites(args, ctx);
|
|
993
|
+
const hooks = args.some((word) => !isFlag(word) &&
|
|
994
|
+
hookConfig(canonical(word.text, ctx.cwd, ctx.home), ctx, undefined));
|
|
995
|
+
return {
|
|
996
|
+
kind: `shell:${program}`,
|
|
997
|
+
tier: inside && !replaces && !hooks ? 2 : 1,
|
|
998
|
+
};
|
|
662
999
|
}
|
|
663
1000
|
const [first, second, third] = command.argv.map((word) => word.text);
|
|
664
1001
|
if (ctx.dirty &&
|
|
@@ -721,6 +1058,15 @@ function classifySimple(command, ctx) {
|
|
|
721
1058
|
}
|
|
722
1059
|
const program = programWord.text;
|
|
723
1060
|
if (program.includes("/") || programWord.glob) {
|
|
1061
|
+
// `/bin/cat ~/.ssh/id_rsa` still reads the key: a path-qualified program
|
|
1062
|
+
// gets a bare one's credential checks, then asks (the path may be any file).
|
|
1063
|
+
const bare = program.split("/").pop();
|
|
1064
|
+
if (!programWord.glob && bare) {
|
|
1065
|
+
const checked = classifySimple({ ...command, argv: [{ ...programWord, text: bare }, ...args] }, ctx);
|
|
1066
|
+
if (!checked.allow && checked.tier === 0) {
|
|
1067
|
+
return checked;
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
724
1070
|
return deny(2);
|
|
725
1071
|
}
|
|
726
1072
|
let verdict;
|
|
@@ -732,7 +1078,11 @@ function classifySimple(command, ctx) {
|
|
|
732
1078
|
}
|
|
733
1079
|
else if (FILE_UTILS.has(program) &&
|
|
734
1080
|
args.some((word) => !(isFlag(word) || word.glob) &&
|
|
735
|
-
gitControlFile(canonical(word.text, ctx.cwd, ctx.home), ctx)
|
|
1081
|
+
(gitControlFile(canonical(word.text, ctx.cwd, ctx.home), ctx) ||
|
|
1082
|
+
inGitDir(canonical(word.text, ctx.cwd, ctx.home))))) {
|
|
1083
|
+
verdict = deny(0);
|
|
1084
|
+
}
|
|
1085
|
+
else if (program === "git" && cloneIntoProtected(args, ctx)) {
|
|
736
1086
|
verdict = deny(0);
|
|
737
1087
|
}
|
|
738
1088
|
else {
|
|
@@ -762,14 +1112,16 @@ function classifySimple(command, ctx) {
|
|
|
762
1112
|
continue;
|
|
763
1113
|
}
|
|
764
1114
|
const real = canonical(redirect.target.text, ctx.cwd, ctx.home);
|
|
765
|
-
if (
|
|
766
|
-
gitControlFile(real, ctx)) {
|
|
1115
|
+
if (writeProtected(real, ctx) || gitControlFile(real, ctx)) {
|
|
767
1116
|
return deny(0);
|
|
768
1117
|
}
|
|
769
1118
|
if (redirect.target.glob ||
|
|
770
1119
|
!ctx.writeRoots.some((root) => isInside(real, root))) {
|
|
771
1120
|
return deny(2);
|
|
772
1121
|
}
|
|
1122
|
+
if (hookConfig(real, ctx, undefined)) {
|
|
1123
|
+
return deny(1);
|
|
1124
|
+
}
|
|
773
1125
|
mutates = true;
|
|
774
1126
|
}
|
|
775
1127
|
return allow(mutates);
|
|
@@ -793,6 +1145,31 @@ function changeDirectory(command, ctx) {
|
|
|
793
1145
|
moved: { ...ctx, cwd: canonical(target.text, ctx.cwd, ctx.home) },
|
|
794
1146
|
};
|
|
795
1147
|
}
|
|
1148
|
+
// Parts of a chain that can create files: anything but a plain read, `cd` and
|
|
1149
|
+
// git work that stays inside `.git`.
|
|
1150
|
+
function writesFiles(command) {
|
|
1151
|
+
const [program, ...args] = command.argv;
|
|
1152
|
+
if (program?.text !== "git") {
|
|
1153
|
+
return true;
|
|
1154
|
+
}
|
|
1155
|
+
const git = splitGit(args);
|
|
1156
|
+
return !(git.ok && GIT_KEEPS_TREE.has(git.sub?.text ?? ""));
|
|
1157
|
+
}
|
|
1158
|
+
// After a part that can write, git asks every time unless it runs right where
|
|
1159
|
+
// the workspace's own `.git` sits: git looks there first, and nothing in the
|
|
1160
|
+
// chain may write a `.git`.
|
|
1161
|
+
function runsBlind(command, ctx) {
|
|
1162
|
+
const [program, ...args] = command.argv;
|
|
1163
|
+
if (program?.text !== "git") {
|
|
1164
|
+
return false;
|
|
1165
|
+
}
|
|
1166
|
+
const git = splitGit(args);
|
|
1167
|
+
const repo = join(gitCwd(git, ctx.cwd, ctx.home), ".git");
|
|
1168
|
+
return !(git.ok &&
|
|
1169
|
+
git.dirs.every(({ flag }) => flag === "-C") &&
|
|
1170
|
+
existsSync(repo) &&
|
|
1171
|
+
ctx.writeRoots.some((root) => gitRepoAt(root) === repo));
|
|
1172
|
+
}
|
|
796
1173
|
export function classifyShell(source, ctx) {
|
|
797
1174
|
const parsed = parseShell(source);
|
|
798
1175
|
const summary = redactSummary(source);
|
|
@@ -800,6 +1177,7 @@ export function classifyShell(source, ctx) {
|
|
|
800
1177
|
return askDeny("The hook could not read this command safely. Use a simpler command, or ask the owner.", { detail: source, kind: "shell:unparseable", summary, tier: 1 });
|
|
801
1178
|
}
|
|
802
1179
|
let mutates = false;
|
|
1180
|
+
let wrote = false;
|
|
803
1181
|
let worst;
|
|
804
1182
|
const asked = [];
|
|
805
1183
|
let here = ctx;
|
|
@@ -815,17 +1193,24 @@ export function classifyShell(source, ctx) {
|
|
|
815
1193
|
continue;
|
|
816
1194
|
}
|
|
817
1195
|
const verdict = classifySimple(command, here);
|
|
818
|
-
|
|
1196
|
+
// The disk is read before the chain runs, so a repository an earlier part
|
|
1197
|
+
// clones or builds is not there yet (#12926).
|
|
1198
|
+
const blind = wrote && runsBlind(command, here);
|
|
1199
|
+
if (verdict.allow && !blind) {
|
|
819
1200
|
mutates ||= verdict.mutates;
|
|
1201
|
+
wrote ||= verdict.mutates;
|
|
820
1202
|
continue;
|
|
821
1203
|
}
|
|
822
|
-
if (verdict.tier === 0) {
|
|
1204
|
+
if (!verdict.allow && verdict.tier === 0) {
|
|
823
1205
|
return verdict;
|
|
824
1206
|
}
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
1207
|
+
const ask = shellAsk(command, here);
|
|
1208
|
+
asked.push(blind ? { ...ask, tier: 1 } : ask);
|
|
1209
|
+
const held = blind ? deny(1) : verdict;
|
|
1210
|
+
if (!(held.allow || (worst && !worst.allow && held.tier >= worst.tier))) {
|
|
1211
|
+
worst = held;
|
|
828
1212
|
}
|
|
1213
|
+
wrote ||= writesFiles(command);
|
|
829
1214
|
}
|
|
830
1215
|
if (!worst || worst.allow) {
|
|
831
1216
|
return allow(mutates);
|
|
@@ -857,19 +1242,26 @@ export function applyPatchPaths(command) {
|
|
|
857
1242
|
return paths;
|
|
858
1243
|
}
|
|
859
1244
|
/** The write rule shared by Edit tools and apply_patch: inside a write root, or ask. */
|
|
860
|
-
function classifyWrite(tool, real, ctx) {
|
|
861
|
-
if (
|
|
862
|
-
gitControlFile(real, ctx)) {
|
|
1245
|
+
function classifyWrite(tool, real, ctx, content) {
|
|
1246
|
+
if (writeProtected(real, ctx) || gitControlFile(real, ctx)) {
|
|
863
1247
|
return deny(0);
|
|
864
1248
|
}
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
: askDeny("That file is outside this job's workspace. Needs the owner's approval.", {
|
|
1249
|
+
if (!ctx.writeRoots.some((root) => isInside(real, root))) {
|
|
1250
|
+
return askDeny("That file is outside this job's workspace. Needs the owner's approval.", {
|
|
868
1251
|
detail: `${tool} ${real}`,
|
|
869
1252
|
kind: "write-outside-workspace",
|
|
870
1253
|
summary: redactSummary(`${tool} ${real}`),
|
|
871
1254
|
tier: 1,
|
|
872
1255
|
});
|
|
1256
|
+
}
|
|
1257
|
+
return hookConfig(real, ctx, content)
|
|
1258
|
+
? askDeny("That file decides what runs when you commit. Needs the owner's approval.", {
|
|
1259
|
+
detail: `${tool} ${real}`,
|
|
1260
|
+
kind: "write-hook-config",
|
|
1261
|
+
summary: redactSummary(`${tool} ${real}`),
|
|
1262
|
+
tier: 1,
|
|
1263
|
+
})
|
|
1264
|
+
: allow(true);
|
|
873
1265
|
}
|
|
874
1266
|
export function classifyCall(call, context) {
|
|
875
1267
|
const guard = context.guard ?? protectedPaths(context.home);
|
|
@@ -890,7 +1282,19 @@ export function classifyCall(call, context) {
|
|
|
890
1282
|
if (!target) {
|
|
891
1283
|
return deny(1);
|
|
892
1284
|
}
|
|
893
|
-
|
|
1285
|
+
// The new text, so a `package.json` edit is judged by what it adds.
|
|
1286
|
+
const edits = Array.isArray(input.edits) ? input.edits : [];
|
|
1287
|
+
const content = [
|
|
1288
|
+
text(input.content),
|
|
1289
|
+
text(input.new_string),
|
|
1290
|
+
text(input.new_source),
|
|
1291
|
+
...edits.map((edit) => typeof edit === "object" && edit !== null
|
|
1292
|
+
? text(edit.new_string)
|
|
1293
|
+
: undefined),
|
|
1294
|
+
]
|
|
1295
|
+
.filter(Boolean)
|
|
1296
|
+
.join("\n");
|
|
1297
|
+
return classifyWrite(tool, canonical(target, call.cwd, context.home), ctx, content);
|
|
894
1298
|
}
|
|
895
1299
|
// Codex's freeform file-edit tool: the whole patch is in `command`, so every
|
|
896
1300
|
// path it adds, updates, moves to or deletes is checked like an Edit, and one
|
|
@@ -902,7 +1306,7 @@ export function classifyCall(call, context) {
|
|
|
902
1306
|
}
|
|
903
1307
|
let verdict = allow(true);
|
|
904
1308
|
for (const path of paths) {
|
|
905
|
-
const refused = classifyWrite("apply_patch", canonical(path, call.cwd, context.home), ctx);
|
|
1309
|
+
const refused = classifyWrite("apply_patch", canonical(path, call.cwd, context.home), ctx, text(input.command) ?? "");
|
|
906
1310
|
if (!refused.allow) {
|
|
907
1311
|
if (refused.tier === 0) {
|
|
908
1312
|
return refused;
|