Skip to content
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

Assert response is html before injecting alert or console outputs #27

Merged
merged 2 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Outputs/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Alert implements Output
{
public function output(Collection $detectedQueries, Response $response)
{
if ($response->isRedirection()) {
if (stripos($response->headers->get('Content-Type'), 'text/html') !== 0 || $response->isRedirection()) {
return;
}

Expand Down
12 changes: 11 additions & 1 deletion src/Outputs/Console.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
<?php

namespace BeyondCode\QueryDetector\Outputs;

use Illuminate\Support\Collection;
use Symfony\Component\HttpFoundation\Response;

class Console implements Output
{
public function output(Collection $detectedQueries, Response $response)
{
if ($response->isRedirection()) {
if (stripos($response->headers->get('Content-Type'), 'text/html') !== 0 || $response->isRedirection()) {
return;
}

$content = $response->getContent();

$outputContent = $this->getOutputContent($detectedQueries);

$pos = strripos($content, '</body>');

if (false !== $pos) {
$content = substr($content, 0, $pos) . $outputContent . substr($content, $pos);
} else {
$content = $content . $outputContent;
}

// Update the new content and reset the content length
$response->setContent($content);

$response->headers->remove('Content-Length');
}

protected function getOutputContent(Collection $detectedQueries)
{
$output = '<script type="text/javascript">';
Expand All @@ -33,6 +42,7 @@ protected function getOutputContent(Collection $detectedQueries)
}
$output .= "')";
$output .= '</script>';

return $output;
}
}