|
| 1 | +The OWASP [Java Encoder][java-encoder-project] and OWASP [Java HTML Sanitizer][html-sanitizer-project] projects |
| 2 | +are security libraries for Java web applications that provide output encoding and HTML input sanitization. |
| 3 | + |
| 4 | +The OWASP [JSON Sanitizer][json-sanitizer] Java library is used to ensure both JSON input and output |
| 5 | +are _reasonably_ safe for Java applications. |
| 6 | + |
| 7 | +#### What are they? |
| 8 | + |
| 9 | +Java Encoder package provides the Java application with contextual output encoding of HTML. |
| 10 | +It provides individual methods for HTML, URLs, JavaScript and CSS. |
| 11 | + |
| 12 | +Java HTML Sanitizer is used to sanitize untrusted HTML so that it can be safely handled within a Java application. |
| 13 | +The JAR file is included in a Java application and then a policy is defined for it. |
| 14 | + |
| 15 | +These are both established projects with a regular release history stretching back to 2013. |
| 16 | + |
| 17 | +The JSON Sanitizer is a Java component that will transform arbitrary JSON |
| 18 | +to well-formed JSON as defined by [RFC 4627][rfc4627]. |
| 19 | +This can be used to accept JSON input from an untrusted source and then safely output JSON to other processes. |
| 20 | + |
| 21 | +JSON Sanitizer is a widely used library provided by OWASP, |
| 22 | +and it is a direct dependents for many 1000s of other libraries and in many more applications. |
| 23 | +It is a project that was transferred to OWASP in 2021 by github user `mikesamuel` |
| 24 | +and so this OWASP library is identified as `com.mikesamuel:json-sanitizer`. |
| 25 | + |
| 26 | +#### Why use the libraries? |
| 27 | + |
| 28 | +The use of both Java Encoder and Java HTML Sanitizer is part of a defense in depth approach |
| 29 | +to preventing [cross site scripting][csxss] (XSS) and other attacks. |
| 30 | +They are well established OWASP projects with 'Lab' status. |
| 31 | + |
| 32 | +The OWASP [JSON Sanitizer][json-sanitizer] Java library is widely used, |
| 33 | +for example it is a direct dependency for literally [1000s of Java components][json-sanitizer-dependents], |
| 34 | +and should be considered for JSON specific output normalization and input validation. |
| 35 | +It is less well supported than the Java Encoder or Java HTML Sanitizer, version 1.2.2 was released in January 2021, |
| 36 | +but it is still stable and (really) useful. |
| 37 | + |
| 38 | +#### How to use the libraries |
| 39 | + |
| 40 | +Include the Java Encoder package into a Java application [via Maven][java-encoder]. |
| 41 | +The '[How to Use the OWASP Java Encoder][java-encoder-usage]' documentation explains how to use it in various contexts, |
| 42 | +such as HTML, URLs, JavaScript and CSS. |
| 43 | + |
| 44 | +Follow the [examples][html-sanitizer-examples] provided by Java HTML Sanitizer |
| 45 | +to include the utility and configure it with policy. |
| 46 | + |
| 47 | +The JSON Sanitizer JAR file can be fetched from Maven Central, follow the [Getting Started][json-sanitizer-usage] guide: |
| 48 | + |
| 49 | +```text |
| 50 | +import com.google.json.JsonSanitizer; |
| 51 | +String wellFormedJson = JsonSanitizer.sanitize(myJsonLikeString); |
| 52 | +``` |
| 53 | + |
| 54 | +#### References |
| 55 | + |
| 56 | +* OWASP [Cross Site Scripting prevention][csxss] Cheatsheet |
| 57 | +* OWASP [Java Encoder][java-encoder-github] |
| 58 | +* OWASP [Java HTML Sanitizer][html-sanitizer] |
| 59 | +* OWASP [JSON Sanitizer][json-sanitizer] |
| 60 | + |
| 61 | +---- |
| 62 | + |
| 63 | +The OWASP Developer Guide is a community effort; if there is something that needs changing |
| 64 | +then [submit an issue][issue050304] or [edit on GitHub][edit050304]. |
| 65 | + |
| 66 | +[csxss]: https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet |
| 67 | +[edit050304]: https://github.com/OWASP/DevGuide/blob/main/docs/en/05-implementation/03-secure-libraries/04-java-secure-libs.md |
| 68 | +[html-sanitizer]: https://github.com/OWASP/java-html-sanitizer/releases/latest/ |
| 69 | +[html-sanitizer-examples]: https://github.com/OWASP/java-html-sanitizer/tree/main/owasp-java-html-sanitizer/src/main/java/org/owasp/html/examples |
| 70 | +[html-sanitizer-project]: https://owasp.org/www-project-java-html-sanitizer/ |
| 71 | +[issue050304]: https://github.com/OWASP/DevGuide/issues/new?labels=content&template=request.md&title=Update:%2005-implementation/03-secure-libraries/04-java-secure-libs |
| 72 | +[java-encoder]: http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.owasp.encoder%22 |
| 73 | +[java-encoder-github]: https://github.com/OWASP/owasp-java-encoder/releases/latest/ |
| 74 | +[java-encoder-project]: https://owasp.org/www-project-java-encoder/ |
| 75 | +[java-encoder-usage]: https://owasp.org/www-project-java-encoder/#div-use |
| 76 | +[json-sanitizer]: https://github.com/OWASP/json-sanitizer/releases/latest/ |
| 77 | +[json-sanitizer-dependents]: https://central.sonatype.com/artifact/com.mikesamuel/json-sanitizer/dependents |
| 78 | +[json-sanitizer-usage]: https://github.com/OWASP/json-sanitizer/blob/master/docs/getting_started.md |
| 79 | +[rfc4627]: https://www.ietf.org/rfc/rfc4627.txt |
0 commit comments