Skip to content

Commit e504fa5

Browse files
authored
Fixed another potential NPE in OAuth1 (AsyncHttpClient#1883)
* Added new failing test when there are 2 duplicating form parameters but one if them has null as a value * Fixed potentian NPE when form params include two params with the same name and one of the param has null as a value
1 parent 3e10703 commit e504fa5

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

client/src/main/java/org/asynchttpclient/oauth/OAuthSignatureCalculatorInstance.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ private String encodedParams(ConsumerKey consumerAuth, RequestToken userAuth, lo
143143
if (formParams != null) {
144144
for (Param param : formParams) {
145145
// formParams are not already encoded
146-
parameters.add(Utf8UrlEncoder.percentEncodeQueryElement(param.getName()), Utf8UrlEncoder.percentEncodeQueryElement(param.getValue()));
146+
String value = param.getValue() != null ? Utf8UrlEncoder.percentEncodeQueryElement(param.getValue()) : "";
147+
parameters.add(Utf8UrlEncoder.percentEncodeQueryElement(param.getName()), value);
147148
}
148149
}
149150
if (queryParams != null) {

client/src/test/java/org/asynchttpclient/oauth/OAuthSignatureCalculatorTest.java

+37
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,43 @@ public void testSignatureBaseStringWithNoValueQueryParameter() throws NoSuchAlgo
147147
testSignatureBaseStringWithEncodableOAuthToken(request);
148148
}
149149

150+
@Test
151+
public void testDuplicatingNullValueFormParameter() throws Exception {
152+
// Form parameter with no value in OAuth1 should be treated the same as form parameter with an empty value.
153+
// Tested with http://lti.tools/oauth/
154+
Request request = post("http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b")
155+
.addFormParam("c2", "")
156+
.addFormParam("a3", "2 q")
157+
.addFormParam("c2", null)
158+
.build();
159+
160+
String signatureBaseString = new OAuthSignatureCalculatorInstance()
161+
.signatureBaseString(//
162+
new ConsumerKey(CONSUMER_KEY, CONSUMER_SECRET),
163+
new RequestToken(TOKEN_KEY, TOKEN_SECRET),
164+
request.getUri(),
165+
request.getMethod(),
166+
request.getFormParams(),
167+
request.getQueryParams(),
168+
TIMESTAMP,
169+
NONCE).toString();
170+
assertEquals("POST&" +
171+
"http%3A%2F%2Fexample.com%2Frequest" +
172+
"&a2%3Dr%2520b%26" +
173+
"a3%3D2%2520q%26" +
174+
"a3%3Da%26" +
175+
"b5%3D%253D%25253D%26" +
176+
"c%2540%3D%26" +
177+
"c2%3D%26" +
178+
"c2%3D%26" +
179+
"oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" +
180+
"oauth_nonce%3Dkllo9940pd9333jh%26" +
181+
"oauth_signature_method%3DHMAC-SHA1%26" +
182+
"oauth_timestamp%3D1191242096%26" +
183+
"oauth_token%3Dnnch734d00sl2jdk%26" +
184+
"oauth_version%3D1.0", signatureBaseString);
185+
}
186+
150187
// based on the reference test case from
151188
// http://oauth.pbwiki.com/TestCases
152189
@RepeatedIfExceptionsTest(repeats = 5)

0 commit comments

Comments
 (0)