@@ -52,7 +52,7 @@ public function __construct(
5252 #[Route('/hook/reports/import ' , methods: ['GET ' ])]
5353 public function importReportMocha (Request $ request ): JsonResponse
5454 {
55- $ response = $ this ->checkAuth ($ request , ReportMochaImporter::FILTER_CAMPAIGNS );
55+ $ response = $ this ->checkAuth ($ request , ReportMochaImporter::FILTER_CAMPAIGNS , true );
5656 if ($ response instanceof JsonResponse) {
5757 return $ response ;
5858 }
@@ -75,7 +75,7 @@ public function importReportMocha(Request $request): JsonResponse
7575 #[Route('/import/report/playwright ' , methods: ['GET ' ])]
7676 public function importReportPlaywright (Request $ request ): JsonResponse
7777 {
78- $ response = $ this ->checkAuth ($ request , ReportPlaywrightImporter::FILTER_CAMPAIGNS );
78+ $ response = $ this ->checkAuth ($ request , ReportPlaywrightImporter::FILTER_CAMPAIGNS , false );
7979 if ($ response instanceof JsonResponse) {
8080 return $ response ;
8181 }
@@ -98,7 +98,7 @@ public function importReportPlaywright(Request $request): JsonResponse
9898 /**
9999 * @param array<string> $allowedCampaigns
100100 */
101- private function checkAuth (Request $ request , array $ allowedCampaigns ): ?JsonResponse
101+ private function checkAuth (Request $ request , array $ allowedCampaigns, bool $ forceCampaign ): ?JsonResponse
102102 {
103103 $ token = $ request ->query ->get ('token ' );
104104 $ this ->filename = $ request ->query ->get ('filename ' );
@@ -155,7 +155,19 @@ private function checkAuth(Request $request, array $allowedCampaigns): ?JsonResp
155155 $ this ->platform = in_array ($ this ->platform , ReportMochaImporter::FILTER_PLATFORMS ) ? $ this ->platform : ReportMochaImporter::FILTER_PLATFORMS [0 ];
156156
157157 $ this ->campaign = $ request ->query ->has ('campaign ' ) ? $ request ->query ->get ('campaign ' ) : null ;
158- $ this ->campaign = in_array ($ this ->campaign , $ allowedCampaigns ) ? $ this ->campaign : $ allowedCampaigns [0 ];
158+ if (!in_array ($ this ->campaign , $ allowedCampaigns )) {
159+ if ($ forceCampaign ) {
160+ $ this ->campaign = $ allowedCampaigns [0 ];
161+ } else {
162+ return new JsonResponse ([
163+ 'message ' => sprintf (
164+ 'The campaign "%s" is not allowed (%s). ' ,
165+ $ this ->campaign ,
166+ implode (', ' , $ allowedCampaigns ),
167+ ),
168+ ], Response::HTTP_FORBIDDEN );
169+ }
170+ }
159171
160172 $ this ->startDate = \DateTime::createFromFormat (\DateTime::RFC3339_EXTENDED , $ this ->jsonContent ->stats ->start ?? $ this ->jsonContent ->stats ->startTime );
161173
0 commit comments