Skip to content

Commit 6698195

Browse files
authored
Merge pull request #688 from kwwall/issue-686
Issues 686 & 689 - Create DefaultEncoder from Encoder.DefaultCodecList and Javadoc clean-up
2 parents 010e644 + bd40049 commit 6698195

File tree

13 files changed

+261
-74
lines changed

13 files changed

+261
-74
lines changed

pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@
439439
<plugin>
440440
<groupId>org.cyclonedx</groupId>
441441
<artifactId>cyclonedx-maven-plugin</artifactId>
442-
<version>2.6.0</version>
442+
<version>2.6.1</version>
443443
<executions>
444444
<execution>
445445
<phase>package</phase>
@@ -787,6 +787,8 @@
787787
<configuration>
788788
<doclint>none</doclint>
789789
<source>8</source>
790+
<failOnError>true</failOnError>
791+
<failOnWarnings>true</failOnWarnings>
790792
</configuration>
791793
</plugin>
792794
<plugin>

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,20 @@ public interface Encoder {
160160
/**
161161
* This method is equivalent to calling {@code Encoder.canonicalize(input, restrictMultiple, restrictMixed);}.
162162
*
163-
* The default values for restrictMultiple and restrictMixed come from {@code ESAPI.properties}
163+
* The <i>default</i> values for {@code restrictMultiple} and {@code restrictMixed} come from {@code ESAPI.properties}.
164164
* <pre>
165165
* Encoder.AllowMultipleEncoding=false
166166
* Encoder.AllowMixedEncoding=false
167167
* </pre>
168+
* and the default codecs that are used for canonicalization are the list
169+
* of codecs that comes from:
170+
* <pre>
171+
* Encoder.DefaultCodecList=HTMLEntityCodec,PercentCodec,JavaScriptCodec
172+
* </pre>
173+
* (If the {@code Encoder.DefaultCodecList} property is null or not set,
174+
* these same codecs are listed in the same order. Note that you may supply
175+
* your own codec by using a fully cqualified class name of a class that
176+
* implements {@code org.owasp.esapi.codecs.Codec<T>}.
168177
*
169178
* @see #canonicalize(String, boolean, boolean)
170179
* @see <a href="http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4">W3C specifications</a>
@@ -224,7 +233,25 @@ public interface Encoder {
224233
* Encoder encoder = new DefaultEncoder( list );
225234
* String clean = encoder.canonicalize( request.getParameter( "input" ));
226235
* </pre>
227-
* In ESAPI, the Validator uses the canonicalize method before it does validation. So all you need to
236+
* or alternately, you can just customize {@code Encoder.DefaultCodecList} property
237+
* in the {@code ESAPI.properties} file with your preferred codecs; for
238+
* example:
239+
* <pre>
240+
* Encoder.DefaultCodecList=WindowsCodec,MySQLCodec,PercentCodec
241+
* </pre>
242+
* and then use:
243+
* <pre>
244+
* Encoder encoder = ESAPI.encoder();
245+
* String clean = encoder.canonicalize( request.getParameter( "input" ));
246+
* </pre>
247+
* as you normally would. However, the downside to using the
248+
* {@code ESAPI.properties} file approach does not allow you to vary your
249+
* list of codecs that are used each time. The downside to using the
250+
* {@code DefaultEncoder} constructor is that your code is now timed to
251+
* specific reference implementations rather than just interfaces and those
252+
* reference implementations are what is most likely to change in ESAPI 3.x.
253+
* </p><p>
254+
* In ESAPI, the {@code Validator} uses the {@code canonicalize} method before it does validation. So all you need to
228255
* do is to validate as normal and you'll be protected against a host of encoded attacks.
229256
* <pre>
230257
* String input = request.getParameter( "name" );
@@ -253,14 +280,22 @@ public interface Encoder {
253280
* // disabling strict mode to allow mixed encoding
254281
* String url = ESAPI.encoder().canonicalize( request.getParameter("url"), false, false);
255282
* </pre>
256-
* <b>WARNING!!!</b> Please note that this method is incompatible with URLs and if there exist any HTML Entities
283+
* <b>WARNING #1!!!</b> Please note that this method is incompatible with URLs and if there exist any HTML Entities
257284
* that correspond with parameter values in a URL such as "&amp;para;" in a URL like
258285
* "https://foo.com/?bar=foo&amp;parameter=wrong" you will get a mixed encoding validation exception.
259286
* <p>
260287
* If you wish to canonicalize a URL/URI use the method {@code Encoder.getCanonicalizedURI(URI dirtyUri);}
288+
* </p><p>
289+
* <b>WARNING #2!!!</b> Even if you use {@code WindowsCodec} or {@code UnixCodec}
290+
* as appropriate, file path names in the {@code input} parameter will <b><i>NOT</i></b>
291+
* be canonicalized. It the failure of such file path name canonicalization
292+
* presents a potential security issue, consider using one of the
293+
* {@code Validator.getValidDirectoryPath()} methods instead of or in addition to this method.
261294
*
262295
* @see <a href="http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4">W3C specifications</a>
296+
* @see #canonicalize(String)
263297
* @see #getCanonicalizedURI(URI dirtyUri)
298+
* @see org.owasp.esapi.Validator#getValidDirectoryPath(java.lang.String, java.lang.String, java.io.File, boolean)
264299
*
265300
* @param input
266301
* the text to canonicalize

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -390,17 +390,16 @@ public interface SecurityConfiguration extends EsapiPropertyLoader {
390390
* especially for any supported cipher mode that is considered a streaming mode
391391
* (which is basically anything except CBC for modes that support require an IV).
392392
* For this reason, 'fixed' has now been removed (it was considered <b>deprecated</b>
393-
* since release 2.2.0.0). An <b>ESAPI.properties</b> value of {@Code fixed} for the property
394-
* {@Code Encryptor.ChooseIVMethod} will now result in a {@Code ConfigurationException}
393+
* since release 2.2.0.0). An <b>ESAPI.properties</b> value of {@code fixed} for the property
394+
* {@code Encryptor.ChooseIVMethod} will now result in a {@code ConfigurationException}
395395
* being thrown.
396396
*
397397
* @return A string specifying the IV type. Should be "random". Anything
398-
* else should fail with a {@Code ConfigurationException} being thrown.
398+
* else should fail with a {@code ConfigurationException} being thrown.
399399
*
400-
* @see #getFixedIV()
401400
* @deprecated Use SecurityConfiguration.getStringProp("appropriate_esapi_prop_name") instead.
402401
* This method will be removed in a future release as it is now moot since
403-
* it can only legitimately have the single value of "random".
402+
* it can <i>only</i> legitimately have the single value of "random".
404403
*/
405404
@Deprecated
406405
String getIVType();

src/main/java/org/owasp/esapi/codecs/AbstractCodec.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717

1818

1919
/**
20-
* The Codec interface defines a set of methods for encoding and decoding application level encoding schemes,
21-
* such as HTML entity encoding and percent encoding (aka URL encoding). Codecs are used in output encoding
20+
* The {@code Coded} interface defines a set of methods for encoding and decoding application level encoding schemes,
21+
* such as HTML entity encoding and percent encoding (aka URL encoding). {@code Coded}s are used in output encoding
2222
* and canonicalization. The design of these codecs allows for character-by-character decoding, which is
2323
* necessary to detect double-encoding and the use of multiple encoding schemes, both of which are techniques
2424
* used by attackers to bypass validation and bury encoded attacks in data.
25+
* <p>
26+
* Be sure to see the several <b>WARNING</b>s associated with the detailed
27+
* method descriptions. You will not find that in the "Method Summary" section
28+
* of the javadoc because that only shows the intial sentence.
2529
*
2630
* @author Jeff Williams (jeff.williams .at. aspectsecurity.com) <a
2731
* href="http://www.aspectsecurity.com">Aspect Security</a>
@@ -52,15 +56,13 @@ public AbstractCodec() {
5256
}
5357

5458
/**
55-
* WARNING!! {@code Character} based Codecs will silently transform code points that are not
59+
* {@inheritDoc}
60+
* </p><p>
61+
* <b>WARNING!!</b> {@code Character} based {@code Codec}s will silently transform code points that are not
5662
* legal UTF code points into garbage data as they will cast them to {@code char}s.
57-
* </br></br>
58-
* If you are implementing an {@code Integer} based codec, these will be silently discarded
63+
* Also, if you are implementing an {@code Integer} based codec, these will be silently discarded
5964
* based on the return from {@code Character.isValidCodePoint( int )}. This is the preferred
6065
* behavior moving forward.
61-
*
62-
*
63-
* {@inheritDoc}
6466
*/
6567
@Override
6668
public String encode(char[] immune, String input) {
@@ -79,19 +81,29 @@ public String encode(char[] immune, String input) {
7981
}
8082

8183
/**
82-
* WARNING!!!! Passing a standard char to this method will resolve to the
83-
* @see #encodeCharacter( char[], int )
84-
* method instead of this one!!! YOU HAVE BEEN WARNED!!!!
85-
*
8684
* {@inheritDoc}
85+
* <p>
86+
* <b>WARNING!!!!</b> Passing a standard {@code char} rather than {@code Character} to this method will resolve to the
87+
* {@link #encodeCharacter( char[], char )} method, which will throw an {@code IllegalArgumentException} instead.
88+
* YOU HAVE BEEN WARNED!!!!
8789
*/
8890
@Override
8991
public String encodeCharacter( char[] immune, Character c ) {
9092
return ""+c;
9193
}
9294

95+
96+
/**
97+
* To prevent accidental footgun usage and calling
98+
* {@link #encodeCharacter( char[], int)} when called with {@code char} and
99+
* {@code char} is first silently converted to {@code int} and then the
100+
* unexpected method is called.
101+
*
102+
* @throws IllegalArgumentException to indicate that you called the incorrect method.
103+
*/
93104
public String encodeCharacter(char[] immune, char c) {
94-
throw new IllegalArgumentException("You tried to call encodeCharacter with a char. Nope. Use Character instead!");
105+
throw new IllegalArgumentException("You tried to call encodeCharacter() with a char. Nope. " +
106+
"Use 'encodeCharacter( char[] immune, Character c)' instead!");
95107
}
96108

97109
/* (non-Javadoc)

src/main/java/org/owasp/esapi/codecs/Codec.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public interface Codec<T> {
7777

7878
/**
7979
* Returns the decoded version of the next character from the input string and advances the
80-
* current character in the PushbackSequence. If the current character is not encoded, this
81-
* method MUST reset the PushbackString.
80+
* current character in the {@code PushbackSequence}. If the current character is not encoded, this
81+
* method <i>MUST</i> reset the {@code PushbackString}.
8282
*
8383
* @param input the Character to decode
8484
*
@@ -100,10 +100,25 @@ public interface Codec<T> {
100100
*/
101101
public String getHexForNonAlphanumeric(int c);
102102

103+
/**
104+
* Convert the {@code char} parameter to its octal representation.
105+
* @param c the character for which to return the new representation.
106+
* @return the octal representation.
107+
*/
103108
public String toOctal(char c);
104109

110+
/**
111+
* Convert the {@code char} parameter to its hexadecimal representation.
112+
* @param c the character for which to return the new representation.
113+
* @return the hexadecimal representation.
114+
*/
105115
public String toHex(char c);
106116

117+
/**
118+
* Convert the {@code int} parameter to its hexadecimal representation.
119+
* @param c the character for which to return the new representation.
120+
* @return the hexadecimal representation.
121+
*/
107122
public String toHex(int c);
108123

109124
/**

src/main/java/org/owasp/esapi/codecs/JavaScriptCodec.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ public String encodeCharacter( char[] immune, Character c ) {
8080
*
8181
* Returns the decoded version of the character starting at index, or
8282
* null if no decoding is possible.
83-
* See http://www.planetpdf.com/codecuts/pdfs/tutorial/jsspec.pdf
83+
* </p><p>
8484
* Formats all are legal both upper/lower case:
85+
* <pre>
8586
* \\a - special characters
8687
* \\xHH
8788
* \\uHHHH
8889
* \\OOO (1, 2, or 3 digits)
90+
* </pre>
8991
*/
9092
public Character decodeCharacter( PushbackSequence<Character> input ) {
9193
input.mark();
@@ -215,4 +217,4 @@ public Character decodeCharacter( PushbackSequence<Character> input ) {
215217
return null;
216218
}
217219

218-
}
220+
}

src/main/java/org/owasp/esapi/codecs/UnixCodec.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
/**
20-
* Implementation of the Codec interface for '\' encoding from Unix command shell.
20+
* Implementation of the {@code Codec} interface for '\' encoding from Unix command shell (bash lineage, not csh lineage).
2121
*
2222
* @author Jeff Williams (jeff.williams .at. aspectsecurity.com) <a
2323
* href="http://www.aspectsecurity.com">Aspect Security</a>
@@ -29,9 +29,11 @@ public class UnixCodec extends AbstractCharacterCodec {
2929
/**
3030
* {@inheritDoc}
3131
*
32-
* Returns backslash-encoded character
32+
* @return the backslash-encoded character
3333
*
34-
* @param immune
34+
* @param immune Array of characters that should not be encoded. Use with caution! All
35+
* alphanumeric characters are "immune" by default so you needn't
36+
* include them.
3537
*/
3638
public String encodeCharacter( char[] immune, Character c ) {
3739
char ch = c.charValue();
@@ -53,13 +55,15 @@ public String encodeCharacter( char[] immune, Character c ) {
5355

5456
/**
5557
* {@inheritDoc}
56-
*
57-
* Returns the decoded version of the character starting at index, or
58-
* null if no decoding is possible.
58+
*
5959
* <p>
6060
* Formats all are legal both upper/lower case:
61+
* <pre>
6162
* \x - all special characters
62-
*
63+
* </pre>
64+
*
65+
* @return the decoded version of the character starting at index, or
66+
* null if no decoding is possible.
6367
*/
6468
public Character decodeCharacter( PushbackSequence<Character> input ) {
6569
input.mark();
@@ -79,4 +83,4 @@ public Character decodeCharacter( PushbackSequence<Character> input ) {
7983
return second;
8084
}
8185

82-
}
86+
}

src/main/java/org/owasp/esapi/crypto/CipherText.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ protected boolean canEqual(Object other) {
798798
* proved in 1996 [see http://pssic.free.fr/Extra%20Reading/SEC+/SEC+/hmac-cb.pdf] that
799799
* HMAC security doesn’t require that the underlying hash function be collision resistant,
800800
* but only that it acts as a pseudo-random function, which SHA1 satisfies.
801-
* @param authKey The {@Code SecretKey} used with the computed HMAC-SHA1
801+
* @param authKey The {@code SecretKey} used with the computed HMAC-SHA1
802802
* to ensure authenticity.
803803
* @return The value for the MAC.
804804
*/

src/main/java/org/owasp/esapi/filters/ClickjackFilter.java

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,45 @@
2626
import javax.servlet.http.HttpServletResponse;
2727

2828
/**
29-
* The {@code ClickjackFilter} is discussed at
30-
* @link http://www.owasp.org/index.php/ClickjackFilter_for_Java_EE
29+
* The {@code ClickjackFilter} is configured as follows:
3130
* <pre>
32-
* <filter>
33-
* <filter-name>ClickjackFilterDeny</filter-name>
34-
* <filter-class>org.owasp.filters.ClickjackFilter</filter-class>
35-
* <init-param>
36-
* <param-name>mode</param-name>
37-
* <param-value>DENY</param-value>
38-
* </init-param>
39-
* </filter>
31+
*
32+
* &lt;filter&gt;
33+
* &lt;filter-name&gt;ClickjackFilterDeny&lt;/filter-name&gt;
34+
* &lt;filter-class&gt;org.owasp.filters.ClickjackFilter&lt;/filter-class&gt;
35+
* &lt;init-param&gt;
36+
* &lt;param-name&gt;mode&lt;/param-name&gt;
37+
* &lt;param-value&gt;DENY&lt;/param-value&gt;
38+
* &lt;/init-param&gt;
39+
* &lt;/filter&gt;
4040
*
41-
* <filter>
42-
* <filter-name>ClickjackFilterSameOrigin</filter-name>
43-
* <filter-class>org.owasp.filters.ClickjackFilter</filter-class>
44-
* <init-param>
45-
* <param-name>mode</param-name>
46-
* <param-value>SAMEORIGIN</param-value>
47-
* </init-param>
48-
* </filter>
41+
* &lt;filter&gt;
42+
* &lt;filter-name&gt;ClickjackFilterSameOrigin&lt;/filter-name&gt;
43+
* &lt;filter-class&gt;org.owasp.filters.ClickjackFilter&lt;/filter-class&gt;
44+
* &lt;init-param&gt;
45+
* &lt;param-name&gt;mode&lt;/param-name&gt;
46+
* &lt;param-value&gt;SAMEORIGIN&lt;/param-value&gt;
47+
* &lt;/init-param&gt;
48+
* &lt;/filter&gt;
4949
*
50-
* <!-- use the Deny version to prevent anyone, including yourself, from framing the page -->
51-
* <filter-mapping>
52-
* <filter-name>ClickjackFilterDeny</filter-name>
53-
* <url-pattern>/*</url-pattern>
54-
* </filter-mapping>
50+
* &lt;!-- use the Deny version to prevent anyone, including yourself, from framing the page --&gt;
51+
* &lt;filter-mapping&gt;
52+
* &lt;filter-name&gt;ClickjackFilterDeny&lt;/filter-name&gt;
53+
* &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
54+
* &lt;/filter-mapping&gt;
5555
*
56-
* <!-- use the SameOrigin version to allow your application to frame, but nobody else
57-
* <filter-mapping>
58-
* <filter-name>ClickjackFilterSameOrigin</filter-name>
59-
* <url-pattern>/*</url-pattern>
60-
* </filter-mapping>
56+
* &lt;!-- use the SameOrigin version to allow your application to frame, but nobody else
57+
* &lt;filter-mapping&gt;
58+
* &lt;filter-name&gt;ClickjackFilterSameOrigin&lt;/filter-name&gt;
59+
* &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
60+
* &lt;/filter-mapping&gt;
6161
* </pre>
62+
*
63+
* @see <a href="https://web.archive.org/web/20131020084831/https://www.owasp.org/index.php/ClickjackFilter_for_Java_EE">
64+
* OWASP - Clickjacking Filter for JavaEE</a>
65+
* @see <a href="https://owasp.org/www-community/attacks/Clickjacking">OWASP - Clickjacking Attack</a>
66+
* @see <a href="https://cheatsheetseries.owasp.org/cheatsheets/Clickjacking_Defense_Cheat_Sheet.html">
67+
* OWASP - Clickjacking Defense Cheat Sheet</a>
6268
*/
6369
public class ClickjackFilter implements Filter
6470
{

0 commit comments

Comments
 (0)