@noppu-labs/opencode-permission-reviewer 2.4.1 → 3.0.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +64 -1
  2. package/LICENSE +21 -201
  3. package/LICENSE-APACHE +201 -0
  4. package/NOTICE +14 -6
  5. package/README.md +100 -750
  6. package/SECURITY.md +41 -22
  7. package/dist/explain.js +4932 -3522
  8. package/dist/index.d.ts +520 -513
  9. package/dist/index.js +12595 -9115
  10. package/dist/rpc.js +4 -1
  11. package/dist/tui/config/jsonc.ts +71 -63
  12. package/dist/tui/config/layer-read.ts +120 -0
  13. package/dist/tui/config/loader.ts +61 -513
  14. package/dist/tui/config/risk-policy-clamp.ts +80 -0
  15. package/dist/tui/config/trust-boundary-keys.ts +100 -0
  16. package/dist/tui/config/trust-boundary.ts +135 -0
  17. package/dist/tui/config/trusted-degradation.ts +97 -0
  18. package/dist/tui/config-fields.ts +55 -0
  19. package/dist/tui/config-knobs.ts +119 -0
  20. package/dist/tui/config-policy-rules.ts +86 -0
  21. package/dist/tui/config-risk-policy.ts +59 -0
  22. package/dist/tui/config-rule-condition.ts +150 -0
  23. package/dist/tui/config.ts +61 -430
  24. package/dist/tui/descriptor-io.ts +42 -0
  25. package/dist/tui/is-record.ts +13 -0
  26. package/dist/tui/opencode/event-normalizer.ts +41 -31
  27. package/dist/tui/reviewer-model.ts +87 -0
  28. package/dist/tui/tui.tsx +87 -99
  29. package/dist/tui/types.ts +171 -538
  30. package/dist/tui/ui/components.tsx +32 -136
  31. package/dist/tui/ui/review-connection.ts +32 -0
  32. package/dist/tui/ui/review-feed.ts +131 -0
  33. package/dist/tui/ui/review-overlay.tsx +83 -0
  34. package/dist/tui/ui/review-result.tsx +56 -0
  35. package/dist/tui/ui/review-store.ts +88 -0
  36. package/dist/tui/ui/review-theme.ts +63 -0
  37. package/dist/tui/ui/rpc.ts +5 -2
  38. package/dist/tui/ui/v1-host.ts +39 -0
  39. package/dist/tui/ui/v2.tsx +66 -220
  40. package/dist/tui/ui-protocol.ts +63 -118
  41. package/dist/tui/ui-state.ts +71 -64
  42. package/dist/tui/ui-status-parse.ts +74 -0
  43. package/package.json +18 -17
  44. package/rpc.js +2 -1
  45. package/server.js +2 -2
  46. package/tui.tsx +2 -1
package/dist/rpc.js CHANGED
@@ -6,7 +6,10 @@ var ReviewerRpc = {
6
6
  input: { type: "object", additionalProperties: false },
7
7
  output: { type: "string" }
8
8
  },
