You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rcube_content_filter.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | This file is part of the Roundcube Webmail client |
  5. | Copyright (C) 2011, The Roundcube Dev Team |
  6. | |
  7. | Licensed under the GNU General Public License version 3 or |
  8. | any later version with exceptions for skins & plugins. |
  9. | See the README file for a full license statement. |
  10. | |
  11. | PURPOSE: |
  12. | PHP stream filter to detect evil content in mail attachments |
  13. +-----------------------------------------------------------------------+
  14. | Author: Thomas Bruederli <roundcube@gmail.com> |
  15. +-----------------------------------------------------------------------+
  16. */
  17. /**
  18. * PHP stream filter to detect html/javascript code in attachments
  19. *
  20. * @package Framework
  21. * @subpackage Utils
  22. */
  23. class rcube_content_filter extends php_user_filter
  24. {
  25. private $buffer = '';
  26. private $cutoff = 2048;
  27. function onCreate()
  28. {
  29. $this->cutoff = rand(2048, 3027);
  30. return true;
  31. }
  32. function filter($in, $out, &$consumed, $closing)
  33. {
  34. while ($bucket = stream_bucket_make_writeable($in)) {
  35. $this->buffer .= $bucket->data;
  36. // check for evil content and abort
  37. if (preg_match('/<(script|iframe|object)/i', $this->buffer)) {
  38. return PSFS_ERR_FATAL;
  39. }
  40. // keep buffer small enough
  41. if (strlen($this->buffer) > 4096) {
  42. $this->buffer = substr($this->buffer, $this->cutoff);
  43. }
  44. $consumed += $bucket->datalen;
  45. stream_bucket_append($out, $bucket);
  46. }
  47. return PSFS_PASS_ON;
  48. }
  49. }