Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialization of untrusted data #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ public function buildAndSendMessage(
));

/* Add preferred reply language(s). */
if ($lang = @unserialize($prefs->getValue('reply_lang'))) {
if ($lang = @unserialize($prefs->getValue('reply_lang'), array('allowed_classes' => false))) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could not find a way to set a value of this preference. Is it used available anywhere?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an advanced preference in the "Compose" preference group.

$headers->addHeader('Accept-Language', implode(',', $lang));
}

Expand Down
6 changes: 5 additions & 1 deletion lib/Factory/MailboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public function create($mailbox)
$mailbox = IMP_Mailbox::get($mailbox);

if ($ob = $this->_getCache($mailbox)->get($key)) {
$ob = @unserialize($ob);
$ob = @unserialize($ob, array('allowed_classes' => array(
'IMP_Mailbox_List_Virtual',
'IMP_Mailbox_List_Pop3',
'IMP_Mailbox_List',
Copy link

@tribut tribut Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to include 'IMP_Mailbox'. In our tests, applying this patch without it, broke viewing messages.

)));
}

if (!$ob) {
Expand Down
11 changes: 10 additions & 1 deletion lib/Flags.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@ public function __construct()
}

if ($f_list = $GLOBALS['prefs']->getValue('msgflags')) {
$f_list = @unserialize($f_list);
$f_list = @unserialize($f_list, array('allowed_classes' => array(
'IMP_Flag_Imap_Answered',
'IMP_Flag_Imap_Deleted',
'IMP_Flag_Imap_Draft',
'IMP_Flag_Imap_Flagged',
'IMP_Flag_Imap_Forwarded',
'IMP_Flag_Imap_Junk',
'IMP_Flag_Imap_NotJunk',
'IMP_Flag_Imap_Seen',
Copy link

@tribut tribut Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to include 'IMP_Flag_User' as well as the 'IMP_Flag_System_' classes (not sure about all of them, we've seen errors without IMP_Flag_System_Unseen at least).

)));
if (is_array($f_list)) {
foreach ($f_list as $val) {
$this->_userflags[$val->id] = $val;
Expand Down
11 changes: 8 additions & 3 deletions lib/Ftree/Prefs/Expanded.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ public function __construct()
{
global $prefs;

if (($folders = @unserialize($prefs->getValue('expanded_folders'))) &&
is_array($folders)) {
$value = $prefs->getValue('expanded_folders');
$folders = $value ? json_decode($value, true) : array();
if (null === $folders && json_last_error() === JSON_ERROR_SYNTAX) {
// TODO: Remove backward compatibility with stored values
$folders = @unserialize($value, array('allowed_classes' => false));
}
if (is_array($folders)) {
$this->_data = $folders;
}

Expand All @@ -54,7 +59,7 @@ public function __construct()
*/
public function shutdown()
{
$GLOBALS['prefs']->setValue('expanded_folders', serialize($this->_data));
$GLOBALS['prefs']->setValue('expanded_folders', json_encode($this->_data, JSON_FORCE_OBJECT));
}

/**
Expand Down
10 changes: 8 additions & 2 deletions lib/Ftree/Prefs/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ public function __construct(IMP_Ftree $ftree)
$this->_data = array('INBOX' => 1);

/* Add the list of polled mailboxes from the prefs. */
if ($nav_poll = @unserialize($prefs->getValue('nav_poll'))) {
$value = $prefs->getValue('nav_poll');
$nav_poll = $value ? json_decode($value, true) : array();
if (null === $nav_poll && json_last_error() === JSON_ERROR_SYNTAX) {
// TODO: Remove backward compatibility with stored values
$nav_poll = @unserialize($value, array('allowed_classes' => false));
}
if ($nav_poll) {
$this->_data += $nav_poll;
}

Expand All @@ -59,7 +65,7 @@ public function __construct(IMP_Ftree $ftree)
*/
public function shutdown()
{
$GLOBALS['prefs']->setValue('nav_poll', serialize($this->_data));
$GLOBALS['prefs']->setValue('nav_poll', json_encode($this->_data, JSON_FORCE_OBJECT));
}

/**
Expand Down
7 changes: 5 additions & 2 deletions lib/LoginTasks/SystemTask/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,10 @@ protected function _upgradeVirtualFolders()

$vfolders = $prefs->getValue('vfolder');
if (!empty($vfolders)) {
$vfolders = @unserialize($vfolders);
$vfolders = @unserialize($vfolders, array('allowed_classes' => array(
'IMP_Search_Vfolder_Vinbox',
'IMP_Search_Vfolder_Vtrash',
)));
}

if (empty($vfolders) || !is_array($vfolders)) {
Expand Down Expand Up @@ -577,7 +580,7 @@ protected function _upgradeStationeryToTemplates()
{
global $injector, $prefs;

$slist = @unserialize($prefs->getValue('stationery'));
$slist = @unserialize($prefs->getValue('stationery'), array('allowed_classes' => false));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a deprecation policy, when this property can be removed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is already removed, as a settable preference. We don't remove migrations though.

if (is_array($slist)) {
/* Old entry format:
* 'c' => (string) Content
Expand Down
9 changes: 7 additions & 2 deletions lib/Prefs/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ public function __construct()
{
global $prefs;

$sortpref = @unserialize($prefs->getValue(self::SORTPREF));
$value = $prefs->getValue(self::SORTPREF);
$sortpref = $value ? json_decode($value, true) : array();
if (null === $sortpref && json_last_error() === JSON_ERROR_SYNTAX) {
// TODO: Remove backward compatibility with stored values
$sortpref = @unserialize($value, array('allowed_classes' => false));
}
if (is_array($sortpref)) {
$this->_sortpref = $sortpref;
}
Expand Down Expand Up @@ -106,7 +111,7 @@ public function newSortbyValue($sortby)
*/
protected function _save()
{
$GLOBALS['prefs']->setValue(self::SORTPREF, serialize($this->_sortpref));
$GLOBALS['prefs']->setValue(self::SORTPREF, json_encode($this->_sortpref, JSON_FORCE_OBJECT));
}

/* ArrayAccess methods. */
Expand Down
4 changes: 3 additions & 1 deletion lib/Remote.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class IMP_Remote implements ArrayAccess, IteratorAggregate
*/
public function __construct()
{
$this->_accounts = @unserialize($GLOBALS['prefs']->getValue('remote')) ?: array();
$this->_accounts = @unserialize($GLOBALS['prefs']->getValue('remote'), array('allowed_classes' => array(
'IMP_Remote_Account',
))) ?: array();
}

/**
Expand Down
15 changes: 13 additions & 2 deletions lib/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,15 @@ class_exists($cname)) {
}

if ($f_list = $GLOBALS['prefs']->getValue('filter')) {
$f_list = @unserialize($f_list);
$f_list = @unserialize($f_list, array('allowed_classes' => array(
'IMP_Search_Filter',
'IMP_Search_Filter_Personal',
'IMP_Search_Filter_Attachment',
'IMP_Search_Filter_Autogenerated',
'IMP_Search_Filter_Contacts',
'IMP_Search_Filter_Bulk',
'IMP_Search_Filter_Mailinglist',
)));
if (is_array($f_list)) {
foreach ($f_list as $val) {
if ($val instanceof IMP_Search_Filter) {
Expand Down Expand Up @@ -296,7 +304,10 @@ class_exists($cname)) {
}

if ($pref_vf = $GLOBALS['prefs']->getValue('vfolder')) {
$pref_vf = @unserialize($pref_vf);
$pref_vf = @unserialize($pref_vf, array('allowed_classes' => array(
'IMP_Search_Vfolder_Vinbox',
'IMP_Search_Vfolder_Vtrash',
)));
if (is_array($pref_vf)) {
foreach ($pref_vf as $val) {
if ($val instanceof IMP_Search_Vfolder) {
Expand Down