Ignore:
Timestamp:
Jan 22, 2009 9:47:23 AM (4 years ago)
Author:
thomasb
Message:

Get rid of vulnerable preg_replace eval and create_function (#1485686) + correctly handle base and link tags in html messages

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/roundcubemail/program/include/main.inc

    r2237 r2252  
    588588 * @return string Modified CSS source 
    589589 */ 
    590 function rcmail_mod_css_styles($source, $container_id, $base_url = '') 
    591   { 
    592   $a_css_values = array(); 
     590function rcmail_mod_css_styles($source, $container_id) 
     591  { 
    593592  $last_pos = 0; 
     593  $replacements = new rcube_string_replacer; 
    594594   
    595595  // ignore the whole block if evil styles are detected 
    596596  $stripped = preg_replace('/[^a-z\(:]/', '', rcmail_xss_entitiy_decode($source)); 
    597597  if (preg_match('/expression|behavior|url\(|import/', $stripped)) 
    598     return ''; 
     598    return '/* evil! */'; 
    599599 
    600600  // cut out all contents between { and } 
    601601  while (($pos = strpos($source, '{', $last_pos)) && ($pos2 = strpos($source, '}', $pos))) 
    602602  { 
    603     $key = sizeof($a_css_values); 
    604     $a_css_values[$key] = substr($source, $pos+1, $pos2-($pos+1)); 
    605     $source = substr($source, 0, $pos+1) . "<<str_replacement[$key]>>" . substr($source, $pos2, strlen($source)-$pos2); 
     603    $key = $replacements->add(substr($source, $pos+1, $pos2-($pos+1))); 
     604    $source = substr($source, 0, $pos+1) . $replacements->get_replacement($key) . substr($source, $pos2, strlen($source)-$pos2); 
    606605    $last_pos = $pos+2; 
    607606  } 
    608  
     607   
    609608  // remove html comments and add #container to each tag selector. 
    610609  // also replace body definition because we also stripped off the <body> tag 
     
    622621    $source); 
    623622   
    624   // replace all @import statements to modify the imported CSS sources too 
    625   $styles = preg_replace_callback( 
    626     '/@import\s+(url\()?[\'"]?([^\)\'"]+)[\'"]?(\))?/im', 
    627     create_function('$matches', "return sprintf(\"@import url('./bin/modcss.php?u=%s&c=%s')\", urlencode(make_absolute_url(\$matches[2],'$base_url')), urlencode('$container_id'));"), 
    628     $styles); 
    629    
    630623  // put block contents back in 
    631   $styles = preg_replace_callback( 
    632     '/<<str_replacement\[([0-9]+)\]>>/', 
    633     create_function('$matches', "\$values = ".var_export($a_css_values, true)."; return \$values[\$matches[1]];"), 
    634     $styles); 
     624  $styles = $replacements->resolve($styles); 
    635625 
    636626  return $styles; 
     
    648638{ 
    649639  $out = html_entity_decode(html_entity_decode($content)); 
    650   $out = preg_replace_callback('/\\\([0-9a-f]{4})/i', create_function('$matches', 'return chr(hexdec($matches[1]));'), $out); 
     640  $out = preg_replace_callback('/\\\([0-9a-f]{4})/i', 'rcmail_xss_entitiy_decode_callback', $out); 
    651641  $out = preg_replace('#/\*.*\*/#Um', '', $out); 
    652642  return $out; 
    653643} 
    654644 
     645 
     646/** 
     647 * preg_replace_callback callback for rcmail_xss_entitiy_decode_callback 
     648 * 
     649 * @param array matches result from preg_replace_callback 
     650 * @return string decoded entity 
     651 */  
     652function rcmail_xss_entitiy_decode_callback($matches) 
     653{  
     654  return chr(hexdec($matches[1])); 
     655} 
    655656 
    656657/** 
     
    12101211} 
    12111212 
     1213 
     1214 
     1215/** 
     1216 * Helper class to turn relative urls into absolute ones 
     1217 * using a predefined base 
     1218 */ 
     1219class rcube_base_replacer 
     1220{ 
     1221  private $base_url; 
     1222   
     1223  public function __construct($base) 
     1224  { 
     1225    $this->base_url = $base; 
     1226  } 
     1227   
     1228  public function callback($matches) 
     1229  { 
     1230    return $matches[1] . '="' . make_absolute_url($matches[3], $this->base_url) . '"'; 
     1231  } 
     1232} 
     1233 
     1234 
    12121235?> 
Note: See TracChangeset for help on using the changeset viewer.