Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Prevent fatal error in JsonRpc-Client #5956

Closed
wants to merge 1 commit into from

2 participants

@marcuswinkler

Zend\Server\Json\Response passes the json_decode()'d server response to its method setOptions(), which is type hinted as array. If the server response is anything but an array, this leads to a fatal error.

Maybe the Response class should be altogether more strict in its parsing of the response, but for now I just throw a RuntimeException, as pretty much anything is preferable to a fatal error.

@Ocramius Ocramius added this to the 2.3.1 milestone
@Ocramius Ocramius self-assigned this
@Ocramius Ocramius closed this in a52e105
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
4 library/Zend/Json/Server/Response.php
@@ -79,10 +79,14 @@ public function setOptions(array $options)
*
* @param string $json
* @return void
+ * @throws Exception\RuntimeException
*/
public function loadJson($json)
{
$options = Json::decode($json, Json::TYPE_ARRAY);
+ if (!is_array($options)) {
+ throw new Exception\RuntimeException('json is not a valid response; array expected');
+ }
$this->setOptions($options);
}
View
8 tests/ZendTest/Json/Server/ClientTest.php
@@ -217,6 +217,14 @@ public function testCustomHttpClientUserAgentIsNotOverridden()
$this->assertSame($expectedUserAgent, $this->httpClient->getHeader('User-Agent'));
}
+ public function testScalarServerResponseThrowsException()
+ {
+ $response = $this->makeHttpResponseFrom('false');
+ $this->httpAdapter->setResponse($response);
+ $this->setExpectedException('Zend\Json\Exception\ExceptionInterface');
+ $this->jsonClient->call('foo');
+ }
+
// Helpers
public function setServerResponseTo($nativeVars)
{
View
14 tests/ZendTest/Json/Server/ResponseTest.php
@@ -173,6 +173,20 @@ public function testCastToStringShouldCastToJSON()
$this->assertEquals($this->response->getId(), $test['id']);
}
+ /**
+ * @dataProvider provideScalarJSONResponses
+ */
+ public function testLoadingScalarJSONResponseShouldThrowException($json)
+ {
+ $this->setExpectedException('Zend\Json\Server\Exception\RuntimeException');
+ $this->response->loadJson($json);
+ }
+
+ public function provideScalarJSONResponses()
+ {
+ return array(array(''), array('true'), array('null'), array('3'), array('"invalid"'));
+ }
+
public function getOptions()
{
return array(
Something went wrong with that request. Please try again.