You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JSoupXssFilter.java 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2014 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.utils;
  17. import org.jsoup.Jsoup;
  18. import org.jsoup.nodes.Document;
  19. import org.jsoup.safety.Cleaner;
  20. import org.jsoup.safety.Safelist;
  21. import com.google.inject.Inject;
  22. import com.google.inject.Singleton;
  23. /**
  24. * Implementation of an XSS filter based on JSoup.
  25. *
  26. * @author James Moger
  27. *
  28. */
  29. @Singleton
  30. public class JSoupXssFilter implements XssFilter {
  31. private final Cleaner none;
  32. private final Cleaner relaxed;
  33. @Inject
  34. public JSoupXssFilter() {
  35. none = new Cleaner(Safelist.none());
  36. relaxed = new Cleaner(getRelaxedWhiteList());
  37. }
  38. @Override
  39. public String none(String input) {
  40. return clean(input, none);
  41. }
  42. @Override
  43. public String relaxed(String input) {
  44. return clean(input, relaxed);
  45. }
  46. protected String clean(String input, Cleaner cleaner) {
  47. Document unsafe = Jsoup.parse(input);
  48. Document safe = cleaner.clean(unsafe);
  49. return safe.body().html();
  50. }
  51. /**
  52. * Builds & returns a loose HTML whitelist similar to Github.
  53. *
  54. * https://github.com/github/markup/tree/master#html-sanitization
  55. * @return a loose HTML whitelist
  56. */
  57. protected Safelist getRelaxedWhiteList() {
  58. return new Safelist()
  59. .addTags(
  60. "a", "b", "blockquote", "br", "caption", "cite", "code", "col",
  61. "colgroup", "dd", "del", "div", "dl", "dt", "em", "h1", "h2", "h3", "h4", "h5", "h6", "hr",
  62. "i", "img", "ins", "kbd", "li", "ol", "p", "pre", "q", "samp", "small", "strike", "strong",
  63. "sub", "sup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "tt", "u",
  64. "ul", "var")
  65. .addAttributes("a", "class", "href", "style", "target", "title")
  66. .addAttributes("blockquote", "cite")
  67. .addAttributes("col", "span", "width")
  68. .addAttributes("colgroup", "span", "width")
  69. .addAttributes("div", "class", "style")
  70. .addAttributes("img", "align", "alt", "height", "src", "title", "width")
  71. .addAttributes("ol", "start", "type")
  72. .addAttributes("q", "cite")
  73. .addAttributes("span", "class", "style")
  74. .addAttributes("table", "class", "style", "summary", "width")
  75. .addAttributes("td", "abbr", "axis", "class", "colspan", "rowspan", "style", "width")
  76. .addAttributes("th", "abbr", "axis", "class", "colspan", "rowspan", "scope", "style", "width")
  77. .addAttributes("ul", "type")
  78. .addEnforcedAttribute("a", "rel", "nofollow")
  79. ;
  80. }
  81. }