手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆

drupal的db_query安全过滤

首页 > PHP >

这篇 文章已经是07年的了,不过,我一直没有关注过drupal的db类,所以对于这方面我也根本 就没有在意过。8过,我在我的siterpc中,我也是采用了sprintf的写法,不是为了安全,而是为了格式化。因为在siterpc里我们几乎不接受外部变量,所有的变量都是由我们自己传入,所以对于安全方便忽略了很多。但还是用sprintf来格式化SQL语句了。

因此看到这篇drupal的db_query安全过滤,突然发现,可以让我的代码少写很多了。(参数可变时请使用vsprintf)

以下是原文 :

Drupal通过C风格的字符串输出格式实现了对sql语句的安全过滤。
使用方法:

PHP代码
  1. db_query("SELECT n.nid FROM {node} n WHERE n.type = '%s'"$type);//正确做法  
  2. //这不等于以下语句,使用sprintf并不能避免mysql注入。  
  3. db_query(sprintf("SELECT n.nid FROM {node} n WHERE n.type = '%s'"$type)); //不正确  
drupal db_query核心代码如下:
PHP代码
  1. /** 
  2.  * Indicates the place holders that should be replaced in _db_query_callback(). 
  3.  */  
  4. define('DB_QUERY_REGEXP''/(%d|%s|%%|%f|%b)/');  
  5.   
  6. /** 
  7.  * Runs a basic query in the active database. 
  8.  * 
  9.  * User-supplied arguments to the query should be passed in as separate 
  10.  * parameters so that they can be properly escaped to avoid SQL injection 
  11.  * attacks. 
  12.  * 
  13.  * @param $query 
  14.  *   A string containing an SQL query. 
  15.  * @param ... 
  16.  *   A variable number of arguments which are substituted into the query 
  17.  *   using printf() syntax. Instead of a variable number of query arguments, 
  18.  *   you may also pass a single array containing the query arguments. 
  19.  
  20.  *   Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose 
  21.  *   in '') and %%. 
  22.  * 
  23.  *   NOTE: using this syntax will cast NULL and FALSE values to decimal 0, 
  24.  *   and TRUE values to decimal 1. 
  25.  * 
  26.  * @return 
  27.  *   A database query result resource, or FALSE if the query was not 
  28.  *   executed correctly. 
  29.  */  
  30. function db_query($query) {  
  31.   $args = func_get_args();  
  32.   array_shift($args);  
  33.   $query = db_prefix_tables($query);  
  34.   if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax  
  35.     $args = $args[0];  
  36.   }  
  37.   _db_query_callback($args, TRUE);  
  38.   $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback'$query);  
  39.   return _db_query($query);  
  40. }  
  41.   
  42. /** 
  43.  * Helper function for db_query(). 
  44.  */  
  45. function _db_query_callback($match$init = FALSE) {  
  46.   static $args = NULL;  
  47.   if ($init) {  
  48.     $args = $match;  
  49.     return;  
  50.   }  
  51.   
  52.   switch ($match[1]) {  
  53.     case '%d'// We must use type casting to int to convert FALSE/NULL/(TRUE?)  
  54.       return (int) array_shift($args); // We don't need db_escape_string as numbers are db-safe  
  55.     case '%s': 
  56.       return db_escape_string(array_shift($args)); 
  57.     case '%%':  
  58.       return '%'; 
  59.     case '%f': 
  60.       return (float) array_shift($args); 
  61.     case '%b': // binary data  
  62.       return db_encode_blob(array_shift($args));  
  63.   }  
  64. }  
简单的对一些输入做了处理。参考: http://drupal.org/node/101496

原文来自于:http://www.luochunhui.com/id/315




本站采用创作共享版权协议, 要求署名、非商业和保持一致. 本站欢迎任何非商业应用的转载, 但须注明出自"易栈网-膘叔", 保留原始链接, 此外还必须标注原文标题和链接.

Tags: drupal, php, escape_string

« 上一篇 | 下一篇 »

只显示10条记录相关文章

使用PHP得到所有的HTTP请求头 (浏览: 62682, 评论: 3)
我为什么会选用phpstorm (浏览: 52520, 评论: 5)
快速生成目录树 (浏览: 46711, 评论: 7)
通过file_get_contents来Post数据的实例 (浏览: 46286, 评论: 5)
PHP导入导出Excel方法 (浏览: 45084, 评论: 3)
PHP的XSS攻击过滤函数 (浏览: 42615, 评论: 2)
PHP中Eval的作用 (浏览: 41551, 评论: 4)
超详细:在Mac OS X中配置Apache + PHP + MySQL (浏览: 40230, 评论: 1)
PHP常见错误(二) (浏览: 39692, 评论: 1)
PHP sendmail (浏览: 37841, 评论: 7)

2条记录访客评论

才发现膘叔的blog 汗

Post by tsyan on 2010, November 23, 4:09 PM 引用此文发表评论 #1

膘叔威武,居然在这儿看到你....

Post by 远远 on 2010, November 23, 2:30 PM 引用此文发表评论 #2


发表评论

评论内容 (必填):