mirror of
https://github.com/maximebf/php-debugbar.git
synced 2025-07-23 09:41:48 +02:00
Add static var to set DataHasher
This commit is contained in:
40
src/DebugBar/DataHasher.php
Normal file
40
src/DebugBar/DataHasher.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the DebugBar package.
|
||||
*
|
||||
* (c) 2013 Maxime Bouroumeau-Fuseau
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace DebugBar;
|
||||
|
||||
/**
|
||||
* Request Hasher
|
||||
*/
|
||||
class DataHasher
|
||||
{
|
||||
public function __construct(private string $key)
|
||||
{
|
||||
}
|
||||
|
||||
public function sign($data)
|
||||
{
|
||||
if (is_array($data)){
|
||||
sort($data);
|
||||
}
|
||||
$data = json_encode($data);
|
||||
|
||||
return hash_hmac('sha256', $data, $this->key);
|
||||
}
|
||||
|
||||
public function verify($data, string $signature)
|
||||
{
|
||||
if (is_array($data) && isset($data['signature'])) {
|
||||
unset ($data['signature']);
|
||||
}
|
||||
|
||||
return hash_equals($this->sign($data), $signature);
|
||||
}
|
||||
}
|
@@ -30,6 +30,8 @@ class DebugBar implements ArrayAccess
|
||||
{
|
||||
public static $useOpenHandlerWhenSendingDataHeaders = false;
|
||||
|
||||
public static DataHasher|null $dataHasher;
|
||||
|
||||
protected $collectors = array();
|
||||
|
||||
protected $data;
|
||||
@@ -470,20 +472,38 @@ class DebugBar implements ArrayAccess
|
||||
return $this->jsRenderer;
|
||||
}
|
||||
|
||||
public function setHashKey($key)
|
||||
|
||||
/**
|
||||
* Returns the default data formater
|
||||
*
|
||||
* @return DataHasher
|
||||
*/
|
||||
public static function setDataHasher(DataHasher $dataHasher)
|
||||
{
|
||||
$this->hashKey = $key;
|
||||
static::$dataHasher = $dataHasher;
|
||||
}
|
||||
|
||||
public function getHashSignature($data)
|
||||
/**
|
||||
* Check if the data hasher is set
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasDataHasher() : bool
|
||||
{
|
||||
if ($this->hashKey === null) {
|
||||
throw new DebugBarException('HashKey must be set before running actions');
|
||||
return static::$dataHasher !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data hasher
|
||||
*
|
||||
* @return DataHasher
|
||||
*/
|
||||
public static function getDataHasher() : DataHasher
|
||||
{
|
||||
if (static::$dataHasher === null) {
|
||||
throw new DebugBarException('DataHasher is not set');
|
||||
}
|
||||
|
||||
$data = json_encode($data);
|
||||
|
||||
return hash_hmac('sha256', $data, $this->hashKey);
|
||||
return static::$dataHasher;
|
||||
}
|
||||
|
||||
// --------------------------------------------
|
||||
|
@@ -11,6 +11,9 @@
|
||||
namespace DebugBar;
|
||||
|
||||
use DebugBar\DataCollector\Actionable;
|
||||
use DebugBar\DataCollector\DataCollector;
|
||||
use DebugBar\DataFormatter\DataFormatter;
|
||||
use DebugBar\DataFormatter\DataFormatterInterface;
|
||||
|
||||
/**
|
||||
* Handler to list and open saved dataset
|
||||
@@ -129,10 +132,14 @@ class OpenHandler
|
||||
throw new DebugBarException("Missing 'collector' and/or 'action' parameter in 'execute' operation");
|
||||
}
|
||||
|
||||
if (!DebugBar::hasDataHasher()) {
|
||||
throw new DebugBarException("Not DataHasher is set in DebugBar, which is required for 'execute' operations");
|
||||
}
|
||||
|
||||
// Get the signature and remove if before checking the payload.
|
||||
$signature = $request['signature'];
|
||||
unset ($request['signature']);
|
||||
if (!hash_equals($this->debugBar->getHashSignature($request), $signature)) {
|
||||
|
||||
if (!DebugBar::getDataHasher()->verify($request, $signature)) {
|
||||
throw new DebugBarException("Signature does not match in 'execute' operation");
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user