Use native booleans for prepared SQL params; stop binding db_get_boolean() values (#1019)

Prepared statements were binding DB-specific boolean literals (`'t'/'f'`) produced by `db_get_boolean()`, which breaks PostgreSQL parameter typing in some paths (notably mailbox creation via handler flows). 

This change standardizes bound boolean parameters to native PHP `true`/`false` (which are then cast to 0/1 which seems to work fine within PDO/PostgreSQL).

fixes https://github.com/postfixadmin/postfixadmin/issues/1017
This commit is contained in:
Copilot authored and GitHub committed 2026-04-23 19:28:01 +01:00
1 parent 2cce49a657
commit ae7339afd4
11 files changed
+57 -51

No files matched your search

+27 -13
View File
@@ -571,17 +571,14 @@ function divide_quota($quota)
/**
* Checks if the admin is the owner of the domain (or global-admin)
* @param string $username
* @param string $domain
* @return bool
*/
function check_owner($username, $domain)
function check_owner(string $username, string $domain): bool
{
$table_domain_admins = table_by_key('domain_admins');
$result = db_query_all(
"SELECT 1 FROM $table_domain_admins WHERE username= ? AND (domain = ? OR domain = 'ALL') AND active = ?",
array($username, $domain, db_get_boolean(true))
array($username, $domain, true)
);
if (sizeof($result) == 1 || sizeof($result) == 2) { # "ALL" + specific domain permissions is possible
@@ -613,23 +610,22 @@ function list_domains_for_admin(string $username): array
$query = "SELECT $table_domain.domain FROM $table_domain ";
$condition[] = "$table_domain.domain != 'ALL'";
$result = db_query_one("SELECT username FROM $table_domain_admins WHERE username= :username AND domain='ALL'", ['username' => $username]);
$result = db_query_one("SELECT username FROM $table_domain_admins WHERE username = :username AND domain = 'ALL'", ['username' => $username]);
if (empty($result)) { # not a superadmin
$pvalues['username'] = $username;
$pvalues['active'] = db_get_boolean(true);
$pvalues['backupmx'] = db_get_boolean(false);
$pvalues['active'] = true;
$pvalues['backupmx'] = false;
$query .= " LEFT JOIN $table_domain_admins ON $table_domain.domain=$table_domain_admins.domain ";
$condition[] = "$table_domain_admins.username = :username ";
$condition[] = "$table_domain.active = :active "; # TODO: does it really make sense to exclude inactive...
$condition[] = "$table_domain.backupmx = :backupmx"; # TODO: ... and backupmx domains for non-superadmins?
$condition[] = "$table_domain.backupmx = :backupmx "; # TODO: ... and backupmx domains for non-superadmins?
}
$query .= " WHERE " . join(' AND ', $condition);
$query .= " ORDER BY $table_domain.domain";
$result = db_query_all($query, $pvalues);
return array_column($result, 'domain');
}
@@ -1721,6 +1717,15 @@ function db_execute(string $sql, array $values = [], bool $throw_exceptions = fa
try {
$stmt = $link->prepare($sql);
// PDO and PostgreSQL either require us to explicitly bind values as bools, or it seems to work
// if we pass in 0/1 .... 0/1 is easier?
foreach ($values as $key => $value) {
if (is_bool($value)) {
$values[$key] = (int)$value;
}
}
$stmt->execute($values);
} catch (PDOException $e) {
$error_text = "Invalid query: " . $e->getMessage() . " caused by " . $sql . ' ' . json_encode($values);
@@ -1749,8 +1754,17 @@ function db_query(string $sql, array $values = array(), bool $ignore_errors = fa
$stmt = null;
try {
$stmt = $link->prepare($sql);
// PDO and PostgreSQL either require us to explicitly bind values as bools, or it seems to work
// if we pass in 0/1 .... 0/1 is easier?
foreach ($values as $key => $value) {
if (is_bool($value)) {
$values[$key] = (int)$value;
}
}
$stmt->execute($values);
} catch (PDOException $e) {
$error_text = "Invalid query: " . $e->getMessage() . " caused by " . $sql;
error_log($error_text);
if (defined('PHPUNIT_TEST')) {
@@ -2149,7 +2163,7 @@ function gen_show_status($show_alias)
// Vacation CHECK
if (array_key_exists('show_vacation', $CONF) && $CONF['show_vacation'] == 'YES') {
$stat_result = db_query_one("SELECT * FROM " . table_by_key('vacation') . " WHERE email = ? AND active = ? ", array($show_alias, db_get_boolean(true)));
$stat_result = db_query_one("SELECT * FROM " . table_by_key('vacation') . " WHERE email = ? AND active = ? ", array($show_alias, true));
if (!empty($stat_result)) {
$stat_string .= "<span style='background-color:" . $CONF['show_vacation_color'] . "'>" . $CONF['show_status_text'] . "</span>&nbsp;";
} else {
@@ -2161,7 +2175,7 @@ function gen_show_status($show_alias)
if (array_key_exists('show_disabled', $CONF) && $CONF['show_disabled'] == 'YES') {
$stat_result = db_query_one(
"SELECT * FROM " . table_by_key('mailbox') . " WHERE username = ? AND active = ?",
array($show_alias, db_get_boolean(false))
array($show_alias, false)
);
if (!empty($stat_result)) {
$stat_string .= "<span style='background-color:" . $CONF['show_disabled_color'] . "'>" . $CONF['show_status_text'] . "</span>&nbsp;";
@@ -2177,7 +2191,7 @@ function gen_show_status($show_alias)
$now = "datetime('now')";
}
$stat_result = db_query_one("SELECT * FROM " . table_by_key('mailbox') . " WHERE username = ? AND password_expiry <= $now AND active = ?", array($show_alias, db_get_boolean(true)));
$stat_result = db_query_one("SELECT * FROM " . table_by_key('mailbox') . " WHERE username = ? AND password_expiry <= $now AND active = ?", array($show_alias, true));
if (!empty($stat_result)) {
$stat_string .= "<span style='background-color:" . $CONF['show_expired_color'] . "'>" . $CONF['show_status_text'] . "</span>&nbsp;";
+3 -3
View File
@@ -24,7 +24,7 @@ class Login
{
$query = "SELECT password FROM {$this->key_table} WHERE username = :username AND active = :active";
$values = ['username' => $username, 'active' => db_get_boolean(true)];
$values = ['username' => $username, 'active' => true];
$result = db_query_all($query, $values);
@@ -62,7 +62,7 @@ class Login
{
$sql = "SELECT count(1) FROM {$this->key_table} WHERE username = :username AND active = :active";
$active = db_get_boolean(true);
$active = true;
$values = [
'username' => $username,
@@ -97,7 +97,7 @@ class Login
$values = [
'username' => $username,
'active' => db_get_boolean(true),
'active' => true,
];
// Fetch the domain
+2 -3
View File
@@ -565,8 +565,7 @@ abstract class PFAHandler
foreach ($db_values as $key => $val) {
switch ($this->struct[$key]['type']) { # modify field content for some types
case 'bool':
$val = (string)$val;
$db_values[$key] = db_get_boolean($val);
$db_values[$key] = (bool) $val;
break;
case 'pass':
$val = (string)$val;
@@ -876,7 +875,7 @@ abstract class PFAHandler
public function checkPasswordRecoveryCode($username, $token)
{
$table = table_by_key($this->db_table);
$active = db_get_boolean(true);
$active = true;
$now = date('Y-m-d H:i:s');
+3 -3
View File
@@ -121,7 +121,7 @@ class TotpPf
$table_name = table_by_key($this->table);
$sql = "SELECT totp_secret FROM $table_name WHERE username = :username AND active = :active";
$active = db_get_boolean(true);
$active = true;
$values = [
'username' => $username,
@@ -141,7 +141,7 @@ class TotpPf
$table_name = table_by_key($this->table);
$sql = "SELECT totp_secret FROM $table_name WHERE username = :username AND active = :active";
$active = db_get_boolean(true);
$active = true;
$values = [
'username' => $username,
@@ -193,7 +193,7 @@ class TotpPf
$table_name = table_by_key($this->table);
$sql = "SELECT totp_secret FROM $table_name WHERE username = :username AND active = :active";
$active = db_get_boolean(true);
$active = true;
$values = [
'username' => $username,
+4 -4
View File
@@ -134,7 +134,7 @@ class VacationHandler extends PFAHandler
// tidy up vacation table.
$vacation_data = array(
'active' => db_get_boolean(false),
'active' => false,
);
db_update('vacation', 'email', $this->username, $vacation_data);
@@ -206,12 +206,12 @@ class VacationHandler extends PFAHandler
if (!is_array($row)) {
return false;
}
$boolean = ($row['active'] == db_get_boolean(true));
# TODO: only return true and store the db result array in $this->whatever for consistency with the other classes
return array(
'subject' => isset($row['subject']) ? $row['subject'] : null,
'body' => isset($row['body']) ? $row['body'] : null,
'active' => $boolean,
'active' => (bool) $row['active'],
'interval_time' => isset($row['interval_time']) ? $row['interval_time'] : null,
'activeFrom' => isset($row['activefrom']) ? $row['activefrom'] : null,
'activeUntil' => isset($row['activeuntil']) ? $row['activeuntil'] : null
@@ -235,7 +235,7 @@ class VacationHandler extends PFAHandler
'subject' => $subject,
'body' => $body,
'interval_time' => $interval_time,
'active' => db_get_boolean(true),
'active' => true,
// MySQL 8.x supports a timezone (Y-m-d H:i:sP), but MariaDB doesn't seem to? I'm sure PostgreSQL will.
'activefrom' => $activeFrom->format('Y-m-d H:i:s'),
'activeuntil' => $activeUntil->format('Y-m-d H:i:s'),
+4 -4
View File
@@ -60,14 +60,14 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
$table_mailbox = table_by_key('mailbox');
$table_alias = table_by_key('alias');
$params = [];
$q = "SELECT username from $table_mailbox WHERE active='" . db_get_boolean(true) . "' AND " . db_in_clause("domain", $wanted_domains, $params);
$params = ['active' => true];
$q = "SELECT username from $table_mailbox WHERE active = :active AND " . db_in_clause("domain", $wanted_domains, $params);
$result = db_query_all($q, $params);
$recipients = array_column($result, 'username');
if (intval(safepost('mailboxes_only')) == 0) {
$params2 = [];
$q2 = "SELECT goto FROM $table_alias WHERE active='" . db_get_boolean(true) . "' AND " . db_in_clause("domain", $wanted_domains, $params2);
$params2 = ['active' => true];
$q2 = "SELECT goto FROM $table_alias WHERE active= :active AND " . db_in_clause("domain", $wanted_domains, $params2);
$result2 = db_query_all($q2, $params2);
$recipients = array_merge($recipients, array_column($result2, 'goto'));
}
+2 -2
View File
@@ -397,8 +397,8 @@ if ($db) {
}
$table_admin = table_by_key('admin');
$bool = db_get_boolean(true);
$admins = db_query_all("SELECT * FROM $table_admin WHERE superadmin = '$bool' AND active = '$bool'");
$admins = db_query_all("SELECT * FROM $table_admin WHERE superadmin = :admin AND active = :active", ['admin' => true, 'active' => true]);
if (!empty($admins)) { ?>
+1 -1
View File
@@ -1350,7 +1350,7 @@ function upgrade_1284_mysql_pgsql()
foreach ($result as $row) {
printdebug("Setting superadmin flag for " . $row['username']);
db_update('admin', 'username', $row['username'], array('superadmin' => db_get_boolean(true)));
db_update('admin', 'username', $row['username'], array('superadmin' => true));
}
}
+1 -1
View File
@@ -22,7 +22,7 @@ define('POSTFIXADMIN_CLI', 1);
$table_vacation = table_by_key('vacation');
$active = db_get_boolean(1);
$active = true;
$vacations_that_need_deactivating = db_query_all("SELECT * FROM $table_vacation WHERE activeuntil <= NOW() AND active = :active ", ['active' => $active]);
+1 -8
View File
@@ -48,14 +48,6 @@ class AliasHandlerTest extends \PHPUnit\Framework\TestCase
$x = new AliasHandler(1, 'admin', true);
$values = [
'localpart' => 'david.test',
'domain' => 'example.com',
'active' => 1,
'address' => 'david.test@example.com',
'goto' => ['dest@example.com']
];
try {
$r = $x->init('david.test@example.com');
$this->fail("Should not see this - example.com is not present");
@@ -101,6 +93,7 @@ class AliasHandlerTest extends \PHPUnit\Framework\TestCase
$ret = $dh->save();
$this->assertTrue($ret);
// Need to add 'admin' as a domain_admin
+9 -9
View File
@@ -22,7 +22,7 @@ class TotpPfTest extends TestCase
'maildir' => '/foo/bar',
'local_part' => 'test',
'domain' => 'example.com',
'active' => db_get_boolean(true),
'active' => true,
]);
parent::setUp(); // TODO: Change the autogenerated stub
@@ -97,8 +97,8 @@ class TotpPfTest extends TestCase
[
'username' => 'admin@example.com',
'password' => pacrypt('adminpass'),
'superadmin' => db_get_boolean(true),
'active' => db_get_boolean(true),
'superadmin' => true,
'active' => true,
]
);
db_execute(
@@ -106,7 +106,7 @@ class TotpPfTest extends TestCase
[
'username' => 'admin@example.com',
'domain' => 'example.com',
'active' => db_get_boolean(true),
'active' => true,
]
);
@@ -136,8 +136,8 @@ class TotpPfTest extends TestCase
[
'username' => 'admin@example.com',
'password' => pacrypt('adminpass'),
'superadmin' => db_get_boolean(false),
'active' => db_get_boolean(true),
'superadmin' => false,
'active' => true,
]
);
db_execute(
@@ -145,7 +145,7 @@ class TotpPfTest extends TestCase
[
'username' => 'admin@example.com',
'domain' => 'example.com',
'active' => db_get_boolean(true),
'active' => true,
]
);
@@ -173,7 +173,7 @@ class TotpPfTest extends TestCase
'maildir' => '/foo/bar2',
'local_part' => 'test2',
'domain' => 'example.com',
'active' => db_get_boolean(true),
'active' => true,
]);
db_execute('INSERT INTO mailbox_app_password (username, description, password_hash) VALUES (:username, :description, :password_hash)', ['username' => 'test@example.com', 'description' => 'test foobar', 'password_hash' => pacrypt('foobar-app')]);
@@ -218,7 +218,7 @@ EOF;
'username' => $username,
'password_hash' => pacrypt($plain_password),
'client_ip' => $ip,
'active' => db_get_boolean(true),
'active' => true,
]);
if (count($rows) != 1) {