Skip to content

Check response for avoid inject debugbar on json ajax #1558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,7 @@ public function modifyResponse(Request $request, Response $response)
&& !$this->isJsonRequest($request, $response)
&& $response->getContent() !== false
&& in_array($request->getRequestFormat(), [null, 'html'], true)
&& !$this->isJsonResponse($response)
) {
try {
$this->injectDebugbar($response);
Expand Down Expand Up @@ -912,6 +913,34 @@ protected function isJsonRequest(Request $request, Response $response)
return false;
}

/**
* @param \Symfony\Component\HttpFoundation\Response $response
* @return bool
*/
protected function isJsonResponse(Response $response)
{
if ($response->headers->get('Content-Type') == 'application/json') {
return true;
}

$content = $response->getContent();

if (is_string($content)) {
if (function_exists('json_validate')) {
return json_validate($content);
}

// PHP <= 8.2 check
json_decode($content, true);

return json_last_error() === JSON_ERROR_NONE;
} elseif (is_array($content)) {
return true;
}

return false;
}

/**
* Collects the data from the collectors
*
Expand Down
Loading