Skip to content
Snippets Groups Projects
Commit d61df1f3 authored by Jared Hancock's avatar Jared Hancock
Browse files

api: Don't crash on unexpected data, just warn

parent 864ea4d1
No related branches found
No related tags found
No related merge requests found
...@@ -53,11 +53,11 @@ class TicketApiController extends ApiController { ...@@ -53,11 +53,11 @@ class TicketApiController extends ApiController {
/* /*
Validate data - overwrites parent's validator for additional validations. Validate data - overwrites parent's validator for additional validations.
*/ */
function validate(&$data, $format) { function validate(&$data, $format, $strict=true) {
global $ost; global $ost;
//Call parent to Validate the structure //Call parent to Validate the structure
if(!parent::validate($data, $format)) if(!parent::validate($data, $format, $strict) && $strict)
$this->exerr(400, 'Unexpected or invalid data received'); $this->exerr(400, 'Unexpected or invalid data received');
//Nuke attachments IF API files are not allowed. //Nuke attachments IF API files are not allowed.
......
...@@ -221,7 +221,7 @@ class ApiController { ...@@ -221,7 +221,7 @@ class ApiController {
$this->exerr(400, $parser->lastError()); $this->exerr(400, $parser->lastError());
//Validate structure of the request. //Validate structure of the request.
$this->validate($data, $format); $this->validate($data, $format, false);
return $data; return $data;
} }
...@@ -241,19 +241,25 @@ class ApiController { ...@@ -241,19 +241,25 @@ class ApiController {
* expected. It is assumed that the functions actually implementing the * expected. It is assumed that the functions actually implementing the
* API will further validate the contents of the request * API will further validate the contents of the request
*/ */
function validateRequestStructure($data, $structure, $prefix="") { function validateRequestStructure($data, $structure, $prefix="", $strict=true) {
global $ost;
foreach ($data as $key=>$info) { foreach ($data as $key=>$info) {
if (is_array($structure) and is_array($info)) { if (is_array($structure) and is_array($info)) {
$search = (isset($structure[$key]) && !is_numeric($key)) ? $key : "*"; $search = (isset($structure[$key]) && !is_numeric($key)) ? $key : "*";
if (isset($structure[$search])) { if (isset($structure[$search])) {
$this->validateRequestStructure($info, $structure[$search], "$prefix$key/"); $this->validateRequestStructure($info, $structure[$search], "$prefix$key/", $strict);
continue; continue;
} }
} elseif (in_array($key, $structure)) { } elseif (in_array($key, $structure)) {
continue; continue;
} }
return $this->exerr(400, "$prefix$key: Unexpected data received"); if ($strict)
return $this->exerr(400, "$prefix$key: Unexpected data received");
else
$ost->logWarning('API Unexpected Data',
"$prefix$key: Unexpected data received in API request",
false);
} }
return true; return true;
...@@ -263,11 +269,12 @@ class ApiController { ...@@ -263,11 +269,12 @@ class ApiController {
* Validate request. * Validate request.
* *
*/ */
function validate(&$data, $format) { function validate(&$data, $format, $strict=true) {
return $this->validateRequestStructure( return $this->validateRequestStructure(
$data, $data,
$this->getRequestStructure($format, $data) $this->getRequestStructure($format, $data),
); "",
$strict);
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment