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


db_query("SELECT n.nid FROM {node} n WHERE n.type = '%s'", $type);//正确做法
//这不等于以下语句,使用sprintf并不能避免mysql注入。
db_query(sprintf("SELECT n.nid FROM {node} n WHERE n.type = '%s'", $type)); //不正确
 


drupal db_query核心代码如下:


/**
 * Indicates the place holders that should be replaced in _db_query_callback().
 */

define('DB_QUERY_REGEXP', '/(%d|%s|%%|%f|%b)/');

/**
 * Runs a basic query in the active database.
 *
 * User-supplied arguments to the query should be passed in as separate
 * parameters so that they can be properly escaped to avoid SQL injection
 * attacks.
 *
 * @param $query
 *   A string containing an SQL query.
 * @param ...
 *   A variable number of arguments which are substituted into the query
 *   using printf() syntax. Instead of a variable number of query arguments,
 *   you may also pass a single array containing the query arguments.

 *   Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
 *   in '') and %%.
 *
 *   NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
 *   and TRUE values to decimal 1.
 *
 * @return
 *   A database query result resource, or FALSE if the query was not
 *   executed correctly.
 */

function db_query($query) {
  $args = func_get_args();
  array_shift($args);
  $query = db_prefix_tables($query);
  if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
    $args = $args[0];
  }
  _db_query_callback($args, TRUE);
  $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
  return _db_query($query);
}

/**
 * Helper function for db_query().
 */

function _db_query_callback($match, $init = FALSE) {
  static $args = NULL;
  if ($init) {
    $args = $match;
    return;
  }

  switch ($match[1]) {
    case '%d': // We must use type casting to int to convert FALSE/NULL/(TRUE?)
      return (int) array_shift($args); // We don't need db_escape_string as numbers are db-safe
    case '%s':
      return db_escape_string(array_shift($args));
    case '%%':
      return '%';
    case '%f':
      return (float) array_shift($args);
    case '%b': // binary data
      return db_encode_blob(array_shift($args));
  }
}
 


参考: http://drupal.org/node/101496
Defined tags for this entry: ,

作者 rollenc

| 主要出源 (0)
请对 365 天内更新的文章进行打分
当前分数: 3.12 of 5 。 17 次打分。 245 次点击
Bookmark drupal的db_query安全过滤  at del.icio.us Digg drupal的db_query安全过滤 Mixx drupal的db_query安全过滤 Bloglines drupal的db_query安全过滤 Technorati drupal的db_query安全过滤 Fark this: drupal的db_query安全过滤 Bookmark drupal的db_query安全过滤  at YahooMyWeb Bookmark drupal的db_query安全过滤  at Furl.net Bookmark drupal的db_query安全过滤  at reddit.com Bookmark drupal的db_query安全过滤  at blinklist.com Bookmark drupal的db_query安全过滤  at Spurl.net Bookmark drupal的db_query安全过滤  at NewsVine Bookmark drupal的db_query安全过滤  at Simpy.com Bookmark drupal的db_query安全过滤  at blogmarks Bookmark drupal的db_query安全过滤  with wists Bookmark drupal的db_query安全过滤  at Ma.gnolia.com wong it! Bookmark using any bookmark manager! Stumble It!

0 引用

  1. 没有引用

0 回复

回复显示方式(直线程 | 分线程)
  1. 没有回复

新增回复


You can use [geshi lang=lang_name [,ln={y|n}]][/lang] tags to embed source code snippets
电子邮件地址将不会被显示,而仅将被用于发送电子邮件通知

为了阻止机器人提交垃圾回复,请在相应的文本框中输入你在下面的图片中所看到的字符串。只有在你输入的字符串和图片中的字符串吻合的情况下,你的回复才能被成功提交。请确认你的浏览器支持、并且已经开启了cookies功能,否则的话,你的回复无法被正确地验证。
CAPTCHA