rspec/rules/S5247/java/rule.adoc

71 lines
2.6 KiB
Plaintext
Raw Normal View History

2021-01-27 12:06:36 +01:00
include::../description.adoc[]
include::../ask-yourself.adoc[]
include::../recommended.adoc[]
== Sensitive Code Example
With https://github.com/samskivert/jmustache[JMustache by samskivert]:
----
Mustache.compiler().escapeHTML(false).compile(template).execute(context); // Sensitive
Mustache.compiler().withEscaper(Escapers.NONE).compile(template).execute(context); // Sensitive
----
With https://freemarker.apache.org/[Freemarker]:
----
2021-01-27 13:42:22 +01:00
freemarker.template.Configuration configuration = new freemarker.template.Configuration();
configuration.setAutoEscapingPolicy(DISABLE_AUTO_ESCAPING_POLICY); // Sensitive
2021-01-27 12:06:36 +01:00
----
== Compliant Solution
2021-01-27 13:42:22 +01:00
With https://github.com/samskivert/jmustache[JMustache by samskivert]:
2022-02-04 17:28:24 +01:00
[source,java]
2021-01-27 12:06:36 +01:00
----
Mustache.compiler().compile(template).execute(context); // Compliant, auto-escaping is enabled by default
Mustache.compiler().escapeHTML(true).compile(template).execute(context); // Compliant
----
2021-01-27 13:42:22 +01:00
With https://freemarker.apache.org/[Freemarker]. See https://freemarker.apache.org/docs/api/freemarker/template/Configuration.html#setAutoEscapingPolicy-int-["setAutoEscapingPolicy" documentation] for more details.
2021-01-27 12:06:36 +01:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-01-27 12:06:36 +01:00
----
2021-01-27 13:42:22 +01:00
freemarker.template.Configuration configuration = new freemarker.template.Configuration();
configuration.setAutoEscapingPolicy(ENABLE_IF_DEFAULT_AUTO_ESCAPING_POLICY); // Compliant
2021-01-27 12:06:36 +01:00
----
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
=== on 27 Jan 2021, 11:01:55 Quentin Jaquier wrote:
Other template engine considered, but discarded because they do not have a way to disable the escaping globally:
* https://www.thymeleaf.org/[Thymleaf]:
Auto-escaping is the default. It is not possible to disable it globally in the Java code, https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#unescaped-text[un-escaped text] can be done only in the HTML file.
* https://github.com/spullara/mustache.java[JMustache by spullara]:
Same as Thymleaf. In addition, it is possible https://groups.google.com/g/mustachejava/c/7qh3Ar8MHsc/m/zKc2fvdPAQAJ[to overwrite the behavior by overwriting "encode()" method], but this seems like a workaround and is really not likely to be done by inadvertance without knowing what you are doing.
* https://pebbletemplates.io/[Pebble Templates]
https://pebbletemplates.io/wiki/guide/escaping/[Auto-escaping enabled by default]. Only possible to disable it via the https://pebbletemplates.io/wiki/filter/raw/[raw filter], not globally in the Java code.
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]