Development

Changeset 8260

You must first sign up to be able to contribute.

Changeset 8260

Show
Ignore:
Timestamp:
04/04/08 11:10:11 (1 year ago)
Author:
fabien
Message:

removed both escaping strategy

  • we now have 2 escaping strategy: on and off
  • bc is now equivalent to off
  • both is now equivalent to on
  • if you still use bc or both, you will have an error in your log but you application will still work as expected
  • sf_data is now available when strategy is off (but sf_data contains unescaped data)
  • "normal" variables are now available when strategy is on (but the vars are escaped)
  • everything is BC
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/autoload/sfCoreAutoload.class.php

    r8248 r8260  
    396396  'sfOutputEscaperObjectDecorator' => 'view/escaper', 
    397397  'sfOutputEscaperSafe' => 'view/escaper', 
    398   'sfEscapedViewParameterHolder' => 'view', 
    399398  'sfPHPView' => 'view', 
    400399  'sfPartialView' => 'view', 
  • branches/1.1/lib/config/config/core_compile.yml

    r8201 r8260  
    2424- %SF_SYMFONY_LIB_DIR%/view/sfView.class.php 
    2525- %SF_SYMFONY_LIB_DIR%/view/sfViewParameterHolder.class.php 
    26 - %SF_SYMFONY_LIB_DIR%/view/sfEscapedViewParameterHolder.class.php 
    2726 
    2827# these classes are optionals but very likely to be used (in web context) 
  • branches/1.1/lib/config/config/settings.yml

    r8201 r8260  
    2525 
    2626    # Output escaping settings 
    27     escaping_strategy:      off              # Determines how variables are made available to templates. Accepted values: both, on, off. The value off deactivates escaping completely and gives a slight boost
     27    escaping_strategy:      off              # Determines how variables are made available to templates. Accepted values: on, off
    2828    escaping_method:        ESC_SPECIALCHARS # Function or helper used for escaping. Accepted values: ESC_RAW, ESC_ENTITIES, ESC_JS, ESC_JS_NO_ENTITIES, and ESC_SPECIALCHARS. 
    2929 
  • branches/1.1/lib/plugins/sfPropelPlugin/test/functional/m2mTest.php

    r6482 r8260  
    2020$b = new backendTestBrowser(); 
    2121 
     22sfConfig::set('sf_escaping_strategy', 'off'); 
    2223launch_tests($b); 
    2324 
    24 sfConfig::set('sf_escaping_strategy', 'both'); 
     25sfConfig::set('sf_escaping_strategy', 'on'); 
    2526launch_tests($b); 
    2627 
  • branches/1.1/lib/task/generator/sfGenerateAppTask.class.php

    r8201 r8260  
    7373You can enable output escaping (to prevent XSS) by using the [escaping-strategy|COMMENT] option: 
    7474 
    75   [./symfony generate:app frontend --escaping-strategy=both|INFO] 
     75  [./symfony generate:app frontend --escaping-strategy=on|INFO] 
    7676 
    7777You can enable session token in forms (to prevent CSRF) by defining 
  • branches/1.1/lib/task/generator/skeleton/app/app/config/settings.yml

    r8201 r8260  
    2626 
    2727    # Output escaping settings 
    28     escaping_strategy:      ##ESCAPING_STRATEGY##            # Determines how variables are made available to templates. Accepted values: both, on, off. The value off deactivates escaping completely and gives a slight boost
     28    escaping_strategy:      ##ESCAPING_STRATEGY##            # Determines how variables are made available to templates. Accepted values: on, off
    2929    escaping_method:        ESC_SPECIALCHARS # Function or helper used for escaping. Accepted values: ESC_RAW, ESC_ENTITIES, ESC_JS, ESC_JS_NO_ENTITIES, and ESC_SPECIALCHARS. 
    3030 
  • branches/1.1/lib/view/escaper/sfOutputEscaper.class.php

    r8201 r8260  
    7575   * @param string $escapingMethod the escaping method (a PHP callable) to apply to the value 
    7676   * @param mixed $value the value to escape 
    77    * @param mixed the escaped value 
    7877   * 
    7978   * @return mixed Escaping value 
     
    8382  public static function escape($escapingMethod, $value) 
    8483  { 
    85     if (is_null($value) || 'esc_raw' == $escapingMethod
     84    if (is_null($value)
    8685    { 
    8786      return $value; 
  • branches/1.1/lib/view/sfView.class.php

    r8227 r8260  
    150150  protected function initializeAttributeHolder($attributes = array()) 
    151151  { 
    152     $attributeHolder = false === sfConfig::get('sf_escaping_method') ? new sfViewParameterHolder() : new sfEscapedViewParameterHolder(); 
    153     $attributeHolder->initialize($this->dispatcher, $attributes, array( 
     152    if ('both' == sfConfig::get('sf_escaping_strategy')) 
     153    { 
     154      $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Escaping strategy "both" is deprecated, please use "on".', 'priority' => sfLogger::ERR))); 
     155      sfConfig::set('sf_escaping_strategy', 'on'); 
     156    } 
     157    else if ('bc' == sfConfig::get('sf_escaping_strategy')) 
     158    { 
     159      $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Escaping strategy "bc" is deprecated, please use "off".', 'priority' => sfLogger::ERR))); 
     160      sfConfig::set('sf_escaping_strategy', 'off'); 
     161    } 
     162 
     163    $attributeHolder = new sfViewParameterHolder($this->dispatcher, $attributes, array( 
    154164      'escaping_method'   => sfConfig::get('sf_escaping_method'), 
    155165      'escaping_strategy' => sfConfig::get('sf_escaping_strategy'), 
  • branches/1.1/lib/view/sfViewParameterHolder.class.php

    r6176 r8260  
    33/* 
    44 * This file is part of the symfony package. 
    5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 
     5 * (c) Fabien Potencier <fabien.potencier@symfony-project.com> 
    66 *  
    77 * For the full copyright and license information, please view the LICENSE 
     
    1010 
    1111/** 
    12  * A Template Context stores all parameters that will be available to templates. 
     12 * sfViewParameterHolder stores all variables that will be available to the template. 
     13 * 
     14 * It can also escape variables with an escaping method. 
    1315 * 
    1416 * @package    symfony 
     
    2022{ 
    2123  protected 
    22     $dispatcher = null; 
     24    $dispatcher     = null, 
     25    $escaping       = null, 
     26    $escapingMethod = null; 
     27 
     28  /** 
     29   * Constructor. 
     30   */ 
     31  public function __construct(sfEventDispatcher $dispatcher, $parameters = array(), $options = array()) 
     32  { 
     33    $this->initialize($dispatcher, $parameters, $options); 
     34  } 
    2335 
    2436  /** 
     
    2941   * @param  array             An associative array of options. 
    3042   * 
    31    * @return Boolean  true, if initialization completes successfully, otherwise false. 
     43   * <b>Options:</b> 
     44   * 
     45   * # <b>escaping_strategy</b> - [off]              - The escaping strategy (on or off) 
     46   * # <b>escaping_method</b>   - [ESC_SPECIALCHARS] - The escaping method (ESC_RAW, ESC_ENTITIES, ESC_JS, ESC_JS_NO_ENTITIES, or ESC_SPECIALCHARS) 
     47   * 
     48   * @return Boolean   true, if initialization completes successfully, otherwise false. 
    3249   * 
    3350   * @throws <b>sfInitializationException</b> If an error occurs while initializing this view parameter holder. 
     
    4158 
    4259    $this->add($parameters); 
     60 
     61    $this->setEscaping(isset($options['escaping_strategy']) ? $options['escaping_strategy'] : false); 
     62    $this->setEscapingMethod(isset($options['escaping_method']) ? $options['escaping_method'] : 'ESC_SPECIALCHARS'); 
    4363  } 
    4464 
     
    5070  public function isEscaped() 
    5171  { 
    52     return false
     72    return in_array($this->getEscaping(), array('on', true), true)
    5373  } 
    5474 
     
    5777   * 
    5878   * @return array An array of view parameters 
     79   * 
     80   * @throws InvalidArgumentException 
    5981   */ 
    6082  public function toArray() 
    6183  { 
    62     return $this->getAll(); 
     84    $attributes = array(); 
     85 
     86    if ($this->isEscaped()) 
     87    { 
     88      $attributes['sf_data'] = sfOutputEscaper::escape($this->getEscapingMethod(), $this->getAll()); 
     89      foreach ($attributes['sf_data'] as $key => $value) 
     90      { 
     91        $attributes[$key] = $value; 
     92      } 
     93    } 
     94    else if (in_array($this->getEscaping(), array('off', false), true)) 
     95    { 
     96      $attributes = $this->getAll(); 
     97      $attributes['sf_data'] = sfOutputEscaper::escape(ESC_RAW, $this->getAll()); 
     98    } 
     99    else 
     100    { 
     101      throw new InvalidArgumentException(sprintf('Unknown strategy "%s".', $this->getEscaping())); 
     102    } 
     103 
     104    return $attributes; 
     105  } 
     106 
     107  /** 
     108   * Gets the default escaping strategy associated with this view. 
     109   * 
     110   * The escaping strategy specifies how the variables get passed to the view. 
     111   * 
     112   * @return string the escaping strategy 
     113   */ 
     114  public function getEscaping() 
     115  { 
     116    return $this->escaping; 
     117  } 
     118 
     119  /** 
     120   * Sets the escape character strategy. 
     121   * 
     122   * @param string Escape code 
     123   */ 
     124  public function setEscaping($escaping) 
     125  { 
     126    $this->escaping = $escaping; 
     127  } 
     128 
     129  /** 
     130   * Returns the name of the function that is to be used as the escaping method. 
     131   * 
     132   * If the escaping method is empty, then that is returned. The default value 
     133   * specified by the sub-class will be used. If the method does not exist (in 
     134   * the sense there is no define associated with the method), an exception is 
     135   * thrown. 
     136   * 
     137   * @return string The escaping method as the name of the function to use 
     138   * 
     139   * @throws InvalidArgumentException If the method does not exist 
     140   */ 
     141  public function getEscapingMethod() 
     142  { 
     143    if (empty($this->escapingMethod)) 
     144    { 
     145      return $this->escapingMethod; 
     146    } 
     147 
     148    if (!defined($this->escapingMethod)) 
     149    { 
     150      throw new InvalidArgumentException(sprintf('The escaping method "%s" is not available.', $this->escapingMethod)); 
     151    } 
     152 
     153    return constant($this->escapingMethod); 
     154  } 
     155 
     156  /** 
     157   * Sets the escaping method for the current view. 
     158   * 
     159   * @param string Method for escaping 
     160   */ 
     161  public function setEscapingMethod($method) 
     162  { 
     163    $this->escapingMethod = $method; 
    63164  } 
    64165 
     
    70171  public function serialize() 
    71172  { 
     173    $this->set('_sf_escaping_method', $this->escapingMethod); 
     174    $this->set('_sf_escaping', $this->escaping); 
     175 
    72176    $tmp = clone $this; 
    73177    foreach ($tmp->getNames() as $key) 
     
    80184    $tmp->dispatcher = null; 
    81185 
    82     return serialize($tmp->getAll()); 
     186    $serialized = serialize($tmp->getAll()); 
     187 
     188    $this->remove('_sf_escaping_method'); 
     189    $this->remove('_sf_escaping'); 
     190 
     191    return $serialized; 
    83192  } 
    84193 
     
    91200 
    92201    $this->initialize(sfContext::hasInstance() ? sfContext::getInstance()->getEventDispatcher() : new sfEventDispatcher()); 
     202 
     203    $this->setEscapingMethod($this->remove('_sf_escaping_method')); 
     204    $this->setEscaping($this->remove('_sf_escaping')); 
    93205  } 
    94206} 
  • branches/1.1/test/functional/escapingTest.php

    r8201 r8260  
    1818 
    1919$b-> 
    20   get('/escaping/both')-> 
    21   isStatusCode(200)-> 
    22   isRequestParameter('module', 'escaping')-> 
    23   isRequestParameter('action', 'both')-> 
    24   responseContains('<h1>Lorem &lt;strong&gt;ipsum&lt;/strong&gt; dolor sit amet.</h1>')-> 
    25   responseContains('<h2>Lorem &lt;strong&gt;ipsum&lt;/strong&gt; dolor sit amet.</h2>'); 
    26 ; 
    27  
    28 $b-> 
    2920  get('/escaping/on')-> 
    3021  isStatusCode(200)-> 
    3122  isRequestParameter('module', 'escaping')-> 
    3223  isRequestParameter('action', 'on')-> 
    33   responseContains('<h1>-</h1>')-> 
     24  responseContains('<h1>Lorem &lt;strong&gt;ipsum&lt;/strong&gt; dolor sit amet.</h1>')-> 
    3425  responseContains('<h2>Lorem &lt;strong&gt;ipsum&lt;/strong&gt; dolor sit amet.</h2>'); 
    3526; 
     
    4132  isRequestParameter('action', 'off')-> 
    4233  responseContains('<h1>Lorem <strong>ipsum</strong> dolor sit amet.</h1>')-> 
    43   responseContains('<h2>-</h2>'); 
     34  responseContains('<h2>Lorem <strong>ipsum</strong> dolor sit amet.</h2>'); 
    4435; 
  • branches/1.1/test/functional/fixtures/project/apps/frontend/modules/escaping/actions/actions.class.php

    r8201 r8260  
    1818  } 
    1919 
    20   public function executeBoth() 
    21   { 
    22     sfConfig::set('sf_escaping_strategy', 'both'); 
    23   } 
    24  
    2520  public function executeOn() 
    2621  { 
  • branches/1.1/test/functional/fixtures/project/apps/frontend/modules/escaping/templates/indexSuccess.php

    r6723 r8260  
    1 <h1><?php echo isset($var) ? $var : '-' ?></h1> 
    2 <h2><?php echo isset($sf_data) ? $sf_data->get('var') : '-' ?></h2> 
     1<h1><?php echo $var ?></h1> 
     2<h2><?php echo $sf_data->get('var') ?></h2> 
  • branches/1.1/test/unit/view/sfViewParameterHolderTest.php

    r7107 r8260  
    33/* 
    44 * This file is part of the symfony package. 
    5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 
     5 * (c) Fabien Potencier <fabien.potencier@symfony-project.com> 
    66 *  
    77 * For the full copyright and license information, please view the LICENSE 
     
    1212require_once($_test_dir.'/unit/sfContextMock.class.php'); 
    1313 
    14 $t = new lime_test(5, new lime_output_color()); 
     14$t = new lime_test(24, new lime_output_color()); 
     15 
     16define('ESC_SPECIALCHARS', 'esc_specialchars'); 
     17function esc_specialchars($value) 
     18
     19  return "-ESCAPED-$value-ESCAPED-"; 
     20
     21 
     22define('ESC_RAW', 'esc_raw'); 
     23function esc_raw($value) 
     24
     25  return $value; 
     26
    1527 
    1628class myRequest 
     
    3446} 
    3547 
    36 $context = sfContext::getInstance(array('request' => 'myRequest')); 
     48$context = sfContext::getInstance(array( 
     49  'request' => 'myRequest', 
     50)); 
    3751$dispatcher = $context->dispatcher; 
    3852 
     
    4256// ->initialize() 
    4357$t->diag('->initialize()'); 
    44 $p = new sfViewParameterHolder(); 
     58$p = new sfViewParameterHolder($dispatcher); 
     59$t->is($p->get('sf_user'), $context->user, '->initialize() add some symfony shortcuts as parameters'); 
     60$t->is($p->get('sf_request'), $context->request, '->initialize() add some symfony shortcuts as parameters'); 
     61$t->is($p->get('sf_response'), $context->response, '->initialize() add some symfony shortcuts as parameters'); 
     62 
    4563$p->initialize($dispatcher, array('foo' => 'bar')); 
    4664$t->is($p->get('foo'), 'bar', '->initialize() takes an array of default parameters as its second argument'); 
    4765 
     66$p->initialize($dispatcher, array(), array('escaping_strategy' => 'on', 'escaping_method' => 'ESC_RAW')); 
     67$t->is($p->getEscaping(), 'on', '->initialize() takes an array of options as its third argument'); 
     68$t->is($p->getEscapingMethod(), ESC_RAW, '->initialize() takes an array of options as its third argument'); 
     69 
    4870// ->isEscaped() 
    4971$t->diag('->isEscaped()'); 
    50 $t->is($p->isEscaped(), false, '->isEscaped() always returns false'); 
     72$p->setEscaping('on'); 
     73$t->is($p->isEscaped(), true, '->isEscaped() returns true if data will be escaped'); 
     74$p->setEscaping('off'); 
     75$t->is($p->isEscaped(), false, '->isEscaped() returns false if data won\'t be escaped'); 
     76 
     77// ->getEscaping() ->setEscaping() 
     78$t->diag('->getEscaping() ->setEscaping()'); 
     79$p->initialize($dispatcher); 
     80$p->setEscaping('on'); 
     81$t->is($p->getEscaping(), 'on', '->setEscaping() changes the escaping strategy'); 
     82 
     83// ->getEscapingMethod() ->setEscapingMethod() 
     84$t->diag('->getEscapingMethod() ->setEscapingMethod()'); 
     85$p->setEscapingMethod('ESC_RAW'); 
     86$t->is($p->getEscapingMethod(), ESC_RAW, '->setEscapingMethod() changes the escaping method'); 
     87 
     88$p->setEscapingMethod(''); 
     89$t->is($p->getEscapingMethod(), '', '->getEscapingMethod() returns an empty value if the method is empty'); 
     90 
     91try 
     92
     93  $p->setEscapingMethod('nonexistant'); 
     94  $p->getEscapingMethod(); 
     95  $t->fail('->getEscapingMethod() throws an InvalidArgumentException if the escaping method does not exist'); 
     96
     97catch (InvalidArgumentException $e) 
     98
     99  $t->pass('->getEscapingMethod() throws an InvalidArgumentException if the escaping method does not exist'); 
     100
    51101 
    52102// ->toArray() 
     
    55105$a = $p->toArray(); 
    56106$t->is($a['foo'], 'bar', '->toArray() returns an array representation of the parameter holder'); 
     107 
     108// escaping strategies 
     109$p = new sfViewParameterHolder(new sfEventDispatcher(), array('foo' => 'bar')); 
     110 
     111try 
     112{ 
     113  $p->setEscaping('null'); 
     114  $p->toArray(); 
     115  $t->fail('->toArray() throws an InvalidArgumentException if the escaping strategy does not exist'); 
     116} 
     117catch (InvalidArgumentException $e) 
     118{ 
     119  $t->pass('->toArray() throws an InvalidArgumentException if the escaping strategy does not exist'); 
     120} 
     121 
     122$t->diag('Escaping strategy to on'); 
     123$p->setEscaping('on'); 
     124$values = $p->toArray(); 
     125$t->is(count($values), 2, '->toArray() knows about the "on" strategy'); 
     126$t->is(count($values['sf_data']), 1, '->toArray() knows about the "on" strategy'); 
     127$t->is($values['foo'], '-ESCAPED-bar-ESCAPED-', '->toArray() knows about the "on" strategy'); 
     128$t->is($values['sf_data']['foo'], '-ESCAPED-bar-ESCAPED-', '->toArray() knows about the "on" strategy'); 
     129 
     130$t->diag('Escaping strategy to off'); 
     131$p->setEscaping('off'); 
     132$values = $p->toArray(); 
     133$t->is(count($values), 2, '->toArray() knows about the "off" strategy'); 
     134$t->is(count($values['sf_data']), 1, '->toArray() knows about the "on" strategy'); 
     135$t->is($values['foo'], 'bar', '->toArray() knows about the "off" strategy'); 
     136$t->is($values['sf_data']['foo'], 'bar', '->toArray() knows about the "off" strategy'); 
    57137 
    58138// ->serialize() / ->unserialize() 
     
    63143 
    64144// template.filter_parameters 
    65 $p = new sfViewParameterHolder(); 
    66 $p->initialize($dispatcher); 
     145$p = new sfViewParameterHolder($dispatcher); 
    67146$t->is($p->get('sf_request'), $context->request, '->initialize() add some symfony shortcuts as parameters'); 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.