File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed
Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -321,7 +321,9 @@ private function getPostedToken(RequestInterface $request): ?string
321321 if ($ body !== '' ) {
322322 $ json = json_decode ($ body );
323323 if ($ json !== null && json_last_error () === JSON_ERROR_NONE ) {
324- return $ json ->{$ this ->config ->tokenName } ?? null ;
324+ $ tokenValue = $ json ->{$ this ->config ->tokenName } ?? null ;
325+
326+ return is_string ($ tokenValue ) ? $ tokenValue : null ;
325327 }
326328
327329 parse_str ($ body , $ parsed );
Original file line number Diff line number Diff line change @@ -342,4 +342,34 @@ public function testGetPostedTokenReturnsNullWhenMaliciousData(): void
342342
343343 $ this ->assertNull ($ method ($ request ));
344344 }
345+
346+ public function testGetPostedTokenReturnsTokenFromJsonInput (): void
347+ {
348+ $ _POST = [];
349+ $ jsonBody = json_encode (['csrf_test_name ' => '8b9218a55906f9dcc1dc263dce7f005a ' ]);
350+ $ request = $ this ->createIncomingRequest ()->setBody ($ jsonBody );
351+ $ method = $ this ->getPrivateMethodInvoker ($ this ->createMockSecurity (), 'getPostedToken ' );
352+
353+ $ this ->assertSame ('8b9218a55906f9dcc1dc263dce7f005a ' , $ method ($ request ));
354+ }
355+
356+ public function testGetPostedTokenReturnsTokenFromFormEncodedInput (): void
357+ {
358+ $ _POST = [];
359+ $ formBody = 'csrf_test_name=8b9218a55906f9dcc1dc263dce7f005a ' ;
360+ $ request = $ this ->createIncomingRequest ()->setBody ($ formBody );
361+ $ method = $ this ->getPrivateMethodInvoker ($ this ->createMockSecurity (), 'getPostedToken ' );
362+
363+ $ this ->assertSame ('8b9218a55906f9dcc1dc263dce7f005a ' , $ method ($ request ));
364+ }
365+
366+ public function testGetPostedTokenReturnsNullFromMaliciousJsonInput (): void
367+ {
368+ $ _POST = [];
369+ $ maliciousJson = json_encode (['csrf_test_name ' => ['malicious ' => 'data ' ]]);
370+ $ request = $ this ->createIncomingRequest ()->setBody ($ maliciousJson );
371+ $ method = $ this ->getPrivateMethodInvoker ($ this ->createMockSecurity (), 'getPostedToken ' );
372+
373+ $ this ->assertNull ($ method ($ request ));
374+ }
345375}
You can’t perform that action at this time.
0 commit comments