Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Commit

Permalink
Fix exploit of user's names/emails breaking "To"
Browse files Browse the repository at this point in the history
Before being added to the "To" string, emails and names are cleared of <
> , and names are additionally cleared of @. See
Magnesium\Message\Base::removeToStringBreakingSymbols() DocBlock for
more details.
  • Loading branch information
Florian Gärber committed May 26, 2017
1 parent 09fe824 commit 500d340
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/Magnesium/Message/Base.php
Expand Up @@ -227,8 +227,35 @@ public function getFromString()
protected function formatEmailString(string $email, string $name = null)
{
return $name
? sprintf('%s <%s>', $name, $email)
: $email;
? sprintf(
'%s <%s>',
$this->removeToStringBreakingSymbols($name, false),
$this->removeToStringBreakingSymbols($email, true))
: $this->removeToStringBreakingSymbols($email, true);
}

/**
*
* Should a user have chosen a name like "no1@example.com, Not Okay <no2@example.com>, Sherbert",
* Mailgun would accept the following to: "user@example.com, user2@example.com, no1@example.com, Not Okay <no2@example.com>, Sherbert <hello@example.com>"
* and would send it accordingly, which is unwanted behavior.
* Removing only "," breaks the To-string, sending the message to
* "user@example.com, user2@example.com, no1@example.com Not Okay <no2@example.com> Sherbert" hello@example.com,
* revealing email addresses of other users.
* Removing only either of "<,>" or "@," breaks the string the same way.
* Only removing "<>@," from the string prevents breaking (as far as I know).
*
* Also use an input validation library like Respect/Validation or find
* another way to prevent emails and names from containing "<>,"!
*
* @param string $string
* @param bool $isEmail
*
* @return string
*/
protected function removeToStringBreakingSymbols(string $string, bool $isEmail)
{
return str_replace($isEmail ? ['>', '<', ','] : ['>', '<', ',', '@'], '', $string);
}

/**
Expand Down

0 comments on commit 500d340

Please sign in to comment.