Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ protected void checkRootUser(ServerSecurityUser user) {
}

protected String decode(final String command) {
return command.replace("&amp;", " ").replace("&lt;", "<").replace("&gt;", ">").replace("&quot;", "\"").replace("&#039;", "'");
return command.replace("&amp;", "&").replace("&lt;", "<").replace("&gt;", ">").replace("&quot;", "\"").replace("&#039;", "'");
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

While this change correctly fixes the decoding of &amp;, the manual chain of replace() calls is not a robust solution for decoding HTML entities. It only handles a small, fixed set of entities and can be inefficient, as each replace() call potentially creates a new String object and scans the string from the beginning. A better approach would be to use a dedicated library, such as Apache Commons Text's StringEscapeUtils.unescapeHtml4(), which provides a comprehensive and optimized implementation. If adding a new dependency is not an option, consider a more efficient single-pass implementation using StringBuilder to avoid creating multiple intermediate strings.

}

protected String error2json(final String error, final String detail, final Throwable exception, final String exceptionArgs,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright © 2021-present Arcade Data Ltd ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: 2021-present Arcade Data Ltd ([email protected])
* SPDX-License-Identifier: Apache-2.0
*/
package com.arcadedb.server.http.handler;

import com.arcadedb.server.BaseGraphServerTest;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class PostCommandHandlerDecodeTest extends BaseGraphServerTest {

@Test
void testJavaScriptFunctionWithLogicalAndOperatorViaHTTP() throws Exception {
executeCommand(0, "sql",
"DEFINE FUNCTION Test.getMatchRating \"return 1==1 && 0==0\" LANGUAGE js");

final var response = executeCommand(0, "sql",
"SELECT `Test.getMatchRating`() as result");

assertThat(response.toString()).contains("\"result\":true");
}

@Test
void testJavaScriptFunctionWithLogicalAndOperatorDirectCommand() {
getServer(0).getDatabase(getDatabaseName()).command("sql",
"DEFINE FUNCTION Test.getMatchRating2 \"return 1==1 && 0==0\" LANGUAGE js");

final var result = getServer(0).getDatabase(getDatabaseName()).query("sql",
"SELECT `Test.getMatchRating2`() as result");

assertThat(result.next().<Boolean>getProperty("result")).isTrue();
}

@Test
void testDecodeMethodCorrectlyHandlesHtmlEntities() {
final var handler = new PostCommandHandler(getServer(0).getHttpServer()) {
public String testDecode(final String command) {
return decode(command);
}
};

// Test the decode method directly with HTML-encoded entities
String encoded = "return 1==1 &amp;&amp; 0==0";
String decoded = handler.testDecode(encoded);

// Should decode &amp; to & not to space
assertThat(decoded).isEqualTo("return 1==1 && 0==0");

// Test other HTML entities too
assertThat(handler.testDecode("&lt;script&gt;")).isEqualTo("<script>");
assertThat(handler.testDecode("&quot;quoted&quot;")).isEqualTo("\"quoted\"");
assertThat(handler.testDecode("&#039;apostrophe&#039;")).isEqualTo("'apostrophe'");
}
}
Loading