-
Notifications
You must be signed in to change notification settings - Fork 18
Fragile data in response #1
Changes from 2 commits
314f73f
e733264
2ce4dbc
470eeed
a82dc24
34b0b8b
6a82d85
b50c9d7
bf6e750
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,13 +13,15 @@ | |
use RuntimeException; | ||
use Zend\ProblemDetails\Exception\InvalidResponseBodyException; | ||
use Zend\ProblemDetails\Exception\ProblemDetailsException; | ||
use Zend\ProblemDetails\ProblemDetailsResponse; | ||
use Zend\ProblemDetails\ProblemDetailsResponseFactory; | ||
|
||
class ProblemDetailsResponseFactoryTest extends TestCase | ||
{ | ||
use ProblemDetailsAssertionsTrait; | ||
|
||
private $request; | ||
private $factory; | ||
|
||
protected function setUp() : void | ||
{ | ||
$this->request = $this->prophesize(ServerRequestInterface::class); | ||
|
@@ -176,4 +178,14 @@ public function testFactoryRendersPreviousExceptionsInDebugMode() : void | |
$this->assertEquals(101010, $payload['exception']['stack'][0]['code']); | ||
$this->assertEquals('first', $payload['exception']['stack'][0]['message']); | ||
} | ||
|
||
public function testFragileDataInExceptionMessageShouldBeHiddenInResponseBodyInNoneDebugMode() | ||
{ | ||
$fragileMessage = 'Your SQL or password here'; | ||
$exception = new \Exception($fragileMessage); | ||
|
||
$response = $this->factory->createResponseFromThrowable($this->request->reveal(), $exception); | ||
|
||
$this->assertNotContains($fragileMessage, (string) $response->getBody()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a tough call. Generally, we want to include some usable detail here. If we never include the exception message, then what do we include in order to provide useful details to the end-user consuming the API? I would argue that if you have exceptions that have sensitive details in them, you should be catching them, and doing one of the following:
Alternately, we could have a flag for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to provide details you should use Show exception details out of box is dangerous and we should care about this in most common cases by serve safe message. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Anyway I think is better to show There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make it a flag, then, and have it default to not use non- There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 However I worried about number of constructor parameters:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The underlying stream is generally backed by a PHP resource. We cannot guarantee that something else has not written to that, particularly if you are working in an async environment; as such, writing directly to it is unsafe. That's what the "body factory" is for: to generate a new stream that we can inject into a new response, which we can then safely write to.
Okay, so we should separate that out. Do you want to do that?
No: we need a default message to include when the flag is disabled; additionally, it should be configurable. (MOAR CONSTRUCTOR ARGS!) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which message? I don't like how status logic is implemented (and affects on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Title doesn't always depend on status; we simply populate the title and type based on the status if they are not provided. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But question is: when you want to hide details, that user need to see something more? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, let me put it another way: if you make it empty and/or nullable, you'll have to change the signature of That's a subject for another pull request entirely at that point: should Yes, the spec says it can be omitted. My argument is that In the case of exceptions, if we do not include the exception message, even something like "An error occurred on the server preventing processing of your request" is helpful, as then you know it's not likely something you did, but a server-side issue or a bug in the API you're hitting. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IF we're going to declare this explicitly, then we should also include their types via a docblock.