@agent-corpus/policy-manager 0.9.0 → 0.11.0
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/README.md +126 -29
- package/dist/adapters.d.ts +3 -1
- package/dist/adapters.js +37 -18
- package/dist/classifier.d.ts +98 -0
- package/dist/classifier.js +343 -0
- package/dist/cli.js +66 -7
- package/dist/config.d.ts +2 -0
- package/dist/config.js +6 -2
- package/dist/evidence.d.ts +1 -0
- package/dist/evidence.js +60 -0
- package/dist/flow.d.ts +4 -0
- package/dist/flow.js +91 -18
- package/dist/gitalias.d.ts +13 -0
- package/dist/gitalias.js +33 -0
- package/dist/grants.d.ts +9 -9
- package/dist/grants.js +46 -30
- package/dist/hard.d.ts +63 -0
- package/dist/hard.js +1637 -0
- package/dist/helpers.d.ts +59 -0
- package/dist/helpers.js +304 -0
- package/dist/merged.d.ts +6 -0
- package/dist/merged.js +23 -0
- package/dist/nativerules.d.ts +44 -0
- package/dist/nativerules.js +653 -0
- package/dist/permissive.js +179 -7
- package/dist/policy.d.ts +12 -0
- package/dist/policy.js +228 -45
- package/dist/profile.d.ts +7 -0
- package/dist/profile.js +32 -1
- package/dist/prompt.d.ts +32 -0
- package/dist/prompt.js +334 -0
- package/dist/reads.d.ts +46 -0
- package/dist/reads.js +812 -0
- package/dist/replay.d.ts +42 -0
- package/dist/replay.js +174 -1
- package/dist/service.d.ts +12 -1
- package/dist/service.js +23 -6
- package/dist/session.d.ts +76 -13
- package/dist/session.js +373 -116
- package/dist/shell.d.ts +2 -0
- package/dist/shell.js +1 -1
- package/dist/shellscan.d.ts +58 -0
- package/dist/shellscan.js +395 -0
- package/dist/statepaths.d.ts +41 -0
- package/dist/statepaths.js +706 -0
- package/dist/types.d.ts +11 -1
- package/package.json +1 -1
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether the working file is the committed blob: the same bytes, or, when the blob has no CR,
|
|
3
|
+
* the same bytes once the working file's CRLF line ends become LF (a `core.autocrlf` checkout).
|
|
4
|
+
* No clean filter or `.gitattributes` setting is applied.
|
|
5
|
+
*/
|
|
6
|
+
export declare function sameContent(working: Buffer, blob: Buffer): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* A script allowed to read a credential for one narrow job, such as posting a review as a GitHub
|
|
9
|
+
* App. It is trusted only as committed on the default branch and only with the argument shapes
|
|
10
|
+
* listed for it; anything else stays with a person.
|
|
11
|
+
*/
|
|
12
|
+
export interface CredentialHelper {
|
|
13
|
+
/** Repository-relative path with `/` separators, such as `tools/review-bot.mjs`. */
|
|
14
|
+
path: string;
|
|
15
|
+
/**
|
|
16
|
+
* Accepted argument lists after the script. Each token is a literal, a list of literal
|
|
17
|
+
* alternatives, or a string with placeholders (`repos/<repo>/pulls/<int>/reviews`).
|
|
18
|
+
*/
|
|
19
|
+
args: Array<Array<string | string[]>>;
|
|
20
|
+
/**
|
|
21
|
+
* Where the helper's checkout must come from: normalized `host/owner/` prefixes (or a whole
|
|
22
|
+
* `host/owner/repo`) that `remote.origin.url` must match, such as `github.com/example-org/`.
|
|
23
|
+
*/
|
|
24
|
+
remotes: string[];
|
|
25
|
+
}
|
|
26
|
+
export declare class HelperError extends Error {
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* `host/path` for an https, ssh or scp-style (`git@host:path`) URL, with the host and path in
|
|
30
|
+
* lower case and any `.git` and trailing `/` removed. Anything else (a local path, `file:`) is
|
|
31
|
+
* undefined: its content does not come from a hosted remote.
|
|
32
|
+
*/
|
|
33
|
+
export declare function normalizeRemote(url: string): string | undefined;
|
|
34
|
+
export declare const MAX_HELPERS = 20;
|
|
35
|
+
/** Index of the first shape `args` matches, with the `<file>` values it named. */
|
|
36
|
+
export declare function matchShape(helper: CredentialHelper, args: string[]): {
|
|
37
|
+
index: number;
|
|
38
|
+
files: string[];
|
|
39
|
+
} | undefined;
|
|
40
|
+
/** Validates the profile's `credentialHelpers`. */
|
|
41
|
+
export declare function parseCredentialHelpers(raw: unknown): CredentialHelper[];
|
|
42
|
+
export type HelperMatch = {
|
|
43
|
+
kind: "none";
|
|
44
|
+
} | {
|
|
45
|
+
kind: "match";
|
|
46
|
+
helper: CredentialHelper;
|
|
47
|
+
shape: number;
|
|
48
|
+
files: string[];
|
|
49
|
+
} | {
|
|
50
|
+
kind: "refused";
|
|
51
|
+
reason: string;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Whether `node <script> args...` runs a listed helper. `none`: the script is not a listed helper.
|
|
55
|
+
* `refused`: it is at a listed helper's path but is changed, untracked, linked or run with other
|
|
56
|
+
* arguments, so a person decides. `match`: the committed helper with an accepted shape.
|
|
57
|
+
*/
|
|
58
|
+
export declare function helperMatch(helpers: CredentialHelper[], cwd: string, argv: string[]): HelperMatch;
|
|
59
|
+
//# sourceMappingURL=helpers.d.ts.map
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { lstatSync, readFileSync, realpathSync } from "node:fs";
|
|
3
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
4
|
+
import { git } from "./evidence.js";
|
|
5
|
+
/** Git with replace refs ignored, so `refs/replace/*` cannot change what is read. */
|
|
6
|
+
const exact = (top, args) => git(top, ["--no-replace-objects", ...args]);
|
|
7
|
+
/** The raw bytes of a blob, with replace refs ignored and no filters applied. */
|
|
8
|
+
function blobBytes(top, oid) {
|
|
9
|
+
return execFileSync("git", ["-c", "core.fsmonitor=false", "-C", top, "--no-replace-objects", "cat-file", "blob", oid], {
|
|
10
|
+
timeout: 3000,
|
|
11
|
+
maxBuffer: 4 * 1024 * 1024,
|
|
12
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
13
|
+
env: Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith("GIT_"))),
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Whether the working file is the committed blob: the same bytes, or, when the blob has no CR,
|
|
18
|
+
* the same bytes once the working file's CRLF line ends become LF (a `core.autocrlf` checkout).
|
|
19
|
+
* No clean filter or `.gitattributes` setting is applied.
|
|
20
|
+
*/
|
|
21
|
+
export function sameContent(working, blob) {
|
|
22
|
+
if (working.equals(blob))
|
|
23
|
+
return true;
|
|
24
|
+
if (blob.includes(0x0d))
|
|
25
|
+
return false;
|
|
26
|
+
return Buffer.from(working.toString("latin1").replace(/\r\n/g, "\n"), "latin1").equals(blob);
|
|
27
|
+
}
|
|
28
|
+
export class HelperError extends Error {
|
|
29
|
+
}
|
|
30
|
+
/** A remote prefix: a lowercase host, then at least an owner; a trailing `/` makes it a prefix. */
|
|
31
|
+
const REMOTE_PREFIX = /^[a-z0-9][a-z0-9.-]*(?::[0-9]{1,5})?(?:\/[a-z0-9][a-z0-9_.-]*)+\/?$/;
|
|
32
|
+
/**
|
|
33
|
+
* `host/path` for an https, ssh or scp-style (`git@host:path`) URL, with the host and path in
|
|
34
|
+
* lower case and any `.git` and trailing `/` removed. Anything else (a local path, `file:`) is
|
|
35
|
+
* undefined: its content does not come from a hosted remote.
|
|
36
|
+
*/
|
|
37
|
+
export function normalizeRemote(url) {
|
|
38
|
+
let host;
|
|
39
|
+
let path;
|
|
40
|
+
const full = /^(?:https|ssh|git\+ssh):\/\/(?:[^@/\s]+@)?([^/:\s]+)(?::[0-9]{1,5})?\/(.+)$/i.exec(url);
|
|
41
|
+
const scp = /^(?:[^@/\s:]+@)?([^/:\s]+):(?!\/)(.+)$/.exec(url);
|
|
42
|
+
if (full?.[1] && full[2])
|
|
43
|
+
[host, path] = [full[1], full[2]];
|
|
44
|
+
else if (scp?.[1] && scp[2] && !/^[A-Za-z]$/.test(scp[1]))
|
|
45
|
+
[host, path] = [scp[1], scp[2]];
|
|
46
|
+
else
|
|
47
|
+
return undefined;
|
|
48
|
+
path = path
|
|
49
|
+
.replace(/\/+$/, "")
|
|
50
|
+
.replace(/\.git$/i, "")
|
|
51
|
+
.replace(/\/+$/, "");
|
|
52
|
+
const segments = path.split("/");
|
|
53
|
+
if (!segments.length || segments.some((s) => !/^[A-Za-z0-9][A-Za-z0-9_.-]*$/.test(s)))
|
|
54
|
+
return undefined;
|
|
55
|
+
return `${host}/${path}`.toLowerCase();
|
|
56
|
+
}
|
|
57
|
+
function remoteMatches(remotes, normalized) {
|
|
58
|
+
return remotes.some((prefix) => prefix.endsWith("/") ? normalized.startsWith(prefix) : normalized === prefix);
|
|
59
|
+
}
|
|
60
|
+
export const MAX_HELPERS = 20;
|
|
61
|
+
const MAX_SHAPES = 50;
|
|
62
|
+
const MAX_TOKENS = 40;
|
|
63
|
+
const MAX_WORD = 1024;
|
|
64
|
+
/** Placeholder values. Each is checked whole; none may contain whitespace. */
|
|
65
|
+
const PLACEHOLDERS = {
|
|
66
|
+
// Each segment starts with a letter or digit, so neither is `.` or `..`.
|
|
67
|
+
repo: (v) => /^[A-Za-z0-9][A-Za-z0-9_.-]{0,99}\/[A-Za-z0-9][A-Za-z0-9_.-]{0,99}$/.test(v),
|
|
68
|
+
int: (v) => /^[0-9]{1,12}$/.test(v),
|
|
69
|
+
sha: (v) => /^[0-9a-f]{7,40}$/.test(v),
|
|
70
|
+
word: (v) => /^[A-Za-z0-9][A-Za-z0-9_.-]{0,99}$/.test(v),
|
|
71
|
+
url: (v) => /^https:\/\/github\.com\/[A-Za-z0-9_./#-]{1,300}$/.test(v),
|
|
72
|
+
// No pattern characters, no tilde the shell expands, and no device file such as /dev/stdin. The caller also checks
|
|
73
|
+
// where the file is and that it may be read.
|
|
74
|
+
file: (v) => v.length > 0 &&
|
|
75
|
+
!/[*?[\]{}]/.test(v) &&
|
|
76
|
+
// `~` only right after a letter or digit (a Windows short name such as RUNNER~1).
|
|
77
|
+
!/(?:^|[^A-Za-z0-9])~/.test(v) &&
|
|
78
|
+
!/^\/dev\//.test(v),
|
|
79
|
+
// Any one argument; the caller refuses one the shell would expand.
|
|
80
|
+
text: () => true,
|
|
81
|
+
};
|
|
82
|
+
function parts(token) {
|
|
83
|
+
const out = [];
|
|
84
|
+
let rest = token;
|
|
85
|
+
while (rest.length) {
|
|
86
|
+
const open = rest.indexOf("<");
|
|
87
|
+
const close = open < 0 ? -1 : rest.indexOf(">", open);
|
|
88
|
+
const name = close < 0 ? undefined : rest.slice(open + 1, close);
|
|
89
|
+
if (open < 0 || name === undefined || !(name in PLACEHOLDERS)) {
|
|
90
|
+
if (open >= 0 && close >= 0)
|
|
91
|
+
throw new HelperError(`unknown placeholder in ${token}`);
|
|
92
|
+
out.push({ literal: rest });
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
if (open > 0)
|
|
96
|
+
out.push({ literal: rest.slice(0, open) });
|
|
97
|
+
out.push({ placeholder: name });
|
|
98
|
+
rest = rest.slice(close + 1);
|
|
99
|
+
}
|
|
100
|
+
if (out.some((part, i) => i > 0 && "placeholder" in part && "placeholder" in (out[i - 1] ?? {})))
|
|
101
|
+
throw new HelperError(`adjacent placeholders in ${token}`);
|
|
102
|
+
if (out.some((p) => "placeholder" in p && p.placeholder === "text") && out.length > 1)
|
|
103
|
+
throw new HelperError(`<text> must be a whole argument in ${token}`);
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
/** Matches `word` against a token's parts; `files` collects the values of `<file>` placeholders. */
|
|
107
|
+
function matchParts(list, word, files) {
|
|
108
|
+
const [head, ...rest] = list;
|
|
109
|
+
if (!head)
|
|
110
|
+
return word.length === 0;
|
|
111
|
+
if ("literal" in head)
|
|
112
|
+
return (word.startsWith(head.literal) && matchParts(rest, word.slice(head.literal.length), files));
|
|
113
|
+
const check = PLACEHOLDERS[head.placeholder];
|
|
114
|
+
if (!check)
|
|
115
|
+
return false;
|
|
116
|
+
const next = rest[0];
|
|
117
|
+
// A placeholder ends at the end of the word or right before the next literal.
|
|
118
|
+
for (let end = word.length; end >= 1; end--) {
|
|
119
|
+
if (next && "literal" in next && !word.startsWith(next.literal, end))
|
|
120
|
+
continue;
|
|
121
|
+
if (!next && end !== word.length)
|
|
122
|
+
continue;
|
|
123
|
+
const value = word.slice(0, end);
|
|
124
|
+
if (!check(value))
|
|
125
|
+
continue;
|
|
126
|
+
const found = [];
|
|
127
|
+
if (matchParts(rest, word.slice(end), found)) {
|
|
128
|
+
if (head.placeholder === "file")
|
|
129
|
+
files.push(value);
|
|
130
|
+
files.push(...found);
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
function matchToken(token, word, files) {
|
|
137
|
+
if (Array.isArray(token))
|
|
138
|
+
return token.includes(word);
|
|
139
|
+
return matchParts(parts(token), word, files);
|
|
140
|
+
}
|
|
141
|
+
/** Index of the first shape `args` matches, with the `<file>` values it named. */
|
|
142
|
+
export function matchShape(helper, args) {
|
|
143
|
+
if (args.some((word) => word.length > MAX_WORD))
|
|
144
|
+
return undefined;
|
|
145
|
+
for (const [index, shape] of helper.args.entries()) {
|
|
146
|
+
if (shape.length !== args.length)
|
|
147
|
+
continue;
|
|
148
|
+
const files = [];
|
|
149
|
+
if (shape.every((token, i) => matchToken(token, args[i] ?? "", files)))
|
|
150
|
+
return { index, files };
|
|
151
|
+
}
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
/** Validates the profile's `credentialHelpers`. */
|
|
155
|
+
export function parseCredentialHelpers(raw) {
|
|
156
|
+
if (raw === undefined)
|
|
157
|
+
return [];
|
|
158
|
+
if (!Array.isArray(raw) || raw.length > MAX_HELPERS)
|
|
159
|
+
throw new HelperError(`credentialHelpers must list at most ${MAX_HELPERS} helpers`);
|
|
160
|
+
return raw.map((entry) => {
|
|
161
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
162
|
+
throw new HelperError("each credential helper must be an object");
|
|
163
|
+
const { path, args, remotes, ...extra } = entry;
|
|
164
|
+
if (!Array.isArray(remotes) ||
|
|
165
|
+
remotes.length === 0 ||
|
|
166
|
+
remotes.length > 20 ||
|
|
167
|
+
remotes.some((r) => typeof r !== "string" || r.length > 200 || !REMOTE_PREFIX.test(r)))
|
|
168
|
+
throw new HelperError("each credential helper needs remotes: lowercase host/owner/ prefixes such as github.com/org/");
|
|
169
|
+
if (Object.keys(extra).length)
|
|
170
|
+
throw new HelperError(`unknown credential helper key: ${Object.keys(extra)[0]}`);
|
|
171
|
+
if (typeof path !== "string" ||
|
|
172
|
+
path.length > 300 ||
|
|
173
|
+
!/^[A-Za-z0-9_.@-]+(?:\/[A-Za-z0-9_.@-]+)*$/.test(path) ||
|
|
174
|
+
path.split("/").some((part) => part === "." || part === ".."))
|
|
175
|
+
throw new HelperError("a credential helper path must be repository-relative, with / separators");
|
|
176
|
+
if (!Array.isArray(args) || args.length === 0 || args.length > MAX_SHAPES)
|
|
177
|
+
throw new HelperError(`credential helper ${path} must list 1 to ${MAX_SHAPES} argument shapes`);
|
|
178
|
+
for (const shape of args) {
|
|
179
|
+
if (!Array.isArray(shape) || shape.length === 0 || shape.length > MAX_TOKENS)
|
|
180
|
+
throw new HelperError(`each argument shape of ${path} must list 1 to ${MAX_TOKENS} tokens`);
|
|
181
|
+
for (const token of shape) {
|
|
182
|
+
if (Array.isArray(token)) {
|
|
183
|
+
if (token.length === 0 ||
|
|
184
|
+
token.some((t) => typeof t !== "string" || t.length === 0 || t.length > 200))
|
|
185
|
+
throw new HelperError(`alternatives in ${path} must be non-empty strings`);
|
|
186
|
+
}
|
|
187
|
+
else if (typeof token !== "string" || token.length === 0 || token.length > 300)
|
|
188
|
+
throw new HelperError(`a token in ${path} must be a non-empty string`);
|
|
189
|
+
else
|
|
190
|
+
parts(token);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return { path, args: args, remotes: remotes };
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* The default branch on `origin`, by full ref name only, so a tag or branch called `origin/HEAD`
|
|
198
|
+
* cannot stand in for it: `refs/remotes/origin/HEAD` when it points into `refs/remotes/origin/`,
|
|
199
|
+
* else `refs/remotes/origin/main`.
|
|
200
|
+
*/
|
|
201
|
+
function defaultRef(top) {
|
|
202
|
+
const candidates = [];
|
|
203
|
+
try {
|
|
204
|
+
const target = exact(top, ["symbolic-ref", "--quiet", "refs/remotes/origin/HEAD"]);
|
|
205
|
+
if (/^refs\/remotes\/origin\/[^\s]+$/.test(target) && target !== "refs/remotes/origin/HEAD")
|
|
206
|
+
candidates.push(target);
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
/* no origin/HEAD */
|
|
210
|
+
}
|
|
211
|
+
candidates.push("refs/remotes/origin/main");
|
|
212
|
+
for (const ref of candidates)
|
|
213
|
+
try {
|
|
214
|
+
// `show-ref --verify` takes only a full name and never falls back to a shorter match.
|
|
215
|
+
exact(top, ["show-ref", "--verify", "--quiet", ref]);
|
|
216
|
+
return ref;
|
|
217
|
+
}
|
|
218
|
+
catch {
|
|
219
|
+
/* try the next */
|
|
220
|
+
}
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Whether `node <script> args...` runs a listed helper. `none`: the script is not a listed helper.
|
|
225
|
+
* `refused`: it is at a listed helper's path but is changed, untracked, linked or run with other
|
|
226
|
+
* arguments, so a person decides. `match`: the committed helper with an accepted shape.
|
|
227
|
+
*/
|
|
228
|
+
export function helperMatch(helpers, cwd, argv) {
|
|
229
|
+
const program = basename(argv[0] ?? "").toLowerCase();
|
|
230
|
+
const script = argv[1];
|
|
231
|
+
if (!helpers.length || (program !== "node" && program !== "node.exe") || script === undefined)
|
|
232
|
+
return { kind: "none" };
|
|
233
|
+
// Matched by file name first, then by where it really is, so any way of naming it is checked.
|
|
234
|
+
const named = helpers.filter((h) => basename(script) === basename(h.path));
|
|
235
|
+
if (!named.length)
|
|
236
|
+
return { kind: "none" };
|
|
237
|
+
const refused = (reason) => ({ kind: "refused", reason });
|
|
238
|
+
let real;
|
|
239
|
+
let top;
|
|
240
|
+
try {
|
|
241
|
+
const lexical = resolve(cwd, script);
|
|
242
|
+
if (lstatSync(lexical).isSymbolicLink())
|
|
243
|
+
return refused("Credential helper is a symlink");
|
|
244
|
+
real = realpathSync.native(lexical);
|
|
245
|
+
if (!lstatSync(real).isFile())
|
|
246
|
+
return refused("Credential helper is not a regular file");
|
|
247
|
+
top = realpathSync.native(git(dirname(real), ["rev-parse", "--show-toplevel"]));
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
return refused("Credential helper is not inside a Git checkout");
|
|
251
|
+
}
|
|
252
|
+
const helper = named.find((h) => join(top, ...h.path.split("/")) === real);
|
|
253
|
+
if (!helper)
|
|
254
|
+
return refused("Credential helper is not at its listed path in the checkout");
|
|
255
|
+
try {
|
|
256
|
+
git(top, ["ls-files", "--error-unmatch", "--", helper.path]);
|
|
257
|
+
}
|
|
258
|
+
catch {
|
|
259
|
+
return refused("Credential helper is not tracked");
|
|
260
|
+
}
|
|
261
|
+
// A clone or `git init` anywhere has its own origin; only a checkout whose origin is a listed
|
|
262
|
+
// hosted remote, read as configured and as git resolves it, can vouch for the helper.
|
|
263
|
+
let origin;
|
|
264
|
+
try {
|
|
265
|
+
const configured = git(top, ["config", "--get", "remote.origin.url"]);
|
|
266
|
+
const resolved = git(top, ["remote", "get-url", "origin"]);
|
|
267
|
+
let pushUrl = "";
|
|
268
|
+
try {
|
|
269
|
+
pushUrl = git(top, ["config", "--get", "remote.origin.pushurl"]);
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
/* no push URL */
|
|
273
|
+
}
|
|
274
|
+
if (configured === resolved && !pushUrl)
|
|
275
|
+
origin = normalizeRemote(configured);
|
|
276
|
+
}
|
|
277
|
+
catch {
|
|
278
|
+
origin = undefined;
|
|
279
|
+
}
|
|
280
|
+
if (!origin || !remoteMatches(helper.remotes, origin))
|
|
281
|
+
return refused("Credential helper's checkout does not come from a listed remote");
|
|
282
|
+
const ref = defaultRef(top);
|
|
283
|
+
if (!ref)
|
|
284
|
+
return refused("Credential helper's default branch is unknown");
|
|
285
|
+
let committed;
|
|
286
|
+
let current;
|
|
287
|
+
try {
|
|
288
|
+
const oid = exact(top, ["rev-parse", "--verify", "--quiet", `${ref}:${helper.path}`]);
|
|
289
|
+
if (!/^[0-9a-f]{40,64}$/.test(oid))
|
|
290
|
+
throw new Error("not a blob id");
|
|
291
|
+
committed = blobBytes(top, oid);
|
|
292
|
+
current = readFileSync(real);
|
|
293
|
+
}
|
|
294
|
+
catch {
|
|
295
|
+
return refused("Credential helper is not on the default branch");
|
|
296
|
+
}
|
|
297
|
+
if (!sameContent(current, committed))
|
|
298
|
+
return refused("Credential helper differs from the default branch");
|
|
299
|
+
const shape = matchShape(helper, argv.slice(2));
|
|
300
|
+
if (!shape)
|
|
301
|
+
return refused("Credential helper run with arguments outside its allowed shapes");
|
|
302
|
+
return { kind: "match", helper, shape: shape.index, files: shape.files };
|
|
303
|
+
}
|
|
304
|
+
//# sourceMappingURL=helpers.js.map
|
package/dist/merged.d.ts
CHANGED
|
@@ -11,4 +11,10 @@ export interface MergedEvidence {
|
|
|
11
11
|
export declare function mergedEvidence(cwd: string, ref: string): MergedEvidence;
|
|
12
12
|
/** The remote tip must match the fetched tracking ref, or a delete could discard unseen commits. */
|
|
13
13
|
export declare function remoteTipMatches(cwd: string, branch: string): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Whether deleting local branch `branch` loses no commit: its tip is reachable from a
|
|
16
|
+
* remote-tracking ref (which covers a tip equal to its upstream). The commits stay recoverable
|
|
17
|
+
* from that ref. A tip that exists only locally is not.
|
|
18
|
+
*/
|
|
19
|
+
export declare function onRemote(cwd: string, branch: string): boolean;
|
|
14
20
|
//# sourceMappingURL=merged.d.ts.map
|
package/dist/merged.js
CHANGED
|
@@ -93,4 +93,27 @@ export function remoteTipMatches(cwd, branch) {
|
|
|
93
93
|
return false;
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Whether deleting local branch `branch` loses no commit: its tip is reachable from a
|
|
98
|
+
* remote-tracking ref (which covers a tip equal to its upstream). The commits stay recoverable
|
|
99
|
+
* from that ref. A tip that exists only locally is not.
|
|
100
|
+
*/
|
|
101
|
+
export function onRemote(cwd, branch) {
|
|
102
|
+
if (!BRANCH_NAME.test(branch) || /^(?:main|master|HEAD)$/.test(branch))
|
|
103
|
+
return false;
|
|
104
|
+
try {
|
|
105
|
+
const tip = git(cwd, ["rev-parse", "--verify", `refs/heads/${branch}^{commit}`]);
|
|
106
|
+
return (git(cwd, [
|
|
107
|
+
"for-each-ref",
|
|
108
|
+
"--count=1",
|
|
109
|
+
"--contains",
|
|
110
|
+
tip,
|
|
111
|
+
"--format=%(refname)",
|
|
112
|
+
"refs/remotes/",
|
|
113
|
+
]) !== "");
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
96
119
|
//# sourceMappingURL=merged.js.map
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Request } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* The user's own permission rules in each harness, read (never written) so the policy manager
|
|
4
|
+
* agrees with them: a user deny or ask rule beats any approval here (no hook decision, so the
|
|
5
|
+
* harness's own flow applies), and a user allow rule counts as the person's answer.
|
|
6
|
+
*/
|
|
7
|
+
export type NativeVerdict = "allow" | "ask" | "deny";
|
|
8
|
+
export interface NativeMatch {
|
|
9
|
+
verdict: NativeVerdict;
|
|
10
|
+
/** The rule that decided it, as the user wrote it. */
|
|
11
|
+
rule: string;
|
|
12
|
+
/** The file it came from. */
|
|
13
|
+
source: string;
|
|
14
|
+
}
|
|
15
|
+
/** `*` matches any run of characters, including none and spaces. O(text × pattern) worst case. */
|
|
16
|
+
export declare function wildcard(pattern: string, text: string): boolean;
|
|
17
|
+
/** Gitignore-style path glob over `/` segments: `**` any segments, `*` and `?` within one. */
|
|
18
|
+
export declare function pathGlob(pattern: string, path: string): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* One Bash rule against one subcommand's text. Without `*` the rule is the exact command.
|
|
21
|
+
* `cmd:*` is the same as `cmd *`; a rule whose only `*` is a trailing ` *` also matches the bare
|
|
22
|
+
* command (`Bash(ls *)` matches `ls`, not `lsof`). Otherwise `*` matches any text, spaces too.
|
|
23
|
+
*/
|
|
24
|
+
export declare function bashRuleMatches(content: string, text: string): boolean;
|
|
25
|
+
type CodexDecision = "allow" | "prompt" | "forbidden";
|
|
26
|
+
interface PrefixRule {
|
|
27
|
+
pattern: string[][];
|
|
28
|
+
decision: CodexDecision;
|
|
29
|
+
text: string;
|
|
30
|
+
}
|
|
31
|
+
/** Parses the `prefix_rule(...)` calls of a rules file. Other statements are skipped. */
|
|
32
|
+
export declare function parseCodexRules(text: string): PrefixRule[];
|
|
33
|
+
/**
|
|
34
|
+
* The invocations Codex evaluates for a shell script: each command of a linear chain of plain
|
|
35
|
+
* words joined by `&&`, `||`, `;` or `|`; anything else (redirects, substitutions, assignments,
|
|
36
|
+
* globs, control flow) is one `bash -lc <script>` invocation.
|
|
37
|
+
*/
|
|
38
|
+
export declare function codexInvocations(script: string): string[][];
|
|
39
|
+
/** The user's own rule verdict for a request in its harness, or undefined when none applies. */
|
|
40
|
+
export declare function nativeRules(request: Request): NativeMatch | undefined;
|
|
41
|
+
/** For tests: forget cached rule files. */
|
|
42
|
+
export declare function clearNativeRuleCache(): void;
|
|
43
|
+
export {};
|
|
44
|
+
//# sourceMappingURL=nativerules.d.ts.map
|