Skip to content

Commit

Permalink
Merge pull request #7 from whitesource/BS/Bugs/Deal_With_Null
Browse files Browse the repository at this point in the history
Changed the encoders and tests to accept null inputs and return null
  • Loading branch information
BenShmuely authored Jul 29, 2021
2 parents b4283e2 + 9ee6fab commit 46d99c7
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 47 deletions.
21 changes: 19 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.whitesource</groupId>
<artifactId>curekit</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>

<name>curekit</name>
<description>A repository containing code security remediation solutions used by WhiteSource Cure</description>
Expand Down Expand Up @@ -108,10 +108,14 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
</configuration>
<version>${maven.javadoc.version}</version>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>deploy</phase>
<goals>
<goal>jar</goal>
</goals>
Expand All @@ -125,7 +129,7 @@
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<phase>deploy</phase>
<goals>
<goal>sign</goal>
</goals>
Expand Down Expand Up @@ -182,4 +186,17 @@
</repository>
</distributionManagement>


<profiles>
<profile>
<id>jdk-11-config</id>
<activation>
<jdk>11</jdk>
</activation>
<properties>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
</properties>
</profile>
</profiles>

</project>
99 changes: 65 additions & 34 deletions src/main/java/io/whitesource/cure/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.List;
import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.owasp.encoder.Encode;
Expand All @@ -19,7 +18,10 @@ public class Encoder {
* @param param An argument or part of an argument for the operating systems command.
* @return Encoded parameter.
*/
public static String forOsCommand(@NonNull final String param) {
public static String forOsCommand(final String param) {
if (param == null) {
return null;
}
return forOsCommand(param, new char[] {});
}

Expand All @@ -31,7 +33,10 @@ public static String forOsCommand(@NonNull final String param) {
* @param charsToIgnore Array of characters to not encode.
* @return Encoded parameter.
*/
public static String forOsCommand(@NonNull final String param, char[] charsToIgnore) {
public static String forOsCommand(final String param, char[] charsToIgnore) {
if (param == null) {
return null;
}
StringBuilder sb = new StringBuilder();
for (char c : param.toCharArray()) {
sb.append(encodeCharacterForOsCommand(c, charsToIgnore));
Expand All @@ -45,8 +50,10 @@ public static String forOsCommand(@NonNull final String param, char[] charsToIgn
* @param contents arrays {@link Object} contains all the contents.
* @return encoded log content.
*/
public static String[] forLogContent(@NonNull final Object[] contents) {

public static String[] forLogContent(final Object[] contents) {
if (contents == null) {
return null;
}
List<String> results = new ArrayList<>();

for (Object content : contents) {
Expand All @@ -61,12 +68,15 @@ public static String[] forLogContent(@NonNull final Object[] contents) {
* @param content {@link Object} contains the content.
* @return encoded log content.
*/
public static String forLogContent(@NonNull final Object content) {
public static String forLogContent(final Object content) {
if (content == null) {
return null;
}
return content
.toString()
.replaceAll("[\n|\r|\t]", "_")
.replaceAll("<", "&lt")
.replaceAll(">", "&gt");
.toString()
.replaceAll("[\n|\r|\t]", "_")
.replaceAll("<", "&lt")
.replaceAll(">", "&gt");
}

/**
Expand All @@ -75,11 +85,14 @@ public static String forLogContent(@NonNull final Object content) {
* @param content contains the content to be sanitized.
* @return encoded Html content.
*/
public static String forCrlf(@NonNull final String content) {
public static String forCrlf(final String content) {
if (content == null) {
return null;
}
return StringUtils.replaceEach(
content.toString(),
new String[] {"\n", "\\n", "\r", "\\r", "%0d", "%0D", "%0a", "%0A", "\025"},
new String[] {"", "", "", "", "", "", "", "", ""});
content.toString(),
new String[] {"\n", "\\n", "\r", "\\r", "%0d", "%0D", "%0a", "%0A", "\025"},
new String[] {"", "", "", "", "", "", "", "", ""});
}

/**
Expand All @@ -88,8 +101,10 @@ public static String forCrlf(@NonNull final String content) {
* @param content {@link Object} contains the content.
* @return encoded JavaScript block.
*/
public static String forJavaScriptBlockXss(@NonNull final Object content) {

public static String forJavaScriptBlockXss(final Object content) {
if (content == null) {
return null;
}
return Encode.forJavaScriptBlock(formatToString(content));
}

Expand All @@ -100,8 +115,10 @@ public static String forJavaScriptBlockXss(@NonNull final Object content) {
* @param content {@link Object} contains the content.
* @return encoded Html content.
*/
public static String forHtmlContentXss(@NonNull final Object content) {

public static String forHtmlContentXss(final Object content) {
if (content == null) {
return null;
}
return Encode.forHtmlContent(formatToString(content));
}

Expand All @@ -111,8 +128,10 @@ public static String forHtmlContentXss(@NonNull final Object content) {
* @param content {@link Object} contains the content.
* @return encoded Html Attribute.
*/
public static String forHtmlAttributeXss(@NonNull final Object content) {

public static String forHtmlAttributeXss(final Object content) {
if (content == null) {
return null;
}
return Encode.forHtmlAttribute(formatToString(content));
}

Expand All @@ -129,8 +148,10 @@ public static String forHtmlAttributeXss(@NonNull final Object content) {
* @param content {@link Object} contains the content.
* @return encoded JavaScript string.
*/
public static String forJavaScriptXss(@NonNull final Object content) {

public static String forJavaScriptXss(final Object content) {
if (content == null) {
return null;
}
return Encode.forJavaScript(formatToString(content));
}

Expand All @@ -141,8 +162,10 @@ public static String forJavaScriptXss(@NonNull final Object content) {
* @param content {@link Object} contains the content.
* @return encoded CSS String.
*/
public static String forCssStringXss(@NonNull final Object content) {

public static String forCssStringXss(final Object content) {
if (content == null) {
return null;
}
return Encode.forCssString(formatToString(content));
}

Expand All @@ -154,8 +177,10 @@ public static String forCssStringXss(@NonNull final Object content) {
* @param content {@link Object} contains the content.
* @return encoded Uri component.
*/
public static String forUriComponentXss(@NonNull final Object content) {

public static String forUriComponentXss(final Object content) {
if (content == null) {
return null;
}
return Encode.forUriComponent(formatToString(content));
}

Expand All @@ -168,8 +193,10 @@ public static String forUriComponentXss(@NonNull final Object content) {
* @param content {@link Object} contains the content.
* @return encoded CSS url.
*/
public static String forCssUrlXss(@NonNull final Object content) {

public static String forCssUrlXss(final Object content) {
if (content == null) {
return null;
}
return Encode.forCssUrl(formatToString(content));
}

Expand All @@ -186,8 +213,10 @@ public static String forCssUrlXss(@NonNull final Object content) {
* @param content {@link Object} contains the content.
* @return encoded Html unquoted Attribute.
*/
public static String forHtmlUnquotedAttributeXss(@NonNull final Object content) {

public static String forHtmlUnquotedAttributeXss(final Object content) {
if (content == null) {
return null;
}
return Encode.forHtmlUnquotedAttribute(formatToString(content));
}

Expand All @@ -200,8 +229,10 @@ public static String forHtmlUnquotedAttributeXss(@NonNull final Object content)
* @param content {@link Object} contains the content.
* @return encoded JavaScript attribute.
*/
public static String forJavaScriptAttributeXss(@NonNull final String content) {

public static String forJavaScriptAttributeXss(final String content) {
if (content == null) {
return null;
}
return Encode.forJavaScriptAttribute(content);
}

Expand All @@ -221,8 +252,8 @@ private static String encodeCharacterForOsCommand(char charToEncode, char[] char

private static boolean isAlphaNumeric(char charToEncode) {
return !((charToEncode < '0' || charToEncode > '9')
&& (charToEncode < 'A' || charToEncode > 'Z')
&& (charToEncode < 'a' || charToEncode > 'z'));
&& (charToEncode < 'A' || charToEncode > 'Z')
&& (charToEncode < 'a' || charToEncode > 'z'));
}

private static String formatToString(Object content) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/io/whitesource/cure/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class FileUtils {
* @return True - if the file is outside the base dir, False - otherwise.
*/
public static boolean isFileOutsideDir(
@NonNull final String filePath, @NonNull final String baseDirPath) throws IOException {
@NonNull final String filePath, @NonNull final String baseDirPath) throws IOException {
File file = new File(filePath);
File baseDir = new File(baseDirPath);
return !file.getCanonicalPath().startsWith(baseDir.getCanonicalPath());
Expand All @@ -31,7 +31,10 @@ public static boolean isFileOutsideDir(
* @param filePath The file path.
* @return Normalized path.
*/
public static String normalize(@NonNull final String filePath) {
public static String normalize(final String filePath) {
if (filePath == null) {
return null;
}
return FilenameUtils.normalize(filePath);
}
}
6 changes: 3 additions & 3 deletions src/test/java/io/whitesource/cure/EncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void forOsCommand_operatingSystem_successfullyWithResult() {

@Test
void forOsCommand_null_successfully() {
Assertions.assertThrows(NullPointerException.class, () -> forOsCommand(null));
Assertions.assertNull(forOsCommand(null));
}

@Test
Expand All @@ -39,7 +39,7 @@ void forCrlf_htmlContent_successfullyWithResult() {

@Test
void forCrlf_null_successfully() {
Assertions.assertThrows(NullPointerException.class, () -> forCrlf(null));
Assertions.assertNull(forCrlf(null));
}

@Test
Expand Down Expand Up @@ -77,7 +77,7 @@ void forLogContent_fullEncodingCapabilities_successfullyWithResult() {
@Test
void forLogContent_null_successfully() {

Assertions.assertThrows(NullPointerException.class, () -> forLogContent(null));
Assertions.assertNull(forLogContent(null));
}

@Test
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/io/whitesource/cure/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ void isFileInDir_inside_successfullyWithResult() throws IOException {
@Test
void isFileInDir_null_successfully() {
Assertions.assertThrows(
NullPointerException.class, () -> FileUtils.isFileOutsideDir(null, null));
NullPointerException.class, () -> FileUtils.isFileOutsideDir(null, null));

Assertions.assertThrows(
NullPointerException.class,
() -> FileUtils.isFileOutsideDir("file-path-place-holder", null));
NullPointerException.class,
() -> FileUtils.isFileOutsideDir("file-path-place-holder", null));

Assertions.assertThrows(
NullPointerException.class,
() -> FileUtils.isFileOutsideDir(null, "base-dir-place-holder"));
NullPointerException.class,
() -> FileUtils.isFileOutsideDir(null, "base-dir-place-holder"));
}

@Test
Expand All @@ -52,6 +52,6 @@ void normalize_validInput_successfullyWithResult() throws IOException {

@Test
void normalize_null_successfully() {
Assertions.assertThrows(NullPointerException.class, () -> FileUtils.normalize(null));
Assertions.assertNull(FileUtils.normalize(null));
}
}

0 comments on commit 46d99c7

Please sign in to comment.