Skip to content

Commit 488797e

Browse files
authored
Merge pull request #27 from stevelacey/html-check
Assert response is html before injecting alert or console outputs
2 parents d790bcf + c4ce25a commit 488797e

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/Outputs/Alert.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Alert implements Output
99
{
1010
public function output(Collection $detectedQueries, Response $response)
1111
{
12-
if ($response->isRedirection()) {
12+
if (stripos($response->headers->get('Content-Type'), 'text/html') !== 0 || $response->isRedirection()) {
1313
return;
1414
}
1515

src/Outputs/Console.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
<?php
2+
23
namespace BeyondCode\QueryDetector\Outputs;
4+
35
use Illuminate\Support\Collection;
46
use Symfony\Component\HttpFoundation\Response;
57

68
class Console implements Output
79
{
810
public function output(Collection $detectedQueries, Response $response)
911
{
10-
if ($response->isRedirection()) {
12+
if (stripos($response->headers->get('Content-Type'), 'text/html') !== 0 || $response->isRedirection()) {
1113
return;
1214
}
15+
1316
$content = $response->getContent();
17+
1418
$outputContent = $this->getOutputContent($detectedQueries);
19+
1520
$pos = strripos($content, '</body>');
21+
1622
if (false !== $pos) {
1723
$content = substr($content, 0, $pos) . $outputContent . substr($content, $pos);
1824
} else {
1925
$content = $content . $outputContent;
2026
}
27+
2128
// Update the new content and reset the content length
2229
$response->setContent($content);
30+
2331
$response->headers->remove('Content-Length');
2432
}
33+
2534
protected function getOutputContent(Collection $detectedQueries)
2635
{
2736
$output = '<script type="text/javascript">';
@@ -33,6 +42,7 @@ protected function getOutputContent(Collection $detectedQueries)
3342
}
3443
$output .= "')";
3544
$output .= '</script>';
45+
3646
return $output;
3747
}
3848
}

0 commit comments

Comments
 (0)