我是否需要在此示例 magento 代码中避免 sql 注入?
Do i need to avoid sql injection in this example magento code?
需要避免以下源代码sql-injection
?
如果示例的 $some_text
是 sql-injected
攻击,下面的源代码是危险的吗?
通用 Magento 代码
$tmp_sale_info_collection = Mage::getModel('some/module')
->getCollection()
->addFieldToFilter('seller_id', array('eq' => $some_text));
使用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');
使用fetchAll()
样式1
$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);
使用fetchAll()
风格2
$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);
哪个危险,哪个不危险?
=================== 已编辑 ========================= ==
修改秒
$orderItem = Mage::getModel('sales/order_item')->getCollection();
$orderItem->getSelect()
->加入内部(
大批(
'order' => Mage::getSingleton('core/resource')->getTableName('sales/order')
),
'order.entity_id = '。 $some_text。 '
)
->哪里('product_id=?', $some_text)
->顺序('main_table.order_id DESC');
我认为你不应该使用完整的 SQL 声明。所以,不应该使用“(4)Use fetchAll() style2”
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')
OUTPUT 集合是 ::
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 查询是 ::
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 = '\" 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);
需要避免以下源代码sql-injection
?
如果示例的 $some_text
是 sql-injected
攻击,下面的源代码是危险的吗?
通用 Magento 代码
$tmp_sale_info_collection = Mage::getModel('some/module') ->getCollection() ->addFieldToFilter('seller_id', array('eq' => $some_text));
使用
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');
使用
fetchAll()
样式1$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);
使用
fetchAll()
风格2$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);
哪个危险,哪个不危险?
=================== 已编辑 ========================= ==
修改秒
$orderItem = Mage::getModel('sales/order_item')->getCollection(); $orderItem->getSelect() ->加入内部( 大批( 'order' => Mage::getSingleton('core/resource')->getTableName('sales/order') ), 'order.entity_id = '。 $some_text。 ' ) ->哪里('product_id=?', $some_text) ->顺序('main_table.order_id DESC');
我认为你不应该使用完整的 SQL 声明。所以,不应该使用“(4)Use fetchAll() style2”
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')
OUTPUT 集合是 ::
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 查询是 ::
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 = '\" 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);