Skip to content

Commit

Permalink
[KUEoGBey] Sanitize text input and add quotes to allow special charac…
Browse files Browse the repository at this point in the history
…ters in Atomic procedures (#3613)
  • Loading branch information
gem-neo4j authored Jun 7, 2023
1 parent d9578a9 commit efd60f4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/src/main/java/apoc/atomic/Atomic.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public Stream<AtomicResults> update(@Name("container") Object nodeOrRelationship

retry(executionContext, (context) -> {
oldValue[0] = entity.getProperty(property);
String statement = "WITH $container as n with n set n." + property + "=" + operation + ";";
String statement = "WITH $container as n with n set n." + Util.sanitize(property, true) + "=" + operation + ";";
Map<String, Object> properties = MapUtil.map("container", entity);
return context.tx.execute(statement, properties);
}, times);
Expand Down
59 changes: 59 additions & 0 deletions core/src/test/java/apoc/atomic/AtomicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import apoc.util.TestUtil;
import org.apache.commons.lang.ArrayUtils;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -30,6 +31,7 @@
import org.neo4j.test.rule.DbmsRule;
import org.neo4j.test.rule.ImpermanentDbmsRule;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -370,4 +372,61 @@ public void testConcurrentUpdate() throws Exception {
long salary = TestUtil.singleResultFirstColumn(db, "MATCH (n:Person {name:'Tom'}) RETURN n.salary1 as salary;");
assertEquals(100L, salary);
}

@Test
public void testPropertyNamesWithSpecialCharacters(){
db.executeTransactionally("" +
"CREATE (p:Person { " +
"`person.name`:'Tom', " +
"`person.age`: 1, " +
"`person.friends`: [\"Fred\", \"George\"], " +
"`person.nickname`: 'Tom' " +
"})");

String match = "MATCH (n:Person {`person.name`:'Tom'})";
String returnStmt = "YIELD oldValue, newValue RETURN oldValue, newValue";

// ADD
TestUtil.testCall(
db,
match + " CALL apoc.atomic.add(n, 'person.age', 1) " + returnStmt, (r) -> {
Assert.assertEquals(1L, r.get("oldValue"));
Assert.assertEquals(2L, r.get("newValue"));
});
// SUBTRACT
TestUtil.testCall(
db,
match + " CALL apoc.atomic.subtract(n,'person.age', 1) " + returnStmt, (r) -> {
Assert.assertEquals(2L, r.get("oldValue"));
Assert.assertEquals(1L, r.get("newValue"));
});
// CONCAT
TestUtil.testCall(
db,
match + " CALL apoc.atomic.concat(n,'person.nickname', \"my\") "+ returnStmt, (r) -> {
Assert.assertEquals("Tom", r.get("oldValue"));
Assert.assertEquals("Tommy", r.get("newValue"));
});
// INSERT
TestUtil.testCall(
db,
match + " CALL apoc.atomic.insert(n,'person.friends', 1, \"Ron\") " + returnStmt, (r) -> {
assertArrayEquals(new String[]{"Fred", "George"},(String[]) r.get("oldValue"));
assertArrayEquals(new String[]{"Fred", "Ron", "George"},(String[]) r.get("newValue"));
});
// REMOVE
TestUtil.testCall(
db,
match + " CALL apoc.atomic.remove(n,'person.friends', 1) " + returnStmt, (r) -> {
assertEquals(List.of("Fred", "Ron", "George"), r.get("oldValue"));
assertArrayEquals(new String[]{"Fred", "George"},(String[]) r.get("newValue"));
});
// UPDATE
TestUtil.testCall(
db,
match + " CALL apoc.atomic.update(n,'person.age','n.`person.age` * 3') " + returnStmt, (r) -> {
Assert.assertEquals(1L, r.get("oldValue"));
Assert.assertEquals(3L, r.get("newValue"));
});
}
}

0 comments on commit efd60f4

Please sign in to comment.