getsentry / sentry-php

The official PHP SDK for Sentry (sentry.io)
https://sentry.io
MIT License
1.83k stars 452 forks source link

[2.0] SanitizeHttpHeadersMiddleware error while submitting event #665

Closed mindplay-dk closed 6 years ago

mindplay-dk commented 6 years ago

Just for testing, I've placed a deliberate throw new RuntimeException('whoa') somewhere in my code, as I'm attempting to submit my first event via Sentry\Client::captureException():

image

Note that I'm not using ErrorHandler - we have an error-handler in our stack already, so I'm attempting to use just the Sentry\Client, which seems like something you had more in mind with version 2 than with version 1. (?)

I'm getting the following inner/outer exceptions:

Fatal error: Uncaught TypeError: Argument 2 passed to Sentry\Middleware\MiddlewareStack::Sentry\Middleware{closure}() must implement interface Psr\Http\Message\ServerRequestInterface or be null, array given, called in /mnt/c/workspace/test/kodus-media-admin/vendor/sentry/sentry/src/Middleware/SanitizeHttpHeadersMiddleware.php on line 77 and defined in /mnt/c/workspace/test/kodus-media-admin/vendor/sentry/sentry/src/Middleware/MiddlewareStack.php on line 154

TypeError: Argument 2 passed to Sentry\Middleware\MiddlewareStack::Sentry\Middleware{closure}() must implement interface Psr\Http\Message\ServerRequestInterface or be null, array given, called in /mnt/c/workspace/test/kodus-media-admin/vendor/sentry/sentry/src/Middleware/SanitizeHttpHeadersMiddleware.php on line 77 in /mnt/c/workspace/test/kodus-media-admin/vendor/sentry/sentry/src/Middleware/MiddlewareStack.php on line 154

The tail of the stack-trace looks like this:

36 0.2087 1689856 Sentry\Middleware\SanitizeHttpHeadersMiddleware->__invoke( object(Sentry\Event)[406], object(Closure)[367], object(Zend\Diactoros\ServerRequest)[373], object(RuntimeException)[315], array(1) ) .../MiddlewareStack.php:155
37 0.2087 1690824 Sentry\Middleware\MiddlewareStack->Sentry\Middleware{closure}( object(Sentry\Event)[406], array(4), object(RuntimeException)[315], array(1) ) .../SanitizeHttpHeadersMiddleware.php:77

Looks like $request is in deed an array and not a PSR Request:

image

I'm not sure why that's happening - maybe a bug in the previous middleware?

I'm doing no additional configuration of the client beyond just ["dsn" => "..."].

Here's a dump of all installed package versions:

