Slashdot Mirror


Essential PHP Security

Michael J. Ross writes "Given the remarkable popularity of PHP for developing dynamic Web sites, as well as the ever-increasing need for security on those same sites, one would think that there would be great demand for — and comparable supply of — books that explain how to create secure sites using PHP. However, such is not the case, and even the most extensive general purpose PHP books may only devote a single chapter to this critical topic, if that much. Essential PHP Security, written by PHP expert Chris Shiflett, aims to fill the gap." Read the rest of Michael's review. Essential PHP Security author Chris Shiflett pages 109 publisher O'Reilly Media rating 7 reviewer Michael J. Ross ISBN 059600656X summary A concise introduction to PHP security principles and practices.

O'Reilly has a Web page for the book, where they offer a sample chapter (Chapter 4: Sessions and Cookies), in PDF format, as well as the book's table of contents, index, errata, and links to the online version of the book, in O'Reilly's Safari service. As of the writing of this review, the confirmed errata is reassuringly sparse, and the unconfirmed errata is nonexistent, which speaks well of the author keeping on top of reader feedback — a worthy quality not shared by all technical writers. The author also has his own Web site dedicated to the book, where he has posted a table of contents, brief reader reviews, and two free chapters in PDF format: Chapters 2 (Forms and URLs) and 4.

In the book's forward, Andi Gutmans briefly explains how increasing Internet usage has resulted in a corresponding increase in security risks, for individuals and businesses operating online. He also notes that most of the security problems related to PHP-based applications, are not the result of weaknesses in the language itself, but rather in the way that developers have used the language in creating those applications. The intent of the book is to bring together the guidelines and lessons learned for writing secure PHP code, into a single volume. He concludes by noting that most of the principles presented in the book apply equally well to other Web development languages.

The bulk of the book's material is organized into seven chapters, focusing on the following topics: forms and URLs, databases and SQL, sessions and cookies, includes, files and commands, authentication and authorization, and shared hosting. These are preceded by an introduction, which oddly is labeled as a chapter. The true chapters are succeeded by three appendices, which cover the topics of configuration directives, functions, and cryptography. A short index rounds out the volume.

In the introduction, Shiflett presents the security-related PHP features, principles, and best practices that he uses as a foundation throughout the rest of the book, when focusing on the specific PHP topics covered by all of the subsequent chapters. The two features of PHP discussed are: register globals, of which most experienced PHP developers know the dangers, and PHP's error reporting capabilities. The four principles espoused by the author for writing secure PHP systems are: safeguard redundancy, minimum privileges, clarity through simplicity, and minimizing data exposure. The heart of the book appears to be his four recommended practices: tempering usability with security, tracking input and output data, filtering all input, and escaping or encoding output to preserve its meaning.

The seven topic chapters that follow the introduction provide fairly terse coverage of how those principles and practices are put to use, when designing and implementing forms, URLs, SQL commands, sessions, cookies, etc. Each subtopic within them is discussed briefly, and illustrated with code snippets.

If anyone is well-suited to writing such a work, it is Chris Shiflett, a well-known authority on PHP security, a respected contributor to the PHP community, founder and spokesman of the PHP Security Consortium, and founder and President of Brain Bulb, a PHP consulting firm.

In light of the author's expertise, one would presume that he would make every effort to write the definitive volume on PHP security — covering every conceivable topic, including: execution of system commands, verification of user IDs and authorization, e-mail spamming via Web forms, (the related topic of) exclusion of bots, and remote procedure calls. However, Essential PHP Security does not discuss those critical matters specifically. Moreover, the topics chosen are discussed in a rather cursory manner. The code samples throughout the book are generally quite minimal, with little to no explanation as to how they work. In addition, many of the techniques presented are but variations on the theme of "filter user input." These weaknesses may be why the book clocks in at only 109 pages. In fact, the seven core chapters comprise only 71 pages, leaving the reader to wonder how PHP security could possibly be adequately plumbed by such a short treatment.

On the other hand, there is something to be said for terse writing, as wizened fans of Kernighan and Richie's C language classic can attest. In agreement would be any developer who has purchased one of the many 700+ page technical tomes that turn out to be padded with excessive margins, poorly-tested code, and pointless appendices lifted from the respective products' documentation. Perhaps Shiflett intended his book to be more a primer on PHP security, rather than a comprehensive coverage — and hence the title of the book. As such, it would primarily be of value to PHP developers unfamiliar with basic security pitfalls and defenses. Regardless, any PHP developer would be wise to begin with this book as a first step towards PHP security mastery, but even wiser if they were to follow it up with more substantial works, as well as keeping current by reading security-focused Web sites and other current publications.

Michael J. Ross is a freelance writer, computer consultant, and the editor of PristinePlanet.com's free newsletter."

