Development

Changeset 8266

You must first sign up to be able to contribute.

Changeset 8266

Show
Ignore:
Timestamp:
04/04/08 11:59:19 (3 months ago)
Author:
francois
Message:

[doc 1.1] documented changes in the output escaping

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • doc/branches/1.1/book/07-Inside-the-View-Layer.txt

    r8253 r8266  
    916916    <script>alert(document.cookie)</script> 
    917917 
    918 You could escape your output manually by enclosing every unsure value in a call to `htmlentities()`, but that approach would be very repetitive and error-prone. Instead, symfony provides a special system, called output escaping, which automatically escapes every variable output in a template. It is activated by a simple parameter in the application `settings.yml`. 
     918You could escape your output manually by enclosing every unsure value in a call to `htmlspeciachars()`, but that approach would be very repetitive and error-prone. Instead, symfony provides a special system, called output escaping, which automatically escapes every variable output in a template. It is activated by a simple parameter in the application `settings.yml`. 
    919919 
    920920### Activating Output Escaping 
     
    922922Output escaping is configured globally for an application in the `settings.yml` file. Two parameters control the way that output escaping works: the strategy determines how the variables are made available to the view, and the method is the default escaping function applied to the data. 
    923923 
    924 The next sections describe these settings in detail but, basically, all you need to do to activate output escaping is to set the `escaping_strategy` parameter to `both` instead of its default value `bc`, as shown in Listing 7-42. 
     924Basically, all you need to do to activate output escaping is to set the `escaping_strategy` parameter to `on` instead of its default value `off`, as shown in Listing 7-42. 
    925925 
    926926Listing 7-42 - Activating Output Escaping, in `frontend/config/settings.yml` 
     
    928928    all: 
    929929      .settings: 
    930         escaping_strategy: both 
    931         escaping_method:   ESC_ENTITIE
    932  
    933 This will add `htmlentities()` to all variable output by default. For instance, suppose that you define a `test` variable in an action as follows: 
     930        escaping_strategy: on 
     931        escaping_method:   ESC_SPECIALCHAR
     932 
     933This will add `htmlspecialchars()` to all variable output by default. For instance, suppose that you define a `test` variable in an action as follows: 
    934934 
    935935    [php] 
     
    940940    [php] 
    941941    echo $test; 
    942      => ><script>alert(document.cookie)</script> 
    943  
    944 Activating output escaping also gives access to an `$sf_data` variable in every template. It is a container object referencing all the escaped variables. So you can also output the test variable with the following: 
     942     => <script>alert(document.cookie)</script> 
     943 
     944In addition, every template has access to an `$sf_data` variable, which is a container object referencing all the escaped variables. So you can also output the test variable with the following: 
    945945 
    946946    [php] 
    947947    echo $sf_data->get('test'); 
    948     => ><script>alert(document.cookie)</script> 
     948    => <script>alert(document.cookie)</script> 
    949949 
    950950>**TIP** 
    951951>The $sf_data object implements the Array interface, so instead of using the `$sf_data->get('myvariable')`, you can retrieve escaped values by calling `$sf_data['myvariable']`. But it is not a real array, so functions like `print_r()` will not work as expected. 
    952952 
    953 This object also gives you access to the unescaped, or raw, data. This is useful when a variable stores HTML code meant to be interpreted by the browser, provided that you trust this variable. Call the `getRaw()` method when you need to output the raw data. 
     953`$sf_data` also gives you access to the unescaped, or raw, data. This is useful when a variable stores HTML code meant to be interpreted by the browser, provided that you trust this variable. Call the `getRaw()` method when you need to output the raw data. 
    954954 
    955955    [php] 
     
    959959You will have to access raw data each time you need variables containing HTML to be really interpreted as HTML. You can now understand why the default layout uses `$sf_data->getRaw('sf_content')` to include the template, rather than a simpler `$sf_content`, which breaks when output escaping is activated. 
    960960 
    961 ### Escaping Strategy 
    962  
    963 The `escaping_strategy` setting determines the way variables are output by default. The following are the possible values: 
    964  
    965   * `bc` (backward compatible mode): Variables are not escaped, but an escaped version of each variable is available through the `$sf_data` container. So the data is raw by default, unless you choose to use the escaped value via the `$sf_data` object. This is the default value, and you should be aware that with this strategy, your application is subject to XSS attack risks. 
    966   * `both`: All variables are escaped by default. Values are also made available in the `$sf_data` container. This is the recommended strategy, since you will be at risk only if you voluntarily output raw data. In some cases, you will have to use unescaped data--for instance, if you output a variable that contains HTML with the intention that this HTML be rendered as such in the browser. So be aware that if you switch to this strategy with a partially developed application, some features may break. The best choice is to use this setting right from the beginning. 
    967   * `on`: Values are available only in the `$sf_data` container. This is the most secure and fastest way to deal with escaping, because each time you output a variable, you must choose if you want to use the escaped version with `get()` or the raw version with `getRaw()`. So you are always aware of the possibility that data may be corrupted. 
    968   * `off`: Turns off output escaping. The `$sf_data` container is not available in templates. You can choose to use this strategy rather than `bc` to speed up your application if you are sure that you will never need to access escaped data. 
     961When `escaping_strategy` is `off`, `$sf_data` is still available, but it always returns raw data. 
     962 
     963>**TIP** 
     964>Symfony 1.0 had two other possible values for `escaping_strategy`. `bc` now fallbacks to `off`, and `both` now fallbacks to `on`. Using any of these values still work, but will log an error. 
    969965 
    970966### Escaping Helpers 
     
    973969 
    974970  * `ESC_RAW`: Doesn't escape the value. 
     971  * `ESC_SPECIALCHARS`: Applies the PHP function `htmlspecialchars()` to the input. 
    975972  * `ESC_ENTITIES`: Applies the PHP function `htmlentities()` to the input with `ENT_QUOTES` as the quote style. 
    976973  * `ESC_JS`: Escapes a value to be put into a JavaScript string that is going to be used as HTML. This is useful for escaping things where HTML is going to be dynamically changed using JavaScript. 
     
    979976### Escaping Arrays and Objects 
    980977 
    981 Output escaping not only works for strings, but also for arrays and objects. Any values that are objects or arrays will pass on their escaped state to their children. Assuming your strategy is set to `both`, Listing 7-43 demonstrates the escaping cascade. 
     978Output escaping not only works for strings, but also for arrays and objects. Any values that are objects or arrays will pass on their escaped state to their children. Assuming your strategy is set to `on`, Listing 7-43 demonstrates the escaping cascade. 
    982979 
    983980Listing 7-43 - Escaping Also Works for Arrays and Objects 
     
    10161013     => sfOutputEscaperObjectDecorator 
    10171014 
    1018 This explains why some usual PHP functions (like `array_shift()`, `print_r()`, and so on) don't work on escaped arrays anymore. But they can be still be accessed using `[]`, be traversed using foreach, and they give back the right result with `count()` (`count()` works only with PHP 5.2 or later). And in templates, the data should be read-only anyway, so most access will be through the methods that do work. 
     1015This explains why some usual PHP functions (like `array_shift()`, `print_r()`, and so on) don't work on escaped arrays anymore. But they can be still be accessed using `[]`, be traversed using `foreach`, and they give back the right result with `count()` (`count()` works only with PHP 5.2 or later). And in templates, the data should be read-only anyway, so most access will be through the methods that do work. 
    10191016 
    10201017You still have a way to retrieve the raw data through the `$sf_data` object. In addition, methods of escaped objects are altered to accept an additional parameter: an escaping method. So you can choose an alternative escaping method each time you display a variable in a template, or opt for the `ESC_RAW` helper to deactivate escaping. See Listing 7-44 for an example. 
  • doc/branches/1.1/book/18-Performance.txt

    r7705 r8266  
    544544          auto_start: false 
    545545 
    546 The same applies for the database (as explained in the "Tweaking the Model" section earlier in this chapter) and output escaping feature (see Chapter 7). If your application makes no use of them, deactivate them for a small performance gain, this time in the `settings.yml` file (see Listing 18-20). 
    547  
    548 Listing 18-20 - Turning Features Off, in `frontend/config/settings.yml` 
     546The same applies for the database feature (as explained in the "Tweaking the Model" section earlier in this chapter). If your application makes no use of a database, deactivate it for a small performance gain, this time in the `settings.yml` file (see Listing 18-20). 
     547 
     548Listing 18-20 - Turning Database Features Off, in `frontend/config/settings.yml` 
    549549 
    550550    all: 
    551551      .settings: 
    552552        use_database:      off    # Database and model features 
    553         escaping_strategy: off    # Output escaping feature 
    554553 
    555554As for the security features (see Chapter 6), you can deactivate them in the `filters.yml` file, as shown in Listing 18-21. 
  • doc/branches/1.1/book/19-Mastering-Symfony-s-Configuration-Files.txt

    r7705 r8266  
    5959`i18n`                  | Enables interface translation (see Chapter 13). Set it to `on` for multilingual applications. | `off` 
    6060`logging_enabled`       | Enables logging of symfony events. Set it to off when you want to ignore the logging.yml settings and turn symfony logging off completely. | `on` 
    61 `escaping_strategy`     | Enables and defines the policy of the output escaping feature (see Chapter 7). Set it to `off` if you don't use the `$sf_data` container in your templates. | `bc
     61`escaping_strategy`     | Enables the output escaping feature (see Chapter 7). Set it to `on` if you want data passed to your templates to be escaped. | `off
    6262`cache`                 | Enables template caching (see Chapter 12). Set it to `on` if one of your modules includes `cache.yml` file. The cache filter (`sfCacheFilter`) is enabled only if it is on. | `off` in development, `on` in production 
    6363`web_debug`             | Enables the web debug toolbar for easy debugging (see Chapter 16). Set it to `on` to display the toolbar on every page. The web debug filter (`sfWebDebugFilter`) is enabled ony if it is on. | `on` in development, `off` in production 
     
    7575Output escaping settings control the way the variables are accessible in the template (see Chapter 7). The `settings.yml` file includes two settings for this feature: 
    7676 
    77   * The `escaping_strategy` setting can take the value `bc`, `both`, `on`, or `off`. 
    78   * The escaping_method setting can be set to `ESC_RAW`, `ESC_ENTITIES`, `ESC_JS`, or `ESC_JS_NO_ENTITIES`. 
     77  * The `escaping_strategy` setting can take the value`on`, or `off`. 
     78  * The escaping_method setting can be set to `ESC_RAW`, `ESC_SPECIALCHARS`, `ESC_ENTITIES`, `ESC_JS`, or `ESC_JS_NO_ENTITIES`. 
    7979 
    8080#### Routing Settings