Code Monkey home page Code Monkey logo

Comments (6)

leizongmin avatar leizongmin commented on July 17, 2024 1

This is because the style attribute is not allow in the default whiteList. So you need to add the style attributes to your custom whitelist first.

const xss = require('xss');

// get a copy of default whiteList
const whiteList = xss.getDefaultWhiteList();

// allow style attribute for td tag
whiteList.td.push('style');

// specified you custom whiteList
const myxss = new xss.FilterXSS({
  whiteList,
  css: false,
});

// do what you want
html = myxss.process(innerText);
console.log(html);

from js-xss.

lwr avatar lwr commented on July 17, 2024 1

@leizongmin I don't think it is the common case, I believe mosts people only want keep inline styles but not leave all of them not filtered

from js-xss.

Gabxi avatar Gabxi commented on July 17, 2024

Thanks that did the trick!,

sorry to add, but is there a way to specify attr with valid values like class can only be set to certain values?

from js-xss.

lwr avatar lwr commented on July 17, 2024

why you suggest using {css:false} ? It means just leave nearly all risk passthrough.

myxss = new xss.FilterXSS({css:false,whiteList})
myxss.process('<div style="color:red;background:url(java/**/script:alert(1)">TEST</div>')

// unexpected
// output: '<div style="color:red;background:url(java/**/script:alert(1)">TEST</div>'
myxss = new xss.FilterXSS({whiteList})
myxss.process('<div style="color:red;background:url(java/**/script:alert(1)">TEST</div>')

// expected
// output: '<div style="color:red;">TEST</div>'

from js-xss.

leizongmin avatar leizongmin commented on July 17, 2024

@lwr This is because some people didn't want to filter the style attribute for some reasons.

from js-xss.

lemoustachiste avatar lemoustachiste commented on July 17, 2024

I did it like this:

function handleTagAttr (tag, name, value, isWhiteAttr) {
  if (name === 'style') {
    return `${name}="${cssfilter(value)}"`;
  }
}

const options = {
  whiteList: modifyWhiteList(),
  css: false,
  stripIgnoreTagBody: true,
  onTagAttr: handleTagAttr
};
const sanitizer = new xss.FilterXSS(options);

from js-xss.

Related Issues (20)

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.