【问题标题】:nodejs filtering inputsnodejs过滤输入
【发布时间】:2012-02-07 00:36:02
【问题描述】:

在 PHP 中过滤输入数据我使用函数 htmlspecialchars 和 mysql_real_escape_string。 nodejs中有这样的功能吗?

我需要检查我的路由器函数中的所有输入,以防止像 xss 这样的黑客攻击。 谢谢!

【问题讨论】:

    标签: php node.js filtering


    【解决方案1】:

    node-validator 是完美的库,它具有许多用于验证和卫生/过滤的功能,例如:

    entityDecode()                  //Decode HTML entities
    entityEncode()
    xss()                           //Remove common XSS attack vectors from text (default)
    xss(true)                       //Remove common XSS attack vectors from images
    

    contains(str)
    notContains(str)
    regex(pattern, modifiers)       //Usage: regex(/[a-z]/i) or regex('[a-z]','i')
    notRegex(pattern, modifiers)
    len(min, max)                   //max is optional
    isUUID(version)                 //Version can be 3 or 4 or empty, see http://en.wikipedia.org/wiki/Universally_unique_identifier
    isDate()                        //Uses Date.parse() - regex is probably a better choice
    isAfter(date)                   //Argument is optional and defaults to today
    isBefore(date)                  //Argument is optional and defaults to today
    isIn(options)                   //Accepts an array or string
    

    【讨论】:

    • 谢谢。该库中是否有类似 php 的 strip_tags 函数?
    • 我没看到,这里是function strip_tags(oldString) { return oldString.replace(/(<([^>]+)>)/ig,""); }
    【解决方案2】:

    Google Caja HTML sanitizer 有一个 NodeJS 包。或者你使用here提供的答案:

    function escapeHtml(unsafe) {
      return unsafe
          .replace(/&/g, "&")
          .replace(/</g, "&lt;")
          .replace(/>/g, "&gt;")
          .replace(/"/g, "&quot;")
          .replace(/'/g, "&#039;");
    }
    

    对于 SQL,这取决于您使用的库,但其中大多数会转义参数化查询。

    【讨论】:

      猜你喜欢
      • 2014-06-15
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 2011-08-23
      • 2014-05-25
      • 2011-04-17
      • 1970-01-01
      相关资源
      最近更新 更多