Skip to content

Commit

Permalink
[master] replace jsoup.clean with .replaceall for quotes (#219)
Browse files Browse the repository at this point in the history
* replace jsoup.clean with .replaceall for quotes

* bump version
remove extra import

* remove safelist import

* replace > and < as well
  • Loading branch information
alexisgeoffrey authored Sep 6, 2022
1 parent 82ff75f commit 41d77c2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SHELL := /bin/bash
.SHELLFLAGS := -eu -o pipefail -c

VERSION := 8
WOVN_VERSION := 1.14.3
WOVN_VERSION := 1.14.4
TARGET_DIR = ${PWD}
MAVEN = docker run -i --rm -v ${TARGET_DIR}:/project -v wovnjava-maven_repo:/root/.m2 -w /project maven:3-jdk-$(VERSION) mvn
WEBSITE_CONFIG_FILE = pom.xml
Expand Down
4 changes: 2 additions & 2 deletions docker/java8/hello/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
<dependency>
<groupId>com.github.wovnio</groupId>
<artifactId>wovnjava</artifactId>
<version>1.14.3</version>
<version>1.14.4</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/wovnjava-1.14.3-jar-with-dependencies.jar</systemPath>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/wovnjava-1.14.4-jar-with-dependencies.jar</systemPath>
</dependency>
<dependency>
<groupId>org.json</groupId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.github.wovnio</groupId>
<artifactId>wovnjava</artifactId>
<name>wovnjava</name>
<version>1.14.3</version>
<version>1.14.4</version>
<url>https://github.com/WOVNio/wovnjava</url>

<licenses>
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/github/wovnio/wovnjava/HtmlConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.jsoup.parser.Tag;
import org.jsoup.safety.Safelist;

class HtmlConverter {
private final Document doc;
Expand Down Expand Up @@ -133,7 +132,11 @@ private void removeForm() {
String type = element.attr("type");
if (type != null && type.toLowerCase().equals("hidden")) {
if (element.hasAttr("value")) {
String original = Jsoup.clean(element.attr("value"), Safelist.none());
String original = element.attr("value")
.replaceAll("\"", "&quot;")
.replaceAll("\'", "&#39;")
.replaceAll(">", "&gt;")
.replaceAll("<", "&lt;");
String key = htmlReplaceMarker.generateKey();
element.attr("value", key);
htmlReplaceMarker.addValue(key, original);
Expand Down
24 changes: 22 additions & 2 deletions src/test/java/com/github/wovnio/wovnjava/HtmlConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,29 @@ public void testRemoveForm() throws ConfigurationError {
assertEquals(original.replace("INPUT", "input").replace("VALUE", "value"), stripExtraSpaces(converter.restore(html)));
}

public void testRemoveForm__Sanitize() throws ConfigurationError {
public void testRemoveForm__Sanitize__DoubleQuotes() throws ConfigurationError {
String original = "<html lang=\"en\"><head></head><body><form><input type=\"hidden\" name=\"csrf\" value=\"&quot;&gt;&lt;script &gt;alert(String.fromCharCode(88,83,83))&lt;/script&gt;\"></form></body></html>";
String sanitized = "<html lang=\"en\"><head><link rel=\"alternate\" hreflang=\"en\" href=\"https://site.com/global/tokyo/\"><link rel=\"alternate\" hreflang=\"fr\" href=\"https://site.com/fr/global/tokyo/\"><link rel=\"alternate\" hreflang=\"ja\" href=\"https://site.com/ja/global/tokyo/\"></head><body><form><input type=\"hidden\" name=\"csrf\" value=\"\"&gt;\"></form></body></html>";
String sanitized = "<html lang=\"en\"><head><link rel=\"alternate\" hreflang=\"en\" href=\"https://site.com/global/tokyo/\"><link rel=\"alternate\" hreflang=\"fr\" href=\"https://site.com/fr/global/tokyo/\"><link rel=\"alternate\" hreflang=\"ja\" href=\"https://site.com/ja/global/tokyo/\"></head><body><form><input type=\"hidden\" name=\"csrf\" value=\"&quot;&gt;&lt;script &gt;alert(String.fromCharCode(88,83,83))&lt;/script&gt;\"></form></body></html>";
Settings settings = TestUtil.makeSettings(new HashMap<String, String>() {{ put("supportedLangs", "en,fr,ja"); }});
HtmlConverter converter = this.createHtmlConverter(settings, location, original);
String html = converter.strip();

assertEquals(sanitized, stripExtraSpaces(converter.restore(html)));
}

public void testRemoveForm__Sanitize__SingleQuotes() throws ConfigurationError {
String original = "<html lang=\"en\"><head></head><body><form><input type=\"hidden\" name=\"csrf\" value=\'&#39;&gt;&lt;script &gt;alert(String.fromCharCode(88,83,83))&lt;/script&gt;\'></form></body></html>";
String sanitized = "<html lang=\"en\"><head><link rel=\"alternate\" hreflang=\"en\" href=\"https://site.com/global/tokyo/\"><link rel=\"alternate\" hreflang=\"fr\" href=\"https://site.com/fr/global/tokyo/\"><link rel=\"alternate\" hreflang=\"ja\" href=\"https://site.com/ja/global/tokyo/\"></head><body><form><input type=\"hidden\" name=\"csrf\" value=\"&#39;&gt;&lt;script &gt;alert(String.fromCharCode(88,83,83))&lt;/script&gt;\"></form></body></html>";
Settings settings = TestUtil.makeSettings(new HashMap<String, String>() {{ put("supportedLangs", "en,fr,ja"); }});
HtmlConverter converter = this.createHtmlConverter(settings, location, original);
String html = converter.strip();

assertEquals(sanitized, stripExtraSpaces(converter.restore(html)));
}

public void testStrip__Sanitize() throws ConfigurationError {
String original = "<html lang=\"en\"><head></head><body><a title=\"&quot;&gt;&lt;script &gt;alert(String.fromCharCode(88,83,83))&lt;/script&gt;\"></a></body></html>";
String sanitized = "<html lang=\"en\"><head><link rel=\"alternate\" hreflang=\"en\" href=\"https://site.com/global/tokyo/\"><link rel=\"alternate\" hreflang=\"fr\" href=\"https://site.com/fr/global/tokyo/\"><link rel=\"alternate\" hreflang=\"ja\" href=\"https://site.com/ja/global/tokyo/\"></head><body><a title=\"&quot;><script >alert(String.fromCharCode(88,83,83))</script>\"></a></body></html>";
Settings settings = TestUtil.makeSettings(new HashMap<String, String>() {{ put("supportedLangs", "en,fr,ja"); }});
HtmlConverter converter = this.createHtmlConverter(settings, location, original);
String html = converter.strip();
Expand Down

0 comments on commit 41d77c2

Please sign in to comment.