forked from Onlineberatung/onlineBeratung-messageService
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use escaping for html characters
- Loading branch information
Showing
9 changed files
with
155 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 0 additions & 69 deletions
69
src/main/java/de/caritas/cob/messageservice/api/helper/Helper.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/de/caritas/cob/messageservice/api/helper/UrlEncodingDecodingUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package de.caritas.cob.messageservice.api.helper; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLDecoder; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* Util class to provide url encoding and decoding. | ||
*/ | ||
public class UrlEncodingDecodingUtils { | ||
|
||
private UrlEncodingDecodingUtils() {} | ||
|
||
/** | ||
* URL encoding for a given string. | ||
* | ||
* @return the encoded string | ||
*/ | ||
public static String urlEncodeString(String stringToEncode) { | ||
try { | ||
return URLEncoder.encode(stringToEncode, StandardCharsets.UTF_8.name()); | ||
|
||
} catch (UnsupportedEncodingException ex) { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Url decoding for a given string. | ||
* | ||
* @return the decoded string | ||
*/ | ||
public static String urlDecodeString(String stringToDecode) { | ||
try { | ||
return URLDecoder.decode(stringToDecode, StandardCharsets.UTF_8.name()); | ||
|
||
} catch (UnsupportedEncodingException ex) { | ||
return null; | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/de/caritas/cob/messageservice/api/helper/XssProtection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package de.caritas.cob.messageservice.api.helper; | ||
|
||
import static org.apache.commons.lang3.StringUtils.EMPTY; | ||
import static org.apache.commons.lang3.StringUtils.isNotBlank; | ||
|
||
import org.springframework.web.util.HtmlUtils; | ||
|
||
/** | ||
* Provides escaping of html characters for XSS protection. | ||
*/ | ||
public class XssProtection { | ||
|
||
private XssProtection() { | ||
} | ||
|
||
/** | ||
* Escape HTML code from a text (XSS-Protection) | ||
* | ||
* @return the given text without html | ||
*/ | ||
public static String escapeHtml(String text) { | ||
if (isNotBlank(text)) { | ||
return HtmlUtils.htmlEscape(text); | ||
} | ||
return EMPTY; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 0 additions & 49 deletions
49
src/test/java/de/caritas/cob/messageservice/api/helper/HelperTest.java
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
src/test/java/de/caritas/cob/messageservice/api/helper/UrlEncodingDecodingUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package de.caritas.cob.messageservice.api.helper; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class UrlEncodingDecodingUtilsTest { | ||
|
||
private static final String DECODED_STRING = "töst#$"; | ||
private static final String ENCODED_STRING = "t%C3%B6st%23%24"; | ||
|
||
@Test | ||
public void urlEncodeString_Should_ReturnEncodedString() { | ||
assertEquals(ENCODED_STRING, UrlEncodingDecodingUtils.urlEncodeString(DECODED_STRING)); | ||
} | ||
|
||
@Test | ||
public void urlDecodeString_Should_ReturnEncodedString() { | ||
assertEquals(DECODED_STRING, UrlEncodingDecodingUtils.urlDecodeString(ENCODED_STRING)); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
src/test/java/de/caritas/cob/messageservice/api/helper/XssProtectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package de.caritas.cob.messageservice.api.helper; | ||
|
||
import static de.caritas.cob.messageservice.api.helper.XssProtection.escapeHtml; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import org.junit.Test; | ||
|
||
public class XssProtectionTest { | ||
|
||
@Test | ||
public void escapeHtml_Should_escapeStandardHtml() { | ||
String normalized = escapeHtml("<strong>Lorem Ipsum</strong>"); | ||
|
||
assertThat(normalized, is("<strong>Lorem Ipsum</strong>")); | ||
} | ||
|
||
@Test | ||
public void escapeHtml_Should_escapeJavascriptCode() { | ||
String normalized = escapeHtml("Lorem Ipsum<script>alert('1');</script>"); | ||
|
||
assertThat(normalized, is("Lorem Ipsum<script>alert('1');</script>")); | ||
} | ||
|
||
@Test | ||
public void escapeHtml_ShouldNot_removeNewlinesFromText() { | ||
String normalized = escapeHtml("Lorem Ipsum\nLorem Ipsum"); | ||
|
||
assertThat(normalized, is("Lorem Ipsum\nLorem Ipsum")); | ||
} | ||
|
||
@Test | ||
public void escapeHtml_Should_escapeHtmlFromText_And_ShouldNot_removeNewlines() { | ||
String normalized = escapeHtml("<b>Lorem Ipsum</b>\nLorem Ipsum<script>alert('1');" | ||
+ "</script>"); | ||
|
||
assertThat(normalized, is("<b>Lorem Ipsum</b>\nLorem Ipsum<script>alert('1');</script>")); | ||
} | ||
|
||
@Test | ||
public void removeHTMLFromText_Should_returnEmptyString_When_inputIsNull() { | ||
String normalized = escapeHtml(null); | ||
|
||
assertThat(normalized, is("")); | ||
} | ||
|
||
@Test | ||
public void removeHTMLFromText_Should_returnEmptyString_When_inputIsEmptyString() { | ||
String normalized = escapeHtml(""); | ||
|
||
assertThat(normalized, is("")); | ||
} | ||
|
||
} |