-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclass-wp-codebox-fuzz-suite-runner.php
More file actions
1968 lines (1821 loc) · 106 KB
/
Copy pathclass-wp-codebox-fuzz-suite-runner.php
File metadata and controls
1968 lines (1821 loc) · 106 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* PHP in-process fuzz suite runner.
*
* @package WPCodebox
*/
defined( 'ABSPATH' ) || exit;
class WP_Codebox_Fuzz_Suite_Runner {
/** @param array<string,mixed> $input Ability input. @return array<string,mixed>|WP_Error */
public function run( array $input ): array|WP_Error {
$cases = is_array( $input['cases'] ?? null ) ? $input['cases'] : array();
$runner_capabilities = self::fuzz_suite_php_runner_capabilities( $input );
$missing_capabilities = self::fuzz_suite_missing_required_capabilities( $input, $runner_capabilities );
$requested_runner_mode = self::fuzz_suite_requested_runner_mode( $input );
if ( 'php-in-process' !== $requested_runner_mode ) {
return self::fuzz_suite_runner_capability_error_result(
$input,
$cases,
$runner_capabilities,
self::fuzz_suite_diagnostic(
'error',
'wp_codebox_fuzz_suite_runner_mode_unavailable',
'wp-codebox/run-fuzz-suite is an in-process WordPress ability only; use wp-codebox run-fuzz-suite --runner-mode=runtime-backed or @automattic/wp-codebox-playground/public executeWordPressFuzzSuite for browser, editor, page, CRUD, runtime, or runtime-action coverage.',
array( 'requested_runner_mode' => $requested_runner_mode, 'available_runner_mode' => $runner_capabilities['mode'], 'missing_capabilities' => $missing_capabilities, 'runtime_backed_execution' => self::fuzz_suite_runtime_backed_execution_contract(), 'required_support' => array( 'cli' => 'wp-codebox run-fuzz-suite --runner-mode=runtime-backed', 'typescript_public_facade' => '@automattic/wp-codebox-playground/public executeWordPressFuzzSuite', 'episode_methods' => array( 'step', 'reset' ) ) )
)
);
}
if ( self::fuzz_suite_require_coverage( $input ) && ! empty( $missing_capabilities ) ) {
return self::fuzz_suite_runner_capability_error_result(
$input,
$cases,
$runner_capabilities,
self::fuzz_suite_diagnostic( 'error', 'wp_codebox_fuzz_suite_required_runner_capabilities_unsupported', 'Fuzz suite requires runner capabilities that are not available in PHP in-process mode.', array( 'missing_capabilities' => $missing_capabilities, 'runner_mode' => $runner_capabilities['mode'] ) )
);
}
$results = array();
$diagnostics = array();
$artifact_refs = array();
foreach ( $cases as $index => $case ) {
if ( ! is_array( $case ) ) {
$diagnostic = self::fuzz_suite_diagnostic( 'error', 'wp_codebox_fuzz_case_invalid', 'Fuzz suite case must be an object.', array( 'case_index' => $index ) );
$diagnostics[] = $diagnostic;
$results[] = self::fuzz_suite_case_result( 'case-' . (string) $index, 'error', array( $diagnostic ) );
continue;
}
$result = self::execute_fuzz_suite_case( $case, $input, $index );
$results[] = $result;
foreach ( $result['diagnostics'] ?? array() as $diagnostic ) {
$diagnostics[] = $diagnostic;
}
foreach ( $result['artifactRefs'] ?? array() as $artifact_ref ) {
$artifact_refs[] = $artifact_ref;
}
}
$summary = self::fuzz_suite_summary( $results );
$status = self::fuzz_suite_status( $summary );
return array(
'success' => 'passed' === $status,
'schema' => 'wp-codebox/fuzz-suite-result/v1',
'status' => $status,
'suite' => array_filter(
array(
'id' => (string) ( $input['id'] ?? '' ),
'version' => (string) ( $input['version'] ?? '' ),
),
static fn( mixed $value ): bool => '' !== $value
),
'summary' => $summary,
'cases' => $results,
'diagnostics' => $diagnostics,
'artifactRefs' => self::dedupe_fuzz_suite_artifact_refs( $artifact_refs ),
'metadata' => array( 'canonical_ability' => 'wp-codebox/run-fuzz-suite', 'runner' => 'wp-codebox/fuzz-suite-runner/v1', 'runnerCapabilities' => $runner_capabilities ),
);
}
/** @param array<string,mixed> $suite Suite input. @param array<int,mixed> $cases Suite cases. @param array<string,mixed> $runner_capabilities Runner capabilities. @param array<string,mixed> $diagnostic Diagnostic. @return array<string,mixed> */
private static function fuzz_suite_runner_capability_error_result( array $suite, array $cases, array $runner_capabilities, array $diagnostic ): array {
$results = array();
foreach ( $cases as $index => $case ) {
$case_id = is_array( $case ) ? (string) ( $case['id'] ?? $case['case_id'] ?? ( 'case-' . (string) $index ) ) : ( 'case-' . (string) $index );
$results[] = self::fuzz_suite_case_result( $case_id, 'skipped', array( $diagnostic ) );
}
return array(
'success' => false,
'schema' => 'wp-codebox/fuzz-suite-result/v1',
'status' => 'error',
'suite' => array_filter( array( 'id' => (string) ( $suite['id'] ?? '' ), 'version' => (string) ( $suite['version'] ?? '' ) ), static fn( mixed $value ): bool => '' !== $value ),
'summary' => self::fuzz_suite_summary( $results ),
'cases' => $results,
'diagnostics' => array( $diagnostic ),
'artifactRefs' => array(),
'metadata' => array( 'canonical_ability' => 'wp-codebox/run-fuzz-suite', 'runner' => 'wp-codebox/fuzz-suite-runner/v1', 'runnerCapabilities' => $runner_capabilities ),
);
}
/** @param array<string,mixed> $suite Optional suite input. @return array<string,mixed> */
private static function fuzz_suite_php_runner_capabilities( array $suite = array() ): array {
return self::fuzz_suite_runner_capabilities_contract( $suite );
}
/** @return array<string,array<string,mixed>> */
public static function fuzz_suite_supported_runner_capabilities(): array {
return array(
'php-in-process' => self::fuzz_suite_runner_capabilities_contract(),
'runtime-backed' => self::fuzz_suite_runtime_backed_runner_capabilities_contract(),
);
}
/** @return array<string,mixed> */
public static function fuzz_suite_runtime_backed_execution_contract(): array {
return array(
'supported_by_this_ability' => false,
'ability_execution_mode' => 'php-in-process-only',
'public_runtime_backed_path' => 'wp-codebox run-fuzz-suite --runner-mode=runtime-backed',
'supported_public_facade' => '@automattic/wp-codebox-playground/public executeWordPressFuzzSuite',
'required_episode_methods' => array( 'step', 'reset' ),
'failure_code' => 'wp_codebox_fuzz_suite_runner_mode_unavailable',
);
}
/** @param array<string,mixed> $suite Optional suite input. @return array<string,mixed> */
public static function fuzz_suite_runner_capabilities_contract( array $suite = array() ): array {
$capabilities = array(
'schema' => 'wp-codebox/fuzz-runner-capabilities/v1',
'mode' => 'php-in-process',
'capabilities' => array( 'target:ability', 'target:http', 'target:rest' ),
'targetKinds' => array( 'ability', 'http', 'rest' ),
'unsupportedRequiredCapabilities' => array(),
);
if ( ! empty( $suite ) ) {
$capabilities['unsupportedRequiredCapabilities'] = self::fuzz_suite_missing_required_capabilities( $suite, $capabilities );
}
return array(
'schema' => $capabilities['schema'],
'mode' => $capabilities['mode'],
'capabilities' => $capabilities['capabilities'],
'targetKinds' => $capabilities['targetKinds'],
'unsupportedRequiredCapabilities' => $capabilities['unsupportedRequiredCapabilities'],
);
}
/** @param array<string,mixed> $suite Optional suite input. @return array<string,mixed> */
public static function fuzz_suite_runtime_backed_runner_capabilities_contract( array $suite = array() ): array {
$capabilities = array(
'schema' => 'wp-codebox/fuzz-runner-capabilities/v1',
'mode' => 'runtime-backed',
'capabilities' => array( 'target:ability', 'target:command', 'target:http', 'target:rest', 'target:runtime', 'target:runtime-action', 'runtime', 'runtime-action:admin_page', 'runtime-action:browser', 'runtime-action:browser_probe', 'runtime-action:crud_operation', 'runtime-action:db_operation', 'runtime-action:editor_open', 'runtime-action:page', 'runtime-action:php', 'runtime-action:rest_request', 'runtime-action:wp_cli', 'db_operation', 'rest-mutation:fixture-opt-in', 'mutation-isolation-artifact', 'delete-boundary-artifact' ),
'targetKinds' => array( 'ability', 'command', 'http', 'rest', 'runtime', 'runtime-action' ),
'operationKinds' => array( 'read', 'crud', 'mutation-isolation', 'delete-boundary' ),
'runtimeActionTypes' => array( 'admin_page', 'browser', 'browser_probe', 'crud_operation', 'db_operation', 'editor_open', 'page', 'php', 'rest_request', 'wp_cli' ),
'commands' => array( 'wp-codebox.checkpoint-create', 'wp-codebox.checkpoint-list', 'wp-codebox.checkpoint-restore', 'wordpress.ability', 'wordpress.browser-actions', 'wordpress.browser-page-load', 'wordpress.browser-probe', 'wordpress.collect-workload-result', 'wordpress.crud-operation', 'wordpress.db-operation', 'wordpress.editor-open', 'wordpress.ensure-plugin-active', 'wordpress.http-request', 'wordpress.plugin-state', 'wordpress.rest-performance-observation', 'wordpress.rest-request', 'wordpress.run-php', 'wordpress.run-workload', 'wordpress.server-page-load', 'wordpress.simulated-admin-page-load', 'wordpress.simulated-frontend-page-load', 'wordpress.wp-cli' ),
'unsupportedRequiredCapabilities' => array(),
);
if ( ! empty( $suite ) ) {
$capabilities['unsupportedRequiredCapabilities'] = self::fuzz_suite_missing_required_capabilities( $suite, $capabilities );
}
return $capabilities;
}
/** @param array<string,mixed> $suite Suite input. */
private static function fuzz_suite_require_coverage( array $suite ): bool {
return true === ( $suite['requireCoverage'] ?? $suite['require_coverage'] ?? false );
}
/** @param array<string,mixed> $suite Suite input. */
private static function fuzz_suite_requested_runner_mode( array $suite ): string {
$metadata = is_array( $suite['metadata'] ?? null ) ? $suite['metadata'] : array();
$mode = (string) ( $suite['runnerMode'] ?? $suite['runner_mode'] ?? $metadata['runnerMode'] ?? $metadata['runner_mode'] ?? 'php-in-process' );
return '' === $mode || 'auto' === $mode ? 'php-in-process' : $mode;
}
/** @param array<string,mixed> $suite Suite input. @param array<string,mixed> $runner_capabilities Runner capabilities. @return string[] */
private static function fuzz_suite_missing_required_capabilities( array $suite, array $runner_capabilities ): array {
$required = self::fuzz_suite_required_capabilities( $suite );
$available = array_fill_keys( array_map( 'strval', $runner_capabilities['capabilities'] ?? array() ), true );
foreach ( (array) ( $runner_capabilities['targetKinds'] ?? array() ) as $kind ) {
$available[ 'target:' . (string) $kind ] = true;
}
foreach ( (array) ( $runner_capabilities['runtimeActionTypes'] ?? array() ) as $type ) {
$available[ 'runtime-action:' . (string) $type ] = true;
}
foreach ( (array) ( $runner_capabilities['commands'] ?? array() ) as $command ) {
$available[ 'command:' . (string) $command ] = true;
}
return array_values( array_filter( $required, static fn( string $capability ): bool => ! isset( $available[ $capability ] ) ) );
}
/** @param array<string,mixed> $suite Suite input. @return string[] */
private static function fuzz_suite_required_capabilities( array $suite ): array {
$metadata = is_array( $suite['metadata'] ?? null ) ? $suite['metadata'] : array();
$required = is_array( $metadata['requiredRunnerCapabilities'] ?? null ) ? $metadata['requiredRunnerCapabilities'] : ( is_array( $metadata['required_runner_capabilities'] ?? null ) ? $metadata['required_runner_capabilities'] : array() );
$capabilities = array_map( 'strval', is_array( $required['capabilities'] ?? null ) ? $required['capabilities'] : array() );
$capabilities = array_merge( $capabilities, self::fuzz_suite_declared_target_capabilities( $suite ) );
foreach ( is_array( $required['targetKinds'] ?? null ) ? $required['targetKinds'] : ( is_array( $required['target_kinds'] ?? null ) ? $required['target_kinds'] : array() ) as $kind ) {
$capabilities[] = 'target:' . (string) $kind;
}
foreach ( is_array( $required['runtimeActionTypes'] ?? null ) ? $required['runtimeActionTypes'] : ( is_array( $required['runtime_action_types'] ?? null ) ? $required['runtime_action_types'] : array() ) as $type ) {
$capabilities[] = 'runtime-action:' . (string) $type;
}
foreach ( ( is_array( $required['commands'] ?? null ) ? $required['commands'] : array() ) as $command ) {
$capabilities[] = 'command:' . (string) $command;
}
return array_values( array_unique( array_filter( $capabilities, static fn( string $capability ): bool => '' !== $capability ) ) );
}
/** @param array<string,mixed> $suite Suite input. @return string[] */
private static function fuzz_suite_declared_target_capabilities( array $suite ): array {
$capabilities = array();
$targets = array();
if ( is_array( $suite['target'] ?? null ) ) {
$targets[] = $suite['target'];
}
foreach ( is_array( $suite['cases'] ?? null ) ? $suite['cases'] : array() as $case ) {
if ( is_array( $case ) && is_array( $case['target'] ?? null ) ) {
$targets[] = $case['target'];
}
}
foreach ( $targets as $target ) {
$kind = (string) ( $target['kind'] ?? '' );
if ( '' !== $kind ) {
$capabilities[] = 'target:' . $kind;
}
}
foreach ( is_array( $suite['cases'] ?? null ) ? $suite['cases'] : array() as $case ) {
if ( ! is_array( $case ) ) {
continue;
}
$target = is_array( $case['target'] ?? null ) ? $case['target'] : ( is_array( $suite['target'] ?? null ) ? $suite['target'] : array() );
if ( 'runtime-action' !== (string) ( $target['kind'] ?? '' ) ) {
continue;
}
$input = is_array( $case['input'] ?? null ) ? $case['input'] : array();
$type = (string) ( $input['type'] ?? '' );
if ( '' !== $type ) {
$capabilities[] = 'runtime-action:' . $type;
}
}
return array_values( array_unique( array_filter( $capabilities, static fn( string $capability ): bool => '' !== $capability ) ) );
}
/** @param array<string,mixed> $case Fuzz case. @param array<string,mixed> $suite Suite input. @return array<string,mixed> */
private static function execute_fuzz_suite_case( array $case, array $suite, int $index ): array {
$case_id = (string) ( $case['id'] ?? $case['case_id'] ?? ( 'case-' . (string) $index ) );
$diagnostics = array();
$artifacts = self::fuzz_suite_declared_artifact_refs( $case );
$steps = self::fuzz_suite_case_steps( $case );
if ( empty( $steps ) ) {
$target = is_array( $case['target'] ?? null ) ? $case['target'] : ( is_array( $suite['target'] ?? null ) ? $suite['target'] : array() );
$steps = array( self::fuzz_suite_target_step( $case, $target ) );
}
$status = 'passed';
$observations = array();
foreach ( $steps as $step_index => $step ) {
if ( ! is_array( $step ) ) {
$diagnostic = self::fuzz_suite_diagnostic( 'error', 'wp_codebox_fuzz_step_invalid', 'Fuzz suite step must be an object.', array( 'case_id' => $case_id, 'step_index' => $step_index ) );
$diagnostics[] = $diagnostic;
$status = 'error';
break;
}
$step_result = self::execute_fuzz_suite_step( $step, $case, $suite, $case_id, $observations, $artifacts );
$observations[] = $step_result['observation'];
foreach ( $step_result['artifactRefs'] ?? array() as $artifact_ref ) {
$artifacts[] = $artifact_ref;
}
if ( ! empty( $step_result['diagnostic'] ) ) {
$diagnostics[] = $step_result['diagnostic'];
}
if ( 'passed' !== $step_result['status'] ) {
$status = $step_result['status'];
if ( 'skipped' !== $status ) {
break;
}
}
}
return self::fuzz_suite_case_result( $case_id, $status, $diagnostics, $artifacts, array( 'observations' => $observations ) );
}
/** @param array<string,mixed> $case Fuzz case. @return array<int,array<string,mixed>> */
private static function fuzz_suite_case_steps( array $case ): array {
$steps = array();
$phases = is_array( $case['phases'] ?? null ) ? $case['phases'] : array();
foreach ( array( 'setup', 'action', 'assert' ) as $phase ) {
foreach ( is_array( $phases[ $phase ] ?? null ) ? $phases[ $phase ] : array() as $step ) {
if ( is_array( $step ) ) {
$step['phase'] = $phase;
}
$steps[] = $step;
}
}
return $steps;
}
/** @param array<string,mixed> $case Fuzz case. @param array<string,mixed> $target Target. @return array<string,mixed> */
private static function fuzz_suite_target_step( array $case, array $target ): array {
$kind = (string) ( $target['kind'] ?? '' );
$entrypoint = (string) ( $target['entrypoint'] ?? $target['id'] ?? '' );
$input = $case['input'] ?? array();
$args = is_array( $input['args'] ?? null ) ? $input['args'] : array();
if ( empty( $args ) && is_array( $input ) ) {
if ( 'rest' === $kind ) {
$args = self::fuzz_suite_args_from_map( array( 'path' => $input['path'] ?? $input['route'] ?? $entrypoint, 'method' => $input['method'] ?? 'GET', 'params-json' => $input['params'] ?? null, 'headers-json' => $input['headers'] ?? null, 'body-json' => $input['bodyJson'] ?? $input['body_json'] ?? null ) );
} elseif ( 'http' === $kind ) {
$args = self::fuzz_suite_args_from_map( array( 'url' => $input['url'] ?? $input['path'] ?? $entrypoint, 'method' => $input['method'] ?? 'GET', 'headers-json' => $input['headers'] ?? null, 'body' => $input['body'] ?? null ) );
} elseif ( 'ability' === $kind ) {
$args = self::fuzz_suite_args_from_map( array( 'name' => $entrypoint, 'input' => $input['input'] ?? $input['payload'] ?? null ) );
} elseif ( 'runtime-action' === $kind ) {
return self::fuzz_suite_runtime_action_step( $input );
}
}
$command = match ( $kind ) {
'rest' => 'wordpress.rest-request',
'http' => 'wordpress.http-request',
'ability' => 'wordpress.ability',
default => $entrypoint,
};
return array_filter( array( 'command' => $command, 'args' => $args, 'targetKind' => $kind, 'targetId' => $entrypoint ), static fn( mixed $value ): bool => '' !== $value && ! ( is_array( $value ) && empty( $value ) ) );
}
/** @param array<string,mixed> $input Runtime action input. @return array<string,mixed> */
private static function fuzz_suite_runtime_action_step( array $input ): array {
$type = (string) ( $input['type'] ?? '' );
if ( 'rest_request' === $type ) {
return array(
'command' => 'wordpress.rest-request',
'args' => self::fuzz_suite_args_from_map( array( 'path' => $input['path'] ?? $input['route'] ?? null, 'method' => $input['method'] ?? 'GET', 'params-json' => $input['params'] ?? null, 'headers-json' => $input['headers'] ?? null, 'body-json' => $input['bodyJson'] ?? $input['body_json'] ?? null ) ),
);
}
if ( 'wp_cli' === $type ) {
return array(
'command' => 'wordpress.wp-cli',
'args' => self::fuzz_suite_args_from_map( array( 'command' => $input['command'] ?? null ) ),
'action' => $type,
);
}
if ( 'php' === $type ) {
return array(
'command' => 'wordpress.run-php',
'action' => $type,
);
}
if ( 'browser' === $type ) {
return array(
'command' => 'wordpress.browser-actions',
'args' => self::fuzz_suite_browser_action_args( $input ),
'action' => $type,
);
}
if ( 'browser_probe' === $type ) {
return array(
'command' => 'wordpress.browser-probe',
'args' => self::fuzz_suite_args_from_map( array( 'url' => $input['url'] ?? null, 'wait-for' => $input['wait_for'] ?? $input['waitFor'] ?? null, 'duration' => $input['duration'] ?? null, 'capture' => self::fuzz_suite_csv_arg( $input['capture'] ?? null ), 'viewport' => $input['viewport'] ?? null ) ),
'action' => $type,
);
}
if ( 'editor_open' === $type ) {
return array(
'command' => 'wordpress.editor-open',
'args' => self::fuzz_suite_args_from_map( array( 'target' => $input['target'] ?? null, 'post-id' => $input['post_id'] ?? $input['postId'] ?? null, 'post-type' => $input['post_type'] ?? $input['postType'] ?? null, 'url' => $input['url'] ?? null, 'wait-selector' => $input['wait_selector'] ?? $input['waitSelector'] ?? null, 'wait-timeout' => isset( $input['timeout_ms'] ) ? ( (string) $input['timeout_ms'] . 'ms' ) : ( isset( $input['timeoutMs'] ) ? ( (string) $input['timeoutMs'] . 'ms' ) : null ), 'capture' => self::fuzz_suite_csv_arg( $input['capture'] ?? null ) ) ),
'action' => $type,
);
}
if ( 'admin_page' === $type || 'page' === $type ) {
return array(
'command' => 'admin_page' === $type ? 'wordpress.simulated-admin-page-load' : 'wordpress.simulated-frontend-page-load',
'args' => self::fuzz_suite_args_from_map( array( 'path' => $input['path'] ?? null, 'url' => $input['url'] ?? null, 'method' => $input['method'] ?? null, 'query-json' => $input['query'] ?? null, 'body-json' => $input['body'] ?? null, 'user' => $input['user'] ?? null, 'session' => $input['session'] ?? null, 'capture-diagnostics' => self::fuzz_suite_csv_arg( $input['capture_diagnostics'] ?? $input['captureDiagnostics'] ?? null ) ) ),
'action' => $type,
);
}
return array( 'command' => 'wordpress.runtime-action', 'args' => self::fuzz_suite_args_from_map( array( 'type' => $type ) ), 'action' => $type );
}
/** @param array<string,mixed> $input Runtime browser action input. @return string[] */
private static function fuzz_suite_browser_action_args( array $input ): array {
$operation = (string) ( $input['operation'] ?? '' );
$step = array_filter(
array(
'kind' => 'wait' === $operation ? 'waitFor' : $operation,
'url' => $input['url'] ?? null,
'selector' => $input['selector'] ?? null,
'text' => $input['text'] ?? null,
'value' => $input['value'] ?? null,
'key' => $input['key'] ?? null,
'duration' => $input['duration'] ?? null,
'waitFor' => $input['wait_for'] ?? $input['waitFor'] ?? null,
'capture' => 'capture' === $operation && is_array( $input['capture'] ?? null ) ? $input['capture'] : null,
),
static fn( mixed $value ): bool => null !== $value && '' !== $value
);
return self::fuzz_suite_args_from_map( array( 'url' => ( isset( $input['url'] ) && 'navigate' !== $operation ) ? $input['url'] : null, 'steps-json' => array( $step ), 'capture' => self::fuzz_suite_csv_arg( $input['capture'] ?? null ) ) );
}
private static function fuzz_suite_csv_arg( mixed $value ): ?string {
return is_array( $value ) && ! empty( $value ) ? implode( ',', array_map( 'strval', $value ) ) : null;
}
/** @param array<string,mixed> $values Values. @return string[] */
private static function fuzz_suite_args_from_map( array $values ): array {
$args = array();
foreach ( $values as $key => $value ) {
if ( null === $value || '' === $value ) {
continue;
}
$args[] = (string) $key . '=' . ( is_array( $value ) || is_object( $value ) ? wp_json_encode( $value ) : (string) $value );
}
return $args;
}
/** @param array<string,mixed> $step Step. @param array<string,mixed> $case Case. @param array<string,mixed> $suite Suite. @return array<string,mixed> */
private static function execute_fuzz_suite_step( array $step, array $case, array $suite, string $case_id, array $prior_observations = array(), array $prior_artifacts = array() ): array {
$command = (string) ( $step['command'] ?? '' );
$args = self::fuzz_suite_parse_args( is_array( $step['args'] ?? null ) ? $step['args'] : array() );
$observation = array_filter(
array(
'command' => $command,
'phase' => (string) ( $step['phase'] ?? '' ),
'targetKind' => (string) ( $step['targetKind'] ?? '' ),
'targetId' => (string) ( $step['targetId'] ?? '' ),
'action' => (string) ( $step['action'] ?? '' ),
),
static fn( mixed $value ): bool => '' !== $value
);
if ( ! empty( $prior_observations ) ) {
$observation['prior_observations'] = $prior_observations;
}
try {
return match ( $command ) {
'wordpress.ensure-plugin-active' => self::execute_fuzz_suite_plugin_activation( $args, $observation, $case_id ),
'wordpress.ensure-external-http-guardrail' => self::execute_fuzz_suite_external_http_guardrail( $args, $observation ),
'wordpress.rest-route-inventory' => self::execute_fuzz_suite_rest_route_inventory( $args, $observation, $case_id ),
'wordpress.inventory-database' => self::execute_fuzz_suite_database_inventory( $args, $observation, $case_id ),
'wordpress.admin-page-inventory' => self::execute_fuzz_suite_admin_page_inventory( $args, $observation ),
'wordpress.fuzz-admin-pages' => self::execute_fuzz_suite_admin_page_fuzz( $args, $observation ),
'wordpress.rest-request' => self::execute_fuzz_suite_rest_request( $args, $observation, $case_id ),
'wordpress.http-request' => self::execute_fuzz_suite_http_request( $args, $observation, $case_id ),
'wordpress.trace-browser-coverage' => self::execute_fuzz_suite_browser_coverage( $args, $case, $suite, $observation, $case_id ),
'wordpress.ability' => self::execute_fuzz_suite_ability( $args, $observation, $case_id ),
'wordpress.summarize-fuzz-artifacts' => self::execute_fuzz_suite_artifact_summary( $args, $case, $suite, $observation, $case_id ),
'wordpress.collect-workload-result' => self::execute_fuzz_suite_collect_artifact( $args, $case, $observation, $prior_artifacts ),
'wordpress.run-workload', 'wordpress.run-declarative-fuzz' => self::execute_fuzz_suite_workload_step( $args, $command, $case, $observation, $case_id ),
default => self::fuzz_suite_step_unsupported( $command, $observation, $case_id ),
};
} catch ( Throwable $throwable ) {
return array( 'status' => 'error', 'observation' => $observation, 'diagnostic' => self::fuzz_suite_diagnostic( 'error', 'wp_codebox_fuzz_step_exception', $throwable->getMessage(), array( 'case_id' => $case_id, 'command' => $command ) ) );
}
}
/** @param array<string,string> $args Args. @param array<string,mixed> $observation Observation. @return array<string,mixed> */
private static function execute_fuzz_suite_rest_route_inventory( array $args, array $observation, string $case_id ): array {
if ( ! function_exists( 'rest_get_server' ) ) {
require_once ABSPATH . WPINC . '/rest-api.php';
}
self::refresh_fuzz_suite_rest_server();
$server = rest_get_server();
$routes = $server ? $server->get_routes() : array();
$namespace_filter = array_values( array_filter( array_map( 'trim', explode( ',', (string) ( $args['namespaces'] ?? '' ) ) ) ) );
$items = array();
$namespaces = array();
foreach ( $routes as $route => $handlers ) {
$namespace = trim( strtok( ltrim( (string) $route, '/' ), '/' ) ?: '' );
if ( ! empty( $namespace_filter ) && ! self::rest_route_matches_namespace_filter( (string) $route, $namespace_filter ) ) {
continue;
}
$methods = array();
$arg_names = array();
$endpoints = array();
foreach ( is_array( $handlers ) ? $handlers : array() as $handler ) {
if ( ! is_array( $handler ) ) {
continue;
}
$endpoint_methods = self::rest_route_inventory_methods( $handler['methods'] ?? array() );
$endpoint_args = array();
foreach ( (array) ( $handler['args'] ?? array() ) as $arg_name => $arg_schema ) {
$arg_names[] = (string) $arg_name;
$endpoint_args[] = self::rest_route_inventory_arg( (string) $arg_name, is_array( $arg_schema ) ? $arg_schema : array() );
}
$methods = array_merge( $methods, $endpoint_methods );
$endpoints[] = array(
'methods' => $endpoint_methods,
'permission' => self::rest_route_inventory_permission( $handler ),
'args' => $endpoint_args,
);
}
$item = array(
'route' => (string) $route,
'namespace' => $namespace,
'methods' => array_values( array_unique( $methods ) ),
'argNames' => array_values( array_unique( $arg_names ) ),
'endpoints' => $endpoints,
);
$route_schema = self::rest_route_inventory_schema( is_array( $handlers ) ? $handlers : array() );
if ( ! empty( $route_schema ) ) {
$item['schema'] = $route_schema;
}
$items[] = $item;
if ( '' !== $namespace ) {
$namespaces[] = $namespace;
}
}
$observation['artifact'] = (string) ( $args['artifact'] ?? 'route_inventory' );
$observation['route_count'] = count( $items );
$observation['namespaces'] = array_values( array_unique( $namespaces ) );
$observation['payload'] = array(
'schema' => 'wp-codebox/wordpress-rest-route-inventory/v1',
'command' => 'wordpress.rest-route-inventory',
'routes' => $items,
'namespaces' => $observation['namespaces'],
);
return array( 'status' => 'passed', 'observation' => $observation );
}
/** @param array<string,string> $args Args. @param array<string,mixed> $observation Observation. @return array<string,mixed> */
private static function execute_fuzz_suite_admin_page_inventory( array $args, array $observation ): array {
$inventory = self::fuzz_suite_admin_page_inventory( $args );
$observation['page_count'] = count( $inventory['pages'] );
$observation['menu_loaded'] = (bool) $inventory['menuLoaded'];
$observation['payload'] = $inventory;
return array( 'status' => 'passed', 'observation' => $observation );
}
/** @param array<string,string> $args Args. @param array<string,mixed> $observation Observation. @return array<string,mixed> */
private static function execute_fuzz_suite_admin_page_fuzz( array $args, array $observation ): array {
$inventory = self::fuzz_suite_admin_page_inventory( $args );
$max_pages = max( 1, (int) ( $args['max_pages'] ?? 80 ) );
$targets = array_slice( $inventory['pages'], 0, $max_pages );
$visits = array();
$skipped = array();
foreach ( $targets as $target ) {
$url = (string) ( $target['canonicalUrl'] ?? '' );
$skip_reason = self::fuzz_suite_admin_page_skip_reason( $target, $url );
if ( null !== $skip_reason ) {
$skipped[] = array( 'target' => $target, 'reason' => $skip_reason );
continue;
}
$visits[] = array(
'target' => $target,
'method' => 'GET',
'status' => 'planned',
'reason' => 'public PHP fuzz runner records safe admin coverage without issuing browser requests.',
);
}
$payload = array(
'schema' => 'wp-codebox/wordpress-admin-page-coverage/v1',
'contract' => array(
'safety_class' => 'read_only',
'command' => 'wordpress.fuzz-admin-pages',
'admin_inventory_schema' => $inventory['schema'],
),
'targets' => $targets,
'visits' => $visits,
'skipped' => $skipped,
'request_logs' => array(),
'query_attribution' => array(),
'metrics' => array(
'target_count' => count( $targets ),
'visit_count' => count( $visits ),
'skipped_count' => count( $skipped ),
'menu_loaded' => (bool) $inventory['menuLoaded'],
),
'inventory' => $inventory,
);
$observation['artifact'] = (string) ( $args['artifact'] ?? 'admin_page_coverage' );
$observation['target_count'] = count( $targets );
$observation['visit_count'] = count( $visits );
$observation['skipped_count'] = count( $skipped );
$observation['payload'] = $payload;
return array( 'status' => 'passed', 'observation' => $observation );
}
/** @param array<string,string> $args Args. @param array<string,mixed> $observation Observation. @return array<string,mixed> */
private static function execute_fuzz_suite_external_http_guardrail( array $args, array $observation ): array {
if ( function_exists( 'wp_codebox_bench_run_external_http_guardrail_step' ) ) {
$payload = WP_Codebox_WordPress_Runtime_Primitives::external_http_guardrail(
array(
'action' => 'install',
'allowlistDomains' => self::csv_fuzz_suite_arg( (string) ( $args['allowlist'] ?? '' ) ),
'blockNetwork' => filter_var( $args['block_network'] ?? true, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ) ?? true,
)
);
$observation['payload'] = $payload;
}
$observation['installed'] = true;
$observation['allowlist'] = self::csv_fuzz_suite_arg( (string) ( $args['allowlist'] ?? '' ) );
return array( 'status' => 'passed', 'observation' => $observation );
}
/** @param array<string,string> $args Args. @return array<string,mixed> */
private static function fuzz_suite_admin_page_inventory( array $args ): array {
$diagnostics = array();
if ( ( ! isset( $GLOBALS['menu'] ) || ! is_array( $GLOBALS['menu'] ) ) && function_exists( 'is_user_logged_in' ) && is_user_logged_in() ) {
if ( ! defined( 'WP_ADMIN' ) ) {
define( 'WP_ADMIN', true );
}
global $menu, $submenu;
if ( ! is_array( $submenu ?? null ) ) {
$submenu = array();
}
if ( defined( 'ABSPATH' ) && file_exists( ABSPATH . 'wp-admin/includes/admin.php' ) ) {
require_once ABSPATH . 'wp-admin/includes/admin.php';
}
if ( defined( 'ABSPATH' ) && file_exists( ABSPATH . 'wp-admin/menu.php' ) ) {
require_once ABSPATH . 'wp-admin/menu.php';
}
}
$menu_loaded = isset( $GLOBALS['menu'] ) && is_array( $GLOBALS['menu'] );
if ( ! $menu_loaded ) {
$diagnostics[] = array( 'surface' => 'admin', 'code' => 'admin-menu-not-loaded', 'message' => 'The admin menu globals are not populated in this request context.' );
}
$pages = array();
foreach ( (array) ( $GLOBALS['menu'] ?? array() ) as $item ) {
if ( is_array( $item ) ) {
$pages[] = self::fuzz_suite_admin_page_descriptor( (string) ( $item[2] ?? '' ), (string) ( $item[0] ?? '' ), (string) ( $item[1] ?? '' ) );
}
}
foreach ( (array) ( $GLOBALS['submenu'] ?? array() ) as $parent_slug => $items ) {
foreach ( (array) $items as $item ) {
if ( is_array( $item ) ) {
$pages[] = self::fuzz_suite_admin_page_descriptor( (string) ( $item[2] ?? '' ), (string) ( $item[0] ?? '' ), (string) ( $item[1] ?? '' ), (string) $parent_slug );
}
}
}
return array(
'schema' => 'wp-codebox/wordpress-admin-page-inventory/v1',
'command' => 'wordpress.admin-page-inventory',
'status' => $menu_loaded ? 'ok' : 'unsupported',
'adminUrl' => function_exists( 'admin_url' ) ? admin_url() : '',
'menuLoaded' => $menu_loaded,
'user' => self::fuzz_suite_admin_user_context(),
'pages' => array_values( array_filter( $pages, static fn( array $page ): bool => '' !== ( $page['menuSlug'] ?? '' ) ) ),
'diagnostics' => $diagnostics,
);
}
private static function fuzz_suite_admin_page_descriptor( string $menu_slug, string $title, string $capability, string $parent_slug = '' ): array {
$page = array(
'menuSlug' => $menu_slug,
'pageTitle' => self::fuzz_suite_strip_tags( $title ),
'menuTitle' => self::fuzz_suite_strip_tags( $title ),
'capability' => $capability,
'canAccess' => '' === $capability || ! function_exists( 'current_user_can' ) ? null : current_user_can( $capability ),
'canonicalUrl' => self::fuzz_suite_admin_page_url( $menu_slug, $parent_slug ),
);
if ( '' !== $parent_slug ) {
$page['parentSlug'] = $parent_slug;
}
return $page;
}
private static function fuzz_suite_admin_page_url( string $menu_slug, string $parent_slug = '' ): string {
if ( str_ends_with( $menu_slug, '.php' ) ) {
$path = $menu_slug;
} elseif ( str_contains( $menu_slug, '.php' ) ) {
$path = $menu_slug;
} elseif ( '' !== $parent_slug && str_contains( $parent_slug, '.php' ) ) {
$path = $parent_slug . '?page=' . rawurlencode( $menu_slug );
} else {
$path = 'admin.php?page=' . rawurlencode( $menu_slug );
}
return function_exists( 'admin_url' ) ? admin_url( $path ) : $path;
}
/** @param array<string,mixed> $target Target. */
private static function fuzz_suite_admin_page_skip_reason( array $target, string $url ): ?array {
if ( false === ( $target['canAccess'] ?? null ) ) {
return array( 'code' => 'capability_denied', 'message' => 'The current runtime user cannot access this admin page.', 'capability' => $target['capability'] ?? '' );
}
foreach ( array( 'action=delete', 'action=install', 'action=update', 'action=activate', 'action=deactivate', '_wpnonce=' ) as $pattern ) {
if ( str_contains( $url, $pattern ) ) {
return array( 'code' => 'destructive_or_nonce_action', 'message' => 'The admin page URL looks like a mutation or nonce-protected action.', 'pattern' => $pattern );
}
}
return null;
}
/** @return array<string,mixed> */
private static function fuzz_suite_admin_user_context(): array {
$user = function_exists( 'wp_get_current_user' ) ? wp_get_current_user() : null;
return array(
'isLoggedIn' => function_exists( 'is_user_logged_in' ) ? is_user_logged_in() : false,
'id' => is_object( $user ) && isset( $user->ID ) ? (int) $user->ID : 0,
'roles' => is_object( $user ) && isset( $user->roles ) ? array_values( array_map( 'strval', (array) $user->roles ) ) : array(),
);
}
/** @param array<string,string> $args Args. @param array<string,mixed> $observation Observation. @return array<string,mixed> */
private static function execute_fuzz_suite_database_inventory( array $args, array $observation, string $case_id ): array {
global $wpdb;
if ( ! is_object( $wpdb ) || ! method_exists( $wpdb, 'get_results' ) ) {
return array( 'status' => 'skipped', 'observation' => $observation, 'diagnostic' => self::fuzz_suite_diagnostic( 'warning', 'wp_codebox_fuzz_database_unavailable', 'WordPress database connection is not available.', array( 'case_id' => $case_id ) ) );
}
$prefix = isset( $wpdb->prefix ) ? (string) $wpdb->prefix : '';
$tables = self::fuzz_suite_database_tables( $wpdb, $prefix );
$totals = array(
'tableCount' => count( $tables ),
'rowCount' => (int) array_sum( array_map( static fn( array $table ): int => (int) ( $table['rowCount'] ?? 0 ), $tables ) ),
'columnCount' => (int) array_sum( array_map( static fn( array $table ): int => count( (array) ( $table['columns'] ?? array() ) ), $tables ) ),
'indexCount' => (int) array_sum( array_map( static fn( array $table ): int => count( (array) ( $table['indexes'] ?? array() ) ), $tables ) ),
'dataBytes' => (int) array_sum( array_map( static fn( array $table ): int => (int) ( $table['dataBytes'] ?? 0 ), $tables ) ),
'indexBytes' => (int) array_sum( array_map( static fn( array $table ): int => (int) ( $table['indexBytes'] ?? 0 ), $tables ) ),
'totalBytes' => (int) array_sum( array_map( static fn( array $table ): int => (int) ( $table['totalBytes'] ?? 0 ), $tables ) ),
);
$observation['artifact'] = (string) ( $args['artifact'] ?? 'db_inventory' );
$observation['table_count'] = $totals['tableCount'];
$observation['payload'] = array(
'schema' => 'wp-codebox/wordpress-db-inventory/v1',
'command' => 'wordpress.inventory-database',
'status' => 'ok',
'prefix' => $prefix,
'tables' => $tables,
'totals' => $totals,
'diagnostics' => array(),
);
return array( 'status' => 'passed', 'observation' => $observation );
}
/** @return array<int,array<string,mixed>> */
private static function fuzz_suite_database_tables( object $wpdb, string $prefix ): array {
$tables = array();
foreach ( self::fuzz_suite_database_query_rows( $wpdb, 'SHOW TABLE STATUS' ) as $status ) {
$name = (string) ( $status['Name'] ?? '' );
if ( '' === $name ) {
continue;
}
$data_bytes = (int) ( $status['Data_length'] ?? 0 );
$index_bytes = (int) ( $status['Index_length'] ?? 0 );
$tables[] = array(
'name' => $name,
'baseName' => self::fuzz_suite_database_base_table_name( $name, $prefix ),
'classification' => self::fuzz_suite_database_table_classification( $name, $prefix ),
'engine' => (string) ( $status['Engine'] ?? '' ),
'rowCount' => (int) ( $status['Rows'] ?? 0 ),
'dataBytes' => $data_bytes,
'indexBytes' => $index_bytes,
'totalBytes' => $data_bytes + $index_bytes,
'columns' => self::fuzz_suite_database_columns( $wpdb, $name ),
'indexes' => self::fuzz_suite_database_indexes( $wpdb, $name ),
'status' => array(
'engine' => (string) ( $status['Engine'] ?? '' ),
'rows' => isset( $status['Rows'] ) ? (int) $status['Rows'] : null,
'collation' => (string) ( $status['Collation'] ?? '' ),
'dataBytes' => $data_bytes,
'indexBytes' => $index_bytes,
'totalBytes' => $data_bytes + $index_bytes,
),
);
}
return $tables;
}
/** @return array<int,array<string,mixed>> */
private static function fuzz_suite_database_columns( object $wpdb, string $table ): array {
return array_values( array_map( static fn( array $row ): array => array(
'name' => (string) ( $row['Field'] ?? '' ),
'type' => (string) ( $row['Type'] ?? '' ),
'nullable' => 'YES' === strtoupper( (string) ( $row['Null'] ?? '' ) ),
'key' => (string) ( $row['Key'] ?? '' ),
'default' => array_key_exists( 'Default', $row ) && null !== $row['Default'] ? (string) $row['Default'] : null,
'extra' => (string) ( $row['Extra'] ?? '' ),
), self::fuzz_suite_database_query_rows( $wpdb, 'DESCRIBE ' . self::fuzz_suite_database_identifier( $table ) ) ) );
}
/** @return array<int,array<string,mixed>> */
private static function fuzz_suite_database_indexes( object $wpdb, string $table ): array {
return array_values( array_map( static fn( array $row ): array => array(
'name' => (string) ( $row['Key_name'] ?? '' ),
'column' => (string) ( $row['Column_name'] ?? '' ),
'unique' => '0' === (string) ( $row['Non_unique'] ?? '1' ),
'sequence' => isset( $row['Seq_in_index'] ) ? (int) $row['Seq_in_index'] : null,
), self::fuzz_suite_database_query_rows( $wpdb, 'SHOW INDEX FROM ' . self::fuzz_suite_database_identifier( $table ) ) ) );
}
/** @return array<int,array<string,mixed>> */
private static function fuzz_suite_database_query_rows( object $wpdb, string $query ): array {
$rows = $wpdb->get_results( $query, defined( 'ARRAY_A' ) ? ARRAY_A : 'ARRAY_A' );
return is_array( $rows ) ? array_values( array_filter( $rows, 'is_array' ) ) : array();
}
private static function fuzz_suite_database_identifier( string $name ): string {
return '`' . str_replace( '`', '``', $name ) . '`';
}
private static function fuzz_suite_database_base_table_name( string $name, string $prefix ): string {
return '' !== $prefix && str_starts_with( $name, $prefix ) ? substr( $name, strlen( $prefix ) ) : $name;
}
private static function fuzz_suite_database_table_classification( string $name, string $prefix ): string {
if ( '' !== $prefix && str_starts_with( $name, $prefix ) ) {
return in_array( self::fuzz_suite_database_base_table_name( $name, $prefix ), array( 'commentmeta', 'comments', 'links', 'options', 'postmeta', 'posts', 'term_relationships', 'term_taxonomy', 'termmeta', 'terms', 'usermeta', 'users' ), true ) ? 'core' : 'prefixed';
}
return 'external';
}
private static function fuzz_suite_strip_tags( string $value ): string {
return function_exists( 'wp_strip_all_tags' ) ? wp_strip_all_tags( $value ) : strip_tags( $value );
}
/** @param string[] $namespace_filter Namespace filters. */
private static function rest_route_matches_namespace_filter( string $route, array $namespace_filter ): bool {
$route = '/' . ltrim( $route, '/' );
foreach ( $namespace_filter as $namespace ) {
$namespace = trim( (string) $namespace, '/' );
if ( '' !== $namespace && str_starts_with( $route, '/' . $namespace ) ) {
return true;
}
}
return false;
}
private static function rest_route_inventory_methods( mixed $methods ): array {
if ( is_string( $methods ) ) {
return array_values( array_filter( array_map( 'trim', explode( ',', strtoupper( $methods ) ) ) ) );
}
$raw = array_merge( array_keys( (array) $methods ), array_values( (array) $methods ) );
$normalized = array();
foreach ( $raw as $method ) {
if ( is_string( $method ) && '' !== $method && strtoupper( $method ) === $method ) {
$normalized[] = $method;
}
}
return array_values( array_unique( $normalized ) );
}
private static function rest_route_inventory_permission( array $handler ): array {
if ( ! array_key_exists( 'permission_callback', $handler ) ) {
return array( 'mode' => 'none' );
}
$callback = $handler['permission_callback'];
if ( '__return_true' === $callback ) {
return array( 'mode' => 'public', 'callbackType' => 'function' );
}
return array( 'mode' => 'callback', 'callbackType' => self::rest_route_inventory_callback_type( $callback ) );
}
private static function rest_route_inventory_callback_type( mixed $callback ): string {
if ( is_string( $callback ) ) {
return 'function';
}
if ( is_array( $callback ) ) {
return 'method';
}
if ( $callback instanceof Closure ) {
return 'closure';
}
if ( is_object( $callback ) && is_callable( $callback ) ) {
return 'invokable';
}
return is_callable( $callback ) ? 'callable' : 'unknown';
}
private static function rest_route_inventory_arg( string $name, array $schema ): array {
$arg = array( 'name' => $name, 'required' => ! empty( $schema['required'] ) );
foreach ( array( 'type', 'format' ) as $key ) {
if ( isset( $schema[ $key ] ) && ( is_string( $schema[ $key ] ) || is_array( $schema[ $key ] ) ) ) {
$arg[ $key ] = $schema[ $key ];
}
}
if ( isset( $schema['enum'] ) && is_array( $schema['enum'] ) ) {
$arg['enum'] = array_slice( array_values( $schema['enum'] ), 0, 25 );
}
if ( isset( $schema['description'] ) && is_string( $schema['description'] ) ) {
$description = function_exists( 'wp_strip_all_tags' ) ? wp_strip_all_tags( $schema['description'] ) : strip_tags( $schema['description'] );
$arg['description'] = substr( $description, 0, 240 );
}
$arg['defaultPresent'] = array_key_exists( 'default', $schema );
$arg['validateCallback'] = array_key_exists( 'validate_callback', $schema );
$arg['sanitizeCallback'] = array_key_exists( 'sanitize_callback', $schema );
return $arg;
}
private static function rest_route_inventory_schema( array $handlers ): array {
foreach ( $handlers as $handler ) {
if ( ! is_array( $handler ) || ! isset( $handler['schema'] ) || ! is_array( $handler['schema'] ) ) {
continue;
}
$schema = $handler['schema'];
$descriptor = array();
foreach ( array( 'title', 'type' ) as $key ) {
if ( isset( $schema[ $key ] ) && ( is_string( $schema[ $key ] ) || is_array( $schema[ $key ] ) ) ) {
$descriptor[ $key ] = $schema[ $key ];
}
}
if ( isset( $schema['properties'] ) && is_array( $schema['properties'] ) ) {
$descriptor['properties'] = array_slice( array_values( array_map( 'strval', array_keys( $schema['properties'] ) ) ), 0, 100 );
}
return $descriptor;
}
return array();
}
/** @param string[] $args Args. @return array<string,string> */
private static function fuzz_suite_parse_args( array $args ): array {
$parsed = array();
foreach ( $args as $arg ) {
$parts = explode( '=', (string) $arg, 2 );
$parsed[ $parts[0] ] = $parts[1] ?? '';
}
return $parsed;
}
/** @param array<string,string> $args Args. @param array<string,mixed> $observation Observation. @return array<string,mixed> */
private static function execute_fuzz_suite_plugin_activation( array $args, array $observation, string $case_id ): array {
$plugin = trim( (string) ( $args['plugin'] ?? '' ) );
if ( '' === $plugin ) {
return array( 'status' => 'error', 'observation' => $observation, 'diagnostic' => self::fuzz_suite_diagnostic( 'error', 'wp_codebox_fuzz_plugin_missing', 'Plugin activation step requires plugin=<plugin-file>.', array( 'case_id' => $case_id ) ) );
}
if ( ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if ( ! is_plugin_active( $plugin ) ) {
$result = activate_plugin( $plugin );
if ( is_wp_error( $result ) ) {
return array( 'status' => 'failed', 'observation' => $observation, 'diagnostic' => self::fuzz_suite_diagnostic( 'error', 'wp_codebox_fuzz_plugin_activation_failed', $result->get_error_message(), array( 'case_id' => $case_id, 'plugin' => $plugin ) ) );
}
}
self::refresh_fuzz_suite_rest_server();
$observation['plugin'] = $plugin;
return array( 'status' => 'passed', 'observation' => $observation );
}
/** @param array<string,string> $args Args. @param array<string,mixed> $observation Observation. @return array<string,mixed> */
private static function execute_fuzz_suite_rest_request( array $args, array $observation, string $case_id ): array {
$path = (string) ( $args['path'] ?? $args['route'] ?? '' );
if ( '' === $path || ! str_starts_with( $path, '/' ) ) {
return array( 'status' => 'error', 'observation' => $observation, 'diagnostic' => self::fuzz_suite_diagnostic( 'error', 'wp_codebox_fuzz_rest_path_invalid', 'REST fuzz step requires an absolute path.', array( 'case_id' => $case_id, 'path' => $path ) ) );
}
$request = new WP_REST_Request( strtoupper( (string) ( $args['method'] ?? 'GET' ) ), $path );
foreach ( self::json_arg( $args['params-json'] ?? '' ) as $key => $value ) {
$request->set_param( (string) $key, $value );
}
foreach ( self::json_arg( $args['headers-json'] ?? '' ) as $key => $value ) {
$request->set_header( (string) $key, (string) $value );
}
if ( isset( $args['body-json'] ) && '' !== $args['body-json'] ) {
$request->set_body_params( self::json_arg( $args['body-json'] ) );
} elseif ( isset( $args['body'] ) ) {
$request->set_body( $args['body'] );
}
$response = rest_do_request( $request );
$status = (int) $response->get_status();
$observation['status'] = $status;
$observation['path'] = $path;
return array( 'status' => $status >= 500 ? 'failed' : 'passed', 'observation' => $observation, 'diagnostic' => $status >= 500 ? self::fuzz_suite_diagnostic( 'error', 'wp_codebox_fuzz_rest_request_failed', 'REST request returned a server error.', array( 'case_id' => $case_id, 'status' => $status, 'path' => $path ) ) : null );
}
/** @param array<string,string> $args Args. @param array<string,mixed> $observation Observation. @return array<string,mixed> */
private static function execute_fuzz_suite_http_request( array $args, array $observation, string $case_id ): array {
$url = (string) ( $args['url'] ?? $args['path'] ?? '' );
if ( '' === $url || preg_match( '#^https?://#i', $url ) ) {
return array( 'status' => 'error', 'observation' => $observation, 'diagnostic' => self::fuzz_suite_diagnostic( 'error', 'wp_codebox_fuzz_http_url_invalid', 'HTTP fuzz step only accepts same-site relative URLs.', array( 'case_id' => $case_id, 'url' => $url ) ) );
}
$response = wp_remote_request( home_url( '/' . ltrim( $url, '/' ) ), array( 'method' => strtoupper( (string) ( $args['method'] ?? 'GET' ) ), 'timeout' => 15 ) );
if ( is_wp_error( $response ) ) {
return array( 'status' => 'failed', 'observation' => $observation, 'diagnostic' => self::fuzz_suite_diagnostic( 'error', 'wp_codebox_fuzz_http_request_failed', $response->get_error_message(), array( 'case_id' => $case_id, 'url' => $url ) ) );
}
$code = (int) wp_remote_retrieve_response_code( $response );
$observation['status'] = $code;
$observation['url'] = $url;
return array( 'status' => $code >= 500 ? 'failed' : 'passed', 'observation' => $observation );
}
/** @param array<string,string> $args Args. @param array<string,mixed> $case Case. @param array<string,mixed> $suite Suite. @param array<string,mixed> $observation Observation. @return array<string,mixed> */
private static function execute_fuzz_suite_browser_coverage( array $args, array $case, array $suite, array $observation, string $case_id ): array {
$targets = self::fuzz_suite_browser_coverage_targets( $args );
if ( empty( $targets ) ) {
return array( 'status' => 'error', 'observation' => $observation, 'diagnostic' => self::fuzz_suite_diagnostic( 'error', 'wp_codebox_fuzz_browser_coverage_target_missing', 'Browser coverage requires at least one safe same-site path or URL.', array( 'case_id' => $case_id ) ) );
}
$started_at = gmdate( 'Y-m-d\TH:i:s\Z' );
$requests = array();
$failed = 0;
foreach ( $targets as $target ) {
$url = self::fuzz_suite_browser_coverage_url( (string) $target['path'] );
if ( '' === $url ) {
$requests[] = array( 'surface' => $target['surface'], 'path' => $target['path'], 'status' => 'skipped', 'diagnostics' => array( self::fuzz_suite_diagnostic( 'warning', 'wp_codebox_fuzz_browser_coverage_unsafe_url', 'Browser coverage target is not a safe same-site URL.', array( 'case_id' => $case_id, 'path' => $target['path'] ) ) ) );
continue;
}
$response = wp_remote_request( $url, array( 'method' => 'GET', 'timeout' => 15, 'redirection' => 0 ) );
if ( is_wp_error( $response ) ) {
$failed++;
$requests[] = array( 'surface' => $target['surface'], 'path' => $target['path'], 'url' => $url, 'status' => 'failed', 'diagnostics' => array( self::fuzz_suite_diagnostic( 'error', 'wp_codebox_fuzz_browser_coverage_request_failed', $response->get_error_message(), array( 'case_id' => $case_id, 'path' => $target['path'] ) ) ) );
continue;
}