9
- status: { input: { type: "object", additionalProperties: false }, output: { type: "object" } },
9
+ status: {
10
+ input: { type: "object", additionalProperties: false },
11
+ output: { type: "object" }
12
+ },
10
13
  snapshot: {
11
14
  input: { type: "object", additionalProperties: false },
12
15
  output: { type: "object" }
@@ -11,9 +11,9 @@
11
11
  */
12
12
  export function parseJsonc(text: string): Record<string, unknown> {
13
13
  try {
14
- return parseJsoncStrict(text)
14
+ return parseJsoncStrict(text);
15
15
  } catch {
16
- return {}
16
+ return {};
17
17
  }
18
18
  }
19
19
 
@@ -21,80 +21,88 @@ export function parseJsonc(text: string): Record<string, unknown> {
21
21
  * top-level non-object so callers can distinguish a malformed config file
22
22
  * (worth warning about) from a missing one (the common case). */
23
23
  export function parseJsoncStrict(text: string): Record<string, unknown> {
24
- const stripped = stripCommentsAndTrailingCommas(text)
25
- const parsed: unknown = JSON.parse(stripped)
24
+ const stripped = stripCommentsAndTrailingCommas(text);
25
+ const parsed: unknown = JSON.parse(stripped);
26
26
  if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
27
- throw new Error("top-level value is not a JSON object")
27
+ throw new Error("top-level value is not a JSON object");
28
28
  }
29
- return parsed as Record<string, unknown>
29
+ return parsed as Record<string, unknown>;
30
+ }
31
+
32
+ /** One strip pass: the text kept so far and the last non-whitespace
33
+ * character appended to it. When a `}` or `]` arrives right after a `,`, the
34
+ * comma is a trailing comma and is removed. */
35
+ interface StripState {
36
+ out: string;
37
+ lastReal: string;
38
+ }
39
+
40
+ function append(state: StripState, ch: string): void {
41
+ state.out += ch;
42
+ if (ch !== " " && ch !== "\t" && ch !== "\n" && ch !== "\r")
43
+ state.lastReal = ch;
30
44
  }
31
45
 
32
46
  /** Strip JSONC comments and trailing commas while preserving string contents.
33
47
  * Trailing commas are handled in the same pass as comments (both are
34
48
  * string-aware), so a `,}` inside a string value is never corrupted. */
35
49
  export function stripCommentsAndTrailingCommas(input: string): string {
36
- let out = ""
37
- let i = 0
38
- const len = input.length
39
- // Tracks the last non-whitespace character appended to `out`. Used to detect
40
- // trailing commas: when we encounter `}` or `]`, we check if the last real
41
- // character was `,` and remove it.
42
- let lastReal = ""
43
- const append = (ch: string) => {
44
- out += ch
45
- if (ch !== " " && ch !== "\t" && ch !== "\n" && ch !== "\r") lastReal = ch
46
- }
47
-
48
- while (i < len) {
49
- const ch = input[i]!
50
- const next = input[i + 1]
51
-
52
- // String literal — copy verbatim until the closing quote.
53
- if (ch === '"') {
54
- append(ch)
55
- i += 1
56
- while (i < len) {
57
- const c = input[i]!
58
- append(c)
59
- if (c === "\\" && i + 1 < len) {
60
- append(input[i + 1]!)
61
- i += 2
62
- continue
63
- }
64
- i += 1
65
- if (c === '"') break
66
- }
67
- continue
68
- }
50
+ const state: StripState = { out: "", lastReal: "" };
51
+ let i = 0;
52
+ while (i < input.length) i = stripAt(input, i, state);
53
+ return state.out;
54
+ }
69
55
 
70
- // Line comment.
71
- if (ch === "/" && next === "/") {
72
- i += 2
73
- while (i < len && input[i] !== "\n") i += 1
74
- continue
75
- }
56
+ /** Handle the token starting at `i` and return the index after it. */
57
+ function stripAt(input: string, i: number, state: StripState): number {
58
+ const ch = input.charAt(i);
59
+ const next = input[i + 1];
60
+ if (ch === '"') return copyString(input, i, state);
61
+ if (ch === "/" && next === "/") return skipLineComment(input, i);
62
+ if (ch === "/" && next === "*") return skipBlockComment(input, i);
63
+ if ((ch === "}" || ch === "]") && state.lastReal === ",") {
64
+ dropTrailingComma(state);
65
+ }
66
+ append(state, ch);
67
+ return i + 1;
68
+ }
76
69
 
77
- // Block comment.
78
- if (ch === "/" && next === "*") {
79
- i += 2
80
- while (i < len && !(input[i] === "*" && input[i + 1] === "/")) i += 1
81
- i += 2
82
- continue
70
+ /** Copy a string literal verbatim through its closing quote. */
71
+ function copyString(input: string, start: number, state: StripState): number {
72
+ append(state, input.charAt(start));
73
+ let i = start + 1;
74
+ while (i < input.length) {
75
+ const c = input.charAt(i);
76
+ append(state, c);
77
+ if (c === "\\" && i + 1 < input.length) {
78
+ append(state, input.charAt(i + 1));
79
+ i += 2;
80
+ continue;
83
81
  }
82
+ i += 1;
83
+ if (c === '"') break;
84
+ }
85
+ return i;
86
+ }
84
87
 
85
- // Structural close: if the last real character was a trailing comma,
86
- // remove it before appending the closing brace/bracket.
87
- if ((ch === "}" || ch === "]") && lastReal === ",") {
88
- // Walk back over the comma (and any whitespace after it in `out`).
89
- let j = out.length - 1
90
- while (j >= 0 && out[j] !== ",") j -= 1
91
- if (j >= 0) out = out.slice(0, j)
92
- lastReal = ""
93
- }
88
+ function skipLineComment(input: string, start: number): number {
89
+ let i = start + 2;
90
+ while (i < input.length && input[i] !== "\n") i += 1;
91
+ return i;
92
+ }
94
93
 
95
- append(ch)
96
- i += 1
94
+ function skipBlockComment(input: string, start: number): number {
95
+ let i = start + 2;
96
+ while (i < input.length && !(input[i] === "*" && input[i + 1] === "/")) {
97
+ i += 1;
97
98
  }
99
+ return i + 2;
100
+ }
98
101
 
99
- return out
102
+ /** Walk back over the comma (and any whitespace after it in `out`). */
103
+ function dropTrailingComma(state: StripState): void {
104
+ let j = state.out.length - 1;
105
+ while (j >= 0 && state.out[j] !== ",") j -= 1;
106
+ if (j >= 0) state.out = state.out.slice(0, j);
107
+ state.lastReal = "";
100
108
  }
@@ -0,0 +1,120 @@
1
+ import { fstatSync, openSync } from "node:fs";
2
+ import {
3
+ closeQuietly,
4
+ O_NOFOLLOW,
5
+ O_NONBLOCK,
6
+ O_RDONLY,
7
+ readFully,
8
+ } from "../descriptor-io.ts";
9
+ import { parseJsoncStrict } from "./jsonc.ts";
10
+
11
+ /** Cap on a single config file read: the project layer is repository-controlled
12
+ * and read at startup, so an unbounded read lets a committed symlink to an
13
+ * endless source exhaust memory. A file over the cap is treated like any
14
+ * other unreadable layer (warned about and ignored). */
15
+ const CONFIG_READ_CAP_BYTES = 1024 * 1024;
16
+
17
+ export type ConfigLayer =
18
+ | {
19
+ raw: Record<string, unknown>;
20
+ status: "missing" | "ok";
21
+ warning?: undefined;
22
+ }
23
+ | {
24
+ raw: Record<string, unknown>;
25
+ status: "read-error" | "malformed";
26
+ /** A silently unreadable trusted layer must be visible, not
27
+ * indistinguishable from an absent one. */
28
+ warning: string;
29
+ };
30
+
31
+ type LayerText = ConfigLayer | { status: "ok"; text: string };
32
+
33
+ /** Read a JSONC config file. A missing file is the common case (status
34
+ * "missing", no warning). A file that exists but cannot be read or parsed
35
+ * keeps the plugin working with an empty layer, but the status lets the
36
+ * caller treat an unusable TRUSTED layer as a fail-closed condition rather
37
+ * than silently falling back to defaults.
38
+ *
39
+ * The read goes through an explicitly opened descriptor: O_NOFOLLOW rejects
40
+ * a symlinked layer (a committed link to a FIFO would otherwise hang startup
41
+ * and a link to an endless source would exhaust memory), O_NONBLOCK keeps a
42
+ * FIFO from blocking before fstat can reject it, fstat requires a regular
43
+ * file, and the byte cap bounds the read. */
44
+ export function readConfigLayer(path: string): ConfigLayer {
45
+ const read = readLayerText(path);
46
+ if (!("text" in read)) return read;
47
+ try {
48
+ return { raw: parseJsoncStrict(read.text), status: "ok" };
49
+ } catch (error) {
50
+ return {
51
+ raw: {},
52
+ status: "malformed",
53
+ warning: `permission-reviewer config at ${path} is malformed and was ignored (${error instanceof Error ? error.message : String(error)})`,
54
+ };
55
+ }
56
+ }
57
+
58
+ function readError(warning: string): ConfigLayer {
59
+ return { raw: {}, status: "read-error", warning };
60
+ }
61
+
62
+ function unreadable(path: string, error: unknown): ConfigLayer {
63
+ const code = (error as { code?: unknown }).code;
64
+ return readError(
65
+ `permission-reviewer config at ${path} exists but could not be read (${code ?? "unknown error"}); the layer was ignored`,
66
+ );
67
+ }
68
+
69
+ /** Read at most the capped bytes through the open descriptor; anything but a
70
+ * missing file that stops the read becomes a `read-error` layer. Never
71
+ * throws. */
72
+ function readLayerText(path: string): LayerText {
73
+ let fd: number | undefined;
74
+ try {
75
+ const opened = openLayer(path);
76
+ if (typeof opened !== "number") return opened;
77
+ fd = opened;
78
+ return readOpenLayer(fd, path);
79
+ } catch (error) {
80
+ return unreadable(path, error);
81
+ } finally {
82
+ closeQuietly(fd);
83
+ }
84
+ }
85
+
86
+ function openLayer(path: string): number | ConfigLayer {
87
+ try {
88
+ // Read-only descriptor open of an existing config path; no file is
89
+ // created here and the descriptor must pass the regular-file check below.
90
+ // codeql[js/insecure-temporary-file]
91
+ return openSync(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
92
+ } catch (error) {
93
+ if ((error as { code?: unknown }).code === "ENOENT")
94
+ return { raw: {}, status: "missing" };
95
+ return unreadable(path, error);
96
+ }
97
+ }
98
+
99
+ function readOpenLayer(fd: number, path: string): LayerText {
100
+ const info = fstatSync(fd);
101
+ if (!info.isFile()) {
102
+ return readError(
103
+ `permission-reviewer config at ${path} is not a regular file and was ignored`,
104
+ );
105
+ }
106
+ if (info.size > CONFIG_READ_CAP_BYTES) {
107
+ return readError(
108
+ `permission-reviewer config at ${path} exceeds the size limit (${CONFIG_READ_CAP_BYTES} bytes) and was ignored`,
109
+ );
110
+ }
111
+ return { status: "ok", text: readBytes(fd, info.size) };
112
+ }
113
+
114
+ function readBytes(fd: number, length: number): string {
115
+ const buffer = Buffer.alloc(length);
116
+ const read = readFully(fd, buffer, length, 0);
117
+ // A concurrent shrink can end the read early; decode only the bytes
118
+ // actually read so no zero-padding leaks into the parser.
119
+ return (read < length ? buffer.subarray(0, read) : buffer).toString("utf8");
120
+ }