Skip to content

Commit

Permalink
Sanitise attr input in FilterTerm to prefent SQL Injection. Fixes GHS…
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac Connor committed Feb 23, 2023
1 parent 57bf25d commit 034ed3e
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions web/includes/FilterTerm.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace ZM;
$validConjunctionTypes = null;

Expand All @@ -13,7 +12,6 @@ function getFilterQueryConjunctionTypes() {
return $validConjunctionTypes;
}


class FilterTerm {
public $filter;
public $index;
Expand All @@ -25,13 +23,14 @@ class FilterTerm {
public $obr;
public $cbr;


public function __construct($filter = null, $term = NULL, $index=0) {
public function __construct($filter = null, $term = null, $index=0) {
$this->filter = $filter;
$validConjunctionTypes = getFilterQueryConjunctionTypes();

$this->index = $index;
$this->attr = $term['attr'];
$this->attr = preg_replace('/[^A-Za-z0-9\.]/', '', $term['attr'], -1, $count);
if ($count) Error("Invalid characters removed from filter attr ${term['attr']}, possible hacking attempt.");
$this->op = $term['op'];
$this->val = $term['val'];
if ( isset($term['cnj']) ) {
Expand Down Expand Up @@ -67,15 +66,14 @@ public function __construct($filter = null, $term = NULL, $index=0) {
public function sql_values() {
$values = array();
if ( !isset($this->val) ) {
Logger::Warning('No value in term'.$this->attr);
Warning('No value in term '.$this->attr);
return $values;
}

$vals = is_array($this->val) ? $this->val : preg_split('/["\'\s]*?,["\'\s]*?/', preg_replace('/^["\']+?(.+)["\']+?$/', '$1', $this->val));
foreach ( $vals as $value ) {
$value_upper = strtoupper($value);
switch ( $this->attr ) {

case 'AlarmedZoneId':
$value = '(SELECT * FROM Stats WHERE EventId=E.Id AND ZoneId='.$value.' AND Score > 0)';
break;
Expand All @@ -86,7 +84,6 @@ public function sql_values() {
$value = '';
break;
case 'MonitorName':
case 'MonitorName':
case 'Name':
case 'Cause':
case 'Notes':
Expand Down

0 comments on commit 034ed3e

Please sign in to comment.