From 92fe42dd959bdb258ef53b5c3b03bb7e9bcd26e3 Mon Sep 17 00:00:00 2001 From: David Goodwin Date: Tue, 29 Sep 2026 14:50:27 +0100 Subject: [PATCH] ensure sql injection is not possible with user supplied data being used as SQL column names in getPageBrowser - due to 2a6f27c3 / #1127 postfixadmin 4.0 does not pass user supplied keys as SQL column names, so is not vulnerable - this just provides a bit of defense in depth. cherry picked from commit 317696013fe3a0c0b1135af0418f94d20599a5ef; test adjusted for 4.0's domain alias limit --- model/PFAHandler.php | 48 +++++++++++++++++++++++++++----------- tests/AliasHandlerTest.php | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 14 deletions(-) diff --git a/model/PFAHandler.php b/model/PFAHandler.php index 00881560..f5569348 100644 --- a/model/PFAHandler.php +++ b/model/PFAHandler.php @@ -654,13 +654,13 @@ abstract class PFAHandler * helper function to build the inner part of the select query * can be used by read_from_db() and for generating the pagebrowser * - * @param array or string - condition (an array will be AND'ed using db_where_clause, a string will be directly used) + * @param array|string $condition - condition (an array will be AND'ed using db_where_clause, a string will be directly used) * (if you use a string, make sure it is correctly escaped!) * - WARNING: will be changed to array only in the future, with an option to include a raw string inside the array - * @param array searchmode - operators to use (=, <, >) if $condition is an array. Defaults to = if not specified for a field. + * @param array $searchmode - operators to use (=, <, >) if $condition is an array. Defaults to = if not specified for a field. * @return array - contains query parts */ - protected function build_select_query($condition, $searchmode) + protected function build_select_query($condition, array $searchmode): array { $select_cols = array(); @@ -752,10 +752,40 @@ abstract class PFAHandler */ public function getPagebrowser($condition, $searchmode) { + if (is_array($condition)) { + $condition = $this->filterSearchCondition($condition); + } $queryparts = $this->build_select_query($condition, $searchmode); return create_page_browser($this->label_field, $queryparts['from_where_order']); } + /** + * restrict a search condition array to fields the caller is allowed to search on. + * + * Used by getList() and getPagebrowser() to avoid information leaks / SQL injection via + * search parameter keys - $condition keys end up as raw SQL column names in + * db_where_clause(), so unknown keys must never reach build_select_query(). + * + * @param array $condition + * @return array - filtered condition + */ + protected function filterSearchCondition(array $condition): array + { + $real_condition = array(); + foreach ($condition as $key => $value) { + # allow only access to fields the user can access to avoid information leaks via search parameters + if (isset($this->struct[$key]) && ($this->struct[$key]['display_in_list'] || $this->struct[$key]['display_in_form'])) { + $real_condition[$key] = $value; + } elseif (($key == '_') && count($this->searchfields)) { + # '_' is the simple-search box; build_select_query() expands it into a LIKE across $this->searchfields + $real_condition[$key] = $value; + } else { + $this->errormsg[] = "Ignoring unknown search field $key"; + } + } + return $real_condition; + } + /** * read_from_db * @@ -841,17 +871,7 @@ abstract class PFAHandler public function getList($condition, $searchmode = array(), $limit = -1, $offset = -1): bool { if (is_array($condition)) { - $real_condition = array(); - foreach ($condition as $key => $value) { - # allow only access to fields the user can access to avoid information leaks via search parameters - if (isset($this->struct[$key]) && ($this->struct[$key]['display_in_list'] || $this->struct[$key]['display_in_form'])) { - $real_condition[$key] = $value; - } elseif (($key == '_') && count($this->searchfields)) { - $real_condition[$key] = $value; - } else { - $this->errormsg[] = "Ignoring unknown search field $key"; - } - } + $real_condition = $this->filterSearchCondition($condition); } else { # warning: no sanity checks are applied if $condition is not an array! $real_condition = $condition; diff --git a/tests/AliasHandlerTest.php b/tests/AliasHandlerTest.php index 71f304d3..34ac03f0 100644 --- a/tests/AliasHandlerTest.php +++ b/tests/AliasHandlerTest.php @@ -395,4 +395,51 @@ class AliasHandlerTest extends \PHPUnit\Framework\TestCase $this->assertEquals(5, count($results)); $this->assertTrue(isset($results['31-test@example.com'])); } + + /** + * getPagebrowser() must apply the same search-field allow-list as getList(). + * Previously it passed $condition straight to build_select_query(), so an + * unknown search key ended up as a raw, unescaped SQL column name in the + * WHERE clause via db_where_clause() - i.e. SQL injection through the + * array key of a caller-supplied search array. + */ + public function testGetPagebrowserRejectsUnknownSearchField() + { + $this->addDomain('example.com', 'admin'); + + // raise the default alias limit (11) so we can add enough aliases below + $dh = new DomainHandler(0, 'admin', true); + $dh->init('example.com'); + $this->assertTrue($dh->set(['aliases' => 99, 'mailboxes' => 88, 'backupmx' => 0, 'active' => 1])); + $this->assertTrue($dh->save()); + + // need more than page_size (10) rows so the pagebrowser is actually populated + foreach (range(1, 15) as $i) { + $addr = "$i-test@example.com"; + $x = new AliasHandler(1, 'admin', true); + $this->assertTrue($x->init($addr)); + $x->set([ + 'localpart' => explode('@', $addr)[0], + 'domain' => 'example.com', + 'active' => 1, + 'address' => $addr, + 'goto' => ['dest@example.com'], + ]); + $this->assertTrue($x->save(), json_encode($x->errormsg)); + } + + $malicious_key = '1); DROP TABLE alias; --'; + + $x = new AliasHandler(0, 'admin', true); + $withMaliciousKey = $x->getPagebrowser([$malicious_key => 'irrelevant'], []); + $this->assertEquals(["Ignoring unknown search field $malicious_key"], $x->errormsg); + + $y = new AliasHandler(0, 'admin', true); + $withoutCondition = $y->getPagebrowser([], []); + + // the unknown key must be dropped entirely (never reach SQL), so the + // result is identical to not having supplied it at all. + $this->assertNotEmpty($withMaliciousKey); + $this->assertEquals($withoutCondition, $withMaliciousKey); + } }