GithubHelp home page GithubHelp logo

js-xss's Introduction

Build Status

过滤XSS攻击

安装

npm install xss

原理

通过标签白名单及属性白名单来过滤HTML标签,同时对包含特殊字符的属性值进行处理。默认配置可过滤大多数的XSS攻击代码,可根据实际应用场景来定制白名单及过滤方法。

使用方法

载入模块

var xss = require('xss');

使用默认的配置

var html = xss('<script>alert("xss");</script>');
console.log(html);

修改默认配置

// 添加或更新白名单中的标签 标签名(小写) = ['允许的属性列表(小写)']
xss.whiteList['p'] = ['class', 'style'];
// 删除默认的白名单标签
delete xss.whiteList['div'];

// 自定义处理属性值函数
xss.onTagAttr = function (tag, attr, vaule) {
  // tag:当前标签名(小写)
  // attr:当前属性名(小写)
  // value:当前属性值
  // 返回新的属性值,如果想使用默认的处理方式,不返回任何值即可
  // 比如把属性值中的双引号替换为&amp;quote;:return value.replace(/"/g, '&amp;quote;');
  // 以下为默认的处理代码:
  if (attr === 'href' || attr === 'src') {
    if (/\/\*|\*\//mg.test(value)) {
      return '#';
    }
    if (/^[\s"'`]*((j\s*a\s*v\s*a|v\s*b|l\s*i\s*v\s*e)\s*s\s*c\s*r\s*i\s*p\s*t\s*|m\s*o\s*c\s*h\s*a):/ig.test(value)) {
      return '#';
    }
  } else if (attr === 'style') {
    if (/\/\*|\*\//mg.test(value)) {
      return '#';
    }
    if (/((j\s*a\s*v\s*a|v\s*b|l\s*i\s*v\s*e)\s*s\s*c\s*r\s*i\s*p\s*t\s*|m\s*o\s*c\s*h\s*a):/ig.test(value)) {
      return '';
    }
  }
};

// 自定义处理不在白名单中的标签
xss.onIgnoreTag = function (tag, html) {
  // tag:当前标签名(小写),如:a
  // html:当前标签的HTML代码,如:<a href="ooxx">
  // 返回新的标签HTML代码,如果想使用默认的处理方式,不返回任何值即可
  // 比如将标签替换为[removed]:return '[removed]';
  // 以下为默认的处理代码:
  return html.replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

使用临时配置

var options = {
  whiteList:   {},        // 若不指定则使用默认配置,可参考xss.whiteList
  onTagAttr:   function () {},  // 若不指定则使用默认配置,可参考xss.onTagAttr
  onIgnoreTag: function () {}   // 若不指定则使用默认配置,可参考xss.onIgnoreTag
};
var html = xss('<script>alert("xss");</script>', options);
console.log(html);

其他应用

测试

单元测试

在源码目录执行命令:npm test

在线测试

在源码目录执行命令:node cli.js,可在命令行中输入HTML代码,并看到过滤后的代码

性能

解析速度为5.81MB/s,而另外一个validator模块的xss()函数速度仅为2.48MB/s

测试代码参考benchmark目录

授权协议

基于MIT协议发布:

Copyright (c) 2012 Lei Zongmin(雷宗民) <[email protected]>
http://ucdok.com

The MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

js-xss's People

Contributors

leizongmin avatar jim4node avatar

Watchers

James Cloos avatar DS avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.