-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime-policy.ts
More file actions
159 lines (137 loc) · 5 KB
/
runtime-policy.ts
File metadata and controls
159 lines (137 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
export interface RuntimePolicy {
network: "allow" | "deny" | { allowHosts: string[] }
filesystem: "sandbox" | "readonly-mounts" | "readwrite-mounts"
commands: string[]
secrets: "none" | "connector-scoped"
approvals: "never" | "on-write" | "on-command"
}
export type RuntimePolicyField = keyof RuntimePolicy
export type RuntimePolicyValidationIssueCode =
| "invalid-network"
| "invalid-filesystem"
| "invalid-command"
| "invalid-secrets"
| "invalid-approvals"
export interface RuntimePolicyValidationIssue {
code: RuntimePolicyValidationIssueCode
field: RuntimePolicyField
message: string
}
export interface RuntimePolicyValidationResult {
valid: boolean
issues: RuntimePolicyValidationIssue[]
}
export interface RuntimeCommandPolicyViolationDetails {
code: "runtime-command-disallowed"
command: string
allowedCommands: string[]
policy: RuntimePolicy
}
export class RuntimePolicyValidationError extends Error {
readonly code = "runtime-policy-invalid" as const
constructor(readonly issues: RuntimePolicyValidationIssue[]) {
super(`Runtime policy is invalid: ${issues.map((issue) => issue.message).join("; ")}`)
this.name = "RuntimePolicyValidationError"
}
toJSON(): { code: "runtime-policy-invalid"; issues: RuntimePolicyValidationIssue[]; message: string; name: string } {
return {
code: this.code,
issues: this.issues,
message: this.message,
name: this.name,
}
}
}
export class RuntimeCommandPolicyViolationError extends Error {
readonly code = "runtime-command-disallowed" as const
readonly command: string
readonly allowedCommands: string[]
readonly policy: RuntimePolicy
constructor(command: string, policy: RuntimePolicy) {
super(`Command is not allowed by runtime policy: ${command}`)
this.name = "RuntimeCommandPolicyViolationError"
this.command = command
this.allowedCommands = [...policy.commands]
this.policy = policy
}
toJSON(): RuntimeCommandPolicyViolationDetails & { message: string; name: string } {
return {
code: this.code,
command: this.command,
allowedCommands: this.allowedCommands,
policy: this.policy,
message: this.message,
name: this.name,
}
}
}
export function validateRuntimePolicy(policy: unknown): RuntimePolicyValidationResult {
const issues: RuntimePolicyValidationIssue[] = []
const candidate = policy as Partial<RuntimePolicy> | null
if (!candidate || typeof candidate !== "object") {
return {
valid: false,
issues: [
{ code: "invalid-network", field: "network", message: "policy must be an object with v0 policy fields" },
{ code: "invalid-filesystem", field: "filesystem", message: "policy must be an object with v0 policy fields" },
{ code: "invalid-command", field: "commands", message: "policy must be an object with v0 policy fields" },
{ code: "invalid-secrets", field: "secrets", message: "policy must be an object with v0 policy fields" },
{ code: "invalid-approvals", field: "approvals", message: "policy must be an object with v0 policy fields" },
],
}
}
if (
candidate.network !== "allow" &&
candidate.network !== "deny" &&
(!candidate.network ||
typeof candidate.network !== "object" ||
!Array.isArray(candidate.network.allowHosts) ||
!candidate.network.allowHosts.every((host) => typeof host === "string" && host.length > 0))
) {
issues.push({
code: "invalid-network",
field: "network",
message: "network must be allow, deny, or an allowHosts list",
})
}
if (!["sandbox", "readonly-mounts", "readwrite-mounts"].includes(candidate.filesystem ?? "")) {
issues.push({
code: "invalid-filesystem",
field: "filesystem",
message: "filesystem must be sandbox, readonly-mounts, or readwrite-mounts",
})
}
if (!Array.isArray(candidate.commands) || !candidate.commands.every((command) => typeof command === "string" && command.length > 0)) {
issues.push({
code: "invalid-command",
field: "commands",
message: "commands must be a list of non-empty command names",
})
}
if (!["none", "connector-scoped"].includes(candidate.secrets ?? "")) {
issues.push({
code: "invalid-secrets",
field: "secrets",
message: "secrets must be none or connector-scoped",
})
}
if (!["never", "on-write", "on-command"].includes(candidate.approvals ?? "")) {
issues.push({
code: "invalid-approvals",
field: "approvals",
message: "approvals must be never, on-write, or on-command",
})
}
return { valid: issues.length === 0, issues }
}
export function assertRuntimePolicy(policy: unknown): asserts policy is RuntimePolicy {
const result = validateRuntimePolicy(policy)
if (!result.valid) {
throw new RuntimePolicyValidationError(result.issues)
}
}
export function assertRuntimeCommandAllowed(command: string, policy: RuntimePolicy): void {
if (!policy.commands.includes(command)) {
throw new RuntimeCommandPolicyViolationError(command, policy)
}
}