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
markasjunk's cmd_learn driver can't find some headers #9005
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I added a few escapeshellarg() here and there just for good practice. I couldn't think of a POC for a RCE, but one day someone else could find one. The real point of this PR is that the %h:headername argument works only with some headers because rcube_imap_generic::fetch() sometimes returns a key that is equal to the one in the query and sometimes it doesn't. For example, the RFC header "message-id" becomes "messageid", so using "%h:message-id" the header is found by fetch() but _do_salearn() can't find the answer, using "%h:messageid" the header is not found by fetch(). Either way you don't get what you deserve.
Thanks for the PR. I just merged the security part separately. I'll see if I can find a better fix for the headers issue. |
What do you think about this patch? --- a/plugins/markasjunk/drivers/cmd_learn.php
+++ b/plugins/markasjunk/drivers/cmd_learn.php
@@ -49,6 +49,11 @@ class markasjunk_cmd_learn
return;
}
+ if (strpos($command, '%h') !== false) {
+ preg_match_all('/%h:([\w_-]+)/', $command, $header_names, PREG_SET_ORDER);
+ $header_names = array_column($header_names, 1);
+ }
+
// backwards compatibility %xds removed in markasjunk v1.12
$command = str_replace('%xds', '%h:x-dspam-signature', $command);
$command = str_replace('%u', escapeshellarg($_SESSION['username']), $command);
@@ -68,24 +73,26 @@ class markasjunk_cmd_learn
$tmp_command = str_replace('%s', escapeshellarg($message->sender['mailto']), $tmp_command);
}
- if (strpos($command, '%h') !== false) {
+ if (!empty($header_names)) {
$storage = $rcube->get_storage();
$storage->check_connection();
$storage->conn->select($src_mbox);
- preg_match_all('/%h:([\w_-]+)/', $tmp_command, $header_names, PREG_SET_ORDER);
+ $headers = $storage->conn->fetchHeader($src_mbox, $uid, true, false, $header_names);
+
foreach ($header_names as $header) {
$val = null;
- if ($msg = $storage->conn->fetchHeader($src_mbox, $uid, true, false, [$header[1]])) {
- $val = !empty($msg->{$header[1]}) ? $msg->{$header[1]} : $msg->others[$header[1]];
+ if ($headers) {
+ $val = $headers->get($header);
+ $val = is_array($val) ? array_first($val) : $val;
}
if (!empty($val)) {
- $tmp_command = str_replace($header[0], escapeshellarg($val), $tmp_command);
+ $tmp_command = str_replace('%h:' . $header, escapeshellarg($val), $tmp_command);
}
else {
if ($debug) {
- rcube::write_log('markasjunk', 'header ' . $header[1] . ' not found in message ' . $src_mbox . '/' . $uid);
+ rcube::write_log('markasjunk', "header {$header} not found in message {$src_mbox}/{$uid}");
}
continue 2; Could you confirm it works? |
It works, and your solution is much more clever than mine |
Fixed. |
michi-s
pushed a commit
to Seclous/roundcubemail
that referenced
this pull request
Oct 27, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I added a few escapeshellarg() here and there just for good practice. I couldn't think of a POC for a RCE, but one day someone else could find one.
The real point of this PR is that the %h:headername argument works only with some headers because rcube_imap_generic::fetch() sometimes returns a key that is equal to the one in the query and sometimes it doesn't.
For example, the RFC header "message-id" becomes "messageid", so
using "%h:message-id" the header is found by fetch() but _do_salearn() can't find the answer,
using "%h:messageid" the header is not found by fetch().
Either way you don't get what you deserve.