Skip to Content »

FlashApe » Essential PHP Security

 Essential PHP Security

  • November 22nd, 2005
  • 1:53 am

Just finished reading the book Essential PHP Security, the O’Relly book by Chris Shiflett. It’s short, only about 100 pages or so, but it hits the mark really well.
Often with web apps you will hear the terms “sql injection”, “cross-site scripting(XSS)”, “spoofing”, “session hijacking” and a bunch of other stuff that doesn’t sound so good. The other of this book doens’t just talk abou them, he offers examples of how these area done, in the hopes of showing the readers how simple these attacks are to pull off, and how easy it is to offer up some defenses against these kinds of attacks.
The main defenses the author pounds into your head when reading this book is to “filter input”, and “escape output”:
� filter input - All data coming in to your app from a remote source (a web form, a database, even session data) should be considered tainted. In his book, the author creates a new array and holds all filtered data in the array, and uses that in his scripts instead of any raw data being sent in.
� escape output - using functions such as htmlentities() and mysql_real_escape_string() to escape any characters that a browser may interpret as html. Example: someone could add a javascript directly into a comment box in someones blog that does soemthing nasty. If that script is not escaped when the the comment is dispalyed on the page, it will run as html and cause some damage.
There are other points he makes about “defense in depth”, which is basically redunt checking of data in a php script, not using register globals etc. and shows you many poins in a web app that have a potential for huge security risks.
For me, the book shed a huge light on a subject that is often talked about, but most of the time not really understood, and often delibrately and completely ignored. Now if you’ll excuse me, I’ve got some glaring security holes to fix.

2 People had this to say...

Gravatar
  • Paul
  • January 13th, 2007
  • 2:30 pm

Thanks for the recommendation of Shiflett’s book. I have several Web sites that accept input and display it, so security is important to me. On some Web sites I see htmlentities being used and others say to use htmlspecialchars. Any input on this?

Gravatar
  • rich
  • January 13th, 2007
  • 9:48 pm

i think it’s just a matter of how ‘deep’ you want to escape it. htmlentities will escape anything that has an html equivilent, htmlspecialchars only looks for a few specific things: angeld brackets, quotes, and ampersands, so I’d imagine it probably runs a little faster. I’m not an expert here though, so maybe there are more issues i’m not aware of between the two.

Want your say?

* Required fields. Your e-mail address will not be published on this site


You can use the following XHTML tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>