LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-27-2013, 12:22 PM   #1
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Rep: Reputation: 53
mysql sanitization


Hi. Besides the formatting requirements of the php application, if I'm using prepared statements correctly do I need to sanitize user input in any other ways? If so, what's the numblest way?
 
Old 09-27-2013, 12:31 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by bluegospel View Post
I'm using prepared statements correctly
That should be enough for input. For output, you sanitize with htmlentities.
 
1 members found this post helpful.
Old 09-27-2013, 02:40 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
If you use prepared-statements (i.e. "placeholders"), you will prevent SQL-injection, because the user-supplied content is never understood to be part of the SQL statement that is to be executed.

You should also "de-fang" the inputs to ensure that users do not attempt to store information which, when replayed to a recipient's browser, would be interpreted as a malicious script. Likewise, when the contents of the database are presented by your program to a recipient's browser, any HTML-like content should be transformed (as described above) into HTML entities. For example, a string "<script>" would become a harmless "&lt;script&gt;" which will cause that literal string to appear in the output without any possibility of it being executed.

(To see exactly what I am talking about ... look at the "view page source" of this very page, to see what the LinuxQuestions forum-software did with the text of this post! The string that I used as my example ... has been transmogrified in precisely the way that I described. Thus, it is impossible for your browser to "execute" my post. You'll also see how the ampersands were also encoded as HTML-entities. You might have to "reload" the page to see the source-code of this posting.)

Last edited by sundialsvcs; 09-27-2013 at 02:45 PM.
 
1 members found this post helpful.
Old 09-27-2013, 02:52 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Or htmlspecialchars if you are using characters above code 127 and don't want them transformed. (Don't forget about parameter#3 ($encoding))
 
1 members found this post helpful.
Old 09-27-2013, 04:53 PM   #5
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Thanks folks. Is the copyright symbol above 127?
 
Old 09-27-2013, 05:02 PM   #6
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
What about xml input/output?
 
Old 09-27-2013, 05:12 PM   #7
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Also, what about when I want to preload forms for member editing?
 
Old 09-27-2013, 05:14 PM   #8
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Quote:
Originally Posted by bluegospel View Post
Also, what about when I want to preload forms for member editing?
I mean, obviously I don't want the user to view the entities.
 
Old 09-27-2013, 05:34 PM   #9
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Oh, okay, I just tested it. The form field interprets it the same as if it were the body.

Last edited by bluegospel; 09-27-2013 at 05:39 PM.
 
Old 09-27-2013, 05:59 PM   #10
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Wait. This is confusing. If the data is stored in a database or xml with htmlspecialchar conversions and then output again with htmlspecialchar conversions you don't get your intended user output. But if you omit the conversion in your output you risk maliscious attacks against your guests.
 
Old 09-27-2013, 06:27 PM   #11
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Okay, I think I get it. Always use htmlspecialchars for input. Sundial was just saying if you do happen to allow literal html strings in storage always convert them for output. Correct?
 
Old 09-28-2013, 03:30 AM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> Thanks folks. Is the copyright symbol above 127?

Yes, its unicode (iso-8859-1, windows-1252) is 0xA9; html-code: &copy;
 
1 members found this post helpful.
Old 10-05-2013, 11:01 AM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> Always use htmlspecialchars for input.

Wrong. Always use it before/during output.
 
Old 10-05-2013, 12:06 PM   #14
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Always use htmlspecialchars for output. Sundial was just saying if you do happen to allow literal html strings in storage always use htmlspecialchars to convert them for output. Correct?
Now it is.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sanitization in Linux er.munishsidana Linux - General 1 06-25-2009 07:05 PM
Can MySQL log on via SSH/bash? mysql:x:27:101:MySQL Server:/var/lib/mysql:/bin/bash Ujjain Linux - Newbie 2 04-24-2009 02:21 PM
mysql error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql. SpellChainz Linux - Newbie 1 06-23-2007 03:35 PM
mysql error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql. Dannux Linux - Software 3 03-24-2006 08:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration