I may be thinking too much on this, but let’s say I have a Website field on database. I’ve used strip_tags to strip all HTML tags. But if the user inputs this
javascript:alert(‘test’)
It will get passed since it’s a string. But then, the HTML will generate
<a href="<?php echo prep_url($website);?>">Website</a> //the code in view file
<a href="javascript:alert('test')">Website</a> //bad
and the Javascript will execute if clicked. Notice too the prep_url doesn’t work.
Any suggestion? I’ve looked at HTMLPurifier, but it is quite big on size and I don’t really want to do some major change.
Thanks
You shouldn’t use
strip_tagsif you expect a url, you should validate the URL and probablyurlencodeit. Here’s one way withfilter_var:So if
filter_var($user_input, FILTER_VALIDATE_URL)isFALSE, don’t accept the user input. This should negate the need for CI’sxss_clean()although you may want to run it anyways when you put it in the HTML attribute. You may need to runprep_urlon the input before validating if you don’t require the user to enter thehttp://part.There are many ways to validate a URL, just pick one you like.