Skip to content

Commit 0822e4b

Browse files
committed
Sanitize value in setHeade call.
Issue: 102771
1 parent 40ab76a commit 0822e4b

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

common/src/main/java/com/genexus/CommonUtil.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public final class CommonUtil
4747
private static DateFormat parse_asctime;
4848
private static final Object http_parse_lock = new Object();
4949

50+
private static final String LOG_USER_ENTRY_WHITELIST_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789+-_=/[]{}\":, ";
51+
private static final String HTTP_HEADER_WHITELIST_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789./;-@(){}[]?,<>\\";
52+
public static final HashMap<Character, Character> LOG_USER_ENTRY_WHITELIST;
53+
public static final HashMap<Character, Character> HTTP_HEADER_WHITELIST;
54+
5055
public static final ILogger logger = LogManager.getLogger(CommonUtil.class);
5156

5257
static
@@ -159,6 +164,9 @@ public Object initialValue()
159164
{"Big5_HKSCS","Big5-HKSCS"},
160165
{"EncodingWrapper","EncodingWrapper"}
161166
};
167+
168+
LOG_USER_ENTRY_WHITELIST = stringToHashMap(LOG_USER_ENTRY_WHITELIST_STRING);
169+
HTTP_HEADER_WHITELIST = stringToHashMap(HTTP_HEADER_WHITELIST_STRING);
162170
}
163171
catch (Exception e)
164172
{
@@ -3443,4 +3451,25 @@ public static String getClassName(String pgmName) {
34433451

34443452
return classPackage + pgmName.replace('\\', '.').trim();
34453453
}
3454+
3455+
private static HashMap<Character, Character> stringToHashMap(String input) {
3456+
HashMap<Character, Character> hashMap = new HashMap<>();
3457+
3458+
for (char c : input.toCharArray()) {
3459+
hashMap.put(c, c);
3460+
}
3461+
return hashMap;
3462+
}
3463+
3464+
public static String Sanitize(String input, HashMap<Character, Character> whiteList) {
3465+
StringBuilder sanitizedInput = new StringBuilder();
3466+
3467+
for (char c : input.toCharArray()) {
3468+
if (whiteList.containsKey(c)) {
3469+
sanitizedInput.append(c);
3470+
}
3471+
}
3472+
3473+
return sanitizedInput.toString();
3474+
}
34463475
}

java/src/test/java/com/genexus/TestCommonUtil.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ private void initialize()
1515
LogManager.initialize(".");
1616
}
1717

18+
@Test
19+
public void testSanitize() {
20+
initialize();
21+
22+
//Test case 1: Sanitize using LogUserEntryWhiteList
23+
String value = "This is a string without Sanitize %@, let's see what happens ";
24+
String expectedResult = "This is a string without Sanitize , lets see what happens ";
25+
String result = CommonUtil.Sanitize(value, CommonUtil.LOG_USER_ENTRY_WHITELIST);
26+
Assert.assertEquals(expectedResult, result);
27+
28+
//Test case 2: Sanitize using HttpHeaderWhiteList
29+
value = "This is a string without Sanitize %@, let's see what happens ";
30+
expectedResult = "ThisisastringwithoutSanitize@,letsseewhathappens";
31+
result = CommonUtil.Sanitize(value, CommonUtil.HTTP_HEADER_WHITELIST);
32+
Assert.assertEquals(expectedResult, result);
33+
}
34+
1835
@Test
1936
public void testFormat() {
2037
initialize();

wrapperjakarta/src/main/java/com/genexus/servlet/http/HttpServletResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.genexus.servlet.http;
22

3+
import com.genexus.CommonUtil;
34
import com.genexus.servlet.ServletOutputStream;
45
import com.genexus.servlet.IServletOutputStream;
56
import java.io.IOException;
@@ -23,7 +24,7 @@ public jakarta.servlet.http.HttpServletResponse getWrappedClass() {
2324
}
2425

2526
public void setHeader(String name, String value) {
26-
resp.setHeader(name, value);
27+
resp.setHeader(name, CommonUtil.Sanitize(value, CommonUtil.HTTP_HEADER_WHITELIST));
2728
}
2829

2930
public void addDateHeader(String name, long date) {

wrapperjavax/src/main/java/com/genexus/servlet/http/HttpServletResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.genexus.servlet.http;
22

3+
import com.genexus.CommonUtil;
34
import com.genexus.servlet.ServletOutputStream;
45
import com.genexus.servlet.IServletOutputStream;
56

@@ -23,7 +24,7 @@ public javax.servlet.http.HttpServletResponse getWrappedClass() {
2324
}
2425

2526
public void setHeader(String name, String value) {
26-
resp.setHeader(name, value);
27+
resp.setHeader(name, CommonUtil.Sanitize(value, CommonUtil.HTTP_HEADER_WHITELIST));
2728
}
2829

2930
public void addDateHeader(String name, long date) {

0 commit comments

Comments
 (0)