Skip to content

Commit

Permalink
Added tests for isUserAgentSupportsSNI and added string length check …
Browse files Browse the repository at this point in the history
…to mitigate errors (#752)

Signed-off-by: t-burch <[email protected]>
Co-authored-by: Thomas Bayer <[email protected]>
  • Loading branch information
t-burch and predic8 authored Jan 12, 2024
1 parent e88ddca commit 3e8a0e7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
7 changes: 3 additions & 4 deletions core/src/main/java/com/predic8/membrane/core/http/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -507,20 +507,19 @@ public boolean isUserAgentSupportsSNI() {
private int getBrowserVersion(String userAgent, String browserID) {
int p = userAgent.indexOf(browserID);
p += browserID.length();

if (userAgent.length() == p)
if (p >= userAgent.length())
return -1;
char c = userAgent.charAt(p++);
if (c != ' ' && c != '/' && c != '_')
return -1;

int version = 0;
while (userAgent.length() != p) {
while (p < userAgent.length()) {
c = userAgent.charAt(p++);
if (c < '0' || c > '9')
break;
version = version * 10 + (c - '0');
}
return version;
}

}
42 changes: 42 additions & 0 deletions core/src/test/java/com/predic8/membrane/core/http/HeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,46 @@ void multipleHeaderWithSameNameNormalized() {
h.add("Foo", "3");
assertEquals("1,2,3", h.getNormalizedValue("Foo"));
}

@ParameterizedTest
@ValueSource(strings = {
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:88.0) Gecko/20100101 Firefox/88.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36 Edg/90.0.818.49",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36 OPR/75.0.3969.149 SNI/hostname.example.com",
"Mozilla/5.0 (iPhone; CPU iPhone OS 14_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Brave/1.23.1 Chrome/89.0.4389.105",
"Mozilla/5.0 (X11; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15 SNI/hostname.example.com"
})
void isUserAgentSupportsSNITrue(String userAgent) {
Header h = new Header();
h.add(USER_AGENT, userAgent);
assertTrue(h.isUserAgentSupportsSNI());
}

@ParameterizedTest
@ValueSource(strings = {
"Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1",
"Mozilla/5.0 (Linux; Android 10; SM-A205U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.105 Mobile Safari/537.36",
"Mozilla/5.0 (SMART-TV; Linux; Tizen 5.0) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/2.2 Chrome/56.0.2924.0 TV Safari/537.36",
})
void isUserAgentSupportsSNIFalse(String userAgent) {
Header h = new Header();
h.add(USER_AGENT, userAgent);
assertFalse(h.isUserAgentSupportsSNI());
}

@Test
void isUserAgentSupportsSNIMinimalUserAgent() {
Header h = new Header();
h.add(USER_AGENT, "curl/7.64.1");
assertFalse(h.isUserAgentSupportsSNI());
}

@Test
void isUserAgentSupportsSNIEmptyUserAgent() {
Header h = new Header();
h.add(USER_AGENT, "");
assertFalse(h.isUserAgentSupportsSNI());
}
}

0 comments on commit 3e8a0e7

Please sign in to comment.