Skip to content

Commit

Permalink
Merge pull request #27 from stevelacey/html-check
Browse files Browse the repository at this point in the history
Assert response is html before injecting alert or console outputs
  • Loading branch information
mpociot committed Jul 24, 2018
2 parents d790bcf + c4ce25a commit 488797e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Outputs/Alert.php
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
@@ -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;
}
}

0 comments on commit 488797e

Please sign in to comment.