From ab8daf516fd4c9e3db6c0e361a38b0e4e63c0fa4 Mon Sep 17 00:00:00 2001 From: MDW Date: Tue, 11 Aug 2026 02:21:54 +0200 Subject: [PATCH] Qual: Remove SQL injection suppressions from Phan baseline (#39450) * Qual: Remove SQL injection suppressions from Phan baseline # Qual: Remove SQL injection suppressions from Phan baseline - Removed SqlInjection suppressions from baseline.txt - Removed SqlInjectionUnquotedEscape suppressions from baseline.txt * Qual: Improve SQL query escaping and formatting in contact list # Qual: Improve SQL query escaping and formatting in contact list Improved the SQL query escaping for compatibility with SqlInjection. Does not fix the inappropriate/incomplete escaping through usage of the 'escapeforlike' for REGEX or PGSQL '~'. * Qual: Add SQLInjection suppression comments # Qual: Add SQLInjection suppression comments Added SQLInjection suppression directives: - In modules_import.class.php, for json_encode operations. - In html.formproduct.class.php, for expression "sanitized" by $db->order(). - In html.formwebportal.class.php, for the WHERE clause conditions (supposes safe argument). --------- Co-authored-by: Laurent Destailleur --- dev/tools/phan/baseline.txt | 8 +------- htdocs/contact/list.php | 8 ++++---- htdocs/core/modules/import/modules_import.class.php | 4 ++-- htdocs/product/class/html.formproduct.class.php | 2 +- htdocs/webportal/class/html.formwebportal.class.php | 2 ++ 5 files changed, 10 insertions(+), 14 deletions(-) diff --git a/dev/tools/phan/baseline.txt b/dev/tools/phan/baseline.txt index 324d608a28f..7385108dfb9 100644 --- a/dev/tools/phan/baseline.txt +++ b/dev/tools/phan/baseline.txt @@ -15,8 +15,6 @@ return [ // PhanTypeMismatchProperty : 15+ occurrences // PhanTypeInvalidDimOffset : 10+ occurrences // PhanTypeMismatchDimFetch : 10+ occurrences - // SqlInjection : 7 occurrences - // SqlInjectionUnquotedEscape : 6 occurrences // PhanTypeArraySuspiciousNull : 5 occurrences // PhanUndeclaredMethod : 5 occurrences // PhanTypeExpectedObjectPropAccess : 4 occurrences @@ -30,7 +28,6 @@ return [ // Currently, file_suppressions and directory_suppressions are the only supported suppressions 'file_suppressions' => [ - 'htdocs/blockedlog/admin/filecheck_diff.php' => ['SqlInjection'], 'htdocs/comm/action/index.php' => ['PhanTypeMismatchProperty'], 'htdocs/comm/action/pertype.php' => ['PhanTypeExpectedObjectPropAccess'], 'htdocs/comm/action/peruser.php' => ['PhanTypeMismatchArgument'], @@ -49,7 +46,6 @@ return [ 'htdocs/compta/sociales/card.php' => ['PhanUndeclaredProperty'], 'htdocs/compta/tva/class/paymentvat.class.php' => ['PhanUndeclaredProperty'], 'htdocs/compta/tva/clients.php' => ['PhanTypeArraySuspiciousNull', 'PhanTypeInvalidDimOffset'], - 'htdocs/contact/list.php' => ['SqlInjectionUnquotedEscape'], 'htdocs/core/actions_addupdatedelete.inc.php' => ['PhanUndeclaredProperty'], 'htdocs/core/actions_massactions.inc.php' => ['PhanUndeclaredProperty'], 'htdocs/core/actions_sendmails.inc.php' => ['PhanUndeclaredProperty'], @@ -86,7 +82,6 @@ return [ 'htdocs/core/modules/facture/doc/pdf_octopus.modules.php' => ['PhanTypeMismatchArgument', 'PhanTypeMismatchArgumentNullable', 'PhanUndeclaredProperty'], 'htdocs/core/modules/holiday/mod_holiday_immaculate.php' => ['PhanTypeMismatchArgument'], 'htdocs/core/modules/hrm/mod_evaluation_advanced.php' => ['PhanUndeclaredProperty'], - 'htdocs/core/modules/import/modules_import.class.php' => ['SqlInjection'], 'htdocs/core/modules/member/modules_cards.php' => ['PhanTypeMismatchArgument'], 'htdocs/core/modules/mrp/doc/pdf_vinci.modules.php' => ['PhanUndeclaredProperty'], 'htdocs/core/modules/mrp/mod_mo_advanced.php' => ['PhanUndeclaredProperty'], @@ -146,7 +141,7 @@ return [ 'htdocs/partnership/partnership_card.php' => ['PhanUndeclaredGlobalVariable'], 'htdocs/partnership/partnership_list.php' => ['PhanUndeclaredProperty'], 'htdocs/product/class/api_products.class.php' => ['PhanTypeMismatchArgument', 'PhanTypeMismatchReturn', 'PhanUndeclaredProperty'], - 'htdocs/product/class/html.formproduct.class.php' => ['PhanUndeclaredProperty', 'SqlInjection'], + 'htdocs/product/class/html.formproduct.class.php' => ['PhanUndeclaredProperty'], 'htdocs/product/class/productfournisseurprice.class.php' => ['PhanUndeclaredMethod', 'PhanUndeclaredProperty'], 'htdocs/product/inventory/class/inventory.class.php' => ['PhanUndeclaredProperty'], 'htdocs/product/price.php' => ['PhanUndeclaredProperty'], @@ -205,7 +200,6 @@ return [ 'htdocs/variants/tpl/productattributevalueline_view.tpl.php' => ['PhanUndeclaredProperty'], 'htdocs/webhook/class/target.class.php' => ['PhanUndeclaredMethod'], 'htdocs/webhook/target_card.php' => ['PhanUndeclaredGlobalVariable'], - 'htdocs/webportal/class/html.formwebportal.class.php' => ['SqlInjection'], 'htdocs/webservices/server_project.php' => ['PhanUndeclaredProperty'], ], // 'directory_suppressions' => ['src/directory_name' => ['PhanIssueName1', 'PhanIssueName2']] can be manually added if needed. diff --git a/htdocs/contact/list.php b/htdocs/contact/list.php index 8bb9f99a5d6..2921bb58669 100644 --- a/htdocs/contact/list.php +++ b/htdocs/contact/list.php @@ -755,12 +755,12 @@ if (isModEnabled('socialnetworks')) { if ($value['active'] && strlen($search_[$key])) { $searchkeyinjsonformat = preg_replace('/"$/', '', preg_replace('/^"/', '', json_encode($search_[$key]))); if (in_array($db->type, array('mysql', 'mysqli'))) { - $sql .= " AND p.socialnetworks REGEXP '\"".$db->escape($db->escapeforlike($key))."\":\"[^\"]*".$db->escape($db->escapeforlike($searchkeyinjsonformat))."'"; + $sql .= " AND p.socialnetworks REGEXP '".$db->escape("\"".$db->escapeforlike($key)."\":\"[^\"]*".$db->escapeforlike($searchkeyinjsonformat))."'"; } elseif ($db->type == 'pgsql') { - $sql .= " AND p.socialnetworks ~ '\"".$db->escape($db->escapeforlike($key))."\":\"[^\"]*".$db->escape($db->escapeforlike($searchkeyinjsonformat))."'"; + $sql .= " AND p.socialnetworks ~ '".$db->escape("\"".$db->escapeforlike($key)."\":\"[^\"]*".$db->escapeforlike($searchkeyinjsonformat))."'"; } else { - // Works with all database but not reliable because search only for social network code starting with earched value - $sql .= " AND p.socialnetworks LIKE '%\"".$db->escape($db->escapeforlike($key))."\":\"".$db->escape($db->escapeforlike($searchkeyinjsonformat))."%'"; + // Works with all database but not reliable because search only for social network code starting with searched value + $sql .= " AND p.socialnetworks LIKE '".$db->escape("%\"".$db->escapeforlike($key."\":\"".$searchkeyinjsonformat)."%")."'"; } } } diff --git a/htdocs/core/modules/import/modules_import.class.php b/htdocs/core/modules/import/modules_import.class.php index 55a7c9f580c..d6fc1e72cea 100644 --- a/htdocs/core/modules/import/modules_import.class.php +++ b/htdocs/core/modules/import/modules_import.class.php @@ -1462,12 +1462,12 @@ class ModeleImports if (empty($sql_listvalues[$socialkey]) || $sql_listvalues[$socialkey] == "null") { $json = new stdClass(); $json->$socialnetwork = $newval; - $sql_listvalues[$socialkey] = json_encode($json); + $sql_listvalues[$socialkey] = json_encode($json); // Supposed proper escape elsewhere!! @phan-suppress-current-line SqlInjection } else { $jsondata = $sql_listvalues[$socialkey]; $json = json_decode($jsondata); $json->$socialnetwork = $newval; - $sql_listvalues[$socialkey] = json_encode($json); + $sql_listvalues[$socialkey] = json_encode($json); // Supposed proper escape elsewhere!! @phan-suppress-current-line SqlInjection } } } else { diff --git a/htdocs/product/class/html.formproduct.class.php b/htdocs/product/class/html.formproduct.class.php index 7b642f04245..b29c0da5500 100644 --- a/htdocs/product/class/html.formproduct.class.php +++ b/htdocs/product/class/html.formproduct.class.php @@ -167,7 +167,7 @@ class FormProduct } } $sortfield = implode(',', $arraysortfield); - $sortorder_unsanitized = implode(',', $arraysortorder); // $db->order sanitizes + $sortorder_unsanitized = implode(',', $arraysortorder); // $db->order sanitizes @phan-suppress-current-line SqlInjection $sql .= $this->db->order($sortfield, $sortorder_unsanitized); dol_syslog(get_class($this).'::loadWarehouses', LOG_DEBUG); diff --git a/htdocs/webportal/class/html.formwebportal.class.php b/htdocs/webportal/class/html.formwebportal.class.php index c57c0b3281a..9a2e5890d62 100644 --- a/htdocs/webportal/class/html.formwebportal.class.php +++ b/htdocs/webportal/class/html.formwebportal.class.php @@ -653,8 +653,10 @@ class FormWebPortal extends Form //We have to join on extrafield table if (strpos($InfoFieldList[4], 'extra') !== false) { $sql .= " as main, " . $this->db->prefix() . $this->db->sanitize($InfoFieldList[0]) . "_extrafields as extra"; + // We trust this argument `$InfoFieldList[4]` @phan-suppress-next-line SqlInjection $sqlwhere .= " WHERE extra.fk_object=main." . $this->db->sanitize($InfoFieldList[2]) . " AND " . $InfoFieldList[4]; } else { + // We trust this argument `$InfoFieldList[4]` @phan-suppress-next-line SqlInjection $sqlwhere .= " WHERE " . $InfoFieldList[4]; } } else {