Skip to content

Commit 0b995b0

Browse files
authored
Merge pull request #689 from microsoftgraph/bugfix/error-payload-verbosity
- fixes a bug where the errors would log sensitive payload information when not set to verbose
2 parents f4ea641 + 204babf commit 0b995b0

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

src/main/java/com/microsoft/graph/http/GraphServiceException.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// ------------------------------------------------------------------------------
22
// Copyright (c) 2017 Microsoft Corporation
3-
//
3+
//
44
// Permission is hereby granted, free of charge, to any person obtaining a copy
55
// of this software and associated documentation files (the "Software"), to deal
66
// in the Software without restriction, including without limitation the rights
77
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
88
// copies of the Software, and to permit persons to whom the Software is
99
// furnished to do so, subject to the following conditions:
10-
//
10+
//
1111
// The above copyright notice and this permission notice shall be included in
1212
// all copies or substantial portions of the Software.
13-
//
13+
//
1414
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1515
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1616
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -174,7 +174,7 @@ public String getResponseMessage() {
174174
public String getMessage() {
175175
return getMessage(verbose);
176176
}
177-
177+
178178
/**
179179
* Gets the HTTP status code
180180
*
@@ -215,7 +215,7 @@ public String getMethod() {
215215
public String getUrl() {
216216
return url;
217217
}
218-
218+
219219
/**
220220
* Gets the request headers
221221
* @return the request headers
@@ -255,12 +255,7 @@ public String getMessage(final boolean verbose) {
255255
if (verbose) {
256256
sb.append(requestBody);
257257
} else {
258-
final int bodyLength = Math.min(MAX_BREVITY_LENGTH, requestBody.length());
259-
final String truncatedBody = requestBody.substring(0, bodyLength);
260-
sb.append(truncatedBody);
261-
if (truncatedBody.length() == MAX_BREVITY_LENGTH) {
262-
sb.append(TRUNCATION_MARKER);
263-
}
258+
sb.append(TRUNCATION_MARKER);
264259
}
265260
}
266261
sb.append(NEW_LINE).append(NEW_LINE);
@@ -402,7 +397,7 @@ public static <T> GraphServiceException createFromConnection(final IHttpRequest
402397
error,
403398
isVerbose);
404399
}
405-
400+
406401
/**
407402
* Creates a Graph service exception from a given failed HTTP request
408403
*

src/main/java/com/microsoft/graph/requests/extensions/ChunkedUploadResult.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public ChunkedUploadResult(ClientException error) {
6666
* @param exception The exception received from server.
6767
*/
6868
public ChunkedUploadResult(GraphServiceException exception) {
69-
this(new ClientException(exception.getMessage(/* verbose */ true), exception));
69+
this(new ClientException(exception.getMessage(), exception));
7070
}
7171

7272
/**
@@ -122,4 +122,4 @@ public UploadSession getSession() {
122122
public ClientException getError() {
123123
return this.error;
124124
}
125-
}
125+
}

src/test/java/com/microsoft/graph/http/GraphServiceExceptionTests.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void testError() {
3131
assertTrue(message.indexOf("truncated") > 0);
3232
assertEquals(error,exception.getServiceError());
3333
}
34-
34+
3535
@Test
3636
public void testVerboseError() {
3737
GraphErrorResponse errorResponse = new GraphErrorResponse();
@@ -82,7 +82,7 @@ public Map<String, String> getHeaders() {
8282
assertTrue(message.indexOf("Error code: Unable to parse error response message") == 0);
8383
assertTrue(message.indexOf("http://localhost") > 0);
8484
}
85-
85+
8686
@Test
8787
public void testNullConnection() {
8888
DefaultLogger logger = new DefaultLogger();
@@ -124,5 +124,24 @@ public InputStream getInputStream() throws IOException {
124124
assertTrue(message.indexOf("Error code: Unable to parse error response message") == 0);
125125
assertTrue(message.indexOf("http://localhost") > 0);
126126
}
127-
127+
@Test
128+
public void requestPayloadShouldNotBePartOfMessageWhenNotVerbose(){
129+
final GraphErrorResponse errorResponse = new GraphErrorResponse();
130+
final GraphError error = new GraphError();
131+
error.code = GraphErrorCodes.UNAUTHENTICATED.toString();
132+
errorResponse.error = error;
133+
final GraphServiceException exception = new GraphServiceException(null,null,new ArrayList<String>(),"requestPayload",401,"Unauthorized",new ArrayList<String>(),errorResponse, false);
134+
final String message = exception.getMessage();
135+
assertFalse(message.indexOf("requestPayload") > 0);
136+
}
137+
@Test
138+
public void requestPayloadShouldBePartOfMessageWhenVerbose(){
139+
final GraphErrorResponse errorResponse = new GraphErrorResponse();
140+
final GraphError error = new GraphError();
141+
error.code = GraphErrorCodes.UNAUTHENTICATED.toString();
142+
errorResponse.error = error;
143+
final GraphServiceException exception = new GraphServiceException(null,null,new ArrayList<String>(),"requestPayload",401,"Unauthorized",new ArrayList<String>(),errorResponse, true);
144+
final String message = exception.getMessage();
145+
assertTrue(message.indexOf("requestPayload") > 0);
146+
}
128147
}

0 commit comments

Comments
 (0)