Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions api/src/main/java/org/openmrs/ui/framework/UiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,10 @@ public String escapeJs(String input) {
if (input == null) {
return null;
}

input = input.replaceAll("\\\\", "\\\\\\\\");
input = input.replaceAll("\n", "\\\\n");
input = input.replaceAll("\r", "\\\\r");
input = input.replaceAll("'", "\\\\'");
input = input.replaceAll("\"", "\\\\\"");
Comment thread
Abhisy2506 marked this conversation as resolved.
Outdated
return input;
Expand Down Expand Up @@ -683,11 +686,11 @@ public String getClientTimezone() {
public void setClientTimezone(String clientTimezone) {
try {
Context.addProxyPrivilege(PrivilegeConstants.EDIT_USERS);

String propertyName = Context.getAdministrationService()
.getGlobalProperty(UiFrameworkConstants.UP_CLIENT_TIMEZONE);
.getGlobalProperty(UiFrameworkConstants.UP_CLIENT_TIMEZONE);
Comment thread
Abhisy2506 marked this conversation as resolved.
String currentClientTimezone = Context.getAuthenticatedUser().getUserProperty(propertyName);

if (currentClientTimezone == null || !currentClientTimezone.equals(clientTimezone)) {
Context.getUserService().setUserProperty(Context.getAuthenticatedUser(), propertyName, clientTimezone);
}
Expand Down
22 changes: 22 additions & 0 deletions api/src/test/java/org/openmrs/ui/framework/UiUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,26 @@ public void urlBind_shouldProperlyBindPatientAndVisit() {

}

@Test
public void escapeJs_shouldEscapeBackslash() {
Assert.assertEquals("Foo \\\\", ui.escapeJs("Foo \\"));
}

@Test
public void escapeJs_shouldPreventXSSViaBackslashInjection() {
// exact payload from the bug report
String result = ui.escapeJs("Foo \\\"}];alert(0);[// Bar");
Assert.assertTrue(result.contains("\\\\")); // backslash must be doubled
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kindly asserting only contains("\\"); is too weak and could pass even if the escaping regresses.
so please can either assert the exact expected escaped output for the full payload using Assert.assertEquals(expected, ui.escapeJs(input)), or
assert stronger conditions example that quotes are escaped and that the unescaped breakout sequence .

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @jwnasambu , thanks for reviewing it. Yeah i have added stricter assertion and you can now review that too.

}

@Test
public void escapeJs_shouldEscapeDoubleQuote() {
Assert.assertEquals("say \\\"hi\\\"", ui.escapeJs("say \"hi\""));
}

@Test
public void escapeJs_shouldReturnNullForNullInput() {
Assert.assertNull(ui.escapeJs(null));
}

}
Loading