TypeRocket / typerocket

TypeRocket is a highly integrated MVC WordPress framework with beautiful UI components for the modern developer.
https://typerocket.com
454 stars 62 forks source link

[bug] Fields might throw if there are no request fields #236

Closed jdoubleu closed 3 years ago

jdoubleu commented 3 years ago

The HTTP Fields might throw an exception if a request has no fields. That might be the case where these fields are optional.

Analysis

This exception is caused due to how the Fields constructor is written. During internal request processing by TypeRocket, an empty array (of arguments) is passed to the Fields class. In that case, the Fields class obtains the fields from the current request. Since there are none, the request returns null. Later, null is passed to PHP's built-in ArrayObject::exchangeArray(), which causes the exception.

Exception trace:

InvalidArgumentException thrown with message "Passed variable is not an array or object"

Stacktrace:
#21 InvalidArgumentException in /path/to/plugins/typerocket-pro-v5/typerocket/vendor/typerocket/core/src/Http/Fields.php:36
#20 ArrayObject:exchangeArray in /path/to/plugins/typerocket-pro-v5/typerocket/vendor/typerocket/core/src/Http/Fields.php:36
#19 TypeRocket\Http\Fields:__construct in [internal]:0
#18 ReflectionClass:newInstanceArgs in /path/to/plugins/typerocket-pro-v5/typerocket/vendor/typerocket/core/src/Core/Resolver.php:34
#17 TypeRocket\Core\Resolver:resolve in /path/to/plugins/typerocket-pro-v5/typerocket/vendor/typerocket/core/src/Core/Resolver.php:109
#16 TypeRocket\Core\Resolver:getDependencies in /path/to/plugins/typerocket-pro-v5/typerocket/vendor/typerocket/core/src/Core/Resolver.php:71

Steps to reproduce

  1. Create a HTTP Fields class. It does not have to provide any "fillable" fields.
  2. Create a controller and use the newly created fields class as parameter to an action, so it will be dependency injected.
  3. Send a GET request for example, with no fields added.
  4. Observe the exception thrown.