Details
Replace the direct concatenation with an allowlist of permitted column names:
// modules/preventivi/ajax/select.php — FIXED
$allowed_stati = ['is_pianificabile', 'is_completato', 'is_fatturabile', 'is_concluso'];
$stato = !empty($superselect['stato']) && in_array($superselect['stato'], $allowed_stati)
? $superselect['stato']
: 'is_pianificabile';
$where[] = '('.$stato.' = 1)';
// modules/ordini/ajax/select.php — FIXED
$allowed_stati = ['is_fatturabile', 'is_evadibile', 'is_completato'];
$stato = !empty($superselect['stato']) && in_array($superselect['stato'], $allowed_stati)
? $superselect['stato']
: 'is_fatturabile';
$where[] = '`or_statiordine`.'.$stato.' = 1';
// modules/contratti/ajax/select.php — FIXED
$allowed_stati = ['is_pianificabile', 'is_completato', 'is_fatturabile'];
$stato = !empty($superselect['stato']) && in_array($superselect['stato'], $allowed_stati)
? $superselect['stato']
: 'is_pianificabile';
$where[] = '`idstato` IN (SELECT `id` FROM `co_staticontratti` WHERE '.$stato.' = 1)';
This approach is recommended because the stato parameter represents a database column name (not a value), so prepared statements cannot be used here. The allowlist ensures only known-safe column names are accepted.
Option B: Regex Validation (Alternative)
If the set of column names is dynamic, validate the format strictly:
$stato = !empty($superselect['stato']) ? $superselect['stato'] : 'is_pianificabile';
if (!preg_match('/^[a-z_]+$/i', $stato)) {
$stato = 'is_pianificabile'; // fallback to safe default
}
$where[] = '('.$stato.' = 1)';
This ensures only alphabetic characters and underscores are accepted, preventing any SQL injection.
Option C: Backtick Quoting (Supplementary)
In addition to validation, wrap the column name in backticks to treat it as an identifier:
$where[] = '(`'.str_replace('`', '', $stato).'` = 1)';
Note: This alone is insufficient without input validation but provides defense-in-depth.
Global Recommendation
Audit all usages of $superselect across the codebase. Any value from $superselect that is used as part of a SQL expression (not as a parameterized value) must be validated against an allowlist. The prepare() function is already used correctly in other parts of the code — the issue is specifically where $superselect values are used as column names or bare expressions.
Credits
Omar Ramirez