<?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>Guillermo Rauch&#039;s Devthought &#187; Server side</title>
	<atom:link href="http://www.devthought.com/category/blog/server-side/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.devthought.com</link>
	<description></description>
	<lastBuildDate>Mon, 30 Jan 2012 16:38:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Staying up with Node.JS</title>
		<link>http://www.devthought.com/2012/01/29/staying-up-with-node-js/</link>
		<comments>http://www.devthought.com/2012/01/29/staying-up-with-node-js/#comments</comments>
		<pubDate>Sun, 29 Jan 2012 15:38:41 +0000</pubDate>
		<dc:creator>Guillermo Rauch</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Server side]]></category>

		<guid isPermaLink="false">http://www.devthought.com/?p=1360</guid>
		<description><![CDATA[To many beginner Node.JS users, a fundamental and immediate apparent disadvantage of writing their web applications with Node.JS lies in the inability to save a file, refresh the browser and see their changes live. This &#8220;problem&#8221; is rooted of course &#8230; <a href="http://www.devthought.com/2012/01/29/staying-up-with-node-js/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>To many beginner Node.JS users, a fundamental and immediate apparent disadvantage of writing their web applications with Node.JS lies in the inability to save a file, refresh the browser and see their changes live.</p>
<p>This &#8220;problem&#8221; is rooted of course in significantly different architectures. In the case of, for example, PHP applications we traditionally separate the role of the <strong>web server</strong> and <strong>request handler</strong>. The monolithic web server maps incoming requests to the execution of particular files in the file system, which become our handlers.</p>
<p>In most setups, the web server is mostly limited to inspecting two pieces of a HTTP request: the <strong>resource</strong> (like <code>/test.php</code>) and the <strong>Host</strong> header (like <code>www.mydomain.com</code>) for <em>virtual hosts (vhosts)</em> support.</p>
<p><span id="more-1360"></span></p>
<pre class="highlight" lang="bash">
GET /test.php HTTP/1.1
Host: www.mydomain.com
</pre>
<p>With this in place, it&#8217;s up to the execution of the &#8220;test.php&#8221; file to handle the work and produce the response. If we change this file, even while other requests are ongoing, subsequent requests will be handled by our new code.</p>
<p>In Node.JS, this separation is not part of the core design. You write your web server and requests handlers as part of the same unit. This of course is the result of a tradeoff: Node.JS offers <em>complete control</em> over how the entire system operates, allowing programmability of aspects beyond the execution of a request. This includes for example, control over the <em>connections</em> that trigger the requests, easy awareness of other connections/requests, and other details that made the development of software like <a href="http://socket.io">Socket.IO</a> possible.</p>
<div class="annotations">
<div class="annotation" style="top: 40px; left: 300px;">^ The web server</div>
<div class="annotation" style="top: 140px; left: 200px;">^ The request handler</div>
<pre class="highlight" lang="javascript">
var server = require('http').createServer(onRequest);
server.listen(80);

function onRequest (req, res) {
  res.writeHead(200);
  res.end('Hello world');
});
</pre>
</div>
<p>The need for seamless code reloads extends into the realm of production deployment as well: one needs to be able to serve new requests with fresh code immediately, without breaking existing ones (such as file uploads or content transfer).</p>
<h2>The up solution</h2>
<p>Over at <a href="https://www.learnboost.com">LearnBoost</a>, we&#8217;ve solved these problems with two small projects: <a href="http://github.com/learnboost/distribute">distribute</a> and <a href="http://github.com/learnboost/up">up</a>.</p>
<p>In order to reload code without dropping requests, no changes to a codebase are needed other than ensuring that there&#8217;s a file that exports a <code>http.Server</code> as a module (let&#8217;s call it <code>server.js</code>).</p>
<pre class="highlight" lang="javascript">
  var server = require('express').createServer();
  // … code
  module.exports = server;
</pre>
<p>Then, during development you can leverage <a href="http://github.com/learnboost/up">up</a> from the CLI:</p>
<pre class="highlight" lang="bash">
$ up --watch --port 8080 server.js
</pre>
<p>The <code>--watch</code> flag will watch the working directory for changes. <code>up</code> will start workers to handle the requests, and reload them seamlessly. Alternatively, one can also do this programmatically. In the following example I listen on the <code>SIGUSR2</code> signal to trigger a reload, which allows for easy interoperability between components.</p>
<pre class="highlight" lang="javascript">
var up = require('up')
  , master = http.createServer().listen(8080)

var srv = up(master, __dirname + '/server');

process.on('SIGUSR2', function () {
  srv.reload();
});
</pre>
<h2>The power to distribute</h2>
<p>The server returned by <code>up</code> builds on top of another LearnBoost project called <a href="http://github.com/learnboost/distribute">distribute</a>, which leverages <a href="http://github.com/nodejitsu/node-http-proxy">node-http-proxy</a> by <a href="http://nodejitsu.com">NodeJitsu</a></p>
<p>The idea is simple: we can handle requests to be proxied in a middleware-style API. This allows us to solve, for example, the lack of multiple VHosts:</p>
<pre class="highlight" lang="javascript">
var httpServer = require('http').createServer()
  , srv = require('distribute')(httpServer);

httpServer.listen(80);

srv.use(function (req, res, next) {
  if ('blog.learnboost.com' == req.headers.host) {
    next(3500); // port 3500
  } else {
    next(3000); // otherwise port 3000
  }
});
</pre>
<p>And since it&#8217;s possible to re-use and chain multiple middleware functions together, it provides a proven foundation for code re-usability. In fact, it&#8217;s possible to leverage existing Express/Connect middleware like query string or cookie parsing, should a certain proxying function need it.</p>
<p>Head to <a href="http://github.com/learnboost">GitHub</a> for the projects, or find them on NPM as <code>distribute</code> and <code>up</code>. And stay tuned for an upcoming distribute middleware component release.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devthought.com/2012/01/29/staying-up-with-node-js/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>A tweet filtering proxy with Node.JS: Part 1</title>
		<link>http://www.devthought.com/2012/01/24/filtering-tweets-for-your-favorite-client-with-node-js/</link>
		<comments>http://www.devthought.com/2012/01/24/filtering-tweets-for-your-favorite-client-with-node-js/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 20:16:57 +0000</pubDate>
		<dc:creator>Guillermo Rauch</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Server side]]></category>

		<guid isPermaLink="false">http://www.devthought.com/?p=1304</guid>
		<description><![CDATA[We all have that guy who tweets too much about a certain topic we dislike. Or all those hardcore seasonal fans of the current NFL sensation. Or those annoying spotify/rdio links you&#8217;re never going to click! I&#8217;ve been waiting for &#8230; <a href="http://www.devthought.com/2012/01/24/filtering-tweets-for-your-favorite-client-with-node-js/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>We all have that guy who tweets too much about a certain topic we dislike. Or all those hardcore seasonal fans of the current NFL sensation. Or those annoying spotify/rdio links you&#8217;re never going to click!</p>
<p>I&#8217;ve been waiting for a while for mainstream Twitter clients to allow keyword or pattern based filtering, and when I realized people were starting to tweet about the 49ers the night before the game, I decided to finally address it myself.</p>
<p>The technique I&#8217;m going to describe consists in setting up a proxy to the Twitter API that lives in our computer, powered by Node.JS, that filters back responses based on arbitrary rules we setup. Not only does this give us the flexibility of using JavaScript, but it also means it will work with <em>any</em> Twitter client that leverages the API transparently. Yes, this includes the Twitter website!</p>
<p><span id="more-1304"></span></p>
<h2>The premises</h2>
<p>Before we begin, we must fully understand how the majority of Twitter desktop (and mobile) clients work. </p>
<ol>
<li>
<p>The app first needs to authenticate the user. Two methods can be used:</p>
<p><strong>OAuth</strong>: relies on the user going to a web page that <em>grants</em> access.<br />
  <br /><strong>XAuth</strong>: asks for user and password. This is the most practical method of authentication for a custom client, and this is the technique that <em>Twitter for Mac</em> uses.</p>
</li>
<li>Once this authorization is successful, the Twitter client will make HTTP requests to the Twitter API (located at <code>api.twitter.com</code>). In order to make these requests on <em>behalf of the user</em>, it must first sign them. To sign a request means to add an <code>Authorization</code> header that&#8217;s created with a token obtained on step 1.</li>
<li>Just like any other application that leverages the API, the Twitter client will then exchange content in some format (XML or JSON).</li>
</ol>
<h2>Our plan of action</h2>
<ol>
<li>We&#8217;ll leverage the internal DNS lookup system (<code>/etc/hosts</code>) to override <code>api.twitter.com</code> and make it point to a Node.JS server hosted locally.</li>
<li>Since the Twitter client will attempt to connect to our overridden IP over port <code>443</code>, we&#8217;ll set up a <em>&#8220;second&#8221;</em> loopback interface <code>127.0.0.2</code>. This will prevent our Twitter hack from getting in the way of your projects and apps that try to serve traffic on port <code>443</code>.</li>
<li>To bypass SSL security checks in the client, we&#8217;ll set up a new root certificate for our computer that validates <code>127.0.0.2</code> claims that it is <code>api.twitter.com</code>.</li>
<li>We&#8217;ll forward all requests to Twitter, but we&#8217;ll capture the response of the timeline one to execute our filters.</li>
<li>We&#8217;ll expose a middleware API for filters to be setup.</li>
</ol>
<h2>Forwarding api.twitter.com</h2>
<p>Redirecting <code>api.twitter.com</code> to <code>127.0.0.2</code> is as straightforward as editing <code>/etc/hosts</code>:</p>
<pre class="highlight" lang="bash">
∞ ~ sudo sh -c "echo '127.0.0.2 api.twitter.com' >> /etc/hosts"
∞ ~ tail -n 1 /etc/hosts
127.0.0.2 api.twitter.com
</pre>
<h2>There&#8217;s no place like 127.0.0.2</h2>
<p>Our plan is to capture all the HTTPS traffic going to api.twitter.com over port <code>443</code>, by forwarding it to <code>127.0.0.2</code>. To accomplish this, we must first make that IP resolve to our loopback interface.</p>
<p>On OS X, we can accomplish this by aliasing it to <code>lo0</code>:</p>
<pre class="highlight" lang="bash">
∞ ~ sudo ifconfig lo0 alias 127.0.0.2
</pre>
<p>As a result, we can now bind to port <code>443</code> over distinct IP addresses. As a test, you can run the following Node programs (with <code>sudo</code>):</p>
<pre class="highlight" lang="javascript">
require('http').createServer(function (req, res) {
  res.writeHead(200);
  res.end('welcome to 127.0.0.1:80');
}).listen(80, '127.0.0.1');
</pre>
<pre class="highlight" lang="javascript">
require('http').createServer(function (req, res) {
  res.writeHead(200);
  res.end('welcome to 127.0.0.2:80');
}).listen(80, '127.0.0.2');
</pre>
<p>Pointing your browser to <code>http://127.0.0.1</code> and <code>http://127.0.0.2</code> will yield different responses.</p>
<h2>Port 443 or port 3000?</h2>
<p>In our previous Node servers examples, binding to port <code>80</code> forced us to use <code>sudo</code>, since it&#8217;s considered a <em>privileged port</em>. We can skip this every time we need to run our server by forwarding the traffic destined to port <code>443</code> over <code>127.0.0.2</code> to the unprivileged port <code>3000</code>:</p>
<pre class="highlight" lang="javascript">
∞ ~ sudo ipfw add fwd 127.0.0.2,3000 tcp from any to 127.0.0.2 443 in
</pre>
<p>As a result, we can now run code like this (notice I run <code>listen()</code> with <code>3000</code>)</p>
<pre class="highlight" lang="javascript">
function read (file) { return require('fs').readFileSync(__dirname + '/' + file, 'utf8'); };
require('https').createServer({
    key: read('key.key')
  , cert: read('cert.crt')
}, function (req, res) {
  res.writeHead(200);
  res.end('welcome to ' + req.headers.host + ' bound to 127.0.0.2:3000');
}).listen(3000, '127.0.0.2');
</pre>
<p>But still point our browser to <code>https://api.twitter.com</code>, just like the Twitter client will do:</p>
<p><img src="http://f.cl.ly/items/460U2u0Y0f1y3J3C3Q13/Image%202012.01.22%2010:31:03%20AM.png"></p>
<p>For this example, I used self signed certificates that will trigger a security warning. You can download this example from <a href="http://cl.ly/3X3Q3D1e2I0P0v321U1n">here</a>.</p>
<h2>Hacking SSL</h2>
<p>At this point, not only will our Twitter client be unable to make any requests (since we&#8217;re not proxying them yet), but it will be refuse to establish any connection at all, due to the SSL checks:</p>
<p><img src="http://f.cl.ly/items/0j0b1T2D2E2138413e1A/Image%202012.01.22%2010:39:05%20AM.png"></p>
<p>The client is onto us! Stop SOPA!</p>
<p>Thankfully, it&#8217;s fairly for us to create a new certificate Root (like <em>VeriSign</em>), then generate a certificate request that we sign our own Twitter certificate with. If we then install the root certificate at the system level, the Twitter client and browsers will recognize it as legitimate.</p>
<p>We start by creating our root. For this, we can leverage a perl script that ships with OpenSSL. We also first create a <code>~/root</code> directory where all the generated files will be kept:</p>
<pre class="highlight" lang="bash">
 ∞ ~ mkdir ~/root
 ∞ ~ cd ~/root/
 ∞ root perl /System/Library/OpenSSL/misc/CA.pl -newca
</pre>
<p>This will ask a series of questions, most of which you can respond with the default value. Set the passphrase when asked. When it asks for a <code>Common Name</code>, set it to something easily identifiable like &#8220;Guillermo&#8221;, in case you want to remove the root certificate in the future from your system.</p>
<p>The result will be the creation of a directory called <code>demoCA</code>, which contains the root certificate database.</p>
<p>Now that we rolled our own VeriSign, we want to do what we normally do when we get real certificates for our websites. Generate a key, generate a signing request and sign it ourselves:</p>
<pre class="highlight" lang="bash">
 ∞ root openssl genrsa 1024 > api.twitter.com.key
Generating RSA private key, 1024 bit long modulus
....++++++
.......++++++
e is 65537 (0x10001)
 ∞ root openssl req -new -key api.twitter.com.key -out newreq.pem
</pre>
<p>The second command will again ask a series of questions. The only one that matters is the <code>Common Name</code>. Set it to <code>api.twitter.com</code></p>
<p>Our friend the perl script can also aid in the process of signing a request file (in this case <code>newreq.pem</code>).</p>
<pre class="highlight" lang="bash">
 ∞ root perl /System/Library/OpenSSL/misc/CA.pl -sign
</pre>
<p>The signature process is very difficult and gruesome and that&#8217;s why certificates are very expensive: type in your passcode, write <code>y</code> and press enter.</p>
<p>The signature process will yield a file <code>newcert.pem</code>, which we want to rename to <code>api.twitter.com.crt</code> to easily identify it.</p>
<p>We now have the <code>.key</code> and <code>.crt</code> files that we&#8217;ll give to our Node web server. But before we let the Twitter client attempt a connection, we install the root certificate located at <code>~/root/demoCA</code> by double-clicking it:</p>
<p><img src="http://f.cl.ly/items/3i2l17320g3p0f3Y0z1R/Image%202012.01.22%2011:31:29%20AM.png"></p>
<h2>Proxying</h2>
<p>Let&#8217;s try setting up our newly signed certificate and outputting our incoming requests.</p>
<pre class="highlight" lang="javascript">
require('https').createServer({
    key: read('api.twitter.com.key')
  , cert: read('api.twitter.com.crt')
}, function (req, res) {
  console.error(' - \033[96m%s\033[90m %s\033[39m'
    , req.method, req.url);
  res.writeHead(500);
  res.end();
}).listen(3000, '127.0.0.2');
</pre>
<p>If we run our server and relaunch Twitter for mac, we should see the requests flowing in:</p>
<p><img src="http://f.cl.ly/items/1n313Y0g1a1p0V0e0Y0h/Image%202012.01.22%2011:38:58%20AM.png"></p>
<p>Our own Twitter client now trusts us! And since we're replying with status code <code>500</code>, it reflects it:</p>
<p><img src="http://f.cl.ly/items/2c2W2h0B0f2H1Q2z3R3P/Image%202012.01.22%2011:40:00%20AM.png"></p>
<p>Our proxying strategy is as follows:</p>
<ol>
<li>We capture all requests.</li>
<li>We create new requests (using <a href="http://github.com/visionmedia/superagent">superagent</a>) that we send to twitter over their IP (since <code>api.twitter.com</code> resolves to our own computer).</li>
<li>To avoid dealing with gzip, we want to re-write the responses to not include <code>Content-Encoding</code>. Ideally we'd also re-write the request to exclude <code>Accept-Encoding</code>, but this would invalidate the OAuth signature.</li>
<li>When the response comes back, we send it back unless it targets the <code>home_timeline</code> API endpoint.</li>
<li>For timeline requests, we parse the XML, we filter or change what we want, we re-encode it and send it back. Since we're changing the response body, some headers will need to be altered, like <code>Content-Length</code>.</li>
</ol>
<h2>Re-writing</h2>
<p>To illustrate the rewriting process, consider this example in which we replace all our tweets with the text "haha":</p>
<p><img src="http://f.cl.ly/items/322j3c1q462V3z3R2K33/Image%202012.01.24%208:52:53%20PM.png"></p>
<p>The first thing we want to do is detect the requests for the <code>home_timeline.xml</code> endpoint:</p>
<pre class="highlight" lang="javascript">
// detect timeline xml request
var filter = /home_timeline\.xml/.test(req.url);
</pre>
<p>Then, we want to buffer all the request data for the incoming request:</p>
<pre class="highlight" lang="javascript">
var data = '';
req.on('data', function (chunk) { data += chunk; });
req.on('end', function () {
  // …
});
</pre>
<p>We finally create our new request, and parse the XML with the excellent <a href="http://github.com/polotek/libxmljs">libxmljs</a> by Marco Rogers.</p>
<pre class="highlight" lang="javascript">
var proxy = request[req.method.toLowerCase()]
  ('https://199.59.149.232' + req.url)

if (data) proxy.send(data);

proxy.end(function (apiRes) {
  // delete content-encoding
  delete apiRes.headers['content-encoding'];

  // parse xml
  if (200 == apiRes.status &#038;&#038; filter) {
    var doc = libxml.parseXmlString(apiRes.text);
    doc.find('//text').forEach(function (node, i) {
      node.text('haha');
    });
    proxy(doc.toString());
  } else {
    proxy(apiRes.text);
  }

  function proxy(text) {
    // rewrite content-length
    apiRes.headers['content-length'] = text.length;
    res.writeHead(apiRes.status, apiRes.headers);
    res.end(text);
  }
});
</pre>
<p>This is a slightly over-simplified example. In reality, in order for the OAuth signature to stay valid we need to proxy the headers exactly as we receive them. Node.JS unfortunately lowercases all headers, and drops the original strings. I performed the following replacement to try to restore them to their originals based on convention:</p>
<pre class="highlight" lang="javascript">
for (var i in req.headers) {
  var h = i.replace(/^[a-z]|-[a-z]/g, function (a) {
    return a.toUpperCase();
  });
  proxy.set(h, req.headers[i]);
}
</pre>
<p>In part II I&#8217;ll describe the creation of the API that lives on top of the proxy to ease on the tweet re-writing and filtering process exposed to the end user through a NPM module.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devthought.com/2012/01/24/filtering-tweets-for-your-favorite-client-with-node-js/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>An Object is not a Hash</title>
		<link>http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/</link>
		<comments>http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/#comments</comments>
		<pubDate>Wed, 18 Jan 2012 19:45:50 +0000</pubDate>
		<dc:creator>Guillermo Rauch</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Server side]]></category>

		<guid isPermaLink="false">http://www.devthought.com/?p=1279</guid>
		<description><![CDATA[Following my article A String is not an Error, I want to bring attention to an issue that similarly applies to JavaScript in general, but has special relevance in the Node.JS environment. The problem boils down to the usage of &#8230; <a href="http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Following my article <a href="http://www.devthought.com/2011/12/22/a-string-is-not-an-error/">A String is not an Error</a>, I want to bring attention to an issue that similarly applies to JavaScript in general, but has special relevance in the Node.JS environment.</p>
<p>The problem boils down to the usage of <code class="inline">{}</code> as a data-structure where the keys are supplied by untrusted user input, and the mechanisms that are normally used to assert whether a key exists.</p>
<p>Consider the example of a simple blog created with <a href="http://expressjs.com/">Express</a>. We decide to store blog posts in memory in a <code class="inline">{}</code>, indexed by the blog post slug. For example, this blog post would be <code class="inline">posts['an-object-is-not-a-hash']</code>.</p>
<p><span id="more-1279"></span></p>
<p>We start writing the route <code class="inline">/create</code>, which takes a few POST fields like &#8220;title&#8221; and &#8220;slug&#8221; and passes it to a <code class="inline">Post</code> constructor.</p>
<pre class="highlight" lang="javascript">
var posts = {};

app.post('/create', function (req, res) {
  if (req.body.title &#038;&#038; req.body.slug) {
    // avoid duplicates
    if (!posts[req.body.slug]) {
      posts[req.body.slug] = new Post(req.body);
      res.send(200);
    } else {
      res.send(500);
    }
  }
});
</pre>
<p>Our first stab for trying to avoid duplicates is checking whether the key exists in our object.</p>
<pre class="highlight" lang="javascript">
    if (!posts[req.body.slug]) {
</pre>
<p>Normally this would work fine, but let&#8217;s consider that the user could pick any of the keys that are present in <em>any JavaScript object</em> as a name:</p>
<pre class="highlight" lang="javascript">
 __defineGetter__       __defineSetter__          valueOf
 __lookupGetter__       __lookupSetter__
constructor             hasOwnProperty
isPrototypeOf           propertyIsEnumerable
toLocaleString          toString
</pre>
<p>If the user wanted to, for example, name his blog post <code>"constructor"</code> our program would behave incorrectly. We therefore change our code to leverage <code class="inline">hasOwnProperty</code>, which will allows to check whether the property has been set by us:</p>
<pre class="highlight" lang="javascript">
  if (!posts.hasOwnProperty(req.body.slug)) {
    posts[req.body.slug] = new Post(req.body);
    res.send(200);
  }
</pre>
<p>Most JavaScript programmers are already familiar with <code class="inline">hasOwnProperty</code>, since in the browser world it&#8217;s the standard way of writing libraries that work well in environments where <code class="inline">Object.prototype</code> is modified, so this addition should come to most as no surprise.</p>
<h2>The hasOwnProperty trap</h2>
<p>Our program, however, is still susceptible to potential misbehaving. Let&#8217;s say our user decides to call his blog post <code class="inline">"hasOwnProperty"</code>. The first time our check executes, everything will behave correctly, since the check will return false:</p>
<pre class="highlight" lang="javascript">
 ∞ ~ node
> var a = {};
> a.hasOwnProperty('hasOwnProperty')
false
</pre>
<p>Our code would therefore set the <code class="inline">hasOwnProperty</code> value in our object to the <code class="inline">Post</code> instance, which is an object. We can now simulate what would happen if the user tries that again:</p>
<pre class="highlight" lang="javascript">
&gt; a.hasOwnProperty = {}
{}
&gt; a.hasOwnProperty('hasOwnProperty')
TypeError: Property 'hasOwnProperty' of object #&lt;Object&gt; is not a function
    at [object Context]:1:3
</pre>
<p>As a result:</p>
<ul>
<li>Our code would throw a (potentially) uncaught exception.</li>
<li>Execution of our <code class="inline">/create</code> route would be aborted and response would not be sent to the user.</li>
<li>The request would be left hanging until it times out on both ends.</li>
</ul>
<p>The solution to this problem is to avoid relying on the &#8220;hasOwnProperty&#8221; provided by the object we&#8217;re dealing with, and use the generic one from the <code class="inline">Object.prototype</code>. If we execute it on our test subject, <code class="inline">true</code> will be returned after we set it as expected:</p>
<pre class="highlight" lang="javascript">
> Object.prototype.hasOwnProperty.call(a, 'hasOwnProperty')
true
</pre>
<h2>Conclusions</h2>
<p>The first conclusion from this experiment is that from the moment we decide to use a JavaScript object as a general-purpose hash table <strong>we can no longer rely on any of its inherited properties</strong>, specially <code class="inline">hasOwnProperty</code> (which we&#8217;re normally bound to use). This oversight, as a matter of fact, afflicted the Node.JS core <a href="https://github.com/joyent/node/issues/1707">querystring library</a> in the past.</p>
<p>If your code relies heavily on data structures where the possibility for collisions like this exist, you might want to consider having a <code class="inline">has</code> utility around:</p>
<pre class="highlight" lang="javascript">
function has (obj, key) {
  return Object.prototype.hasOwnProperty.call(obj, key);
}
</pre>
<p>And use it as follows:</p>
<pre class="highlight" lang="javascript">
  if (has(posts, req.body.slug)) { }
</pre>
<p>As a side note, you should generally stick to this method in the browser environment as well. Host objects such as <code class="inline">window</code> in older Internet Explorer versions <em>do not have</em> <code class="inline">hasOwnProperty</code>, leading to potentially inconsistent behavior in your code.</p>
<p>As correctly pointed out by different comments and tweets, another possibility surfaces in Node.JS:</p>
<pre class="highlight" lang="javascript">
  var posts = Object.create(null);
</pre>
<p>This will yield an object with a null prototype, which therefore <em>does not inherit</em> any methods, and would simplify our code to look like the original example:</p>
<pre class="highlight" lang="javascript">
    if (!posts[req.body.slug]) {}
</pre>
<p>Finally, a hashing scheme such as a prefix could do the job as well:</p>
<pre class="highlight" lang="javascript">
    if (!posts['posts-' + req.body.slug]) {
      posts['posts-' + req.body.slug] = new Post(req.body);
    }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>A String is not an Error</title>
		<link>http://www.devthought.com/2011/12/22/a-string-is-not-an-error/</link>
		<comments>http://www.devthought.com/2011/12/22/a-string-is-not-an-error/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 19:11:59 +0000</pubDate>
		<dc:creator>Guillermo Rauch</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Client side]]></category>
		<category><![CDATA[Server side]]></category>

		<guid isPermaLink="false">http://next.devthought.com/?p=1241</guid>
		<description><![CDATA[I decided to write a little article to discourage an unfortunately common pattern in Node.JS modules (and browser JavaScript, to a lesser extent) that can boil down to these two examples: // A: function myFunction () { if (somethingWrong) { &#8230; <a href="http://www.devthought.com/2011/12/22/a-string-is-not-an-error/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I decided to write a little article to discourage an unfortunately common pattern in Node.JS modules (and browser JavaScript, to a lesser extent) that can boil down to these two examples:</p>
<pre class='highlight ' lang="javascript">// A:
function myFunction () {
  if (somethingWrong) {
    throw 'This is my error'
  }
  return allGood;
}
</pre>
<p>and</p>
<pre class='highlight ' lang="javascript">// B: async Node.JS-style callback with signature `fn(err, …)`
function myFunction (callback) {
  doSomethingAsync(function () {
    // …
    if (somethingWrong) {
      callback('This is my error')
    } else {
      callback(null, …);
    }
  });
}
</pre>
<p>In both cases, passing a string instead of an error results in reduced interoperability between modules. It breaks contracts with APIs that might be performing <code>instanceof Error</code> checks, or that want to know <em>more</em> about the error.</p>
<p><span id="more-1241"></span></p>
<p><code>Error</code> objects, as we&#8217;ll see, have very interesting properties in modern JavaScript engines besides holding the message passed to the constructor.</p>
<h2>The stack property</h2>
<p>The fundamental benefit of Error objects is that they automatically keep track of where they were built and originated.</p>
<p>The mechanism of how this happens is specific to each JavaScript engine and/or browser that implements it.</p>
<h2>V8 (Node.JS)</h2>
<p>The way that V8 handles this, which directly affects us who develop in Node.JS is described by the <a href="http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi">StackTraceApi</a> document.</p>
<p>We can test its behavior by simply initializing a <code>Error</code> object and inspecting its <code>stack</code> property:</p>
<pre class='highlight ' lang="javascript">// error.js
var err = new Error();
console.log(typeof err.stack);
console.log(err.stack);</pre>
<pre class="highlight" lang="text">∞ node error.js
string
Error
    at Object.&lt;anonymous&gt; (/private/tmp/error.js:2:11)
    at Module._compile (module.js:411:26)
    at Object..js (module.js:417:10)
    at Module.load (module.js:343:31)
    at Function._load (module.js:302:12)
    at Array.0 (module.js:430:10)
    at EventEmitter._tickCallback (node.js:126:26)
</pre>
<p>As you can see, even without <code>throw</code>ing or passing it around, V8 is able to tell us exactly where that object was created, and how it got there.</p>
<p>By default, V8 limits the stack trace size to 10 frames. You can alter this by changing the <code>Error.stackTraceLimit</code> during runtime.</p>
<pre class='highlight ' lang="javascript">Error.stackTraceLimit = 0; // disables it
Error.stackTraceLimit = Infinity; // disables any limit
</pre>
<h4>Custom Errors</h4>
<p>If you wanted to extend the native <code>Error</code> object so that stack collection is preserved, you can do so by calling the <code>captureStackTrace</code> function. This is an example extracted from the <a href="http://github.com/learnboost/mongoose">Mongoose ODM</a></p>
<pre class='highlight ' lang="javascript">function MongooseError (msg) {
  Error.call(this);
  Error.captureStackTrace(this, arguments.callee);
  this.message = msg;
  this.name = 'MongooseError';
};

MongooseError.prototype.__proto__ = Error.prototype;
</pre>
<p>The second argument passed to the <code>captureStackTrace</code> prevents unnecessary noise  in the <code>stack</code> generation by hiding the <code>MongooseError</code> constructor calls from the stack.</p>
<h4>Beyond stack strings</h4>
<p>As you might have noticed in my previous code example, I purposedly printed the <code>typeof</code> of the <code>stack</code> property. As it turns out, it&#8217;s a regular String with a format optimized for readability.</p>
<p>V8, much like Java, allows complete runtime introspection of the stack. Instead of a string, we can access an array of <code>CallSite</code>s that retain as much information as possible about the function call in the stack, including the object scope (<code>this</code>).</p>
<p>In this example, we override the <code>prepareStackTrace</code> function to access this raw array and examine it. We call `getFileName` and other methods on each <code>CallSite</code> (all the available methods are described <a href="http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi#Customizing_stack_traces">here</a>)</p>
<pre class='highlight ' lang="javascript">function a () {
  b();
}

function b () {
  var err = new Error;

  Error.prepareStackTrace = function (err, stack) {
    return stack;
  };

  Error.captureStackTrace(err, b);

  err.stack.forEach(function (frame) {
    console.error(' call: %s:%d - %s'
      , frame.getFileName()
      , frame.getLineNumber()
      , frame.getFunctionName());
  });
}

a();
</pre>
<p>This is an example of the output this script produces:</p>
<pre class='highlight ' lang="javascript">∞ node error-2.js
call: /private/tmp/error-2.js:3 - a
call: /private/tmp/error-2.js:23 -
call: module.js:432 - Module._compile
call: module.js:450 - Module._extensions..js
call: module.js:351 - Module.load
call: module.js:310 - Module._load
call: module.js:470 - Module.runMain
call: node.js:192 - startup.processNextTick.process._tickCallback
</pre>
<p>All this functionality would of course be non-existent if we were to pass around strings, therefore drastically shrinking our debugging panorama.</p>
<p>For real-world usage, restoring the original <code>prepareStackTrace</code> after overriding it is probably a good idea. Thankfully, TJ Holowaychuck has released a tiny <a href="http://github.com/visionmedia/callsite">callsite</a> module to make this painless.</p>
<h2>Browsers</h2>
<p>Like the V8 document states:</p>
<blockquote>
<p><em>The API described here is specific to V8 and is not supported by any other JavaScript implementations. Most implementations do provide an error.stack property but the format of the stack trace is likely to be different from the format described here</em></p>
</blockquote>
<ul>
<li>Firefox exposes <code>error.stack</code>. It has its own format.</li>
<li>Opera exposes <code>error.stacktrace</code>. It has its own format.</li>
<li>IE has no stack.</li>
</ul>
<p>A very interesting solution to this is provided by <a href="https://github.com/eriwen/javascript-stacktrace">javascript-stacktrace</a>, which attempts to normalize a printed stack trace across all browsers.</p>
<p>On IE (and older versions of Safari), for example, it uses a clever method: it recursively looks for the <code>caller</code> property of a function, calls <code>toString</code> on it and parses out the function name.</p>
<pre class='highlight ' lang="javascript">var currentFunction = arguments.callee.caller;
while (currentFunction) {
  var fn = currentFunction.toString();
  var fname = fn.substring(fn.indexOf(&amp;amp;quot;function&amp;amp;quot;) + 8, fn.indexOf('')) || 'anonymous';
  callstack.push(fname);
  currentFunction = currentFunction.caller;
}
</pre>
<h2>Conclusions</h2>
<ul>
<li>When doing async I/O (in Node.JS or otherwise), since <code>throw</code>ing is not a<br />
possibility (as it results in uncaught exceptions), <code>Error</code>s are the only way to<br />
allow proper stack trace collection.</li>
<li>Even in non-V8 browser environments, it&#8217;s probably a good idea to still initialize<br />
<code>Error</code>s. The API exists in all browsers, and the extended API facilities that V8<br />
provides are bound to be available to most engines in the future.</li>
<li>If you&#8217;re throwing or passing around strings for errors, consider switching today!</li>
</ul>
<p>The examples in the beginning of the post can thus be rewritten this way:</p>
<pre class='highlight ' lang="javascript">// A:
function myFunction () {
  if (somethingWrong) {
    throw new Error('This is my error')
  }
  return allGood;
}
</pre>
<p>and</p>
<pre class='highlight ' lang="javascript">// B: async Node.JS-style callback with signature `fn(err, …)`
function myFunction (callback) {
  doSomethingAsync(function () {
    // …
    if (somethingWrong) {
      callback(new Error('This is my error'))
    } else {
      callback(null, …);
    }
  });
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.devthought.com/2011/12/22/a-string-is-not-an-error/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Accessing a MySQL database from Node.JS</title>
		<link>http://www.devthought.com/2009/12/07/accessing-a-mysql-database-from-nodejs/</link>
		<comments>http://www.devthought.com/2009/12/07/accessing-a-mysql-database-from-nodejs/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 10:15:44 +0000</pubDate>
		<dc:creator>Guillermo Rauch</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Server side]]></category>
		<category><![CDATA[dbslayer]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[node.js]]></category>

		<guid isPermaLink="false">http://devthought.com/?p=1108</guid>
		<description><![CDATA[I just pushed node.dbslayer.js to GitHub, a very basic and easy-to-use interface to DBSlayer. What is DBSlayer? The DBacesslayer aka DBSlayer is a lightweight database abstraction layer suitable for high-load websites where you need the scalable advantages of connection pooling. &#8230; <a href="http://www.devthought.com/2009/12/07/accessing-a-mysql-database-from-nodejs/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I just pushed <a href="http://github.com/Guille/node.dbslayer.js">node.dbslayer.js</a> to GitHub, a very basic and easy-to-use interface to DBSlayer.</p>
<p>What is <a href="http://code.nytimes.com/projects/dbslayer/wiki">DBSlayer</a>?</p>
<blockquote class="quotebox"><div>The DBacesslayer aka DBSlayer is a lightweight database abstraction layer suitable for high-load websites where you need the scalable advantages of connection pooling. Written in C for speed, DBSlayer talks to clients via JSON over HTTP, meaning it&#8217;s simple to monitor and can swiftly interoperate with any web framework you choose.</div>
</blockquote>
<p>Developed by the New York Times to distribute the load of their MySQL servers, DBSlayer allows us to perform queries in a non-blocking way. Running a SQL query is as simple as:</p>
<pre class='highlight ' lang="JavaScript">
connection.query("SELECT * FROM TABLE").addCallback(function(result){
	for (var i = 0, l = result.ROWS.length; i < l; i++){
		var row = result.ROWS[i];
		// do something with the data
	}
});
</pre>
<p>Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devthought.com/2009/12/07/accessing-a-mysql-database-from-nodejs/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>On the subject of login forms security</title>
		<link>http://www.devthought.com/2009/09/05/on-the-subject-of-login-forms-security/</link>
		<comments>http://www.devthought.com/2009/09/05/on-the-subject-of-login-forms-security/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 05:14:26 +0000</pubDate>
		<dc:creator>Guillermo Rauch</dc:creator>
				<category><![CDATA[Server side]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[ssl]]></category>

		<guid isPermaLink="false">http://devthought.com/?p=523</guid>
		<description><![CDATA[This post has a twofold goal. Make users aware of the implications of non-secure login forms is one. The other and most important one, given the nature of this blog&#8217;s audience, is make website makers and webmasters aware of this &#8230; <a href="http://www.devthought.com/2009/09/05/on-the-subject-of-login-forms-security/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This post has a twofold goal. Make users aware of the implications of non-secure login forms is one. The other and most important one, given the nature of this blog&#8217;s audience, is make website makers and webmasters aware of this problem, and how easy they can solve it.</p>
<p>I&#8217;ll also examine the alternative of implementing OpenID as the sole login provider of a website, and how it becomes the most suitable option for the majority of scenarios today.</p>
<p><span id="more-523"></span></p>
<h3>An introduction</h3>
<p>In this article we&#8217;ll consider a login form or website <em>non-secure</em> when sensitive data (for example, but not limited to, emails, passwords, private messages, credit cards information) is transmitted across a digital network without a strong encryption mechanism in place. In the majority of cases, this means transmitting data from one end to the other in <a href="http://en.wikipedia.org/wiki/Plaintext">Plaintext</a>, using the HTTP protocol instead of HTTPS.</p>
<h3>A reality check: for users</h3>
<p>One of the first concerns for <em>users</em> of non-secure websites should be network sniffing. With an utility such as <a href="http://wireshark.com">Wireshark</a> (formerly known as ethereal), an attacker can easily intercept and read packages that contain plaintext data.</p>
<p>This could be anything from chat conversations, or that password you&#8217;re sending to a non-SSL site. When you sign up for a website that doesn&#8217;t implement <em>https</em> remember to use a new, unique and discardable password. Never use the same password more than once!</p>
<h3>A reality check: for webmasters</h3>
<p>With open wireless telecommunication becoming more popular every day, network eavesdropping gets easier and more appealing for attackers. Websites that do not protect their users data will become easy targets for electronic identity theft and other opportunistic crimes.</p>
<p>Most login forms we find today offer no SSL support for sensitive data transmissions. Just browse to your favorite startup or Twitter mashup to see how much they protect your information. Even extremely popular websites such as <a href="http://digg.com/login/">Digg</a>, <a href="http://slashdot.org">Slashdot</a>, <a href="http://stumbleupon.com">StumbleUpon</a>, <a href="http://www.hi5.com/friend/login.do">Hi5</a>, <a href="http://www.reddit.com/login?dest=%2F">Reddit</a>, <a href="http://justin.tv">Justin.TV</a>, <a href="http://www.washingtonpost.com/">Washington Post</a>, <a href="http://members.forbes.com/membership/editprofile.do">Forbes</a>, <a href="http://xanga.com">Xanga</a> fail to protect your authentication information.</p>
<p>All it takes to fix your login forms is to add support for the HTTPS protocol to your web server of choice. Most popular web hosting providers already support it, yet few people take advantage of it.</p>
<p>One of the reasons people are reluctant to build-in SSL is the requirement of a certificate signed by a trusted authority. But it&#8217;s a really <a href="https://www.godaddy.com/gdshop/ssl/ssl.asp">small</a> price, that pays back with a lot of benefits for your organization.</p>
<h3>The JavaScript alternative</h3>
<p>You can make your non-secure forms a little more secure by creating a <a href="http://pajhome.org.uk/crypt/md5/sha1src.html">SHA1</a> hash before the form is sent, instead of sending the plaintext password.</p>
<p>However, this technique, which is implemented by <a href="http://vbulletin.com">vBulletin</a> for example, has its downsides:</p>
<ul>
<li>Only makes it more secure for Javascript-enabled user agents</li>
<li>It&#8217;s not an encryption method, so it&#8217;ll only be useful for login forms, and not for emails, private messages, credit cards, etc</li>
</ul>
<h3>The OpenID Alternative</h3>
<p>The <em>OpenID Authentication 2.0</em> specification takes into consideration the security threats described previously, and the suggested solution is evident:</p>
<blockquote class="quotebox"><div>While the protocol does not require SSL be used, its use is strongly RECOMMENDED.  Current best practices dictate that an OP SHOULD use SSL, with a certificate signed by a trusted authority, to secure its Endpoint URL as well as the interactions with the end user&#8217;s User-Agent.</div>
</blockquote>
<p>While it&#8217;s clear that SSL is optional in the authentication transactions, the following sentence is of interest to us:</p>
<blockquote class="quotebox"><div>Following its own security policies, a Relying Party MAY choose to not complete, or even begin, a transaction if SSL is not being correctly used at these various endpoints.</div>
</blockquote>
<p>If OpenID is implemented, it&#8217;s almost guaranteed for the website owner that the user&#8217;s login information will be protected. The main reason is that the majority of <em>popular</em> OpenID providers respect the security considerations listed in the specification. The attacker can still intercept authentication data if you don&#8217;t have SSL in the callback page that OpenId forwards the user to, but all he could potentially get is a random token and not the password itself.</p>
<h3>Conclusion</h3>
<p>The potential security issues of websites form authentication are varied, and encryption is only a tiny part of the solution. Securing users login should be one of the top priorities in web makers agendas. People value the data and history they&#8217;ve created in the websites they like, and it can be pretty disappointing to lose it all because the creator decided it was worth investing more time in refining the website&#8217;s gradient backgrounds rather than implementing SSL.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devthought.com/2009/09/05/on-the-subject-of-login-forms-security/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Sending email with Symfony 1.2 and Swift 4</title>
		<link>http://www.devthought.com/2009/05/31/sending-email-with-symfony-12-and-swift-4/</link>
		<comments>http://www.devthought.com/2009/05/31/sending-email-with-symfony-12-and-swift-4/#comments</comments>
		<pubDate>Sun, 31 May 2009 18:11:48 +0000</pubDate>
		<dc:creator>Guillermo Rauch</dc:creator>
				<category><![CDATA[Server side]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[smtp]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://devthought.com/?p=971</guid>
		<description><![CDATA[If you had used Symfony 1.2 with Swift 3 before, you probably sent emails like this: try { $mailer = new Swift(new Swift_Connection_NativeMail()); $message = new Swift_Message('The subject', 'The body html', 'text/html'); $mailer->send($message, 'to@user.com', 'noreply@company.com'); $mailer->disconnect(); } catch (Exception $e) &#8230; <a href="http://www.devthought.com/2009/05/31/sending-email-with-symfony-12-and-swift-4/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you had used Symfony 1.2 with Swift 3 before, you probably sent emails like this:</p>
<pre class='highlight ' lang="php">
try
{
  $mailer = new Swift(new Swift_Connection_NativeMail());
  $message = new Swift_Message('The subject', 'The body <b>html</b>', 'text/html');
  $mailer->send($message, 'to@user.com', 'noreply@company.com');
  $mailer->disconnect();
}
catch (Exception $e)
{
  $mailer->disconnect();
}
</pre>
<p>However, due to API changes in Swift 4, that would have thrown an error:</p>
<pre class='highlight ' lang="bash">
Fatal error: Cannot instantiate abstract class Swift in /[...]/apps/frontend/modules/[...]/actions/actions.class.php on line 40
</pre>
<p>It turns out that now the class <code class="inline">Swift</code> is defined as <em>abstract</em>, which means it can&#8217;t be instantiated. Now, classes that you used to instantiate directly, like <code class="inline">Swift_Message</code>, have been added factory methods called <code class="inline">newInstance</code></p>
<pre class='highlight ' lang="php">
require_once('lib/vendor/swift/swift_init.php'); # needed due to symfony autoloader
$mailer = Swift_Mailer::newInstance(Swift_MailTransport::newInstance());
$message = Swift_Message::newInstance('The subject')
         ->setFrom(array('noreply@company.com' => 'Mailer Name'))
         ->setTo(array('email@email.com' => 'Name Lastname'))
         ->setBody('The body <b>html</b>', 'text/html');
$mailer->send($message);
</pre>
<p>Remember that it&#8217;s good practise to get the body HTML from a partial, instead of hardcoding it into the action.</p>
<pre class='highlight ' lang="php">
$mailBody = $this->getPartial('emailPartial', array(/* parameters */));
// ...
->setBody($mailBody, 'text/html');
</pre>
<p>The code has become a lot more readable, easier to customize and write.<br />
More information <a href="http://swiftmailer.org/docs/message-quickref">here</a> and <a href="http://swiftmailer.org/docs/sending-quickref">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devthought.com/2009/05/31/sending-email-with-symfony-12-and-swift-4/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>PHP URL Shortening Class released</title>
		<link>http://www.devthought.com/2009/04/24/php-url-shortening-class-released/</link>
		<comments>http://www.devthought.com/2009/04/24/php-url-shortening-class-released/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 16:27:34 +0000</pubDate>
		<dc:creator>Guillermo Rauch</dc:creator>
				<category><![CDATA[Server side]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[release]]></category>

		<guid isPermaLink="false">http://devthought.com/?p=921</guid>
		<description><![CDATA[I&#8217;ve just released PHPShortener, a PHP class that makes it very easy to encode/decode URLs with services such as tr.im, is.gd, and others. Encoding and decoding is a simple as this: $s = new PHPShortener(); // encode a long url &#8230; <a href="http://www.devthought.com/2009/04/24/php-url-shortening-class-released/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just released <a href="/projects/php/phpshortener/">PHPShortener</a>, a PHP class that makes it very easy to encode/decode URLs with services such as tr.im, is.gd, and others.</p>
<p>Encoding and decoding is a simple as this:</p>
<pre class='highlight ' lang="php">
$s = new PHPShortener();
// encode a long url
$shorturl = $s->encode('http://devthought.com/projects/php/phpshortener/', 'is.gd');
// decode a short url (autodetects the service)
$longurl = $s->decode('http://tr.im/jBBp');
</pre>
<p>Head to the <a href="/projects/php/phpshortener/">project page</a> for downloads and documentation. Fork me on <a href="http://github.com/Guille/phpshortener/tree/master">GitHub</a> if you want to contribute!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devthought.com/2009/04/24/php-url-shortening-class-released/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>The Digg worm that wasn&#039;t</title>
		<link>http://www.devthought.com/2009/04/18/the-digg-worm-that-wasnt/</link>
		<comments>http://www.devthought.com/2009/04/18/the-digg-worm-that-wasnt/#comments</comments>
		<pubDate>Sat, 18 Apr 2009 16:55:16 +0000</pubDate>
		<dc:creator>Guillermo Rauch</dc:creator>
				<category><![CDATA[Server side]]></category>
		<category><![CDATA[digg]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://devthought.com/?p=827</guid>
		<description><![CDATA[Two nights ago I was editing my Digg profile and couldn&#8217;t help but think about the recent Mikeyy and Twitter revolt. Within minutes I had found a XSS exploit that could theoretically allow me to achieve the same. Half an &#8230; <a href="http://www.devthought.com/2009/04/18/the-digg-worm-that-wasnt/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Two nights ago I was editing my Digg profile and couldn&#8217;t help but think about the recent Mikeyy and Twitter revolt. Within minutes I had found a XSS exploit that could theoretically allow me to achieve the same.</p>
<p>Half an hour later, I had a working worm ready to infect everyone that saw my profile page, which also would propagate to theirs.</p>
<p><span id="more-827"></span></p>
<h3>Locating the vulnerability</h3>
<p>XSS vulnerabilities are quite ironic nowadays. Everyone knows about them, almost everyone knows what their most frequent form is, but few seem to test for them or implement systems to avoid them.</p>
<p>The most common form of a XSS exploit scenario is that in which the attacker manages to store malicious code in the host, which when shown is not escaped properly and is thus executed. On Digg, all it took me was to insert a <code class="inline">&lt;script&gt;</code> tag in the &#8220;About me&#8221; textarea.</p>
<p>Less known forms of XSS exploits involve other pathways through which the user sends information:</p>
<ul>
<li><strong>Request headers</strong>. A malicious user, like Al Capone or Mikeyy, could alter the <em>User Agent</em> or <em>Referer</em>, for example, to store malicious code. </li>
<li><strong>Query string</strong>. Commonly known as the PHP_SELF bug, the vulnerable sites are those which print out the request URL without escaping it. For example, if a programmer decides to populate the a <code class="inline">&lt;form action=""&gt;</code> with the unsanitized request URL, an attacker could inject code that can be conveniently distributed with URL shortening services.</li>
</ul>
<p>It&#8217;s also important to remember that preventing XSS exploits is not about escaping <code class="inline">&lt;</code> or <code class="inline">&gt;</code> alone. I recommend checking out <a href="http://ha.ckers.org/xss.html">this cheatsheet</a> for a comprehensive list of possible, real-world attacks.</p>
<h3>Exploiting it</h3>
<p>The exploit code is only anecdotally interesting. I do not encourage anyone to start testing random websites and injecting harmful JavaScript.</p>
<p>The attack plan is as follows</p>
<ol>
<li>
<p>Check the user is logged in, by checking for the absence of a login link.</p>
<pre class='highlight ' lang="javascript">
if ($('#header-login').attr('href')) return;
	</pre>
</li>
<li>
<p>Propagate the script by retrieving the profile edit details form and ajaxly submitting it.</p>
<pre class='highlight ' lang="javascript">
$.ajax({url: 'http://digg.com/settings/about', dataType: 'html', success: function(doc){
	// ...
	$.ajax({url: 'http://digg.com/settings/about', data: form.serialize(), type: 'POST'});
	// ...
}});
	</pre>
</li>
<li>We disable the Digg Bar by posting to http://digg.com/settings/viewing. This time we don&#8217;t really need to retrieve the whole form, because we already have the magic token that prevents CSRF attacks</li>
<li>We shout to friends to check out our profile. When a friend is infected, the <code class="inline">&lt;script&gt;</code> tag also holds the information of the scripter, so that the victim doesn&#8217;t shout back, which could raise suspicions that something fishy is going on.</li>
<li>
<p>We also store the created shouts ids in a cookie, to hide them from the user view as soon as the script loads and avoid users seeing weird shouts they didn&#8217;t voluntarily send.</p>
<pre class='highlight ' lang="javascript">
var shouts = $.cookie('shouts'), shoutedTo = [], shoutIds = [];
if (shouts) {
	shouts = shouts.split(',');
	$(shouts).each(function(i, code){
		code = code.split('|');
		shoutIds.push(code[0]); shoutedTo.push(code[1]);
		document.write('
<style>#shout-' + code[0] + '{ display: none; }</style>

');
	});
	document.write('
<style>#shout-' + shoutIds.sort().reverse().join('-') + '{ display: none; }</style>

');
}
	</pre>
</li>
</ol>
<p>Additionally, I set up a callback page and a very basic form of cross-domain requests (using images) ((Keep in mind that, for security reasons, XMLHttpRequest would not allow me to communicate with my server from digg.com. I thus used (new Image).src to ping the script I set up.)) to inform me of the success of the different stages of the worm (infection, bar disabling, propagation) for each user.</p>
<p>Check out the full script <a href="/wp-content/articles/dugg/dugg.php.txt">here</a>.</p>
<h3>Preventing it</h3>
<p>First of all, it&#8217;s important to constantly keep in mind that user-supplied information should be untrusted. Whenever you input or output information that was originated by a 3rd party, extra caution should be taken.</p>
<p>For PHP developers, preventing XSS exploits usually means passing all user-supplied info through the <code class="inline">htmlentities()</code> function. The caution part is not a cliche, or a meaningless recommendation. It might be obvious for you, as a developer, to escape <code class="inline">$_POST['value']</code> but escaping <code class="inline">$_SERVER['HTTP_USER_AGENT']</code> takes undoubtedly more attention.</p>
<p>The Symfony framework provides one of the arguably most elegant solutions to this problem, with its built-in XSS protection, virtually transparent to the developer. Learn more about <a href="http://www.symfony-project.org/book/1_2/07-Inside-the-View-Layer#chapter_07_output-escaping">this method</a>.</p>
<h3>Conclusion</h3>
<p>I enabled the worm on Digg to try it out with my friends, and it quickly began to spread. After succeeding and collecting about 20 users, I quickly commented it out and let the Digg crew know. Jen Burton, the community manager, responded really quickly, and within an hour or two ((Unlike Twitter)) they had fixed the bug and updated the production site.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devthought.com/2009/04/18/the-digg-worm-that-wasnt/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Symfony scaling: moving your uploads to a NFS share</title>
		<link>http://www.devthought.com/2009/04/01/symfony-scaling-moving-your-uploads-to-a-nfs-share/</link>
		<comments>http://www.devthought.com/2009/04/01/symfony-scaling-moving-your-uploads-to-a-nfs-share/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 21:04:07 +0000</pubDate>
		<dc:creator>Guillermo Rauch</dc:creator>
				<category><![CDATA[Server side]]></category>
		<category><![CDATA[nfs]]></category>
		<category><![CDATA[scaling]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://devthought.com/?p=655</guid>
		<description><![CDATA[A few weeks back a client relaunched a Facebook App that commended me to rewrite. The growth since then has been tremendous, with over 2000 registered users everyday, who upload pictures, make friends and comment on other users&#8217; profiles. My &#8230; <a href="http://www.devthought.com/2009/04/01/symfony-scaling-moving-your-uploads-to-a-nfs-share/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A few weeks back a client relaunched a Facebook App that commended me to rewrite. The growth since then has been tremendous, with over 2000 registered users everyday, who upload pictures, make friends and comment on other users&#8217; profiles. My application has been able to handle that growth perfectly in terms of responsiveness, but I was told that if the growing rate persisted, within only a few days the content would exceed the hard drive capacity.</p>
<p>The natural solution for these scenarios is a <a href="http://en.wikipedia.org/wiki/Network_File_System">NFS</a> mount, so that the content can be distributed across a network, which gives your application virtually limitless storage capacity. <a href="http://joyent.com">Joyent</a> provides this service reliably.</p>
<p>The biggest challenge was to minimize the downtime, so these were the steps we took:</p>
<ol>
<li>Move all current files to the NFS mount to clear up disk space</li>
<li>
<p>Override the <strong>sf_upload_dir</strong> configuration directive. In settings.yml</p>
<pre class='highlight ' lang="xml">
all:
       upload_dir:      /nfsmount/site/uploads
</pre>
</li>
<li>
<p>Adjust the Apache VHost configuration with an alias that points outside the DocumentRoot (hence the need for a special <code class="inline">&lt;Directory&gt;</code>)</p>
<pre class='highlight ' lang="php">
Alias /uploads /nfsmount/site/uploads
<directory "/nfsmount/site/uploads">
       Order allow,deny
       Allow from all
</directory>
</pre>
</li>
<li>Clear symfony cache and reload Apache. If your application is well-written, the change should be transparent to all the modules that deal with file uploads.</p>
<li>Use <code class="inline">cp -uvR</code> to make sure no images were left out from the initial copy.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.devthought.com/2009/04/01/symfony-scaling-moving-your-uploads-to-a-nfs-share/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

