5

I've been playing with Craft for days now and love it. I'm new to Yii but I think I got the hang of everything and how you develop a Craft Plugin. My question is regarding security on input post/get vars.

Reading Yii's documentation is seems the "HtmlPurifier" filter is suggested to protect against XSS - but everything seems to be applied on output?

My question is: Does Craft do anything to "clean" input data OR how do you protect your data against malicious input?

I want to clarify that the data submitted by the user has gone through the validation rules in the Record/Model. Is there anything else you need to keep in mind?

nicael
  • 2,382
  • 7
  • 27
  • 48
naboovalley
  • 2,844
  • 1
  • 16
  • 26

1 Answers1

7

When dealing with XSS concerns, it's important to stay focused on the fact that this is a client-side issue... So in regards to Craft, Twig is the star of the show.

Fortunately, Twig takes measures by default to ensure that your output is safe. For example, any time you're outputting some HTML code via a Twig variable, you're required to add a raw filter to get the HTML to render properly. Otherwise, those special characters get encoded and simply rendered "as is" on the front-end. When you apply the raw filter to a Twig variable, you're essentially telling Twig that you trust whatever data may be coming out of that variable.

Here are a few other Twig filters which may help you out:


Your question puts an emphasis on data input, which isn't typically where XSS issues are addressed. However, there are some things (as you noted) which can help you sort out incoming data.

Validating your data using Yii validation rules is a good start. And I believe the CHtmlPurifier can be useful to you as well. Skimming through the Yii documentation, I believe this helper method can be applied to input data:

$purifier = new \CHtmlPurifier();
$purifier->purify($html)

http://www.yiiframework.com/doc/api/1.1/CHtmlPurifier

Simply apply that helper method to data as it's being stored in your Record, and that should make your data safe for future output.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
Lindsey D
  • 23,974
  • 5
  • 53
  • 110