Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding application/json input handling. #546

Merged
merged 2 commits into from
Feb 13, 2018
Merged

Conversation

ryantology
Copy link
Contributor

References #531

  • Added additional unit test for json data.
  • PHP input is sanitized through json_decode
  • If there is binary or invalid data in the json input the entire payload will be discarded. Could run input through sanitize first?

@@ -789,7 +800,7 @@ public function testGetHttpData()
'SERVER_PORT' => '443',
'SERVER_PROTOCOL' => 'HTTP/1.1',
'REQUEST_METHOD' => 'PATCH',
'QUERY_STRING' => 'q=bitch&l=en',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seemed inappropriate, was removed while working on similar work.

@ryantology
Copy link
Contributor Author

Also References #400 #124

Copy link
Collaborator

@Jean85 Jean85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution!

The solution seems ok, I just commented for two thing that I would like you to change. Also, I think we need a test for the "bad path": a malformed JSON and an empty stream.

@@ -276,6 +276,17 @@ public function setEnvironment($value)
return $this;
}

/**
* Note: Prior to PHP 5.6, a stream opened with php://input can
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're targeting the master branch, PHP < 5.6 is allowed, so you should read that only if PHP is >= 5.6; otherwise we would mess with the app.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't really 'mess up the app'. What will happen is the data will be null. I can adjust the comment to explain that. This doesn't introduce a regression issue because the previous implementation and the current one still return the same broken results for php from the dark ages :)

I can add the condition in. I felt that this is really an edge case and there is no clean way to handle support for PHP < 5.6 with this feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one thing that is a little strange is the unit tests will succeed in < 5.6 but the reality is the feature doesn't work in that version. Not sure if there is a way to resolve that. Just a thought.

(I added the conditional https://github.com/getsentry/sentry-php/pull/546/files#diff-11a1d8314f22caf150ad02a00b294b58R287 )

Copy link
Collaborator

@stayallive stayallive Feb 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data will be empty on < 5.6 but it can also be we fetched the data before the user could (if the use the library for logging for example) causing it to possibly break the user's application.

And yes this will not go well for < 5.6 but we should try and solve that differently if we must. But this could affect the implementors application and that is something we really should avoid at all cost.

So 👍 for the added conditional.

@@ -748,6 +759,11 @@ protected function get_http_data()
// instead of a mapping which goes against the defined Sentry spec
if (!empty($_POST)) {
$result['data'] = $_POST;
} elseif (isset($_SERVER['CONTENT_TYPE']) && stripos($_SERVER['CONTENT_TYPE'], 'application/json') === 0) {
$raw_data = $this->getInputStream() ?: false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we will never need the input stream for other stuff, maybe we can wrap the Json handling inside that method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably add input stream handling for XML as well. This test is just for JSON. I really didn't want to get into XML parsing so I left that out. We actually have to deal with a legacy XML API for one project. Once I have to debug that I'll make a new patch :)

This could be abstracted into a parse() method as it gets bigger. I didn't want to spend too much time on this knowing that 2.0 is on the horizon.

@ryantology
Copy link
Contributor Author

ryantology commented Feb 12, 2018

@Jean85 I added the negative test case. This was good as it identified a need to abstract the json encode stuff for future XML tests.

Will squish after your approval

Copy link
Collaborator

@Jean85 Jean85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM 👍 thank you very much!
Squashing is not necessary, it could be done while merging using the GiHub interface

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants