【问题标题】:Do i need to avoid sql injection in this example magento code?我需要在这个示例 magento 代码中避免 sql 注入吗?
【发布时间】:2018-12-25 22:54:14
【问题描述】:

以下源码需要避开sql-injection? 如果$some_text的例子是sql-injected攻击,下面的源代码是危险的?

  1. 一般 Magento 代码

    $tmp_sale_info_collection = Mage::getModel('some/module')
        ->getCollection()
        ->addFieldToFilter('seller_id', array('eq' => $some_text));
    
  2. 使用getSelect()内连接

    $orderItem = Mage::getModel('sales/order_item')->getCollection();
    $orderItem->getSelect()
        ->joinInner(
            array(
                'order' => Mage::getSingleton('core/resource')->getTableName('sales/order')
            ), 
            'order.entity_id = main_table.order_id'
        )
        ->where('product_id=?', $some_text)
        ->order('main_table.order_id DESC');
    
  3. 使用fetchAll() style1

    $select = $adapter->select()
        ->from($table, array())
        ->where($entityTypeIdField . ' =?', $some_text)
        ->where('attribute_id =?', $some_text)
        ->where('store_id =?', $some_text)
        ->columns('*');
    
    $values = $adapter->fetchAll($select);
    
  4. 使用fetchAll()style2

    $sql_select = "SELECT * from onetable where from_id ='$some_text'";
    $resource = Mage::getModel('core/resource');
    $read = $resource->getConnection('core_read');
    $results = $read->fetchAll($sql_select);
    

哪个危险,哪个不危险?

=================== 已编辑===========================

  1. 修改第二个

    $orderItem = Mage::getModel('sales/order_item')->getCollection(); $orderItem->getSelect() ->加入内部( 大批( 'order' => Mage::getSingleton('core/resource')->getTableName('sales/order') ), 'order.entity_id = '。 $some_text 。 ' ) ->where('product_id=?', $some_text) ->order('main_table.order_id DESC');

【问题讨论】:

    标签: php magento sql-injection


    【解决方案1】:

    Magento 使用带有数据库支持的绑定变量的预处理语句。它们由 PDO、MySQLi 和其他库提供。 如果数据库层不支持绑定变量,则使用特定于数据库的字符串转义函数引用传递给数据库的每个非数字用户提供的值,例如 mysql_real_escape_string()、addslashes()、magic_quotes_gpc、get_magic_quotes_gpc()、stripslashes( )、htmlentities、htmlspecialchars。

    只是为了你的例子:

    在这里,我使用了您的第一个查询:

    $productSku = array('101_7898_2200');
    //$productSku = array('" OR ""="');//  sku= "' OR ''='"; And sku= '" OR ""="';
    
    $attributes = Mage::getSingleton('catalog/config')->getProductAttributes();
    $collection = Mage::getModel('catalog/product')
                ->getCollection()                
                ->addAttributeToFilter('sku', array('eq' => $productSku));
               // ->addAttributeToSelect($attributes);
    
    echo "<pre> product collection query="; print_r($collection->getSelect()->__toString()); 
    echo "<pre> product collection data="; print_r($collection->getData());
    

    这里的 SQL 查询是:

    SELECT 1 AS `status`, `e`.`entity_id`, `e`.`type_id`, `e`.`attribute_set_id`, `e`.`sku` FROM `catalog_product_flat_1` AS `e` WHERE (e.sku = '101_7898_2200')
    

    输出集合是 ::

    Array
    (
    [0] => Array
        (
            [status] => 1
            [entity_id] => 22835
            [type_id] => configurable
            [attribute_set_id] => 9
            [sku] => 101_7898_2200
        )
    
    )
    

    但是当我传递 sql 注入参数时 sku= "' OR ''='";和 sku= '" OR ""="'; 之后,我们得到以下结果:

    SQL 查询是 ::

    选择 1 AS status, e.entity_id, e.type_id, e.attribute_set_id, e.sku FROM @987654343@@ASE9765433@3 (e.sku = '\" OR \"\"=\"')

    OUTPUT 集合是 :: Nothing

    产品集合数据=数组 ( )

    避免 SQL 注入漏洞的最佳建议是“不要直接查询数据库”。您应该使用在这些情况下可以保护您的 ORM。尤其是在从 EAV 表中获取数据时。

    但是,如果您正在运行带有参数输入的本机 sql 查询,您应该使用 Zend_Db_Select 的绑定将查询参数绑定到查询,而不是使用完整的 SQL 语句:

    $query = $this->_connection->select()->from('eav_attribute')->where('attribute_id=?', $attributeId); $result = $this->_connection->fetchAll($query);

    【讨论】:

      【解决方案2】:

      我认为你不应该使用完整的 SQL 语句。所以,不应该使用“(4)Use fetchAll() style2

      【讨论】:

        猜你喜欢
        • 2011-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-16
        • 2015-01-06
        • 2023-04-10
        • 2012-10-21
        • 2017-12-02
        相关资源
        最近更新 更多