Skip to content

Commit 13827d2

Browse files
Updating CSSCodec ESAPI#494
Overriding the CSSCodec encode function to use the EncodingPatternPreservation utility class. Adding in a regex for RGB tuple configurations. Providing test class to verify expected capability.
1 parent fb9965d commit 13827d2

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package org.owasp.esapi.codecs;
1717

18+
import java.util.regex.Pattern;
19+
20+
import org.owasp.esapi.codecs.ref.EncodingPatternPreservation;
21+
1822
/**
1923
* Implementation of the Codec interface for backslash encoding used in CSS.
2024
*
@@ -26,8 +30,21 @@
2630
public class CSSCodec extends AbstractCharacterCodec
2731
{
2832
private static final Character REPLACEMENT = '\ufffd';
29-
30-
33+
//rgb (###,###,###) OR rgb(###%,###%,###%)
34+
//([rR][gG][bB])\s*\(\s*\d{1,3}\s*(\%)?\s*,\s*\d{1,3}\s*(\%)?\s*,\s*\d{1,3}\s*(\%)?\s*\)
35+
private static final String RGB_TRPLT = "([rR][gG][bB])\\s*\\(\\s*\\d{1,3}\\s*(\\%)?\\s*,\\s*\\d{1,3}\\s*(\\%)?\\s*,\\s*\\d{1,3}\\s*(\\%)?\\s*\\)";
36+
private static final Pattern RGB_TRPLT_PATTERN = Pattern.compile(RGB_TRPLT);
37+
38+
@Override
39+
public String encode(char[] immune, String input) {
40+
EncodingPatternPreservation tripletCheck = new EncodingPatternPreservation(RGB_TRPLT_PATTERN);
41+
42+
String inputChk = tripletCheck.captureAndReplaceMatches(input);
43+
44+
String result = super.encode(immune, inputChk);
45+
46+
return tripletCheck.restoreOriginalContent(result);
47+
}
3148
/**
3249
* {@inheritDoc}
3350
*
@@ -63,13 +80,15 @@ public Character decodeCharacter(PushbackSequence<Character> input)
6380
{
6481
input.mark();
6582
Character first = input.next();
83+
System.out.println("First: " + first);
6684
if (first == null || first != '\\')
6785
{
6886
input.reset();
6987
return null;
7088
}
7189

7290
Character second = input.next();
91+
System.out.println("Second: " );
7392
if (second == null) {
7493
input.reset();
7594
return null;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.owasp.esapi.codecs;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class CSSCodecTest {
8+
private static final char[] IMMUNE_STUB = new char[0];
9+
/** Unit In Test*/
10+
private CSSCodec uit = new CSSCodec();
11+
12+
@Test
13+
public void testCSSTripletLeadString() {
14+
assertEquals("rgb(255,255,255)\\21 ", uit.encode(IMMUNE_STUB, "rgb(255,255,255)!"));
15+
assertEquals("rgb(25%,25%,25%)\\21 ", uit.encode(IMMUNE_STUB, "rgb(25%,25%,25%)!"));
16+
}
17+
@Test
18+
public void testCSSTripletTailString() {
19+
assertEquals("\\24 field\\3d rgb(255,255,255)\\21 ", uit.encode(IMMUNE_STUB, "$field=rgb(255,255,255)!"));
20+
assertEquals("\\24 field\\3d rgb(25%,25%,25%)\\21 ", uit.encode(IMMUNE_STUB, "$field=rgb(25%,25%,25%)!"));
21+
}
22+
@Test
23+
public void testCSSTripletStringPart() {
24+
assertEquals("\\24 field\\3d rgb(255,255,255)\\21 ", uit.encode(IMMUNE_STUB, "$field=rgb(255,255,255)!"));
25+
assertEquals("\\24 field\\3d rgb(25%,25%,25%)\\21 ", uit.encode(IMMUNE_STUB, "$field=rgb(25%,25%,25%)!"));
26+
}
27+
@Test
28+
public void testCSSTripletStringMultiPart() {
29+
assertEquals("\\24 field\\3d rgb(255,255,255)\\21 \\20 \\24 field\\3d rgb(255,255,255)\\21 ", uit.encode(IMMUNE_STUB, "$field=rgb(255,255,255)! $field=rgb(255,255,255)!"));
30+
assertEquals("\\24 field\\3d rgb(25%,25%,25%)\\21 \\20 \\24 field\\3d rgb(25%,25%,25%)\\21 ", uit.encode(IMMUNE_STUB, "$field=rgb(25%,25%,25%)! $field=rgb(25%,25%,25%)!"));
31+
assertEquals("\\24 field\\3d rgb(255,255,255)\\21 \\20 \\24 field\\3d rgb(25%,25%,25%)\\21 ", uit.encode(IMMUNE_STUB, "$field=rgb(255,255,255)! $field=rgb(25%,25%,25%)!"));
32+
}
33+
}

0 commit comments

Comments
 (0)