behat/gherkin                       v4.5.1                       Gherkin DSL parser for PHP 5.3
clue/stream-filter                  v1.4.0                       A simple and modern approach to stream filtering in PHP
codeception/codeception             2.5.0                        BDD-style testing framework
codeception/phpunit-wrapper         6.0.12                       PHPUnit classes used by Codeception
codeception/stub                    2.0.4                        Flexible Stub wrapper for PHPUnit's Mock Builder
container-interop/container-interop 1.2.0                        Promoting the interoperability of container objects (DIC, SL, etc.)
dchesterton/image                   dev-master 7658cbc           Image metadata library
doctrine/instantiator               1.1.0                        A small, lightweight utility to instantiate objects in PHP without invokin...
eloquent/composer-npm-bridge        4.0.1                        NPM integration for Composer packages.
facebook/webdriver                  1.6.0                        A PHP client for Selenium WebDriver
guzzlehttp/guzzle                   6.3.3                        Guzzle is a PHP HTTP client library
guzzlehttp/promises                 v1.3.1                       Guzzle promises library
guzzlehttp/psr7                     1.4.2                        PSR-7 message implementation that also provides common utility methods
hamcrest/hamcrest-php               v1.2.2                       This is the PHP port of Hamcrest Matchers
http-interop/http-server-handler    1.0.0                        Common interface for HTTP server-side request handler
http-interop/http-server-middleware 1.1.1                        Common interface for HTTP server-side middleware
kodus/admin-ui                      1.3.0.x-dev 77b4739          Kodus Administration UI
kodus/amd                           1.0.4                        RequireJS integration for Kodus
kodus/authentication                0.6.3                        User Authentication
kodus/database                      0.7.6                        Kodus integration for mindplay/sql
kodus/file-cache                    1.1.0                        Minimal PSR-16 cache-implementation
kodus/installer                     0.2.2                        Installs kodus-module packages
kodus/jpeg                          1.0.1                        simple service-wrapper for the jpeg-recompress tool (for Linux/Mac/Windows)
kodus/mail                          1.0.0                        Simple, modern library of services for sending UTF-8 e-mail
kodus/media                         1.0.0.x-dev d7a27c7          Media management module
kodus/mock-cache                    1.0.1                        PSR-16 cache-implementation for integration tests
kodus/score                         dev-sentry-prototype 90e9a3a Kodus System Core
kodus/session                       2.1.0                        A simple interface for storing and retrieving session data without the use...
kodus/sql-split                     1.0.0                        A simple facility to split SQL files into individual queries - supports My...
kodus/tempkit                       1.0.1                        Implements a server-side strategy for collection and recovery of PSR-7 Upl...
kodus/test                          0.4.8                        provides common dependencies for testing Kodus packages
kodus/ticket                        0.3.4                        Ticket service
kodus/user                          0.8.0                        User repository API
kodus/user-session                  0.4.3
kodus/user-ui                       0.4.6
kodus/uuid-v4                       1.1.0                        Simple UUID v4 generator
league/flysystem                    1.0.47                       Filesystem abstraction: Many filesystems, one API.
marcj/topsort                       1.1.0                        High-Performance TopSort/Dependency resolving algorithm
middlewares/client-ip               v0.7.0                       Middleware to detect the client ip and save it as a request attribute
mindplay/composer-locator           2.1.3                        Locates Composer package root folders by package name
mindplay/implant                    2.0.2                        Simple packaging and dependency sorting for embedded JS and CSS assets
mindplay/kissform                   0.3.4                        Model-driven form rendering and input validation
mindplay/kisstpl                    1.3.3                        View service / template engine for plain PHP templates
mindplay/lang                       1.1.2                        ultra-simple, super-lightweight localization facility
mindplay/middleman                  3.0.3                        PSR-7 middleware dispatcher. Let's stop trying to make this complicated.
mindplay/pager                      1.1.0                        Renders HTML pagers. Nothing else.
mindplay/readable                   1.1.1                        Formats PHP values as human-readable strings
mindplay/sql                        0.7.5                        Database framework and query builder
mindplay/timber                     2.3.0                        Timber is a request router with regular expression support, high performan...
mindplay/unbox                      2.0.2                        Fast, simple, easy-to-use DI container
mockery/mockery                     0.9.9                        Mockery is a simple yet flexible PHP mock object framework for use in unit...
myclabs/deep-copy                   1.8.1                        Create deep copies (clones) of your objects
paragonie/random_compat             v9.99.99                     PHP 5.x polyfill for random_bytes() and random_int() from PHP 7
phar-io/manifest                    1.0.1                        Component for reading phar.io manifest information from a PHP Archive (PHAR)
phar-io/version                     1.0.1                        Library for handling version information and constraints
php-http/client-common              1.7.0                        Common HTTP Client implementations and tools for HTTPlug
php-http/curl-client                v1.7.1                       cURL client for PHP-HTTP
php-http/discovery                  1.4.0                        Finds installed HTTPlug implementations and PSR-7 message factories
php-http/httplug                    v1.1.0                       HTTPlug, the HTTP client abstraction for PHP
php-http/message                    1.7.0                        HTTP Message related tools
php-http/message-factory            v1.0.2                       Factory interfaces for PSR-7 HTTP Message
php-http/promise                    v1.0.0                       Promise used for asynchronous HTTP requests
phpdocumentor/reflection-common     1.0.1                        Common reflection classes used by phpdocumentor to reflect the code structure
phpdocumentor/reflection-docblock   4.3.0                        With this component, a library can provide support for annotations via Doc...
phpdocumentor/type-resolver         0.4.0
phpspec/prophecy                    1.8.0                        Highly opinionated mocking framework for PHP 5.3+
phpunit/php-code-coverage           5.3.2                        Library that provides collection, processing, and rendering functionality ...
phpunit/php-file-iterator           1.4.5                        FilterIterator implementation that filters files based on a list of suffixes.
phpunit/php-text-template           1.2.1                        Simple template engine.
phpunit/php-timer                   1.0.9                        Utility class for timing
phpunit/php-token-stream            2.0.2                        Wrapper around PHP's tokenizer extension.
phpunit/phpunit                     6.5.13                       The PHP Unit Testing framework.
phpunit/phpunit-mock-objects        5.0.10                       Mock Object library for PHPUnit
psr/container                       1.0.0                        Common Container Interface (PHP FIG PSR-11)
psr/http-message                    1.0.1                        Common interface for HTTP messages
psr/http-server-handler             1.0.0                        Common interface for HTTP server-side request handler
psr/http-server-middleware          1.0.0                        Common interface for HTTP server-side middleware
psr/log                             1.0.2                        Common interface for logging libraries
psr/simple-cache                    1.0.1                        Common interfaces for simple caching
ramsey/uuid                         3.8.0                        Formerly rhumsaa/uuid. A PHP 5.4+ library for generating RFC 4122 version ...
sebastian/code-unit-reverse-lookup  1.0.1                        Looks up which function or method a line of code belongs to
sebastian/comparator                2.1.3                        Provides the functionality to compare PHP values for equality
sebastian/diff                      2.0.1                        Diff implementation
sebastian/environment               3.1.0                        Provides functionality to handle HHVM/PHP environments
sebastian/exporter                  3.1.0                        Provides the functionality to export PHP variables for visualization
sebastian/global-state              2.0.0                        Snapshotting of global state
sebastian/object-enumerator         3.0.3                        Traverses array structures and object graphs to enumerate all referenced o...
sebastian/object-reflector          1.1.1                        Allows reflection of object attributes, including inherited and non-public...
sebastian/recursion-context         3.0.0                        Provides functionality to recursively process PHP variables
sebastian/resource-operations       1.0.0                        Provides a list of PHP built-in functions that operate on resources
sebastian/version                   2.0.1                        Library that helps with managing the version number of Git-hosted PHP proj...
sentry/sentry                       2.0.x-dev 263e0ad            A PHP client for Sentry (http://getsentry.com)
symfony/browser-kit                 v4.1.6                       Symfony BrowserKit Component
symfony/console                     v3.4.17                      Symfony Console Component
symfony/css-selector                v4.1.6                       Symfony CssSelector Component
symfony/debug                       v4.1.6                       Symfony Debug Component
symfony/dom-crawler                 v4.1.6                       Symfony DomCrawler Component
symfony/event-dispatcher            v4.1.6                       Symfony EventDispatcher Component
symfony/filesystem                  v3.4.17                      Symfony Filesystem Component
symfony/finder                      v4.1.6                       Symfony Finder Component
symfony/options-resolver            v3.4.17                      Symfony OptionsResolver Component
symfony/polyfill-ctype              v1.9.0                       Symfony polyfill for ctype functions
symfony/polyfill-mbstring           v1.9.0                       Symfony polyfill for the Mbstring extension
symfony/process                     v4.1.6                       Symfony Process Component
symfony/yaml                        v4.1.6                       Symfony Yaml Component
theseer/tokenizer                   1.1.0                        A small library for converting tokenized PHP source code into XML and pote...
webmozart/assert                    1.3.0                        Assertions to validate method input/output with nice error messages.
willdurand/negotiation              v2.3.1                       Content Negotiation tools for PHP provided as a standalone library.
zendframework/zend-diactoros        1.8.6                        PSR HTTP Message implementations

Anything else I can do or provide to help fix this?

Jean85 commented 6 years ago

Thank you a lot for providing this! It's an invaluable way to help us. We will investigate this, but we probably have some changes to apply to the branch. We'll see...

ste93cry commented 6 years ago

First of all, thanks for the feedback, I think you're the first one ever trying the 2.0 branch and your feedback is welcome and highly appreciated. As said by @Jean85 we might have some BC changes incoming starting from next week, but we're not sure yet so keep an eye on the repository if you want to work with this version as its API should be almost complete and pretty stable now but it may not be in the future

I'm not sure why that's happening - maybe a bug in the previous middleware?

The bug is not in the previous middleware but in the SanitizeHttpHeadersMiddleware middleware itself. In fact the event payload contains an array representing the request, but the middleware receives a PSR-7 object that they can use in whichever way they want to retrieve data and fill the event information. Unfortunately in some refactoring I probably missed that I've called a local variable with the same name as the argument parameter $request, which means that it gets overridden and then passed to the next middleware causing the bug

https://github.com/getsentry/sentry-php/blob/263e0adbcfbb8e818ba1b937001263d134a02338/src/Middleware/SanitizeHttpHeadersMiddleware.php#L56-L58

The fix is pretty simple, if you want we would be glad to accept a PR that fixes the issue

Jean85 commented 6 years ago

I'll do the PR!

ste93cry commented 6 years ago

This should be fixed. If you're still trying the 2.0 branch updating should make it work. Please let us know if the problem still occurs, but please note that BC changes are coming and so using this version now could mean a lot of work to keep it up with the new developments until the API reach a stable point

pkd809 commented 5 years ago

Parse error: syntax error, unexpected '?', expecting variable (T_VARIABLE) in /home/vagrant/vendor/zendframework/zend-diactoros/src/functions/marshal_uri_from_sapi.php on line 83

Getting the above error when I initialize senry fucntion.

ste93cry commented 5 years ago

This error does not belongs to this issue. Please open an issue in zendframework/zend-diactoros or check that your PHP version is at least 7.1