Skip to content

Commit

Permalink
Protect WAF: Ensure request body is parsed correctly (#39262)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Nate Weller <[email protected]>
  • Loading branch information
2 people authored and ice9js committed Sep 10, 2024
1 parent e522e27 commit 6487d3c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions projects/packages/waf/changelog/fix-waf-request-post-data
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Waf: Ensure that request body is parsed correctly
14 changes: 9 additions & 5 deletions projects/packages/waf/src/class-waf-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,21 @@ public function get_get_vars() {
* @return array{string, scalar}[]
*/
public function get_post_vars() {
$content_type = $this->get_header( 'content-type' );
if ( ! empty( $_POST ) ) {
// If $_POST is populated, use it.
return flatten_array( $_POST );
} elseif ( strpos( $this->get_header( 'content-type' ), 'application/json' ) !== false ) {
} elseif ( strpos( $content_type, 'application/json' ) !== false ) {
// Attempt to decode JSON requests.
$decoded_json = json_decode( $this->get_body(), true ) ?? array();
return flatten_array( $decoded_json, 'json', true );
} else {
// Attempt to retrieve all parameters when method used isn't POST
$body = $this->get_body();
parse_str( $body, $params );
} elseif ( strpos( $content_type, 'application/x-www-form-urlencoded' ) !== false ) {
// Attempt to decode url-encoded data
parse_str( $this->get_body(), $params );
return flatten_array( $params );
} else {
// Don't try to parse any other content types
return array();
}
}

Expand Down
17 changes: 15 additions & 2 deletions projects/packages/waf/tests/php/unit/test-waf-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public function testGetVarsPost() {
}

/**
* Test that the Waf_Request class returns $_POST data correctly decoded from JSON via Waf_Request::get_post_vars().
* Test that the Waf_Request class returns POST-ed data correctly decoded from JSON via Waf_Request::get_post_vars().
*/
public function testGetVarsPostWithJson() {
$_SERVER['CONTENT_TYPE'] = 'application/json';
Expand All @@ -329,10 +329,23 @@ public function testGetVarsPostWithJson() {
unset( $_SERVER['CONTENT_TYPE'] );
}

/**
* Test that the Waf_Request class returns POST data correctly when the content is XML
*/
public function testGetVarsPostWithXml() {
$_SERVER['CONTENT_TYPE'] = 'text/xml';
$request = $this->mock_request(
array(
'body' => '<?xml version="1.0"?><methodCall><methodName>methodName</methodName><params><param><value><string>AB</string></value></param></params></methodCall>',
)
);
$this->assertEmpty( $request->get_post_vars() );
}

/**
* Test that the Waf_Request class returns any parameters when HTTP method isn't POST.
*/
public function testGetVarsPostHttpMethodNotPost() {
public function testGetVarsPostWithUrlEncoded() {
$_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
$request = $this->mock_request(
array(
Expand Down

0 comments on commit 6487d3c

Please sign in to comment.