Skip to content

Commit 825be2c

Browse files
authored
Check response for avoid inject debugbar on json ajax
1 parent 6fd181a commit 825be2c

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/LaravelDebugbar.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -762,8 +762,8 @@ public function modifyResponse(Request $request, Response $response)
762762
$app['log']->error('Debugbar exception: ' . $e->getMessage());
763763
}
764764
} elseif (
765-
$this->isJsonRequest($request) &&
766-
$app['config']->get('debugbar.capture_ajax', true)
765+
$app['config']->get('debugbar.capture_ajax', true) &&
766+
($this->isJsonRequest($request) || $this->isJsonResponse($response))
767767
) {
768768
try {
769769
if ($this->hasCollector('views') && $response->headers->has('X-Inertia')) {
@@ -790,11 +790,11 @@ public function modifyResponse(Request $request, Response $response)
790790
}
791791
} elseif (
792792
!$app['config']->get('debugbar.inject', true) ||
793-
($response->headers->has('Content-Type') &&
794-
strpos($response->headers->get('Content-Type'), 'html') === false) ||
793+
strpos((string) $response->headers->get('Content-Type'), 'html') === false ||
795794
$request->getRequestFormat() !== 'html' ||
796795
$response->getContent() === false ||
797-
$this->isJsonRequest($request)
796+
$this->isJsonRequest($request) ||
797+
$this->isJsonResponse($response)
798798
) {
799799
try {
800800
// Just collect + store data, don't inject it.
@@ -862,6 +862,32 @@ protected function isJsonRequest(Request $request)
862862
return (isset($acceptable[0]) && $acceptable[0] == 'application/json');
863863
}
864864

865+
/**
866+
* @param \Symfony\Component\HttpFoundation\Response $response
867+
* @return bool
868+
*/
869+
protected function isJsonResponse(Response $response)
870+
{
871+
if ($response->headers->get('Content-Type') == 'application/json') {
872+
return true;
873+
}
874+
875+
try {
876+
$content = $response->getContent();
877+
878+
if (is_string($content)) {
879+
$content = json_decode($content, true);
880+
}
881+
882+
if (is_array($content)) {
883+
return true;
884+
}
885+
} catch (Exception $e) {
886+
}
887+
888+
return false;
889+
}
890+
865891
/**
866892
* Collects the data from the collectors
867893
*

0 commit comments

Comments
 (0)