Skip to content

Commit 29ac837

Browse files
author
Jared Hendrickson
committed
Simplified content-type checks, added error response when an unknown or unsupported content type is specified
1 parent d9a6824 commit 29ac837

3 files changed

Lines changed: 35 additions & 8 deletions

File tree

pfSense-pkg-API/files/etc/inc/api/framework/APIModel.inc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,20 @@ class APIModel {
6868
}
6969

7070
public function validate() {
71+
# Checks API status and requirements
7172
$this->check_enable();
7273
$this->check_server_ip();
7374
$this->check_version();
75+
76+
# Checks request data
77+
$this->check_request_data();
78+
79+
# Checks authentication and authorization if required
7480
if ($this->requires_auth) {
7581
$this->check_authentication();
7682
$this->check_authorization();
7783
}
84+
7885
$this->validate_payload();
7986

8087
if (count($this->errors) === 0) {
@@ -152,4 +159,11 @@ class APIModel {
152159
$this->errors[] = APIResponse\get(6);
153160
}
154161

162+
# Check if our requested content-type is supported and parsed data correctly, sets error if not
163+
private function check_request_data() {
164+
if ($this->initial_data === false) {
165+
$this->errors[] = APIResponse\get(11);
166+
}
167+
}
168+
155169
}

pfSense-pkg-API/files/etc/inc/api/framework/APIResponse.inc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,16 @@ function get($id, $data=[], $all=false) {
8181
],
8282
10 => [
8383
"status" => "server error",
84-
"code" => "500",
84+
"code" => 500,
8585
"return" => $id,
8686
"message" => "Your API request was valid but no actions were specified for this endpoint",
8787
],
88+
11 => [
89+
"status" => "bad request",
90+
"code" => 400,
91+
"return" => $id,
92+
"message" => "Specified content type is not supported"
93+
],
8894
// 1000-1999 reserved for /system API calls
8995
1000 => [
9096
"status" => "bad request",

pfSense-pkg-API/files/etc/inc/api/framework/APITools.inc

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,20 @@ use Firebase\JWT\JWT;
3232

3333
# Checks our content type header and parses the content accordingly
3434
function get_request_data() {
35-
# Support application/x-www-form-urlencoded content type
36-
if (strtolower($_SERVER["HTTP_CONTENT_TYPE"] === "application/x-www-form-urlencoded")) {
37-
return $_GET;
38-
}
39-
# Support application/json content type
40-
elseif (strtolower($_SERVER["HTTP_CONTENT_TYPE"] === "application/json")) {
41-
return json_decode(file_get_contents('php://input'), true);
35+
$content_types = [
36+
"application/x-www-form-urlencoded" => $_GET,
37+
"application/json" => json_decode(file_get_contents('php://input'), true)
38+
];
39+
# If client passed in a static content type via header, attempt to parse data using that type
40+
if (!empty($_SERVER["HTTP_CONTENT_TYPE"])) {
41+
# Check if content type is supported, if so return corresponding parsed request data
42+
if (array_key_exists($_SERVER["HTTP_CONTENT_TYPE"], $content_types)) {
43+
return $content_types[$_SERVER["HTTP_CONTENT_TYPE"]];
44+
}
45+
# Return false if the content type is unknown or unsupported
46+
else {
47+
return false;
48+
}
4249
}
4350
# If a static content type was not provided, attempt to determine the content type
4451
else {

0 commit comments

Comments
 (0)