Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KUEoGBey] Sanitize text input and add quotes to allow special characters in Atomic procedures #425

Merged
merged 1 commit into from
Jun 5, 2023
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
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 @@ -203,7 +203,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
60 changes: 60 additions & 0 deletions core/src/test/java/apoc/atomic/AtomicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -32,6 +33,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 @@ -392,4 +394,62 @@ 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"));
});
}
}