Prevent SQL injection with the new Api ?

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Haniman
Posts: 35
Joined: 02 Sep 2009, 22:52

Prevent SQL injection with the new Api ?

Postby Haniman » 04 Nov 2010, 15:21

Hello,

in the olde version was an escapeQuotes function.

Is there something similar in the new API? Could find one so far.


thx :)
Haniman
Posts: 35
Joined: 02 Sep 2009, 22:52

Postby Haniman » 05 Nov 2010, 10:50

i just wrote my own :D
User avatar
jpardoe
Posts: 132
Joined: 31 Aug 2009, 20:54

Postby jpardoe » 05 Nov 2010, 11:27

Care to share?
bart4president.com
Posts: 74
Joined: 23 Mar 2010, 02:35

Postby bart4president.com » 05 Nov 2010, 16:53

Yes I am quite interested in this as well!
ext0sus
Posts: 71
Joined: 17 Oct 2010, 16:14

Postby ext0sus » 05 Nov 2010, 17:20

Using PreparedStatements would probably be the best way to do it but I can't find a way to integrate it with DBManager. E.g:

Code: Select all

PreparedStatement sql = dbManager.getConnection().prepareStatement("SELECT password FROM accounts WHERE username=?");
sql.setString(1, name);

But there's no way of getting the result into an SFSArray as far as I know.

As for writing your own: I tried searching to see if I could find an example. Every article I read said "use a PreparedStatement". But I assume you would just want to search the string for any single quotes (') and put a backslash (\) or another single quote infront of them.

Is it possible that SFS does it behind the scenes? I tried injecting SQL into my own application's login and I just got an error.
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 06 Nov 2010, 07:05

The use of PreparedStatement is indeed a good solution. I am adding a note in our todo list to better support them in the current API.

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
Haniman
Posts: 35
Joined: 02 Sep 2009, 22:52

Postby Haniman » 07 Nov 2010, 09:43

I just did an

public static String escapeQuotes(String inString ){
return inString.replaceAll("'", "''");
}

This forms all quotes in doublequotes, which makes them appear as an empty string.

This only avoids the simpliest injections. The only thing that really helps are these prepared statements. But i didnt tried them yet.

Just too much to do to get my game running on the new server.
I am near to cancel the portation ^^
User avatar
jpardoe
Posts: 132
Joined: 31 Aug 2009, 20:54

Postby jpardoe » 07 Nov 2010, 09:47

Stick with it Haniman! You are always learning new things which will only help you in the future!

Can someone post a working code example of prepared statement use?
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 22 Nov 2010, 07:36

UPDATE:
I need to correct myself on this:
The use of PreparedStatement is indeed a good solution. I am adding a note in our todo list to better support them in the current API.


You can already use PreparedStatement quite easily:

Code: Select all

Connection conn = getParentZone().getDBManager().getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM people WHERE age > ?");
stmt.setInt(1, 35);

// Obtain ResultSet
ResultSet res = stmt.executeQuery();

// Obtain SFSArray
ISFSArray arr = SFSArray.newFromResultSet(res);


Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
jpardoe
Posts: 132
Joined: 31 Aug 2009, 20:54

Postby jpardoe » 22 Nov 2010, 08:18

Great, thanks!
Democre
Posts: 77
Joined: 16 Sep 2010, 17:58

Postby Democre » 25 Nov 2010, 16:03

Also, remember to close the connections when done using them.
User avatar
jpardoe
Posts: 132
Joined: 31 Aug 2009, 20:54

Postby jpardoe » 26 Nov 2010, 05:21

How?
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 27 Nov 2010, 07:16

connection.close()
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
jpardoe
Posts: 132
Joined: 31 Aug 2009, 20:54

Postby jpardoe » 27 Nov 2010, 07:21

So simple! :)

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 61 guests