@@ -2596,34 +2596,57 @@ public function action_restart(Request $request)
2596
2596
)]
2597
2597
public function execute_command_by_uuid (Request $ request )
2598
2598
{
2599
- $ data = $ request ->validate ([
2600
- 'command ' => 'required|string|max:255 ' ,
2601
- ]);
2599
+ // TODO: Need to review this from security perspective, to not allow arbitrary command execution
2600
+ $ allowedFields = ['command ' ];
2602
2601
$ teamId = getTeamIdFromToken ();
2603
2602
if (is_null ($ teamId )) {
2604
2603
return invalidTokenResponse ();
2605
2604
}
2606
2605
$ uuid = $ request ->route ('uuid ' );
2607
- if (!$ uuid ) {
2606
+ if (! $ uuid ) {
2608
2607
return response ()->json (['message ' => 'UUID is required. ' ], 400 );
2609
2608
}
2610
2609
$ application = Application::ownedByCurrentTeamAPI ($ teamId )->where ('uuid ' , $ request ->uuid )->first ();
2611
- if (!$ application ) {
2610
+ if (! $ application ) {
2612
2611
return response ()->json (['message ' => 'Application not found. ' ], 404 );
2613
2612
}
2613
+ $ return = validateIncomingRequest ($ request );
2614
+ if ($ return instanceof \Illuminate \Http \JsonResponse) {
2615
+ return $ return ;
2616
+ }
2617
+ $ validator = customApiValidator ($ request ->all (), [
2618
+ 'command ' => 'string|required ' ,
2619
+ ]);
2620
+
2621
+ $ extraFields = array_diff (array_keys ($ request ->all ()), $ allowedFields );
2622
+ if ($ validator ->fails () || ! empty ($ extraFields )) {
2623
+ $ errors = $ validator ->errors ();
2624
+ if (! empty ($ extraFields )) {
2625
+ foreach ($ extraFields as $ field ) {
2626
+ $ errors ->add ($ field , 'This field is not allowed. ' );
2627
+ }
2628
+ }
2629
+
2630
+ return response ()->json ([
2631
+ 'message ' => 'Validation failed. ' ,
2632
+ 'errors ' => $ errors ,
2633
+ ], 422 );
2634
+ }
2614
2635
2615
2636
$ container = getCurrentApplicationContainerStatus ($ application ->destination ->server , $ application ->id )->firstOrFail ();
2616
2637
$ status = getContainerStatus ($ application ->destination ->server , $ container ['Names ' ]);
2617
2638
2618
- if ('running ' !== $ status ) {
2619
- return ;
2639
+ if ($ status !== 'running ' ) {
2640
+ return response ()->json ([
2641
+ 'message ' => 'Application is not running. ' ,
2642
+ ], 400 );
2620
2643
}
2621
2644
2622
2645
$ commands = collect ([
2623
- executeInDocker ($ container ['Names ' ], $ data [ ' command ' ] ),
2646
+ executeInDocker ($ container ['Names ' ], $ request -> command ),
2624
2647
]);
2625
2648
2626
- $ res = instant_remote_process ($ commands , $ application ->destination ->server );
2649
+ $ res = instant_remote_process (command: $ commands , server: $ application ->destination ->server );
2627
2650
2628
2651
return response ()->json ([
2629
2652
'message ' => 'Command executed. ' ,
0 commit comments