-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclass-wp-codebox-sandbox-tool-policy-normalizer.php
More file actions
309 lines (269 loc) · 11.7 KB
/
Copy pathclass-wp-codebox-sandbox-tool-policy-normalizer.php
File metadata and controls
309 lines (269 loc) · 11.7 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/**
* Sandbox tool policy normalization.
*
* @package WPCodebox
*/
defined( 'ABSPATH' ) || exit;
final class WP_Codebox_Sandbox_Tool_Policy_Normalizer {
public const SCHEMA = 'wp-codebox/sandbox-tool-policy/v1';
public const VERSION = 1;
public const TOOL_BRIDGE_SCHEMA = 'wp-codebox/tool-bridge/v1';
public const TOOL_BRIDGE_VERSION = 1;
public const HOST_POLICY_SCHEMA = 'wp-codebox/host-tool-policy/v1';
public const HOST_POLICY_VERSION = 1;
private const TOOL_DENIAL_SCHEMA = 'wp-codebox/tool-allowlist-denial/v1';
private const AGENTS_API_RUNTIME_ENVIRONMENT = 'environment';
private const AGENTS_API_RUNTIME_CAPABILITY_SCOPE = 'capability_scope';
private const AGENTS_API_RUNTIME_LOCAL = 'runtime_local';
private WP_Codebox_Runtime_Tool_Policy_Descriptor $descriptor_resolver;
public function __construct() {
$this->descriptor_resolver = new WP_Codebox_Runtime_Tool_Policy_Descriptor();
}
/** @param array<string,mixed> $input Ability input. @return array<string,mixed>|WP_Error */
public function normalize_for_task_input( array $input ): array|WP_Error {
$policy = is_array( $input['sandbox_tool_policy'] ?? null ) ? $input['sandbox_tool_policy'] : array();
$bridge = is_array( $input['tool_bridge'] ?? null ) ? $input['tool_bridge'] : array();
if ( empty( $policy ) && ! empty( $bridge ) ) {
$policy = $this->policy_from_tool_bridge( $bridge );
}
if ( empty( $policy ) && function_exists( 'apply_filters' ) ) {
$bridge = apply_filters( 'wp_codebox_tool_bridge', array(), WP_Codebox_Agent_Task::string_list( $input['allowed_tools'] ?? array() ), $input );
if ( is_array( $bridge ) ) {
$policy = $this->policy_from_tool_bridge( $bridge );
}
}
if ( empty( $policy ) ) {
$policy = $this->from_allowed_tools( WP_Codebox_Agent_Task::string_list( $input['allowed_tools'] ?? array() ), $input );
}
if ( empty( $policy ) && function_exists( 'apply_filters' ) ) {
$policy = apply_filters( 'wp_codebox_resolved_sandbox_tool_policy', $policy, $input );
}
$issues = $this->policy_issues( is_array( $policy ) ? $policy : array() );
if ( ! empty( $issues ) ) {
return new WP_Error(
'wp_codebox_sandbox_tool_policy_invalid',
'Allowed tools require a valid resolved sandbox_tool_policy snapshot.',
array(
'status' => 400,
'schema' => 'wp-codebox/sandbox-tool-policy-validation/v1',
'issues' => $issues,
)
);
}
return $policy;
}
/** @param string[] $allowed_tools @param array<string,mixed> $context @return array<string,mixed> */
public function tool_bridge_from_allowed_tools( array $allowed_tools, array $context = array() ): array {
$policy = $this->from_allowed_tools( $allowed_tools, $context );
if ( empty( $policy ) ) {
return array();
}
return $this->tool_bridge_from_policy( $allowed_tools, $policy, $context );
}
/** @param string[] $allowed_tools @param array<string,mixed> $policy @param array<string,mixed> $context @return array<string,mixed> */
public function tool_bridge_from_policy( array $allowed_tools, array $policy, array $context = array() ): array {
if ( empty( $policy ) ) {
return array();
}
$bridge = array(
'schema' => self::TOOL_BRIDGE_SCHEMA,
'version' => self::TOOL_BRIDGE_VERSION,
'allowed_tools' => WP_Codebox_Agent_Task::string_list( $allowed_tools ),
'sandbox_tool_policy' => $policy,
'host_policy' => $this->host_policy_from_sandbox_policy( $policy ),
'dispatcher' => array(
'owner' => 'wp-codebox',
'callback' => 'wp_codebox_browser_runtime_tool_callback',
'location' => 'sandbox',
),
'authorization' => array(
'mode' => 'allowlist',
'notes' => 'Only sandbox-visible tools in sandbox_tool_policy are exposed to the runtime agent. Parent control-plane actions remain outside the sandbox bridge.',
),
'redaction' => array(
'notes' => 'Secret values are passed through environment allowlists only and must not be embedded in tool bridge payloads, logs, or dispatcher metadata.',
),
);
if ( function_exists( 'apply_filters' ) ) {
$filtered = apply_filters( 'wp_codebox_resolved_tool_bridge', $bridge, $allowed_tools, $context );
if ( is_array( $filtered ) ) {
$bridge = $filtered;
}
}
return $bridge;
}
/** @param array<string,mixed> $policy @return array<string,mixed> */
public function host_policy_from_sandbox_policy( array $policy ): array {
$tools = array();
$effective = $this->descriptor_resolver->resolve_effective_runtime_tool_policy( $policy );
foreach ( is_array( $effective['tools'] ?? null ) ? $effective['tools'] : array() as $tool ) {
if ( ! is_array( $tool ) ) {
continue;
}
$tools[] = array_filter(
array(
'id' => (string) ( $tool['id'] ?? '' ),
'runtime_tool_id' => (string) ( $tool['runtimeToolId'] ?? $tool['runtime_tool_id'] ?? '' ),
'aliases' => is_array( $tool['aliases'] ?? null ) ? array_values( array_map( 'strval', $tool['aliases'] ) ) : array(),
'execution_location' => (string) ( $tool['executionLocation'] ?? $tool['execution_location'] ?? '' ),
'transport_visibility' => (string) ( $tool['transportVisibility'] ?? $tool['transport_visibility'] ?? '' ),
'allowed' => (bool) ( $tool['allowed'] ?? false ),
'denial_reason' => $this->descriptor_resolver->denial_reason( $tool ) ?? '',
'input_schema' => (string) ( $tool['schema'] ?? '' ),
'policy' => is_array( $tool['policy'] ?? null ) ? $tool['policy'] : array(),
),
static fn( mixed $value ): bool => '' !== $value && array() !== $value
);
}
return array(
'schema' => self::HOST_POLICY_SCHEMA,
'version' => self::HOST_POLICY_VERSION,
'tools' => $tools,
);
}
/** @param string[] $tools @param array<string,mixed> $task_input */
public function validate_task_tools( array $tools, array $task_input ): WP_Error|null {
$policy = $this->normalize_for_task_input( $task_input );
if ( is_wp_error( $policy ) ) {
return $policy;
}
$denied = array();
foreach ( WP_Codebox_Agent_Task::string_list( $tools ) as $tool ) {
$descriptor = $this->descriptor_resolver->resolve_runtime_tool_alias( $policy, $tool );
$reason = $this->descriptor_resolver->denial_reason( $descriptor );
if ( null !== $reason ) {
$denied[] = array(
'tool' => $tool,
'reason' => $reason,
);
}
}
if ( empty( $denied ) ) {
return null;
}
return new WP_Error(
'wp_codebox_tool_not_allowed',
'One or more requested tools are not allowed by the resolved sandbox tool policy.',
array(
'status' => 403,
'schema' => self::TOOL_DENIAL_SCHEMA,
'denied_tools' => $denied,
'allowed_tools' => $this->allowed_tools( $policy ),
'policy_schema' => $policy['schema'] ?? '',
)
);
}
/** @param string[] $allowed_tools @param array<string,mixed> $context @return array<string,mixed> */
public function from_allowed_tools( array $allowed_tools, array $context = array() ): array {
$policy_tools = array();
foreach ( WP_Codebox_Agent_Task::string_list( $allowed_tools ) as $tool_id ) {
$tool = $this->tool_policy_entry( $tool_id, $context );
if ( ! empty( $tool ) ) {
$policy_tools[] = $tool;
}
}
if ( empty( $policy_tools ) ) {
return array();
}
$policy = array(
'schema' => self::SCHEMA,
'version' => self::VERSION,
'tools' => $policy_tools,
);
if ( function_exists( 'apply_filters' ) ) {
$filtered = apply_filters( 'wp_codebox_sandbox_tool_policy', $policy, $allowed_tools, $context );
if ( is_array( $filtered ) ) {
$policy = $filtered;
}
}
return $policy;
}
/** @return string[] */
public function allowed_tools( array $policy ): array {
$effective = $this->descriptor_resolver->resolve_effective_runtime_tool_policy( $policy );
$allowed = array();
foreach ( is_array( $effective['tools'] ?? null ) ? $effective['tools'] : array() as $tool ) {
if ( is_array( $tool ) && null === $this->descriptor_resolver->denial_reason( $tool ) ) {
$allowed[] = (string) $tool['id'];
}
}
return array_values( array_unique( $allowed ) );
}
/** @param array<string,mixed> $policy @return array<int,array<string,string>> */
public function policy_issues( array $policy ): array {
$issues = array();
if ( self::SCHEMA !== ( $policy['schema'] ?? '' ) ) {
$issues[] = array( 'field' => 'schema', 'message' => 'sandbox_tool_policy.schema must be ' . self::SCHEMA . '.' );
}
if ( self::VERSION !== (int) ( $policy['version'] ?? 0 ) ) {
$issues[] = array( 'field' => 'version', 'message' => 'sandbox_tool_policy.version must be ' . self::VERSION . '.' );
}
if ( empty( $policy['tools'] ) || ! is_array( $policy['tools'] ) ) {
$issues[] = array( 'field' => 'tools', 'message' => 'sandbox_tool_policy.tools must be a non-empty array.' );
return $issues;
}
$seen = array();
foreach ( $policy['tools'] as $index => $tool ) {
if ( ! is_array( $tool ) ) {
$issues[] = array( 'field' => 'tools[' . $index . ']', 'message' => 'Each sandbox tool policy tool must be an object.' );
continue;
}
$id = trim( (string) ( $tool['id'] ?? '' ) );
if ( '' === $id ) {
$issues[] = array( 'field' => 'tools[' . $index . '].id', 'message' => 'Tool id must be a non-empty string.' );
} elseif ( isset( $seen[ $id ] ) ) {
$issues[] = array( 'field' => 'tools[' . $index . '].id', 'message' => 'Duplicate tool id: ' . $id . '.' );
}
$seen[ $id ] = true;
if ( '' === trim( (string) ( $tool['runtime_tool_id'] ?? '' ) ) ) {
$issues[] = array( 'field' => 'tools[' . $index . '].runtime_tool_id', 'message' => 'Tool runtime_tool_id must be a non-empty string.' );
}
$runtime = is_array( $tool['runtime'] ?? null ) ? $tool['runtime'] : array();
foreach ( array( self::AGENTS_API_RUNTIME_ENVIRONMENT, self::AGENTS_API_RUNTIME_CAPABILITY_SCOPE ) as $field ) {
if ( '' === trim( (string) ( $runtime[ $field ] ?? '' ) ) ) {
$issues[] = array( 'field' => 'tools[' . $index . '].runtime.' . $field, 'message' => 'Tool runtime.' . $field . ' must be a non-empty string.' );
}
}
if ( ! is_bool( $tool['allowed'] ?? null ) ) {
$issues[] = array( 'field' => 'tools[' . $index . '].allowed', 'message' => 'Tool allowed must be boolean.' );
}
}
return $issues;
}
/** @param array<string,mixed> $context @return array<string,mixed> */
private function tool_policy_entry( string $tool_id, array $context ): array {
$tool_id = trim( $tool_id );
if ( '' === $tool_id ) {
return array();
}
$entry = array(
'id' => $tool_id,
'runtime_tool_id' => $this->provider_safe_runtime_tool_id( $tool_id ),
'execution_location' => 'sandbox',
'transport_visibility' => 'sandbox',
'allowed' => true,
'runtime' => array(
self::AGENTS_API_RUNTIME_ENVIRONMENT => self::AGENTS_API_RUNTIME_LOCAL,
self::AGENTS_API_RUNTIME_CAPABILITY_SCOPE => self::AGENTS_API_RUNTIME_LOCAL,
),
);
if ( function_exists( 'apply_filters' ) ) {
$filtered = apply_filters( 'wp_codebox_sandbox_tool_policy_tool', $entry, $tool_id, $context );
if ( is_array( $filtered ) ) {
$entry = $filtered;
}
}
return $entry;
}
/** @param array<string,mixed> $bridge @return array<string,mixed> */
private function policy_from_tool_bridge( array $bridge ): array {
if ( self::TOOL_BRIDGE_SCHEMA !== ( $bridge['schema'] ?? '' ) || self::TOOL_BRIDGE_VERSION !== (int) ( $bridge['version'] ?? 0 ) ) {
return array();
}
return is_array( $bridge['sandbox_tool_policy'] ?? null ) ? $bridge['sandbox_tool_policy'] : array();
}
private function provider_safe_runtime_tool_id( string $tool_id ): string {
return trim( preg_replace( '/[^A-Za-z0-9_]+/', '_', $tool_id ) ?? '', '_' );
}
}