Skip to content

Commit 8b80371

Browse files
committed
improve thrown exception to include the blackhole reason details
1 parent b64bcff commit 8b80371

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed

src/Controller/Component/SecurityComponent.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ public function requireAuth($actions)
180180
*
181181
* @param \Cake\Controller\Controller $controller Instantiating controller
182182
* @param string $error Error method
183-
* @param \Cake\Controller\Exception\SecurityException $exception Additional debug info describing the cause,
184-
* debug mode only
183+
* @param \Cake\Controller\Exception\SecurityException $exception Additional debug info describing the cause
185184
* @return mixed If specified, controller blackHoleCallback's response, or no return otherwise
186185
* @see \Cake\Controller\Component\SecurityComponent::$blackHoleCallback
187186
* @link http://book.cakephp.org/3.0/en/controllers/components/security.html#handling-blackhole-callbacks
@@ -190,14 +189,30 @@ public function requireAuth($actions)
190189
public function blackHole(Controller $controller, $error = '', SecurityException $exception = null)
191190
{
192191
if (!$this->_config['blackHoleCallback']) {
193-
if (Configure::read('debug') && $exception !== null) {
194-
throw $exception;
195-
}
196-
throw new BadRequestException('The request has been black-holed');
192+
$this->_throwException($exception);
197193
}
198194
return $this->_callback($controller, $this->_config['blackHoleCallback'], [$error, $exception]);
199195
}
200196

197+
/**
198+
* Check debug status and throw an Exception based on the existing one
199+
*
200+
* @param \Cake\Controller\Exception\SecurityException $exception Additional debug info describing the cause
201+
* @throws \Cake\Network\Exception\BadRequestException
202+
*/
203+
protected function _throwException($exception = null)
204+
{
205+
$defaultMessage = 'The request has been black-holed';
206+
if ($exception !== null) {
207+
if (!Configure::read('debug')) {
208+
$exception->setReason($exception->getMessage());
209+
$exception->setMessage($defaultMessage);
210+
}
211+
throw $exception;
212+
}
213+
throw new BadRequestException($defaultMessage);
214+
}
215+
201216
/**
202217
* Sets the actions that require a $method HTTP request, or empty for all actions
203218
*
@@ -323,7 +338,7 @@ protected function _validatePost(Controller $controller)
323338
*
324339
* @param \Cake\Controller\Controller $controller Instantiating controller
325340
* @throws \Cake\Controller\Exception\SecurityException
326-
* @return String fields token
341+
* @return string fields token
327342
*/
328343
protected function _validToken(Controller $controller)
329344
{

src/Controller/Exception/SecurityException.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ class SecurityException extends BadRequestException
2525
*/
2626
protected $_type = 'secure';
2727

28+
/**
29+
* Reason for request blackhole
30+
*
31+
* @var string
32+
*/
33+
protected $_reason = null;
34+
2835
/**
2936
* Getter for type
3037
*
@@ -34,4 +41,36 @@ public function getType()
3441
{
3542
return $this->_type;
3643
}
44+
45+
/**
46+
* Set Message
47+
*
48+
* @param string $message Exception message
49+
* @return void
50+
*/
51+
public function setMessage($message)
52+
{
53+
$this->message = $message;
54+
}
55+
56+
/**
57+
* Set Reason
58+
*
59+
* @param string $reason Reason details
60+
* @return void
61+
*/
62+
public function setReason($reason = null)
63+
{
64+
$this->_reason = $reason;
65+
}
66+
67+
/**
68+
* Get Reason
69+
*
70+
* @return string
71+
*/
72+
public function getReason()
73+
{
74+
return $this->_reason;
75+
}
3776
}

tests/TestCase/Controller/Component/SecurityComponentTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1431,11 +1431,13 @@ public function testBlackholeThrowsBadRequest()
14311431
Configure::write('debug', false);
14321432
try {
14331433
$this->Security->blackHole($this->Controller, 'auth', new SecurityException('error description'));
1434-
} catch (BadRequestException $ex) {
1434+
} catch (SecurityException $ex) {
14351435
$message = $ex->getMessage();
1436+
$reason = $ex->getReason();
14361437
}
14371438
Configure::write('debug', $debug);
14381439
$this->assertEquals('The request has been black-holed', $message);
1440+
$this->assertEquals('error description', $reason);
14391441
}
14401442

14411443
/**

0 commit comments

Comments
 (0)