mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
API: Validate columns parameter against fields in table (#7717)
* Validate columns parameter against fields in table * Removed unused return var * Replace schema check from DB with yaml * Style fixes
This commit is contained in:
committed by
Tony Murray
parent
64aed60f09
commit
00fc5bf0e8
@@ -818,6 +818,7 @@ function get_port_graphs()
|
||||
} else {
|
||||
$columns = 'ifName';
|
||||
}
|
||||
validate_column_list($columns, 'ports');
|
||||
|
||||
// use hostname as device_id if it's all digits
|
||||
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
|
||||
@@ -890,6 +891,7 @@ function get_all_ports()
|
||||
} else {
|
||||
$columns = 'ifName';
|
||||
}
|
||||
validate_column_list($columns, 'ports');
|
||||
$ports = dbFetchRows("SELECT $columns FROM `ports` WHERE `deleted` = 0");
|
||||
|
||||
$output = array(
|
||||
@@ -1693,3 +1695,25 @@ function list_logs()
|
||||
$app->response->headers->set('Content-Type', 'application/json');
|
||||
echo _json_encode($output);
|
||||
}
|
||||
|
||||
function validate_column_list($columns, $tableName)
|
||||
{
|
||||
global $config;
|
||||
|
||||
$column_names = explode(',', $columns);
|
||||
$db_schema = Symfony\Component\Yaml\Yaml::parse(file_get_contents($config['install_dir'] . '/misc/db_schema.yaml'));
|
||||
$valid_columns = array_column($db_schema[$tableName]['Columns'], 'Field');
|
||||
$invalid_columns = array_diff(array_map('trim', $column_names), $valid_columns);
|
||||
|
||||
if (count($invalid_columns) > 0) {
|
||||
$output = array(
|
||||
'status' => 'error',
|
||||
'message' => 'Invalid columns: ' . join(',', $invalid_columns),
|
||||
);
|
||||
$app = \Slim\Slim::getInstance();
|
||||
$app->response->setStatus(400); // Bad request
|
||||
$app->response->headers->set('Content-Type', 'application/json');
|
||||
echo _json_encode($output);
|
||||
$app->stop();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user