views:

244

answers:

7

The Zend Framework Manual says the following:

60.3.1. Escaping Output

One of the most important tasks to perform in a view script is to make sure that output is escaped properly; among other things, this helps to avoid cross-site scripting attacks. Unless you are using a function, method, or helper that does escaping on its own, you should always escape variables when you output them.

Why 'always'? Why do I have to escape variables that have not been created or altered by user input?

A: 

Well, if you have hard-coded values (let's say language translations, which you read from a database or XML file), you don't have to escape them.

But if there is a value that has been created/modified by user, even let's say in admin panel, you have to escape it, because you don't know what kind of data user or if I'm more radical, even administrator, will send.

usoban
+2  A: 

Another thing is many times things that are hard coded one day become user or at least database generated another day. Its just better practice to manage output of variables in your code.

Justin
+4  A: 

As a rule, I would escape anything coming from user input, a data source or even calculations. You want the output to be predictable, escaping ensures that it is. If the value when converted to a string contains characters that break your desired markup, things would get messy.

If you're using a view, $this->escape($variableToEscape) should suffice.

skb
+8  A: 

Users aren't the only source of dodgy strings in output. Consider, for example, the apparently safe string "Romeo & Juliet" coming out of a database. No cross-site scripting there, you say? True enough. Stick it in a web page, however, and the raw ampersand could cause some interesting problems with validation, parsing, etc.

Output escaping isn't just to guard against malicious or accidentally borked input, it ensures that the output is thoroughly sanitised and treated as having no special meaning in the surrounding output format, whether that's HTML, XML, JSON or whatever.

Rob
Is there a way to automatically do this through KO2 and KO3?
Andres
+1  A: 

You could look at it this way: you should always HTML-encode variables, unless you know that they've already been encoded.

Say you have a variable that contains:

foo <b>bar</b>

If you know that it contains HTML tags, and you're okay with that, then you can say that this variable has already been properly HTML-encoded. You could even assign it to a different variable type to make the compiler aware of the distinction (Joel's idea), and have your output functions handle these types without escaping them.

Of course, this means that

foo & <b>bar</b>

is an incorrect value; you would need to ensure that it's:

foo &amp; <b>bar</b>
JW
+1  A: 

I think the best practice here is always escape output unless you intend to output a raw HTML fragment. Even "safe" data can contain characters which need to be escaped. For example, consider the e-mail address '"Bob" <[email protected]>'. If you don't escape it, the browser will think <[email protected]> is a tag.

Adam Crume
+1  A: 

Obviously you want to escape things that are the result of user data to prevent XSS attacks. Since you're often changing what you're republishing and what you're not, you probably can't remember all the places that need to be changed... So even if you get all the nuances correct now, and your site is secure from XSS scripting today, you may at some point may add user input to some variable you're not escaping (or more likely, some variable to some variable to some variable which you're not escaping), which would open you up to XSS attacks.

Escaping by default would prevent that attack.

The other reason is more conceptual: with MVC, all of your markup--which is, by definition, the "view"--should be in your view templates. So if your controller is determining the view, and the view contains all the markup, why not escape your variables?

James Cape