You can purchase Essential PHP Security from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

11 of 132 comments (clear)

  1. Rule #1 by Spy+der+Mann · · Score: 3, Insightful

    Don't use a shared host.

    'Nuff said.

  2. Re:The problem is not PHP security by lukewarmfusion · · Score: 4, Insightful

    There are two kinds of audiences for books like these (my wild speculation to follow):

    1. Developers switching languages who need to know how to implement these security practices in a new language - when I moved from ASP to PHP and others (thank God!) I had to rebuild much of my code library in a new language. Obvious things (to me) like input validation were just a little more difficult without a resource. I've had formal programming education and plenty of real-world experience - now it's just a matter of porting the concepts from one technology to another.

    2. New developers that don't have any idea about secure programming practices - many web developers become programmers to meet their clients' needs. These developers often go from designing and building static websites to building database-driven apps. Whereas your brochure site usually doesn't need to validate input, your web app does - from SQL injection to cross-site scripting, these concepts are foreign to someone.

  3. Re:Christ Shiflett by C_Kode · · Score: 4, Insightful

    They both are very good PHP security guys, but Stefan Esser is quite pathetic. Reading him can be like listening to someone speaking Spanglish. (flipping back and forth between languages) One minute he is professional, the next minute he is a 12 year old. Chris Shiflett should just ignore him and do what he needs to do to get it right. Obviously he made a mistake. Fix it and move on.

  4. Re:Christ Shiflett by dasil003 · · Score: 2, Insightful

    What are the credentials of Chris Shiflett? He's widely touted as a "PHP security expert", but Stefan Esser has a beef with him, and claims that this book contains serious flaws and misunderstandings.

    After reading the post I don't really give a shit what Stefan Esser has to say about the book. Yes there's a flaw, but you won't convince people with such an inflammatory blog post. It looks like he scanned everything to find some flaw so he can rip Chris a new one. I would question if, perhaps, he's envious that Chris got a book published?

    The flaw itself is not some glaring security hole. It's an Apache configuration issue that's pretty tangential to PHP security. It's the responsibility of the web host to avoid this problem. If people are configuring this stuff for themselves then they need to know a lot more than just PHP security. The information to the PHP programmer at worst doesn't work. So what? There are a million ways Apache could be configured that would break some example code. You don't get secure code by mixing and matching a bunch of examples anyway. For the book to be good it needs to impart the principles of PHP security to the reader so that their own code is secure. I don't know whether this book is successful in that, and certainly there is an issue here that could be addressed, but I think it's a stretch to claim that this one example discredits the book. Especially when it comes from someone with a vendetta.

  5. Re:Better session system by Bogtha · · Score: 4, Insightful

    It's not clear from your comment, but you are aware that file-based sessions are just the default in PHP, aren't you? You can implement everything you say within the existing PHP framework by using session_set_save_handler(). You don't have to drop PHP's session handling entirely, just implement your own de/serialisation functions and give them to PHP.

    --
    Bogtha Bogtha Bogtha
  6. Re:Wrong answer by psbrogna · · Score: 2, Insightful

    Out of curiosity, what's wrong with gluing together components? The painting by numbers analogy to programming can work as long as the template and palette is acceptable. Granted, you still have to understand how the systems behaves in its entirety, but you have to in either case.

  7. Re:The problem is not PHP security by masklinn · · Score: 2, Insightful

    There is also a huge gap between languages (or widespread modules features or idioms of the language) helping the writing of secure code, such as Ruby's string having a taintedness flag or Python's DBAPI2, languages that don't go either way, and languages that are just stupid and hard to secure by design.

    And PHP is the latter. Globals, magic_quotes, 15 different functions to escape quotes in DB requests (14 of which don't work, or only work when the moon is green and you have red socks), retarded APIs, stupid naming schemes, lack of deprecation of insecure/retarded/out-of-date APIs (sometimes, deprecating whole APIs or modules is GOOD god damn it, if a function is retarded then you get rid of it, even if it breaks code), PHP is full of dumb features that make it hard to write good, secure code.

    --
    "The way we can tell it's C# instead of Haskell is because it's nine lines instead of two." -- wadler
  8. Re:Wrong answer by Nos. · · Score: 2, Insightful

    Lets see, I work in the security area for my employer and have done web development (not design) work as well. I say that PHP can be used to create rich dynamic sites that are secure. So, now, would you care to back up your accusation of PHP as a programming language being insecure with some real facts?

    Sure, there have been holes in countless PHP applications. However, this is not the fault of the language. In fact, almost all of these problems come down to programmers not properly validating user input before processing it. This is true of pretty much all languages. PHP gets a bad name because its easy to learn and people pick it up and write insecure apps with it. Thats like blaming the hammer when the house falls down.

  9. Re:Wrong answer by dk.r*nger · · Score: 5, Insightful

    Would you mind accommodating your +1 Insightful and tell the world why?
    The same reason that MySQL is crap, because you really, really need stored procedures, views and transactions to keep track of 20.000 messages in 1.500 threads?
    The same reason that Java sucks for everything, always and C never does?
    The same reason that compiled languages are always better than interpreted ones?

    Of course, that reason I'm referring to is arrogance.

    Don't get me wrong. I'm actually halfway to a MSc in Computer Science, and frequently have my ego challenged by kids and their flash 'applications', drag'n'drop VB crap and funny web apps that trust me to let it pass critical information in the URL.. These kids tend to think that I'm learning useless crap because they already know. Naturally I'm all warm inside when I get to give their 'application' 500% speedup by adding an index to a table.

    But what makes PHP itself unsecure? Yes, PHP wants to be proporly configured. And if you let 50 kids run amok on the same server, sure they'll fuck something up (though never outside of the PHP user).

    Now imagine a production webserver, to where only qualified developers has access, and only tested PHP code is put on. Works for me, has for a long time.

    Oh, and concluding that all PHP is paint by numbers because it's a scripting language is just ignorant.
    #include stdio.h anyone? Not enough of a real man to write your own IO routines, so you're stuck with gluing together libc stuff "in a paint-by-numbers style"? bah..

  10. No book can teach you because the bad don't read by SmallFurryCreature · · Score: 2, Insightful
    Two students developed a new website for a volunteer organisation as part of their education. Something like an internship, anyway for them it was a temp job. They didn't finish it and since I volunteerd in the past for doing work for them I am now putting the finisching touches on it.

    Yuck.

    To look at the code I am once again reminded why I always feel sick when someone tells me that I am going to have to work together with a person with a degree. As far as I know they passed with this project yet it is totally and utterly crap. Oh of course it does not work cross-browser. Yes it has many bits of codes wich don't actually doesn't do shit. A 100 line switch statement that always does the same thing for instance but that ain't the worst of it.

    The worst of it is that security is non-existent. They use the old '?page=page1' in the url to switch content. I like this approach in itself as it leaves you with only 1 code file wich is accessible from the outside. I also like to make fucking sure that 'page' is filled only with values that I expect. They just insert it in an sql statement and execute it.

    Shudder.

    Could a book teach guys like this about security? NO.

    It is not the first time I see shit like this. To many IT students just are to young and naive to think about security. Or rather to not think about security but just do it. Nobody had to teach me that trusting userinput is bad. I know it is. How?

    Well, I don't know I just do. Perhaps it is all the years of low level cracking of games where you alter a string somewhere to give you more health/money whatever. Perhaps it is just being a suspicious bastard.

    Security is not a set of easy to follow rules, security is not trusting people.

    PHP is a usefull enough language that unfortunally in its basic install comes with some features that can really bite you in the ass. I always disable them on any server I control and then have to spend a lot of time correcting everyone elses code to work on a secured server. Oh and if I see one more person use PHP native sessions I am going to kill that motherfucker. Especially when it is used to store 1 value. Just use a fucking cookie instead of glogging the HD in resource eating insecure way.

    ANyway, the review of the book? Well it covers the very basics. If you still need to be told this, just stay away from the web. This is akin to a cooking book telling you not to have a boiling pot of water on the first burners of your stove when a little kid is around. What I read of it all falls into the 'duh' category.

    --

    MMO Quests are like orgasms:

    You may solo them, I prefer them in a group.

  11. Must-have for all the PHP developers by garyli · · Score: 3, Insightful
    Chris Shiflett has definitely created a masterpiece that I personally believe only he is capable of. His experience and precise, easy-to-read manner of writing are unparalleled when it comes to PHP security.

    One of the things I liked about this book is that you don't need to be sat next to your PC to read it. Though it has many nice and clear code examples, it's mainly about principles and theory. Excellent to have on the bedside table.

    It isn't a very thick book, but is written in a clear and accessible style, and I found myself going 'aha' all the way through. I read it quickly but have a feeling that I'll return to it often until all those best practices are memorised and I'm 'doing' them.

    What is most useful about this book is the aggregation in one place of descriptions of all of these security attacks and vulnerabilities in PHP code, along with suggestions on dealing with them.

    The only specific attack missing which I would like to have seen information about is email spamming through website forms. However the general principles described in the book will help prevent these attacks as well.

    This book will definitely be a long-term desktop reference for me and mandatory reading for all the PHP developers in my work place. I would definitely reccomend this book to aspiring PHP developers and think it would also benefit some of the more experienced folks out there.

    --
    Webmaster of Spy