Skip to content

Commit 81c028f

Browse files
authored
Sanitize value of User-Agent header (#429)
1 parent 75f0d49 commit 81c028f

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

AndroidSDKCore/src/main/java/com/leanplum/internal/http/LeanplumHttpConnection.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,31 @@ protected void initConnection(
9191
Must include the phrase `gzip` in the `User-Agent` header
9292
https://cloud.google.com/appengine/kb/
9393
*/
94-
urlConnection.setRequestProperty("User-Agent", createUserAgentValue());
94+
urlConnection.setRequestProperty("User-Agent", createUserAgent());
9595
urlConnection.setRequestProperty("Accept-Encoding", Constants.LEANPLUM_SUPPORTED_ENCODING);
9696
}
9797

98-
private String createUserAgentValue() {
98+
/**
99+
* Currently Android uses OkHttp as an HTTP client. We need to remove invalid characters from
100+
* the User-Agent value according to checkValue(String, String) from:
101+
*
102+
* https://github.com/square/okhttp/blob/dabbd56572089cfef00d358edcc87b3f5c73e580/okhttp/src/main/kotlin/okhttp3/Headers.kt#L431
103+
*/
104+
private String createUserAgent() {
105+
String userAgentString = createUserAgentString();
106+
StringBuilder result = new StringBuilder();
107+
108+
// Removing invalid characters
109+
for (int i = 0; i < userAgentString.length(); i++) {
110+
char c = userAgentString.charAt(i);
111+
if (c == '\t' || ('\u0020' <= c && c <= '\u007e')) {
112+
result.append(c);
113+
}
114+
}
115+
return result.toString();
116+
}
117+
118+
private String createUserAgentString() {
99119
Context context = Leanplum.getContext();
100120

101121
return Util.getApplicationName(context)

0 commit comments

Comments
 (0)