<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Shawn Wilsher &#187; Code</title>
	<atom:link href="http://shawnwilsher.com/archives/category/code/feed" rel="self" type="application/rss+xml" />
	<link>http://shawnwilsher.com</link>
	<description></description>
	<lastBuildDate>Sun, 04 Dec 2011 10:37:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How I Review</title>
		<link>http://shawnwilsher.com/archives/537</link>
		<comments>http://shawnwilsher.com/archives/537#comments</comments>
		<pubDate>Mon, 16 May 2011 01:48:19 +0000</pubDate>
		<dc:creator>Shawn Wilsher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Mozilla]]></category>

		<guid isPermaLink="false">http://shawnwilsher.com/?p=537</guid>
		<description><![CDATA[I handle a lot of code review and code feedback requests these days. However, it&#8217;d be great to get more people doing this for a number of reasons: More people exposed to more parts of the code base More review bandwidth so more work can be checked into the tree Less dependence on a small [...]]]></description>
			<content:encoded><![CDATA[<p>I handle a lot of code review and code feedback requests these days.  However, it&#8217;d be great to get more people doing this for a number of reasons:</p>
<ul>
<li>More people exposed to more parts of the code base</li>
<li>More review bandwidth so more work can be checked into the tree</li>
<li>Less dependence on a small set of people</li>
</ul>
<p>In order to get more people doing this, it would be good to document to look for, and how to make sure the code is sound.  I&#8217;m sure every reviewer does things a bit differently, but I&#8217;m going to share my process.  There are two types of review I do these days: feedback and review.</p>
<h3>Feedback</h3>
<p>Feedback is pretty simple to do, and I can usually fly though any patch (even large ones) quickly.  This isn&#8217;t very thorough (in fact, I tend to keep it to general comments), but I look for the following things:</p>
<ul>
<li>correct API usage (XPCOM, jsm, whatever)</li>
<li>internal invariants are not violated</li>
<li>any new APIs created make sense and aren&#8217;t confusing</li>
<li>code style matches what&#8217;s there, or follows the style guide</li>
</ul>
<h3>Review</h3>
<p>Review is more important because once a patch gets r+, it can generally land in the tree.  Consequentially, I tend to spend a lot more time on any given patch.  In addition to all the things I do for a feedback request (which are looked at more closely for a review), I&#8217;ll also look at the following:</p>
<ul>
<li>evaluate how well tested is the code that is being added/modified.  If it isn&#8217;t well tested, I&#8217;ll generally suggest a set of test cases that I feel are the bare minimum this needs to land.</li>
<li>evaluate how this might impact other work going on in this area of the codebase</li>
<li>ensure this doesn&#8217;t add any I/O on the GUI thread</li>
<li>apply the patch and run the tests</li>
<li>if the patch looks like it might regress performance, ask for the author to verify that it does not</li>
</ul>
<p>Note that a number of these things may not be done if I know what the patch author has already done to ensure the patch is safe.</p>
]]></content:encoded>
			<wfw:commentRss>http://shawnwilsher.com/archives/537/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Secure PHP on the Web</title>
		<link>http://shawnwilsher.com/archives/83</link>
		<comments>http://shawnwilsher.com/archives/83#comments</comments>
		<pubDate>Tue, 17 Apr 2007 02:45:52 +0000</pubDate>
		<dc:creator>Shawn Wilsher</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://shawnwilsher.com/archives/83</guid>
		<description><![CDATA[PHP is a powerful and easy language to learn. As a developer, you must use that power in a safe and effective manner. While being easy to learn means that it is easy to pick up, it also means that you may not have any experience with writing secure code. Writing secure code is a [...]]]></description>
			<content:encoded><![CDATA[<p>PHP is a powerful and easy language to learn. As a developer, you must use that power in a safe and effective manner. While being easy to learn means that it is easy to pick up, it also means that you may not have any experience with writing secure code. Writing secure code is a must, however, if you do not want your server to be compromised, user data stolen, your site destroyed, or quite possibly worse.  In this article, you will learn about writing more secure PHP code.<br />
<span id="more-83"></span></p>
<h3>Trusting the User</h3>
<p>First of all, just don&#8217;t do it. The number one, most important rule you can have as a developer is to never, ever, trust the data you receive from your users. <strong>Ever.</strong> A large number of the vulnerabilities (some of which I&#8217;ll be discussing) can actually be avoiding if the proper precautions are taken and the user&#8217;s data is properly processed. As a developer you might think “none of the people that are interested in my web site would be malicious, so I don&#8217;t have to worry about that.”  That isn&#8217;t a wise idea to have, however, as a user could unintentionally enter some data in that exploits a security hole, and now you are up a creek without a paddle.</p>
<p>Let me reiterate this point. Never, ever trust data from the user. Even if you check something client side with JavaScript, you should not trust it. Remember that some people turn off JavaScript, making those checks completely useless. Being paranoid about user input and assuming everything they can do could be malicious will greatly prevent many of the common security vulnerabilities found in PHP applications.</p>
<h3>Descriptive Error Messages</h3>
<p>While these can be a big help when debugging, descriptive error messages can also be a big help to potential attackers. Based on the information given in an error message, a malicious user could find out information about your application&#8217;s database structure, file system layout, or other worse things.  Make sure that any error checking you do with the users data will give the least amount of information needed by the user to correct whatever they did wrong.</p>
<p>In addition to your own generated errors, PHP can report a variety of script errors, warnings, and notices, all of which a malicious user could use against you. The good news is that you can disable these on your production server with an <code>.htaccess</code> directive or in your <code>php.ini</code> file by setting <code>error_reporting</code> to <code>0</code>. Do not try using <code>ini_set</code> though! If a fatal error occurs in your script, the value you specify with <code>ini_set</code> will not have any affect, so everything you are trying to prevent being displayed will still be displayed.</p>
<p>The best way to keep track of the errors, warnings, and notices that are not important to the user, but are useful to you when debugging is to use PHP&#8217;s built in logging functions. You can do this just like you do with error reporting by setting the <code>error_logging</code> directive to <code>true</code> and setting the <code>error_log</code> directive to the filename that you want to log to. If these are set with <code>ini_set</code>, the error simply won&#8217;t be logged so it isn&#8217;t a security risk (just harder for you as the developer to debug).  Once you have the log file setup, simply use the <code>trigger_error</code> function like so:</p>
<pre lang="php">trigger_error('A serious error occurred: DETAILS', E_USER_ERROR);</pre>
<p>The first parameter is a string with whatever you want to be logged. The second parameter is the PHP error type, which defaults to <code>E_USER_NOTICE</code> if left unspecified.</p>
<h3>Insecure Database Setup</h3>
<p>The default username and password for popular databases are well known. Leaving these at their default value allows for easy entry to anyone who is able to figure out where your database server is.  The default setup is the first thing an attacker would try in an attempt to get in, so try not to make their job easier!</p>
<p>It is also a good idea to set up different users with specific privileges for your web application. Ideally, anything that a normal user can do should fall under a limited account. By only allowing the client&#8217;s database user to insert and update tuples, or rows in the table, you drastically cut back on the type of damage they can do. Of course, you&#8217;ll probably need to have some ability to delete an arbitrary tuple, but with this restriction it seems impossible to do. The good news is that there is a trivial way to get around this. Simply add another field to the database called something like <code>is_deleted</code> and have it set to <code>0</code> by default. When you want to delete something, simply set it to <code>1</code>. When you are querying data, just look for results that do not have <code>is_deleted</code> set to <code>1</code>, and it will appear to be deleted. The administrators should use a different database user that allows for deleting, and every so often they can run a script that deletes these entries.</p>
<h3>SQL Injection Attacks</h3>
<p>SQL injection attacks come about from developers trusting their users to not be malicious. These attacks are the most common security hazard when communicating with a database. Since the most popular database system in use today with PHP applications is MySQL, these examples are going to be written with its api functions. Let&#8217;s take a look at the common example of a login script. Generally, they&#8217;ll be some query like this:</p>
<pre lang="php" line="1">$sql = “SELECT * FROM `user` WHERE `uname` = '{$_POST['uname']}' AND `pwd` = '{$_POST['pwd']}'”;
$query = mysql_query($sql);</pre>
<p>While this might look safe, it isn&#8217;t. What if some user entered in “<code>' OR 1 = 1 #</code>” as their username? Now the query will result in all the rows in the table being returned, but that&#8217;s clearly not what we want! The query that is executed will look like this:</p>
<pre lang="sql">SELECT * FROM `user` WHERE `uname` = '' OR 1 = 1 # AND `pwd` = ''</pre>
<p>This clearly isn&#8217;t what we wanted, so let&#8217;s take a look why this is the result. The hash symbol (#) indicates a comment, and MySQL will simply ignore everything after it. Since one always equals one, all tuples will be returned. Now, if the developer didn&#8217;t check for the number of rows and just grabs the first result returned by the query, the attacker will now be logged in as the first user in the user table.  You&#8217;ll often find that the first user is an administrator of some type, so this attacker can now go around your site as an administrator. With a bit more effort, the attacker could create an account for themselves, modify entries in the table, or worse. This all happened because we violated our first rule and trusted user input.</p>
<p>There are two solutions to this problem, and both of which are fairly easy to implement. If you are determined to use the <code>mysql_</code>* functions (or are required to use PHP < 5.1), you have to escape the users input like so:</p>
<pre lang="php" line="1">$uname = mysql_real_escape_string($_POST['uname']);
$pwd = mysql_real_escape_string($_POST['pwd']);
$sql = “SELECT * FROM `user` WHERE `uname` = &#8216;$uname&#8217; AND `pwd` = &#8216;$pwd&#8217;”;
$query = mysql_query($sql);</pre>
<p>Be careful if the <code>magic_quotes_gpc</code> directive is set, as you'll have to remove the slashes it adds before calling <code>mysql_real_escape_string</code> because some things will get escaped twice. The <code>magic_quotes_gpc</code> directive adds a slash before single-quotes, double-quotes, backslashes, and NULs.  MySQL's function escapes three additional characters; \n, \r, and \x1a. As a result, some things are escaped twice, which produces results other than what we intended.</p>
<p>If you are lucky enough to be using PHP 5.1 or later, you'll have the ability to use the PHP Data Object (PDO) class. The PDO class is an abstraction layer used to interface with databases. The best part about it is that it was designed in a way that defending against SQL injection attacks is just part of the process. Let's take a look at how we can write our query with PDO:</p>
<pre lang="php" line="1">$stmt = $db->prepare(“SELECT * FROM user WHERE uname = :uname AND pwd
= :pwd”);
$stmt->execute(array(':uname' => $_POST['uname'], ':pwd' => $_POST['pwd']));
$result = $stmt->fetch();</pre>
<p>Calling <code>PDO->prepare</code> and then <code>PDOStatement->execute</code> gives you two benefits. First, it will properly escape the variables you pass in, preventing the SQL injection attack. Secondly, if you plan on issuing a SQL statement multiple times, calling <code>PDO->prepare</code> initially, then calling <code>PDOStatement->execute</code> each time you want to run the query, your calls will be optimized by the database driver by caching the query plan and meta data. This can result in huge performance benefits. There is also a function that is just like <code>mysql_query</code>, and that's <code>PDO->query</code>, but I wouldn't recommend using it because of the benefits obtained by <code>PDO->prepare</code> and <code>PDOStatement->execute</code>.</p>
<h3>Improper Handling of Includes</h3>
<p>Some websites like to dynamically include another page with a url parameter. While this can be incredibly useful, it can also be incredibly dangerous. The PHP code to include this file might look something like this:</p>
<pre lang="php"><?php include $_GET['p']; ?></pre>
<p>Now, a url like <code>index.php?p=news.html</code>. works just fine. However, we are trusting our user here to not enter something malicious in the query string. What if the url is <code>index.php?p=.htpasswd</code>?  The malicious user suddenly has our <code>.htpasswd</code> file, and with the right resources, could figure out passwords to log in to secured parts of the website. The attacker could potentially get any file that your webserver has permission to access. The fix for this is to check the filename against a list of ones that you are expecting. You should also set the PHP directive <code>open_basedir</code> to the paths that you will be opening files from. PHP will then make sure that you never open a file outside of the specified path(s).</p>
<p>If your server is not configured properly, this hole could be used to execute a remote php script as well, letting the attacker run arbitrary code on your server. To prevent this, set the <code>allow_url_fopen</code> directive to <code>false</code>. This can only be set in your <code>.htaccess</code> file or <code>php.ini</code>, however, so don't try using <code>ini_set</code>.</p>
<h3>Conclusion</h3>
<p>Assuming that your site is safe from attackers without actually taking steps to ensure that it is safe is an accident waiting to happen. Assuming that everyone is out to get you and your site, while it might seem to be a symptom of paranoia, can save you a lot of headaches if something bad were to happen.  Most of the common security problems with PHP are the result of trusting the user, so remember our first rule, never, ever trust the data you receive from your user.</p>
]]></content:encoded>
			<wfw:commentRss>http://shawnwilsher.com/archives/83/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Web Forms 2.0</title>
		<link>http://shawnwilsher.com/archives/67</link>
		<comments>http://shawnwilsher.com/archives/67#comments</comments>
		<pubDate>Thu, 10 Aug 2006 04:20:27 +0000</pubDate>
		<dc:creator>Shawn Wilsher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://shawnwilsher.com/archives/67</guid>
		<description><![CDATA[I&#8217;ve recently picked up the task of implementing part of the Web Forms 2.0 spec from the WHATWG. So far I&#8217;ve got some work done on the RepetitionEvent Model and the RepetitionElement interface. Well, my initial plans were to use XBL to implement a large portion of the code as per conversations with my co-conspirator [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently picked up the task of implementing part of the <a href="http://whatwg.org/specs/web-forms/current-work/">Web Forms 2.0</a> spec from the <a href="http://whatwg.org/">WHATWG</a>.  So far I&#8217;ve got some work done on the <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=347007">RepetitionEvent Model</a> and the <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=347070">RepetitionElement interface</a>.</p>
<p>Well, my initial plans were to use XBL to implement a large portion of the code as per conversations with my co-conspirator <a href="http://weblogs.mozillazine.org/weirdal/">Alex</a>.  Well, <a href="http://web.mit.edu/bzbarsky/www/index.shtml">bz</a> brought up an intersting point &#8211; XBL isn&#8217;t applied to elements that have a CSS property of display:none.  Well, seeing as how repetition templates are supposed to be hidden with that CSS property, I couldn&#8217;t use XBL.</p>
<p>As a result, I get to test my knowledge of C++.  Yey!  I&#8217;d like to state right now that my skills in C++ are not great.  In fact, I have very little expereice with it.  I mean, I only have had two classes in C++, and one was a very basic course.  I feel it goes without saying that I really have my work cut out for myself.</p>
<p>All is not lost, however.  There are some really useful tools that are making this so much easier.  For example, <a href="http://lxr.mozilla.org/seamonkey/">lxr</a> lets me easily look at existing code and see how things are done the &#8220;right&#8221; way.  Then, there is always a ton of documentation available on <a href="http://developer.mozilla.org/">Devmo</a>, <a href="http://www.xulplanet.com/">XUL Planet</a>, and occasionally Google comes into play.  Then of course I always have the wonderful folks on irc in #developers.  Folks like biesi, bz, and timeless have helped me countless times, and I am really greatful.</p>
<p>This is going to be a long and and winding road, but it will be very beneficial for me.  I&#8217;ve already learned a lot, and I&#8217;ve got a lot more to learn.</p>
]]></content:encoded>
			<wfw:commentRss>http://shawnwilsher.com/archives/67/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C++ Humor</title>
		<link>http://shawnwilsher.com/archives/58</link>
		<comments>http://shawnwilsher.com/archives/58#comments</comments>
		<pubDate>Sat, 15 Apr 2006 00:40:42 +0000</pubDate>
		<dc:creator>Shawn Wilsher</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://shawnwilsher.com/archives/58</guid>
		<description><![CDATA[C++ humor...]]></description>
			<content:encoded><![CDATA[<p>C++ is fun!</p>
<pre lang="cpp" line="1">#include<iostream>
using namespace std;

class Babies {};

int
main()
{
  try {
    throw Babies();
  } catch(class Babies e) {
    cout << "Congratulations.  You caught the babies." << endl;
  }
  return 0;
}</pre>
<p>I mean, come on...where else can you legally throw Babies?</p>
]]></content:encoded>
			<wfw:commentRss>http://shawnwilsher.com/archives/58/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>My First Bug</title>
		<link>http://shawnwilsher.com/archives/55</link>
		<comments>http://shawnwilsher.com/archives/55#comments</comments>
		<pubDate>Thu, 30 Mar 2006 16:24:31 +0000</pubDate>
		<dc:creator>Shawn Wilsher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Firefox]]></category>

		<guid isPermaLink="false">http://shawnwilsher.com/archives/55</guid>
		<description><![CDATA[My first bug reported for Firefox, <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=331807">bug 331807</a>.]]></description>
			<content:encoded><![CDATA[<p>That&#8217;s right!  My first bug reported for Firefox, <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=331807">bug 331807</a>.</p>
<p>The bug has to deal with a security error that isn&#8217;t an error in the current version of Firefox, but is an error in Bon Echo, the alpha release of <a href="http://www.mozilla.org/projects/firefox/roadmap.html">Firefox 2.0</a>.  It&#8217;s a good thing that I test these things, as that would have been a big monkey wrench once 2.0 came out.  I&#8217;ve found a workaround for it, but I fear that the workaround leaves the same security hole open that was patched in Bon Echo.</p>
<p>Regrettably, it also happens to be bug that kills the main feature of my most popular extension, RTSE.  It will also kill the main feature in an extension I&#8217;ve been planning to make.  Can we say &#8216;Curses&#8217; anyone?  Of course, this won&#8217;t affect Firefox 1.5.0.*, so those of you who uses the stable builds of Firefox will have nothing to worry about for some time.</p>
<p>I&#8217;ll keep updating this in the comments for anyone that is interested.</p>
]]></content:encoded>
			<wfw:commentRss>http://shawnwilsher.com/archives/55/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>The a ping Attribute</title>
		<link>http://shawnwilsher.com/archives/44</link>
		<comments>http://shawnwilsher.com/archives/44#comments</comments>
		<pubDate>Tue, 14 Feb 2006 03:30:23 +0000</pubDate>
		<dc:creator>Shawn Wilsher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Firefox]]></category>

		<guid isPermaLink="false">http://shawnwilsher.com/?p=44</guid>
		<description><![CDATA[My thoughts on the a ping attribute.]]></description>
			<content:encoded><![CDATA[<p>There has been a recent outcry from many users of Firefox when it was learned that developers implemented the <code>ping</code> attribute for the <code>a</code> element.  Personally, I think people are overreacting, and I hope to present a list of reasons why this is a good thing as opposed to the &#8216;evil&#8217; thing that many are portraying it as.</p>
<p>For background information, this attribute is spec&#8217;d out by <a href="http://www.whatwg.org/">WHATWG</a>, who, as stated on their website, are &#8220;a loose unofficial collaboration of Web browser manufacturers and interested parties who wish to develop new technologies designed to allow authors to write and deploy Applications over the World Wide Web.&#8221;</p>
<p>The <code>ping</code> attribute is supposed to allow tracking of what links a user clicks on.  Now, many might say &#8220;Woah, I don&#8217;t want my clicking to be tracked!&#8221;  However, I can assure you that many websites already do this.  The catch is that they don&#8217;t exactly inform you that they are doing it.  The idea of this spec is to make this easier for developers to do, but at the same to have User Agents (i.e. browsers) give the user options.  The whole spec can be found <a href="http://www.whatwg.org/specs/web-apps/current-work/#ping">here</a>, but in summary, the benefits of this attribute to the user are numerous.</p>
<ul>
<li>It will allow the user to see the final target location plainly</li>
<li>It will allow the user to disable the notifications without losing the underlying link functionality (many methods that currently accomplish the same goal will break if the user disables javascript)</li>
<li>It will allow the browser to send the ping when the user isn&#8217;t actively loading a page so that the target page loads faster</li>
</ul>
<p>Maybe it&#8217;s just me, but it seems to me that this is a good thing.  By making things easier for developers, they&#8217;ll probably use it (especially once a few other browsers pick it up), and by doing so give the users more control over their privacy.  It seems as though most people just pick out the fact that it is now easier for websites to track where you are going, and are completely overlooking the fact that it gives the user a lot more control.  Even with this though, I think most people probably won&#8217;t care, but will get the added security of easily knowing when they are being tracked.  For me, that is significantly better than the current situation on the web.</p>
]]></content:encoded>
			<wfw:commentRss>http://shawnwilsher.com/archives/44/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Syntax Bug, or Program Bug?</title>
		<link>http://shawnwilsher.com/archives/16</link>
		<comments>http://shawnwilsher.com/archives/16#comments</comments>
		<pubDate>Sat, 17 Sep 2005 21:07:54 +0000</pubDate>
		<dc:creator>Shawn Wilsher</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://sdwilsh.mine.nu/?p=16</guid>
		<description><![CDATA[So, while coding today I came across a very strange bug in PHP. It seems as though subtracting two arrays will return null. $_POST['value']-$var['value']=null However, if I wrap that in the floor() function, it works as expected. floor($_POST['tons']-$bot['armor_tons'])=number Go figure. Took me 30 minutes to find that annoying bug.]]></description>
			<content:encoded><![CDATA[<p>So, while coding today I came across a very strange bug in PHP.  It seems as though subtracting two arrays will return null.<br />
<textarea name="code" rows="1" cols="60" class="php:nocontrols">$_POST['value']-$var['value']=null</textarea><br />
However, if I wrap that in the floor() function, it works as expected.<br />
<textarea name="code" rows="1" cols="60" class="php:nocontrols">floor($_POST['tons']-$bot['armor_tons'])=number</textarea></p>
<p>Go figure.  Took me 30 minutes to find that annoying bug.</p>
]]></content:encoded>
			<wfw:commentRss>http://shawnwilsher.com/archives/16/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

