Codeigniter-Desactivar el filtrado XSS en una base post


Estoy tratando de configurar un CMS en la parte posterior de un sitio, pero cada vez que los datos de post tienen un <a href=... en él, los datos de post se eliminan.

Tengo $config['global_xss_filtering'] = TRUE; en la configuración

Mi pregunta ¿hay alguna forma de deshabilitar el filtrado xss para un elemento?

Por ejemplo

$this->input->post('content', true); - lo enciende, pero ¿cómo apagarlo?

Gracias a todos.

PVS

Author: Usman, 2010-09-24

7 answers

Si desea cambiar el comportamiento predeterminado del método post(), puede extender la biblioteca de entrada principal, o si es perezoso, puede cambiar la línea 278 (más o menos) de la biblioteca de entrada para leer:

/**
* Fetch an item from the POST array
*
* @access   public
* @param    string
* @param    bool
* @return   string
*/
function post($index = '', $xss_clean = TRUE)
{
    return $this->_fetch_from_array($_POST, $index, $xss_clean);
}

La única diferencia aquí es que he cambiado la variable {xss_clean a TRUE en lugar de FALSE. Ahora puede desactivar el filtrado global XSS y filtrará automáticamente las entradas a menos que especifique false como segundo parámetro en su llamada al método post() de la biblioteca de entrada. Simplemente un método hacia abajo es el método get(), y se puede cambiar de la misma manera.

Sin embargo, si yo fuera usted, solo extendería la biblioteca nativa, porque hay una buena probabilidad de que se haya olvidado de esto para cuando actualice CodeIgniter, y luego de repente se preguntará por qué está recibiendo XSS atacado. Eso se vería así:

class MY_Input extends CI_Input {

    function My_Input()
    {
        parent::CI_Input();
    }

    function post($index = '', $xss_clean = TRUE)
    {
        return parent::post($index, $xss_clean);
    }
}

Puede obtener más información sobre la extensión de bibliotecas aquí:

Http://codeigniter.com/user_guide/general/creating_libraries.html

 27
Author: treeface,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-08 14:39:29

Si desea mantener activado global xss_clean y anular solo en ciertos casos, puede ampliar la biblioteca de entrada para mantener un clon de $_POST para proporcionar datos sin procesar cuando se le pregunte:

<?php if (!defined('BASEPATH')) exit('No direct access allowed.');
class MY_Input extends CI_Input {

public function __construct() {
    $this->_POST_RAW = $_POST; //clone raw post data 
    parent::__construct(); 
}

public function post($index = null, $xss_clean = TRUE) { 
    if(!$xss_clean){ //if asked for raw post data -eg. post('key', false)-, return raw data. Use with caution.
        return $this->_POST_RAW[$index];
    }
    return parent::post($index, $xss_clean); 
    }
}
?>

De esta manera puede usar $this->input->post('mydata', FALSE) para recuperar datos post sin desinfectar, incluso si xss_clean está habilitado globalmente.

 6
Author: user2966084,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-07 18:40:03

He definido

global $mypost;
$mypost=$_POST;

En el índice.php de mi raíz cms

Entonces en cualquier lugar puedo la variable global como

global $mypost;

$var=isset($mypost["field"])? $mypost["field"]:"";

Siempre que necesite post sin filtro.

Trabajó para mí espero que ayude.

 1
Author: muhammad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-02-10 09:39:37

En mi caso, la solución de treeface no funciona, pero encontré otra manera. Hice MI_Input con _sanitize_globals() y agregué si la construcción está en su lugar donde está desinfectando los datos post.

// Clean $_POST Data
if (is_array($_POST) AND count($_POST) > 0) {
    foreach ($_POST as $key => $val) {
        if($this->_clean_input_keys($key) != 'my_none_sanitize_field')
        $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
    }
}
 1
Author: Tomek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-28 21:36:59

Sí, la extensión a Input con el reemplazo del método post fue muy útil, al igual que el lector astuto que nota que debería ser return parent::post (index index, x xss_clean). Estaba recibiendo errores y no pensé en ese error obvio. Lo arreglé y me voy corriendo.

Lo estamos usando para escapar datos post para usarlos en sentencias sql. Aunque los métodos de base de datos de CI son agradables, tenemos algunas sentencias sql grandes que son más fáciles de codificar a mano.

 0
Author: Zac Imboden,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-18 00:07:30

Trabajando con CI 2.2 Creo que la solución de treeface dejará input->get(), input->cookie() etc. no siendo xss_cleaned. (usamos get in oauth requests, etc.). El cambio de configuración global evita que sean escapados por el constructor y la clase core todavía da por defecto xss_clean a FALSE en estos métodos...

Básicamente he implementado la misma solución en más métodos.

class MY_Input extends CI_Input {

    /* fixes to allow xss_clean to be disabled on a per field basis
    * [ e.g. tinymce html content with style / class / event attributes ]
    * initial ref : http://stackoverflow.com/questions/3788476/codeigniter-disable-xss-filtering-on-a-post-basis
    * this is based on CI 2.2
    * the above (stackoverflow) solution only updates the post method - which means all the rest ( get, get_post, cookie, server, request_headers, get_request_header)
    * NB : we need GET to allow oauth type activities !
    *
    *   1 - change the global config to xss_clean = false [ otherwise the constructor will 'xss_clean' everything before we have a chance to say no ! ]
    *   2 - make all of methods that take the xss_clean parameter use TRUE as default value
    *   3 - we can now pass the second parameter in as FALSE if we do not want to xss_clean
    */

    function get($index = '', $xss_clean = TRUE)
    {
        return parent::get($index, $xss_clean);
    }

    function post($index = '', $xss_clean = TRUE)
    {
        return parent::post($index, $xss_clean);
    }

    function get_post($index = '', $xss_clean = TRUE)
    {
        return parent::get($index, $xss_clean);
    }

    function cookie($index = '', $xss_clean = TRUE)
    {
        return parent::cookie($index, $xss_clean);
    }

    function server($index = '', $xss_clean = TRUE)
    {
        return parent::server($index, $xss_clean);
    }

    function request_headers($xss_clean = TRUE)
    {
        return parent::request_headers($xss_clean);
    }

    function get_request_header($index, $xss_clean = TRUE)
    {
        return parent::get_request_header($index, $xss_clean);
    }

}

Espero que esto sea de alguna ayuda para alguien

 0
Author: pugelarouge,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-31 11:00:05

Puede desactivarlo temporalmente

This this - > config - >set_item ('global_xss_filtering', false);

C c = this this- > input - >post ('content'); entonces vuelve a encenderlo..

This this - > config - >set_item ('global_xss_filtering', true);

 -4
Author: Johnny Tops,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-24 17:50:46