EllisLab text mark
Advanced Search
     
html_escape on set_value
Posted: 03 October 2012 05:21 AM   [ Ignore ]
Joined: 2011-05-26
22 posts

Hi!

For this example I’ve got a input in a view:

<input type="text" name="name" value="<? echo set_value('name'); ?>" /> 

If the form is submitted it will run trought the form_validation in it’s controller. If it fails the value will be returned to the view. But if I fill in:

"><h1>It works!</h1> 

The result will be:

<input type="text" name="name" value=""><h1>It works!</h1>" /> 

After the validation I see the H1! So it’s not escaping with htmlspecialchars or CI’s own function: html_escape.

What’s the best solution to fix this and how do you fixed this?

Just run it trought html_escape in the view, like: ?

<input type="text" name="name" value="<? echo html_escape(set_value('name')); ?>" /> 

Thanks!

 
Posted: 04 October 2012 04:53 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2011-05-26
22 posts

Somebody?

 
Posted: 04 October 2012 06:01 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2006-03-25
1011 posts

That should help you ( htmlentities ):
http://ellislab.com/forums/viewthread/225038/#1032189

Generally I would prefer to use textares for HTML input,
just my personal preference maybe.

 
Posted: 04 October 2012 06:08 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2011-05-26
22 posts

Thanks but as you can see the input is for a name, but if somebody don’t fill in his name but:

"><h1>It works!</h1> 

It “breaks” the code and the H1 is visible when the form validation in the controller fails! So set_value doesn’t escape thinks like the html_escape function do.

So my question is how people fix this or what’s the best practice?

 
Posted: 04 October 2012 06:30 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2006-03-25
1011 posts

I don’t know what is the best praxis but using the “htmlentities” function
should solve the problem breaking your HTML.

 
Posted: 04 October 2012 06:57 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2011-05-26
22 posts

Ok, I litty addition:
The problem is apparently the default value, not the returned value from set_value, that’s likely escaped. Can someone confirm that the value returned from set_value is escaped?

OK:

<input type="text" name="name" value="<? echo set_value('name'); ?>" /> 

LIKE:

<input type="text" name="name" value="&quot;><h1>It works!</h1>" /> 

Not OK (when the form is succesfully submited, but when you go back to the form and $name is the value pasted in):

<input type="text" name="name" value="<? echo set_value('name',$name); ?>" /> 

LIKE:

<input type="text" name="name" value=""><h1>It works!</h1>" /> 

Solution:

<input type="text" name="name" value="<? echo set_value('name',html_escape($name)); ?>" /> 

WILL BE:

<input type="text" name="name" value="&quot;><h1>It works!</h1>" /> 
 
Posted: 06 October 2012 02:29 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2011-05-26
22 posts

Someone?

 
Posted: 06 October 2012 03:03 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2011-10-15
5 posts

set_value() does not escape anything.  You should escape your HTML with html_escape() or add it to a text area.

 
Posted: 06 October 2012 03:11 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2011-10-15
5 posts

I think you may want to look into form_prep() as well which is called automatically when you use set_value().  form_prep() is why you are seeing the HTML escape happening.

From the manual:

form_prep()

Allows you to safely use HTML and characters such as quotes within form elements without breaking out of the form. Consider this example:

$string = ‘Here is a string containing “quoted” text.’;

<input type=“text” name=“myform” value=”$string” />
Since the above string contains a set of quotes it will cause the form to break. The form_prep function converts HTML so that it can be used safely:

<input type=“text” name=“myform” value=”<?php echo form_prep($string); ?>” />
Note: If you use any of the form helper functions listed in this page the form values will be prepped automatically, so there is no need to call this function. Use it only if you are creating your own form elements.

 
Posted: 07 October 2012 06:07 AM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Joined: 2011-05-26
22 posts

If I look at set_value it runs through form_prep. When I take a look at form_prep it runs through html_escape! And html_escape is the same as htmlspecialchars with ENT_QUOTES and the charset.

So.. set_value takes values through htmlspecialchars!
But.. the default value just returns! So that is what I have to escape manuel like:

<input type="text" name="name" value="<? echo set_value('name',html_escape($name)); ?>" /> 

Thanks for the explanation!

 
Posted: 18 October 2012 07:19 PM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Joined: 2010-06-09
7 posts

CodeIgniter automatically passes the $_POST array through htmlspecialchars() when you use set_value(). If you take a look at the set_value() function in the form_helper.php, you will see that the desired value is passed through form_prep() which executes a htmlspecialchars() on the desired field.

In order to avoid this, you need to extend the form_helper adding a third optional parameter that skips the form_prep() execution. Just copy the following code into a MY_form_helper.php and copy it to application/helpers/

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Form Value
 *
 * Grabs a value from the POST array for the specified field so you can
 * re-populate an input field or textarea.  If Form Validation
 * is active it retrieves the info from the validation class
 * NEW: Added a third parameter prep to skip htmlspecialchars escaping
 *
 * @access public
 * @param string
 * @return mixed
 */
if ( ! function_exists('set_value'))
{
 
function set_value($field ''$default ''$prep true)
 
{
  
if (FALSE === ($OBJ =& _get_validation_object()))
  
{
   
if ( ! isset($_POST[$field]))
   
{
    
return $default;
   
}

   
return $prep form_prep($_POST[$field]$field) : $_POST[$field];
  
}

  
return $prep form_prep($OBJ->set_value($field$default), $field) : $OBJ->set_value($field$default);
 
}
}


/* End of file MY_form_helper.php */
/* Location: ./application/helpers/MY_form_helper.php */