I am using Jquery Query Builder to create custom SQL Queries. And I am using Ajax to send the data to server and get the results.
But the problem is that this AJAX request is visible via Inspector Agents. And someone can easily change the query.
So i am trying to figure out ways to prevent sql injection. Here is my Javascript code:
// Define Query Option
var query_options = {
plugins: [
'bt-checkbox','sortable'
],
filters: [
{
id: 'CITY',
label: 'city',
type: 'string',
input: 'select',
multiple: true,
plugin: 'select2',
plugin_config: {
multiple: "multiple",
data: []
},
operators: ['is_not_null', 'in', 'not_in', 'not_equal'],
valueSetter: function(rule, value) {}
}
]
}
// Get 'Cities' to use it in PHP File
$.get('custom/getCities', function(q_cities) {
if (q_cities.length) {
q_cities.forEach( function(element, index) {
query_options.filters[0].plugin_config['data'].push(element['CITY']);
});
}
}, 'json'),
// Create Query
// @NOTE: This is where Sql injection can be made.
$('#btn-get').on('click', function() {
var result = $('#builder-basic').queryBuilder('getSQL');
var query = result.sql;
if (!$.isEmptyObject(result)) {
$.ajax
({
url: 'custom/customquery',
// This part is vulnerable to Sql injections.
data: { query: query },
type: 'post',
dataType: "HTML",
success: function(o)
{
if (o.length) {
console.log(o);
}
}
});
}
});
And this is my PHP code:
// Server Side PHP script
public function customquery()
{
if(isset($_POST['query'])){
$query = $_POST['query'];
}
$qry = "SELECT * FROM MYTABLE WHERE " . $query;
$result = $this->db->select($qry);
echo json_encode($result);
}
I know that there are ways to prevent AJAX Sql injection But I couldn't find any answers to a specific Condition like Jquery Query Builder.
Thanks in advance.