Skip to content

Commit

Permalink
Better handling of broken Idna email addresses.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubinsk committed Jan 14, 2018
1 parent a9631a2 commit 21cb7b3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
35 changes: 34 additions & 1 deletion lib/Horde/ActiveSync/Imap/EasMessageBuilder/Mime.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,51 @@ protected function _buildMessage()
$base = $mime;
}

// We need to catch Idna exceptions that are leaked up through
// Horde_Mail and Horde_Mime. However, just ignoring them leads
// to completely blank emails being generated and sent to the client
// because they are being thrown while generating the entire Mime
// structure. Therefore, if we catch an exception, we try again,
// but this time do not attempt to encode Idna addresses. This
// could potentially lead to incorrect email addresses being sent
// to the client, but it is a better alternative than simply removing
// the email address from the email, thus leaving no record of the
// recipient/sender/cc etc...
try {
$headers = $this->_getHeaders();
// Populate the EAS body structure with the MIME data.
$this->_airsyncBody->data = $base->toString(array(
'headers' => $this->_getHeaders(),
'headers' => $headers,
'stream' => true)
);
} catch (Horde_Idna_Exception $e) {
$this->_logger->err($e->getMessage());
$this->_handleIdnaErrors($headers);
$this->_airsyncBody->data = $base->toString(array(
'headers' => $headers,
'stream' => true)
);
}
$this->_airsyncBody->estimateddatasize = $base->getBytes();
}

/**
* Replace any Horde_Mime-Headers_Addresses objects with
* our own override, which prevents Idna encoding.
*
* @param Horde_Mime_Headers $headers The headers object.
*/
protected function _handleIdnaErrors(Horde_Mime_Headers $headers)
{
foreach (array('from', 'to', 'cc') as $name) {
if ($obj = $headers->getHeader($name)) {
$obj_idn = new Horde_ActiveSync_Mime_Headers_Addresses($name, $obj->full_value);
$headers->removeHeader($name);
$headers->addHeaderOb($obj_idn);
}
}
}

/**
* Handle any truncaction and set properties accordingly.
*/
Expand Down
37 changes: 37 additions & 0 deletions lib/Horde/ActiveSync/Mime/Headers/Addresses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
*
*/
class Horde_ActiveSync_Mime_Headers_Addresses extends Horde_Mime_Headers_Addresses
{
/**
* Do send encoding for addresses.
*
* Needed as a static function because it is used by both single and
* multiple address headers.
*
* @todo Implement with traits.
*
* @param array $alist An array of Horde_Mail_Rfc822_List objects.
* @param array $opts Additional options:
* - charset: (string) Encodes the headers using this charset.
* DEFAULT: UTF-8
* - defserver: (string) The default domain to append to mailboxes.
* DEFAULT: No default name.
*/
public static function doSendEncode($alist, array $opts = array())
{
$opts['idn'] = false;
return parent::doSendEncode($alist, $opts);
}


/**
* @param array $opts See doSendEncode().
*/
protected function _sendEncode($opts)
{
return self::doSendEncode($this->getAddressList(), $opts);
}

}
10 changes: 7 additions & 3 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<email>mrubinsk@horde.org</email>
<active>yes</active>
</lead>
<date>2017-11-26</date>
<date>2018-01-14</date>
<version>
<release>2.39.1</release>
<api>2.39.0</api>
Expand All @@ -21,7 +21,7 @@
</stability>
<license uri="http://www.horde.org/licenses/gpl">GPL-2.0</license>
<notes>
*
*
</notes>
<contents>
<dir baseinstalldir="/" name="/">
Expand Down Expand Up @@ -135,6 +135,9 @@
<file name="TaskRecurrence.php" role="php" />
</dir> <!-- /lib/Horde/ActiveSync/Message -->
<dir name="Mime">
<dir name="Headers">
<file name="Addresses.php" role="php" />
</dir> <!-- /lib/Horde/ActiveSync/Mime/Headers -->
<file name="Iterator.php" role="php" />
</dir> <!-- /lib/Horde/ActiveSync/Mime -->
<dir name="Request">
Expand Down Expand Up @@ -562,6 +565,7 @@
<install as="Horde/ActiveSync/Message/SendMailSource.php" name="lib/Horde/ActiveSync/Message/SendMailSource.php" />
<install as="Horde/ActiveSync/Message/Task.php" name="lib/Horde/ActiveSync/Message/Task.php" />
<install as="Horde/ActiveSync/Message/TaskRecurrence.php" name="lib/Horde/ActiveSync/Message/TaskRecurrence.php" />
<install as="Horde/ActiveSync/Mime/Headers/Addresses.php" name="lib/Horde/ActiveSync/Mime/Headers/Addresses.php" />
<install as="Horde/ActiveSync/Mime/Iterator.php" name="lib/Horde/ActiveSync/Mime/Iterator.php" />
<install as="Horde/ActiveSync/Request/Autodiscover.php" name="lib/Horde/ActiveSync/Request/Autodiscover.php" />
<install as="Horde/ActiveSync/Request/Base.php" name="lib/Horde/ActiveSync/Request/Base.php" />
Expand Down Expand Up @@ -3492,7 +3496,7 @@
<date>2017-11-26</date>
<license uri="http://www.horde.org/licenses/gpl">GPL-2.0</license>
<notes>
*
*
</notes>
</release>
</changelog>
Expand Down

0 comments on commit 21cb7b3

Please sign in to comment.