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

[BUG] getJsonRawBody() never returns false #13501

Closed
michalzielanski opened this Issue Sep 21, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@michalzielanski

michalzielanski commented Sep 21, 2018

The condition in the getJsonRawBody() method looks like this:

let rawBody = this->getRawBody();
if typeof rawBody != "string" {
return false;
}

getRawBody() method looks like this:

let rawBody = this->_rawBody;
if empty rawBody {
let contents = file_get_contents("php://input");
/**
* We need store the read raw body because it can't be read again
*/
let this->_rawBody = contents;
return contents;
}
return rawBody;

file_get_contents("php://input") always return string, so the condition if typeof rawBody != "string" is never satisfied.

I think the getJsonRawBody() method should look like this:

public function getJsonRawBody(boolean associative = false) -> <\stdClass> | array | boolean
{
	var rawBody, data;

	let rawBody = this->getRawBody();
	if rawBody === "" {
		return false;
	}

	let data = json_decode(rawBody, associative);

	if json_last_error() !== JSON_ERROR_NONE {
		return false;
	}

	return data;
}
@Jurigag

This comment has been minimized.

Member

Jurigag commented Sep 21, 2018

I guess you are right, let's consider it as a bug and fix it for next version.

@Jurigag Jurigag added the Bug - Low label Sep 21, 2018

@Jurigag Jurigag added this to the 3.4.1 milestone Sep 21, 2018

CameronHall added a commit to CameronHall/cphalcon that referenced this issue Oct 8, 2018

niden added a commit that referenced this issue Oct 8, 2018

Merge pull request #13516 from CameronHall/bugfix/getRawJsonBody
Fixes #13501: getJsonRawBody() handles empty body and JSON decode errors
@michalzielanski

This comment has been minimized.

michalzielanski commented Oct 9, 2018

Fixed in 0629bb3. Thanks @CameronHall.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment