Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old December 28th, 2007, 09:32 AM
Marbin Marbin is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 17 Marbin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 15 m 20 sec
Reputation Power: 0
SQL Injection and Ruby

I searched a lot in the internet about SQL Injection + Ruby

But I never found an article that can explain me something clearly.

I know that the following approach will help reduce SQL Injection attacks.

User.find(:first, :conditions => ["login = ? AND password = ?", params[:name],
params[assword]])




I found a page in where someone says that the parameters will be somewhat "sanitized".

The question is:

What he mean by sanitized, in which way the parameters are going to be filtered?

Reply With Quote
  #2  
Old December 28th, 2007, 06:43 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 51 m 47 sec
Reputation Power: 797
You need to first understand what SQL injection is all about. The concept works like this:

Assume you're building a SQL statement by hand:
Code:
sql = "select * from table where param = '#{params[:foo]}' "

(or)
Code:
User.find(:first, :conditions => ["login = #{params[:name]} AND password = #{params[:password]}" )


The problem with this approach is that someone can pass a ' in the parameter and break your code. For instance, if I pass "Tim O' Reilly" in there, the SQL will break because of the quote. (i.e.) it will transform to:
Code:
select * from table where param = 'Tim O' Reilly' 

I won't specify how you can misuse this to cause evil here, but you may be able to figure out for yourself. Regardless, the problem is caused here because we put the parameter into the SQL string directly.

The solution is to write some code to properly format the SQL string. For instance:
Code:
sql = "select * from table where param = '" + fix_quotes(params[:foo]) + "' "

where fix_quotes is some function that you wrote yourself to format the quotes. After this function runs, the string comes out looking like this:
Code:
select * from table where param = 'Tim O\' Reilly'

or some such. The problem with rolling your own functions is:
1. You need to write the code to do this:
2. You don't know if you've covered all the special characters or not. For instance, I could pass \ in my params and your SQL would break again.

Ruby gives you an alternative to these problems:
Code:
User.find(:first, :conditions => ["login = ? AND password = ?", params[:name],
params[:password]])

In this case, the SQL is not built directly. Instead, the parameter places are marked with ?. When rails sees this, it will replace the ? with the proper argument and automatically fix them so that special characters don't break the SQL. You can rely on rails doing it all for you and doing it correctly.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month

Reply With Quote
  #3  
Old December 31st, 2007, 06:33 AM
Marbin Marbin is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 17 Marbin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 15 m 20 sec
Reputation Power: 0
I know all that of how SQL Injection is performed but anyway you clear my doubt.

It was simple by using that Ruby procedure that you mention at the end of your reply it will clean unwanted characters from the string.

Thanks.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > SQL Injection and Ruby


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway