Skip to content

Commit 7ee4c56

Browse files
Deepthi M|Updated to show details of error message only when a global property is enabled,Fixed failing test cases and added new tests for the updated method. (#591)
* Deepthi M|Updated to show details of error message only when a global property is enabled,Fixed failing test cases and added new tests for the updated method. * Deepthi M|Fixed global property * Deepthi M|removing white spaces * Deepthi M|Updated description of global property and also added a default value to global property as false when no value is given to it to avoid NPE * Deepthi M|RESTWS-921|Updated as per the review comments-Renamed variables,updated description in config.xml and added spaces after comma.
1 parent f038fbb commit 7ee4c56

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

omod-common/src/main/java/org/openmrs/module/webservices/rest/web/RestConstants.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,12 @@ public class RestConstants {
206206
* Constants used for the Server Log REST Service privilege checking
207207
*/
208208
public static final String PRIV_GET_SERVER_LOGS = "Get Server Logs";
209+
/**
210+
* Global property name used to enable or disable the inclusion of stack trace details
211+
* in the error response.
212+
*
213+
* When this property is set to 'true', stack trace details will be included in error
214+
* responses. When set to 'false', stack trace details will be omitted.
215+
*/
216+
public static String ENABLE_STACK_TRACE_DETAILS_GLOBAL_PROPERTY_NAME = MODULE_ID + ".enableStackTraceDetails";
209217
}

omod-common/src/main/java/org/openmrs/module/webservices/rest/web/RestUtil.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -828,16 +828,21 @@ public static SimpleObject wrapErrorResponse(Exception ex, String reason) {
828828
} else {
829829
map.put("message", "[" + message + "]");
830830
}
831-
StackTraceElement[] steElements = ex.getStackTrace();
832-
if (steElements.length > 0) {
833-
StackTraceElement ste = ex.getStackTrace()[0];
834-
map.put("code", ste.getClassName() + ":" + ste.getLineNumber());
835-
map.put("detail", ExceptionUtils.getStackTrace(ex));
831+
StackTraceElement[] stackTraceElements = ex.getStackTrace();
832+
if (stackTraceElements.length > 0) {
833+
StackTraceElement stackTraceElement = ex.getStackTrace()[0];
834+
String stackTraceDetailsEnabledGp = Context.getAdministrationService()
835+
.getGlobalPropertyValue(RestConstants.ENABLE_STACK_TRACE_DETAILS_GLOBAL_PROPERTY_NAME, "false");
836+
map.put("code", stackTraceElement.getClassName() + ":" + stackTraceElement.getLineNumber());
837+
if ("true".equalsIgnoreCase(stackTraceDetailsEnabledGp)) {
838+
map.put("detail", ExceptionUtils.getStackTrace(ex));
839+
} else {
840+
map.put("detail", "");
841+
}
836842
} else {
837843
map.put("code", "");
838844
map.put("detail", "");
839845
}
840-
841846
return new SimpleObject().add("error", map);
842847
}
843848

omod-common/src/test/java/org/openmrs/module/webservices/rest/web/RestUtilTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
import org.junit.Assert;
1919
import org.junit.Test;
2020
import org.mockito.Mockito;
21+
import org.openmrs.GlobalProperty;
22+
import org.openmrs.api.context.Context;
2123
import org.openmrs.module.webservices.rest.SimpleObject;
24+
import org.openmrs.web.test.BaseModuleWebContextSensitiveTest;
2225
import org.springframework.mock.web.MockHttpServletRequest;
2326

2427
/**
2528
* Tests for the {@link RestUtil} class.
2629
*/
27-
public class RestUtilTest {
30+
public class RestUtilTest extends BaseModuleWebContextSensitiveTest {
2831

2932
/**
3033
* @see RestUtil#ipMatches(String,List)
@@ -201,5 +204,24 @@ public void wrapErrorResponse_shouldSetStackTraceCodeAndDetailEmptyIfNotAvailabl
201204
Assert.assertEquals("", errorResponseMap.get("code"));
202205
Assert.assertEquals("", errorResponseMap.get("detail"));
203206
}
204-
207+
@Test
208+
public void wrapErrorResponse_shouldSetStackTraceDetailsIfGlobalPropEnabled() throws Exception {
209+
Context.getAdministrationService().saveGlobalProperty(
210+
new GlobalProperty(RestConstants.ENABLE_STACK_TRACE_DETAILS_GLOBAL_PROPERTY_NAME, "true"));
211+
Exception ex = new Exception("exceptionmessage");
212+
SimpleObject returnObject = RestUtil.wrapErrorResponse(ex, "wraperrorresponsemessage");
213+
214+
LinkedHashMap errorResponseMap = (LinkedHashMap) returnObject.get("error");
215+
Assert.assertNotEquals("", errorResponseMap.get("detail"));
216+
}
217+
@Test
218+
public void wrapErrorResponse_shouldSetNoStackTraceDetailsIfGlobalPropDisabled() throws Exception {
219+
Context.getAdministrationService().saveGlobalProperty(
220+
new GlobalProperty(RestConstants.ENABLE_STACK_TRACE_DETAILS_GLOBAL_PROPERTY_NAME, "false"));
221+
Exception ex = new Exception("exceptionmessage");
222+
SimpleObject returnObject = RestUtil.wrapErrorResponse(ex, "wraperrorresponsemessage");
223+
224+
LinkedHashMap errorResponseMap = (LinkedHashMap) returnObject.get("error");
225+
Assert.assertEquals("", errorResponseMap.get("detail"));
226+
}
205227
}

omod/src/main/resources/config.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
<defaultValue>true</defaultValue>
8383
<description>If the value of this setting is "true", then nothing is logged while the Swagger specification is being generated.</description>
8484
</globalProperty>
85+
<globalProperty>
86+
<property>@MODULE_ID@.enableStackTraceDetails</property>
87+
<defaultValue>true</defaultValue>
88+
<description>If the value of this setting is "true", then the details of the stackTrace would be shown in the error response. However, the recommendation is to keep it as "false", from the Security perspective, to avoid leaking implementation details.</description>
89+
</globalProperty>
8590

8691
<!-- DWR -->
8792

0 commit comments

Comments
 (0)