File tree Expand file tree Collapse file tree 6 files changed +56
-0
lines changed
main/java/com/iab/gpp/encoder/datatype/encoder
test/java/com/iab/gpp/encoder/datatype/encoder Expand file tree Collapse file tree 6 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,11 @@ public static String encode(int value) {
3939 }
4040
4141 public static int decode (String bitString ) throws DecodingException {
42+ // enforce a length restriction to avoid overflows
43+ // 2^16 has a bit string length of 24
44+ if (bitString .length () > 24 ) {
45+ throw new DecodingException ("FibonacciInteger too long" );
46+ }
4247 if (!BITSTRING_VERIFICATION_PATTERN .matcher (bitString ).matches () || bitString .length () < 2
4348 || bitString .indexOf ("11" ) != bitString .length () - 2 ) {
4449 throw new DecodingException ("Undecodable FibonacciInteger '" + bitString + "'" );
Original file line number Diff line number Diff line change 33import java .util .ArrayList ;
44import java .util .Collections ;
55import java .util .List ;
6+ import java .util .logging .Logger ;
67import java .util .regex .Pattern ;
78import com .iab .gpp .encoder .error .DecodingException ;
89
910public class FibonacciIntegerRangeEncoder {
1011
12+ private static final Logger LOGGER = Logger .getLogger (FibonacciIntegerRangeEncoder .class .getName ());
13+ // NOTE: This is a value roughly the 2x the size of this list
14+ // https://tools.iabtechlab.com/transparencycenter/explorer/business/gpp
15+ static final int MAX_SIZE = 8192 ;
1116 private static Pattern BITSTRING_VERIFICATION_PATTERN = Pattern .compile ("^[0-1]*$" , Pattern .CASE_INSENSITIVE );
1217
1318 public static String encode (List <Integer > value ) {
@@ -70,13 +75,21 @@ public static List<Integer> decode(String bitString) throws DecodingException {
7075 offset = end ;
7176 startIndex = index + 2 ;
7277
78+ if (value .size () + (end - start ) > MAX_SIZE ) {
79+ LOGGER .warning ("FibonacciIntegerRange has too many values" );
80+ break ;
81+ }
7382 for (int j = start ; j <= end ; j ++) {
7483 value .add (j );
7584 }
7685 } else {
7786 int index = bitString .indexOf ("11" , startIndex );
7887 int val = FibonacciIntegerEncoder .decode (bitString .substring (startIndex , index + 2 )) + offset ;
7988 offset = val ;
89+ if (value .size () == MAX_SIZE ) {
90+ LOGGER .warning ("FibonacciIntegerRange has too many values" );
91+ break ;
92+ }
8093 value .add (val );
8194 startIndex = index + 2 ;
8295 }
Original file line number Diff line number Diff line change 33import java .util .ArrayList ;
44import java .util .Collections ;
55import java .util .List ;
6+ import java .util .logging .Logger ;
67import java .util .regex .Pattern ;
78import com .iab .gpp .encoder .error .DecodingException ;
89
910public class FixedIntegerRangeEncoder {
1011
12+ private static final Logger LOGGER = Logger .getLogger (FixedIntegerRangeEncoder .class .getName ());
13+ // NOTE: This is a value roughly the 2x the size of this list
14+ // https://tools.iabtechlab.com/transparencycenter/explorer/business/gpp
15+ private static final int MAX_SIZE = 8192 ;
1116 private static Pattern BITSTRING_VERIFICATION_PATTERN = Pattern .compile ("^[0-1]*$" , Pattern .CASE_INSENSITIVE );
1217
1318 public static String encode (List <Integer > value ) {
@@ -58,11 +63,22 @@ public static List<Integer> decode(String bitString) throws DecodingException {
5863 int end = FixedIntegerEncoder .decode (bitString .substring (startIndex , startIndex + 16 ));
5964 startIndex += 16 ;
6065
66+ if (end < start ) {
67+ throw new DecodingException ("FixedIntegerRange has invalid range" );
68+ }
69+ if (value .size () + (end - start ) > MAX_SIZE ) {
70+ LOGGER .warning ("FixedIntegerRange has too many values" );
71+ break ;
72+ }
6173 for (int j = start ; j <= end ; j ++) {
6274 value .add (j );
6375 }
6476 } else {
6577 int val = FixedIntegerEncoder .decode (bitString .substring (startIndex , startIndex + 16 ));
78+ if (value .size () == MAX_SIZE ) {
79+ LOGGER .warning ("FixedIntegerRange has too many values" );
80+ break ;
81+ }
6682 value .add (val );
6783 startIndex += 16 ;
6884 }
Original file line number Diff line number Diff line change 11package com .iab .gpp .encoder .datatype .encoder ;
22
3+ import static org .junit .jupiter .api .Assertions .assertThrows ;
34import org .junit .jupiter .api .Assertions ;
45import org .junit .jupiter .api .Test ;
56import com .iab .gpp .encoder .error .DecodingException ;
@@ -100,4 +101,9 @@ public void testDecode10() {
100101 }
101102 }
102103
104+ @ Test
105+ public void testDecodeTooLarge () {
106+ String large = FibonacciIntegerEncoder .encode (2 << 17 );
107+ assertThrows (DecodingException .class , () -> FibonacciIntegerEncoder .decode (large ));
108+ }
103109}
Original file line number Diff line number Diff line change 11package com .iab .gpp .encoder .datatype .encoder ;
22
3+ import static org .junit .jupiter .api .Assertions .assertThrows ;
34import java .util .ArrayList ;
45import java .util .Arrays ;
6+ import java .util .List ;
57import org .junit .jupiter .api .Assertions ;
68import org .junit .jupiter .api .Test ;
79import com .iab .gpp .encoder .error .DecodingException ;
@@ -96,4 +98,10 @@ public void testDecode8() {
9698
9799 }
98100 }
101+
102+ @ Test
103+ public void testGiantRange () {
104+ String max = FibonacciIntegerEncoder .encode (FibonacciIntegerRangeEncoder .MAX_SIZE + 1 );
105+ Assertions .assertEquals (List .of (), FibonacciIntegerRangeEncoder .decode ("000000000001111" + max ));
106+ }
99107}
Original file line number Diff line number Diff line change 11package com .iab .gpp .encoder .datatype .encoder ;
22
3+ import static org .junit .jupiter .api .Assertions .assertThrows ;
34import java .util .ArrayList ;
45import java .util .Arrays ;
6+ import java .util .List ;
57import org .junit .jupiter .api .Assertions ;
68import org .junit .jupiter .api .Test ;
79import com .iab .gpp .encoder .error .DecodingException ;
@@ -126,4 +128,10 @@ public void testDecode10() {
126128
127129 }
128130 }
131+
132+ @ Test
133+ public void testGiantRange () {
134+ String max = FibonacciIntegerEncoder .encode (FibonacciIntegerRangeEncoder .MAX_SIZE + 1 );
135+ Assertions .assertEquals (List .of (), FixedIntegerRangeEncoder .decode ("00000000000110000000000000001" + max ));
136+ }
129137}
You can’t perform that action at this time.
0 commit comments