-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclass-wp-codebox-provider-credentials.php
More file actions
206 lines (177 loc) · 8.7 KB
/
Copy pathclass-wp-codebox-provider-credentials.php
File metadata and controls
206 lines (177 loc) · 8.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
<?php
/**
* Generic provider credential boundary contracts.
*
* @package WPCodebox
*/
defined( 'ABSPATH' ) || exit;
final class WP_Codebox_Provider_Credentials {
public const REQUIREMENTS_SCHEMA = 'wp-codebox/provider-credential-requirements/v1';
public const PREFLIGHT_SCHEMA = 'wp-codebox/provider-credential-preflight/v1';
/** @param array<string,mixed> $selection Provider/model/runtime selection metadata. @param array<string,mixed> $input Ability input. @param array<string,mixed> $inheritance Sanitized inheritance metadata. @return array<string,mixed>|WP_Error */
public static function resolve( array $selection, array $input = array(), array $inheritance = array() ): array|WP_Error {
$requirements = self::requirements( $selection, $input, $inheritance );
$preflight = self::preflight( $requirements, $selection, $input, $inheritance );
if ( in_array( (string) ( $preflight['status'] ?? '' ), array( 'missing', 'denied' ), true ) ) {
return new WP_Error( 'wp_codebox_provider_credentials_unavailable', 'Provider credentials are missing or denied for this sandbox scope.', array( 'status' => 403, 'schema' => self::PREFLIGHT_SCHEMA, 'preflight' => $preflight ) );
}
return array(
'requirements' => $requirements,
'preflight' => $preflight,
'secret_env' => self::secret_env_names( $preflight ),
);
}
/** @param array<string,mixed> $selection Provider/model/runtime selection metadata. @param array<string,mixed> $input Ability input. @param array<string,mixed> $inheritance Sanitized inheritance metadata. @return array<string,mixed> */
public static function requirements( array $selection, array $input = array(), array $inheritance = array() ): array {
$requirements = array(
'schema' => self::REQUIREMENTS_SCHEMA,
'provider' => self::safe_slug( $selection['provider'] ?? '' ),
'model' => self::safe_label( $selection['model'] ?? '' ),
'requirements' => array(),
);
if ( function_exists( 'apply_filters' ) ) {
$filtered = apply_filters( 'wp_codebox_provider_credential_requirements', $requirements, $selection, $input, $inheritance );
if ( is_array( $filtered ) ) {
$requirements = $filtered;
}
}
return self::sanitize_requirements( $requirements, $selection );
}
/** @param array<string,mixed> $requirements Redacted requirement contract. @param array<string,mixed> $selection Provider/model/runtime selection metadata. @param array<string,mixed> $input Ability input. @param array<string,mixed> $inheritance Sanitized inheritance metadata. @return array<string,mixed> */
public static function preflight( array $requirements, array $selection, array $input = array(), array $inheritance = array() ): array {
$preflight = array(
'schema' => self::PREFLIGHT_SCHEMA,
'provider' => self::safe_slug( $selection['provider'] ?? $requirements['provider'] ?? '' ),
'model' => self::safe_label( $selection['model'] ?? $requirements['model'] ?? '' ),
'status' => self::has_required_requirements( $requirements ) ? 'missing' : 'not-required',
'requirements' => $requirements['requirements'] ?? array(),
'secret_env' => array(),
'diagnostics' => array(),
'redacted' => true,
);
if ( function_exists( 'apply_filters' ) ) {
$filtered = apply_filters( 'wp_codebox_resolve_provider_credentials', $preflight, $requirements, $selection, $input, $inheritance );
if ( is_array( $filtered ) ) {
$preflight = $filtered;
}
}
return self::sanitize_preflight( $preflight, $requirements, $selection );
}
/** @param array<string,mixed> $preflight Sanitized preflight contract. @return string[] */
public static function secret_env_names( array $preflight ): array {
return self::env_names( $preflight['secret_env'] ?? array() );
}
/** @param array<string,mixed> $requirements Raw requirements. @param array<string,mixed> $selection Provider/model/runtime selection metadata. @return array<string,mixed> */
private static function sanitize_requirements( array $requirements, array $selection ): array {
$entries = array();
foreach ( is_array( $requirements['requirements'] ?? null ) ? $requirements['requirements'] : array() as $requirement ) {
if ( ! is_array( $requirement ) ) {
continue;
}
$name = self::safe_key( $requirement['name'] ?? '' );
if ( '' === $name ) {
continue;
}
$entry = array(
'name' => $name,
'required' => array_key_exists( 'required', $requirement ) ? (bool) $requirement['required'] : true,
);
foreach ( array( 'kind', 'scope', 'source' ) as $field ) {
$value = self::safe_label( $requirement[ $field ] ?? '' );
if ( '' !== $value ) {
$entry[ $field ] = $value;
}
}
$env = self::env_names( $requirement['secret_env'] ?? $requirement['secretEnv'] ?? array() );
if ( ! empty( $env ) ) {
$entry['secretEnv'] = $env;
}
$entries[] = $entry;
}
return array(
'schema' => self::REQUIREMENTS_SCHEMA,
'provider' => self::safe_slug( $selection['provider'] ?? $requirements['provider'] ?? '' ),
'model' => self::safe_label( $selection['model'] ?? $requirements['model'] ?? '' ),
'requirements' => $entries,
'redacted' => true,
);
}
/** @param array<string,mixed> $preflight Raw preflight. @param array<string,mixed> $requirements Sanitized requirements. @param array<string,mixed> $selection Provider/model/runtime selection metadata. @return array<string,mixed> */
private static function sanitize_preflight( array $preflight, array $requirements, array $selection ): array {
$status = self::status( (string) ( $preflight['status'] ?? '' ), ! self::has_required_requirements( $requirements ) );
return array(
'schema' => self::PREFLIGHT_SCHEMA,
'provider' => self::safe_slug( $selection['provider'] ?? $preflight['provider'] ?? $requirements['provider'] ?? '' ),
'model' => self::safe_label( $selection['model'] ?? $preflight['model'] ?? $requirements['model'] ?? '' ),
'status' => $status,
'requirements' => is_array( $requirements['requirements'] ?? null ) ? $requirements['requirements'] : array(),
'secret_env' => self::env_names( $preflight['secret_env'] ?? $preflight['secretEnv'] ?? array() ),
'diagnostics' => self::diagnostics( $preflight['diagnostics'] ?? array() ),
'redacted' => true,
);
}
private static function status( string $status, bool $empty_requirements ): string {
if ( in_array( $status, array( 'available', 'missing', 'denied', 'not-required' ), true ) ) {
return $status;
}
return $empty_requirements ? 'not-required' : 'missing';
}
/** @param array<string,mixed> $requirements Sanitized requirements. */
private static function has_required_requirements( array $requirements ): bool {
foreach ( is_array( $requirements['requirements'] ?? null ) ? $requirements['requirements'] : array() as $requirement ) {
if ( is_array( $requirement ) && (bool) ( $requirement['required'] ?? true ) ) {
return true;
}
}
return false;
}
/** @return array<int,array<string,string>> */
private static function diagnostics( mixed $diagnostics ): array {
$entries = array();
foreach ( is_array( $diagnostics ) ? $diagnostics : array() as $diagnostic ) {
if ( ! is_array( $diagnostic ) ) {
continue;
}
$code = self::safe_key( $diagnostic['code'] ?? '' );
$message = self::safe_reason( $diagnostic['message'] ?? '' );
if ( '' === $code && '' === $message ) {
continue;
}
$entry = array();
if ( '' !== $code ) {
$entry['code'] = $code;
}
if ( '' !== $message ) {
$entry['message'] = $message;
}
$severity = self::safe_key( $diagnostic['severity'] ?? '' );
if ( in_array( $severity, array( 'info', 'warning', 'error' ), true ) ) {
$entry['severity'] = $severity;
}
$entries[] = $entry;
}
return $entries;
}
/** @return string[] */
private static function env_names( mixed $value ): array {
$names = array();
foreach ( WP_Codebox_Agent_Task::string_list( $value ) as $name ) {
if ( 1 === preg_match( '/^[A-Z_][A-Z0-9_]*$/', $name ) ) {
$names[] = $name;
}
}
return array_values( array_unique( $names ) );
}
private static function safe_slug( mixed $value ): string {
return substr( preg_replace( '/[^A-Za-z0-9_.:-]/', '', trim( (string) $value ) ) ?? '', 0, 120 );
}
private static function safe_key( mixed $value ): string {
return substr( preg_replace( '/[^A-Za-z0-9_.:-]/', '', trim( (string) $value ) ) ?? '', 0, 120 );
}
private static function safe_label( mixed $value ): string {
return substr( preg_replace( '/[^A-Za-z0-9 ._:@\/-]/', '', trim( (string) $value ) ) ?? '', 0, 160 );
}
private static function safe_reason( mixed $value ): string {
return substr( preg_replace( '/[^A-Za-z0-9 .,:_@\/-]/', '', trim( (string) $value ) ) ?? '', 0, 240 );
}
}