24

I am trying to find information about Spring Security JPA and if methods like .save() are protected from sql injection.

For instance I have object Customer. that I want to persist to my database. I am using CustomerRepository Spring implementation to operate on that entity. Customer's constructor is using parameters from the user. When everything is staged I am invoking .save(). Is this safe against sql injection or Should I do the check up first?

1 Answer 1

21

.save() is safe, only the usage of native queries is vulnerable.

List results = entityManager.createNativeQuery("Select * from Customer where name = " + name).getResultList();

You can make native queries safe also, if you use a parameter.

Query sqlQuery = entityManager.createNativeQuery("Select * from Customer where name = ?", Customer.class);
List results = sqlQuery.setParameter(1, "John Doe").getResultList();
Sign up to request clarification or add additional context in comments.

5 Comments

so for instance if Customer object has field name, and it is set to be a string: Select * from Customer where name = 'test' and i use save() method, nothing wrong with table wiill happen?
The JDBC driver will escape this data appropriately before the query is executed;
The problem here is when String name = "'Cosmin' or name='Jhon'"
is CrudRepository methods like .save() .delete() is safe for sql injection ?
@jklee Do you have a reference of your argument, it seems to be hard to find it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.