@jalaxy33/pi-permission 0.4.0 → 0.5.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/src/scripts.ts ADDED
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Programs that run source code, and which of their words is that code.
3
+ *
4
+ * A shell script can be read and judged like the command line it is. Every
5
+ * other language is a program the gate cannot parse, so its script becomes an
6
+ * opaque ask: once per file, or once per heredoc body, never for a pipe.
7
+ */
8
+
9
+ import { createHash } from "node:crypto";
10
+ import { normalizeProgram } from "./program";
11
+
12
+ /** Shells whose `-c` script and `-o` values the gate understands. */
13
+ export const SHELLS = new Set(["sh", "bash", "zsh", "dash", "ksh", "fish"]);
14
+
15
+ /** Programs whose first non-flag argument is source code. */
16
+ const INTERPRETERS =
17
+ /^(python[0-9.]*|pypy[0-9.]*|node|nodejs|bun|deno|perl|ruby|php|lua|rscript|osascript|pwsh|powershell|groovy|tclsh|wish)$/;
18
+
19
+ export function isCodeRunner(program: string): boolean {
20
+ return INTERPRETERS.test(normalizeProgram(program).toLowerCase());
21
+ }
22
+
23
+ export function programFamily(program: string): string {
24
+ const name = normalizeProgram(program).toLowerCase();
25
+ if (SHELLS.has(name)) return "shell";
26
+ if (name.startsWith("python") || name.startsWith("pypy")) return "python";
27
+ if (name.startsWith("node")) return "node";
28
+ return name;
29
+ }
30
+
31
+ /**
32
+ * Options that consume the next word. `-c` is here too: its value is inline
33
+ * code, not an operand.
34
+ */
35
+ const VALUE_FLAGS: Record<string, Set<string>> = {
36
+ shell: new Set(["-o", "+o", "-O", "+O", "-c", "--init-file", "--rcfile"]),
37
+ python: new Set(["-m", "-c", "-W", "-X", "-Q", "--check-hash-based-pycs"]),
38
+ node: new Set(["-r", "--require", "--loader", "--import", "-e", "--eval", "-p", "--print", "--inspect-port"]),
39
+ bun: new Set(["-e", "--eval", "-p", "--print"]),
40
+ perl: new Set(["-I", "-M", "-e"]),
41
+ ruby: new Set(["-I", "-r", "-e"]),
42
+ };
43
+
44
+ /** Last letter of a clustered short option that still takes a value (`-euo pipefail`, `-lc '…'`). */
45
+ const CLUSTER_VALUE_LETTERS: Record<string, Set<string>> = {
46
+ shell: new Set(["o", "O", "c"]),
47
+ python: new Set(["m", "W", "X", "Q"]),
48
+ };
49
+
50
+ /** Options that name code inline or a module, so no script file is read. */
51
+ const MODULE_FLAGS: Record<string, Set<string>> = {
52
+ python: new Set(["-m", "-c"]),
53
+ node: new Set(["-e", "--eval", "-p", "--print", "--test"]),
54
+ bun: new Set(["-e", "--eval"]),
55
+ poppler: new Set(),
56
+ };
57
+
58
+ /** Subcommands that run a project task instead of a named script. */
59
+ const RUNNER_SUBCOMMANDS: Record<string, Set<string>> = {
60
+ bun: new Set(["test", "run", "x", "build", "install", "add", "remove", "pm", "create"]),
61
+ deno: new Set(["task", "test", "bench", "fmt", "lint", "check", "doc", "publish"]),
62
+ };
63
+
64
+ const SCRIPT_NAME = /\.(sh|bash|zsh|ksh|dash|fish|py|pyw|js|mjs|cjs|ts|pl|rb|php|lua|ps1|tcl)$/i;
65
+
66
+ function valueFlags(program: string): Set<string> {
67
+ return VALUE_FLAGS[programFamily(program)] ?? new Set<string>();
68
+ }
69
+
70
+ /** Whether this flag's value is code, so a rule matched on it is line-specific. */
71
+ export function flagCarriesCode(program: string, flag: string): boolean {
72
+ return valueFlags(program).has(flag);
73
+ }
74
+
75
+ /** Whether this option consumes the word after it. */
76
+ export function optionTakesValue(program: string, token: string): boolean {
77
+ if (token === "--" || !token.startsWith("-") || token === "-") return false;
78
+ const table = valueFlags(program);
79
+ if (token.includes("=")) return false;
80
+ if (table.has(token)) return true;
81
+ if (token.startsWith("--")) return false;
82
+ if (token.length === 2) return false;
83
+ const letters = CLUSTER_VALUE_LETTERS[programFamily(program)];
84
+ return letters?.has(token[token.length - 1] as string) === true;
85
+ }
86
+
87
+ /** Index of the first word that is not the program and not an option. */
88
+ export function firstOperandIndex(program: string, argv: string[]): number {
89
+ for (let i = 1; i < argv.length; i += 1) {
90
+ const token = argv[i] as string;
91
+ if (token === "--") continue;
92
+ if (optionTakesValue(program, token)) {
93
+ i += 1;
94
+ continue;
95
+ }
96
+ if (token.startsWith("-") && token !== "-") continue;
97
+ return i;
98
+ }
99
+ return -1;
100
+ }
101
+
102
+ /**
103
+ * The positional that names source code. `bash --init-file safe.sh danger.sh`
104
+ * names `danger.sh`; `bun test x.ts` and `python -m pytest x.py` name none.
105
+ */
106
+ export function scriptOperand(program: string, argv: string[]): string | undefined {
107
+ const index = firstOperandIndex(program, argv);
108
+ if (index === -1) return undefined;
109
+ const moduleFlags = MODULE_FLAGS[programFamily(program)];
110
+ if (moduleFlags && argv.slice(1, index).some((token) => moduleFlags.has(token))) return undefined;
111
+ const token = argv[index] as string;
112
+ if (RUNNER_SUBCOMMANDS[programFamily(program)]?.has(token) === true) return undefined;
113
+ if (SCRIPT_NAME.test(token) || token.startsWith("./") || token.startsWith("../") || token.startsWith("/")) {
114
+ return token;
115
+ }
116
+ return undefined;
117
+ }
118
+
119
+ export function scriptHash(text: string): string {
120
+ return createHash("sha1").update(text).digest("hex").slice(0, 16);
121
+ }