Skip to content

Commit bc21afe

Browse files
authored
gracefully handle null within 'stripControls', #906 (#907)
* gracefully handle null within 'stripControls', #906 * add unit tests for 'stripControls' - Check valid string, control characters, empty string, whitespace, and null * fix spelling * add unit tests for 'isEmpty'
1 parent 58be169 commit bc21afe

4 files changed

Lines changed: 23 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ add
150150
```xml
151151
<classifier>jakarta</classifier>
152152
```
153-
and include whatever jakara.servlet:jakarta.servlet-api version you are using with
153+
and include whatever jakarta.servlet:jakarta.servlet-api version you are using with
154154
```xml
155155
<scope>provided</scope>
156156
```

documentation/esapi4java-core-2.5.3.0-release-notes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This is a patch release with the primary intent of providing a Jakarta compatibl
1616
Encryptor.DigitalSignatureAlgorithm=SHA256withDSA # The old SHA1withDSA doesn't support 2048-bit RSA modulus length
1717
Encryptor.DigitalSignatureKeyLength=2048
1818
Note that if you have persisted previous digital signatures that you must continue to verify, you will have to regenerate them.
19-
* Thanks to a PR by @jcputney (PR #799), I have attempted to upload additional artifacts to Maven Central that will be a transformed jar suitable for use with the new 'jakarata.servlet' changes for Jakarata EE 9 and later. (Previously, 'javax.servlet' was the name space). Because we are still supporting JDK 8 at this point, we still need to support the 'javax.servlet' namespace as well. In addition to the standard jar artifacts, there should be a new esapi-<release>-jakarta.jar (which uses 'jakarta.servlet' instead of 'javax.servlet' namespace) as well as corresponding *-javadoc.jar and *-sources.jar files. I am not sure it will work as we have no tests for it, but looing at the binaries, it seems like it should.
19+
* Thanks to a PR by @jcputney (PR #799), I have attempted to upload additional artifacts to Maven Central that will be a transformed jar suitable for use with the new 'jakarta.servlet' changes for Jakarta EE 9 and later. (Previously, 'javax.servlet' was the name space). Because we are still supporting JDK 8 at this point, we still need to support the 'javax.servlet' namespace as well. In addition to the standard jar artifacts, there should be a new esapi-<release>-jakarta.jar (which uses 'jakarta.servlet' instead of 'javax.servlet' namespace) as well as corresponding *-javadoc.jar and *-sources.jar files. I am not sure it will work as we have no tests for it, but looing at the binaries, it seems like it should.
2020
For additional details, see:
2121
https://github.com/ESAPI/esapi-java-legacy/pull/799
2222
https://github.com/ESAPI/esapi-java-legacy/discussions/768

src/main/java/org/owasp/esapi/StringUtilities.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public static String replaceLinearWhiteSpace( String input ) {
3939
* @return the stripped value
4040
*/
4141
public static String stripControls( String input ) {
42+
if ( input == null ) {
43+
return null;
44+
}
4245
StringBuilder sb = new StringBuilder();
4346
for ( int i=0; i<input.length(); i++ ) {
4447
char c = input.charAt( i );

src/test/java/org/owasp/esapi/StringUtilitiesTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,22 @@ public void testReplaceNull() {
8787
assertEquals( " Test ", StringUtilities.replaceNull( " Test ", "Replaced" ) );
8888
assertEquals( "Replaced", StringUtilities.replaceNull( " NULL ", "Replaced" ) );
8989
}
90+
91+
public void testStripControls() {
92+
// valid characters are preserved
93+
assertEquals( "\u0021abc\u007e", StringUtilities.stripControls( "\u0021abc\u007e" ) );
94+
// control characters become spaces
95+
assertEquals( " a b c ", StringUtilities.stripControls( "\u0000a\u0020b\u007fc\uffff" ) );
96+
// blank strings are preserved
97+
assertEquals( "", StringUtilities.stripControls( "" ) );
98+
assertEquals( " ", StringUtilities.stripControls( " " ) );
99+
assertEquals( null, StringUtilities.stripControls( null ) );
100+
}
101+
102+
public void testIsEmpty() {
103+
assertTrue(StringUtilities.isEmpty(null));
104+
assertTrue(StringUtilities.isEmpty(""));
105+
assertFalse(StringUtilities.isEmpty(" "));
106+
assertFalse(StringUtilities.isEmpty("foo"));
107+
}
90108
}

0 commit comments

Comments
 (0)