From 557c55db922c9583eddf3e2b973eb5e1fbc2af4e Mon Sep 17 00:00:00 2001 From: David Goodwin Date: Fri, 20 Nov 2026 14:06:08 +0000 Subject: [PATCH] XSS fixes from @mirko - see also 07dd358d0e12071d53b5f34dbada03fb4c64f06d (thanks @mirko, CVE-2026-127178) --- model/AliasHandler.php | 10 ++++++ templates/list-virtual_mailbox.tpl | 4 +-- templates/list.tpl | 4 +-- tests/AliasHandlerTest.php | 50 ++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 4 deletions(-) diff --git a/model/AliasHandler.php b/model/AliasHandler.php index e23101bc..a0386a33 100644 --- a/model/AliasHandler.php +++ b/model/AliasHandler.php @@ -254,6 +254,16 @@ class AliasHandler extends PFAHandler return false; } + # A valid alias address contains exactly one "@": either "localpart@domain" + # or a "@domain" catch-all. Reject anything else here - otherwise an address + # such as "@domain@@domain" slips past the catch-all shortcut below + # (explode() yields an empty local part, so check_email() is skipped) and is + # stored verbatim, which allows stored HTML/JS injection in the alias list. + if (substr_count($this->id, '@') !== 1) { + $this->errormsg[$this->id_field] = Config::lang('pCreate_alias_address_text_error1'); + return false; + } + list($local_part, $domain) = explode('@', $this->id); # TODO: already checked in set() - does it make sense to check it here also? Only advantage: it's an early check diff --git a/templates/list-virtual_mailbox.tpl b/templates/list-virtual_mailbox.tpl index 37a8e06c..d938175f 100644 --- a/templates/list-virtual_mailbox.tpl +++ b/templates/list-virtual_mailbox.tpl @@ -159,10 +159,10 @@
- + {CSRF_Token}
diff --git a/templates/list.tpl b/templates/list.tpl index 23ba19ba..80837ab6 100644 --- a/templates/list.tpl +++ b/templates/list.tpl @@ -201,11 +201,11 @@ {if $item._can_delete}
- + {CSRF_Token}
diff --git a/tests/AliasHandlerTest.php b/tests/AliasHandlerTest.php index 21c40d96..6dc77483 100644 --- a/tests/AliasHandlerTest.php +++ b/tests/AliasHandlerTest.php @@ -308,6 +308,56 @@ class AliasHandlerTest extends \PHPUnit\Framework\TestCase } } + /** + * An address with more than one "@" must be rejected. Before this was fixed, + * a localpart such as '@example.com@' slipped past the catch-all + * shortcut in validate_new_id() (empty local part => check_email() skipped) + * and was stored verbatim, leading to stored HTML/JS injection in the alias + * list (the id is rendered via $RAW_item in templates/list.tpl). + */ + public function testCannotCreateAliasWithMalformedAddress() + { + $this->addDomain('example.com', 'admin'); + + $_SESSION = [ + 'sessid' => [ + 'roles' => ['global-admin'] + ] + ]; + + # the create-alias webform combines the 'localpart' and 'domain' fields via mergeId() + $handler = new AliasHandler(1, 'admin', true); + $handler->webformConfig(); # switch to webform (localpart + domain) mode + + $address = $handler->mergeId([ + 'localpart' => '@example.com@">', + 'domain' => 'example.com', + ]); + + $this->assertFalse($handler->init($address), "malformed address must be rejected: $address"); + $this->assertNotEmpty($handler->errormsg); + + # nothing must have been stored + $this->assertEmpty(db_query_all('SELECT address FROM alias WHERE address LIKE :a', ['a' => '%svg%'])); + } + + /** + * Regression guard: the above fix must not break legitimate "@domain" catch-all aliases. + */ + public function testCatchallAliasStillAllowed() + { + $this->addDomain('example.com', 'admin'); + + $_SESSION = [ + 'sessid' => [ + 'roles' => ['global-admin'] + ] + ]; + + $handler = new AliasHandler(1, 'admin', true); + $this->assertTrue($handler->init('@example.com'), json_encode($handler->errormsg)); + } + /** * @see https://github.com/postfixadmin/postfixadmin/pull/375 and https://github.com/postfixadmin/postfixadmin/issues/358