This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

SQL injection issue in java code  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have below main method which shows SQL injection flaw (as string concatenation is done here) when scanned for coding standards/rules.
   
I want to remove the SQL injection flaw. As my `name` parameter is dynamic, I cannot set it with `preparedStatement`. What can be a optimal solution to this?

NOTE: Using 2 different queries in `if-else` block will not solve the purpose as I have 7 different parameters to be set dynamically which will introduce overhead as there will be many queries.
 
Ranch Hand
Posts: 227
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recommend Parameterized Queries:



Above code would be useful
 
Saloon Keeper
Posts: 5057
135
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a SQL injection vulnerability only if the dynamic parts of the query aren't checked before being used. If the code ensures that neither "name" nor "subName" contains anything that upsets the SQL statement, then there is no vulnerability.

Santosh Kumar Nayak wrote:...I recommend Parameterized Queries...


Prepared statements do not work in this case, as Abhishek already mentioned.
 
author & internet detective
Posts: 38925
686
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static analysis tool can only tell you about a *potential* issue. Picture the tool telling you "I can't certify this; can you human take another look."

You need to whitelist the parameter field name to ensure it is safe. For example, you might check it only contains letters.
 
Rancher
Posts: 3772
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:
Prepared statements do not work in this case, as Abhishek already mentioned.



Well, they do, but it takes a little more work.
You build up the queryand then assign the parameters in a loop, exactly how depends on where the search criteria is coming from, but a List of Field Name/Search Value is one.
That's how a lot of search queries work, where you don't know exactly what fields are going to be queried on.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!