This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 4758
117
  • 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
Sheriff
Posts: 38572
661
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.
 
Master Rancher
Posts: 3633
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!