Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent javascript injection #436

Open
MichaelRumpler opened this issue Nov 29, 2021 · 0 comments
Open

Prevent javascript injection #436

MichaelRumpler opened this issue Nov 29, 2021 · 0 comments

Comments

@MichaelRumpler
Copy link

Somebody sent a comment in my blog which injected some javascript. This may do some bad things, so I prevented it. This was very easy once I found out how the Transforms work.

I added these lines to lib/Transforms.js:

const antixss = (value) => {
  return String(value)
          .replace(/<script([^>]*)>(.*?)<\/script>/g, "&lt;script$1&gt;$2&lt;\/script&gt;")
          .replace(/(<a [^>]*?)href="javascript:[^"]*"/g, "$1")
          .replace(/(<a [^>]*?)href='javascript:[^']*'/g, "$1")
          .replace(/(<[^>]*) on\w+="[^"]*"/g, "$1")
          .replace(/(<[^>]*) on\w+='[^']*'/g, "$1");
}

module.exports = {
  md5,
  upcase,
  downcase,
  antixss
}

And then I just needed to activate that transform function for some fields in staticman.yml:

  transforms:
    email: md5
    name: antixss
    message: antixss

Now script tags do not get executed anymore and javascript event handlers get removed.

This may be something which others are also interested in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant