Skip to content

Commit 2f5f63c

Browse files
committed
Fix NPE vulnerability in ApiException getCode/getStatusCode by changing return type
to a nullable value (just like com.twilio.exception.RestException)
1 parent 7be7839 commit 2f5f63c

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/main/java/com/twilio/exception/ApiException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ public ApiException(final String message, final Integer code, final String moreI
4444
this.status = status;
4545
}
4646

47-
public int getCode() {
47+
public Integer getCode() {
4848
return code;
4949
}
5050

5151
public String getMoreInfo() {
5252
return moreInfo;
5353
}
5454

55-
public int getStatusCode() {
55+
public Integer getStatusCode() {
5656
return status;
5757
}
5858
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.twilio.exception;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertSame;
7+
8+
@SuppressWarnings("ThrowableInstanceNeverThrown")
9+
public class ApiExceptionTest {
10+
11+
private final String anyMessage = "message for test";
12+
private final Throwable anyCause = new RuntimeException("some root cause");
13+
private final String anyMoreInfo = "more info";
14+
private final int anyErrorCode = 123;
15+
private final int anyHttpStatus = 200;
16+
17+
18+
@Test
19+
public void singleArgConstructorShouldPreserveMessage() {
20+
ApiException error = new ApiException(anyMessage);
21+
assertEquals(anyMessage, error.getMessage());
22+
}
23+
24+
@Test
25+
public void twoArgConstructorShouldPreserveMessageAndCause() {
26+
ApiException error = new ApiException(anyMessage, anyCause);
27+
assertEquals("Message", anyMessage, error.getMessage());
28+
assertSame("Cause", anyCause, error.getCause());
29+
}
30+
31+
@Test
32+
public void fullConstructorShouldPreserveAllValues() {
33+
ApiException error = new ApiException(anyMessage, anyErrorCode, anyMoreInfo, anyHttpStatus, anyCause);
34+
assertEquals("Message", anyMessage, error.getMessage());
35+
assertSame("Cause", anyCause, error.getCause());
36+
assertEquals("More info", anyMoreInfo, error.getMoreInfo());
37+
assertEquals("Error code", anyErrorCode, error.getCode().intValue());
38+
assertEquals("Status code", anyHttpStatus, error.getStatusCode().intValue());
39+
}
40+
41+
@Test
42+
public void getCodeShouldNotThrowExceptionWhenCodeIsNull() {
43+
ApiException error = new ApiException(anyMessage);
44+
assertEquals(null, error.getCode());
45+
}
46+
47+
@Test
48+
public void getStatusCodeShouldNotThrowExceptionWhenCodeIsNull() {
49+
ApiException error = new ApiException(anyMessage);
50+
assertEquals(null, error.getStatusCode());
51+
}
52+
53+
}

0 commit comments

Comments
 (0)