Skip to content

Commit

Permalink
Escape doctrine entries (#657)
Browse files Browse the repository at this point in the history
* Escape doctrine entries

* Add PDO browsertest
  • Loading branch information
barryvdh committed Jun 10, 2024
1 parent 6d6791b commit 11896db
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
Binary file added chromedriver
Binary file not shown.
3 changes: 2 additions & 1 deletion demo/bridge/doctrine/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
$product = new Demo\Product();
$product->setName("foobar");


$entityManager->persist($product);
$entityManager->flush();

$entityManager->createQuery("select p from Demo\\Product p where p.name=:c")->setParameter("c", "<script>alert();</script>")->execute();
render_demo_page();
4 changes: 4 additions & 0 deletions demo/pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
$stmt->execute(array('foo'));
$foo = $stmt->fetch();

$stmt = $pdo->prepare('select * from users where name=?');
$stmt->execute(array('<script>alert();</script>'));
$foo = $stmt->fetch();

$pdo->exec('delete from users');

render_demo_page();
16 changes: 15 additions & 1 deletion src/DebugBar/Bridge/DoctrineCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function collect()
foreach ($this->debugStack->queries as $q) {
$queries[] = array(
'sql' => $q['sql'],
'params' => (object) $q['params'],
'params' => (object) $this->getParameters($q),
'duration' => $q['executionMS'],
'duration_str' => $this->formatDuration($q['executionMS'])
);
Expand All @@ -75,6 +75,20 @@ public function collect()
);
}

/**
* Returns an array of parameters used with the query
*
* @return array
*/
public function getParameters($query) : array
{
$params = [];
foreach ($query['params'] as $name => $param) {
$params[$name] = htmlentities($param?:"", ENT_QUOTES, 'UTF-8', false);
}
return $params;
}

/**
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/DebugBar/Tests/Browser/Bridge/DoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testMonologCollector(): void
});

$this->assertEquals('INSERT INTO products (name) VALUES (?)', $statements[1]);
$this->assertCount(3, $statements);
$this->assertCount(4, $statements);
}

}
34 changes: 34 additions & 0 deletions tests/DebugBar/Tests/Browser/PdoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace DebugBar\Tests\Browser;

use DebugBar\Browser\Bridge\WebDriverElement;

class PdoTest extends AbstractBrowserTest
{
public function testMonologCollector(): void
{
$client = static::createPantherClient();

$client->request('GET', '/demo/pdo.php');

// Wait for Debugbar to load
$crawler = $client->waitFor('.phpdebugbar-body');
usleep(1000);

if (!$this->isTabActive($crawler, 'database')) {
$client->click($this->getTabLink($crawler, 'database'));
}

$crawler = $client->waitForVisibility('.phpdebugbar-panel[data-collector=database]');

$statements = $crawler->filter('.phpdebugbar-panel[data-collector=database] .phpdebugbar-widgets-sql')
->each(function($node){
return $node->getText();
});

$this->assertEquals('insert into users (name) values (?)', $statements[1]);
$this->assertCount(7, $statements);
}

}

0 comments on commit 11896db

Please sign in to comment.