09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 9 ■ HTTP<br />

So the techniques to attack sites have evolved and mutated. Today, would-be attackers find ways<br />

around this policy by using a constellation <strong>of</strong> attacks called cross-site scripting (known by the acronym<br />

XSS to prevent confusion with Cascading Style Sheets). These techniques include things like finding the<br />

fields on a web page where the site will include snippets <strong>of</strong> user-provided data without properly<br />

escaping them, and then figuring out how to craft a snippet <strong>of</strong> data that will perform some<br />

compromising action on behalf <strong>of</strong> the user or send private information to a third party. Next, the wouldbe<br />

attackers release a link or code containing that snippet onto a popular web site, bulletin board, or in<br />

spam e-mails, hoping that thousands <strong>of</strong> people will click and inadvertently assist in their attack against<br />

the site.<br />

There are a collection <strong>of</strong> techniques that are important for avoiding cross-site scripting; you can find<br />

them in any good reference on web development. The most important ones include the following:<br />

• When processing a form that is supposed to submit a POST request, always<br />

carefully disregard any GET parameters.<br />

• Never support URLs that produce some side effect or perform some action simply<br />

through being the subject <strong>of</strong> a GET.<br />

• In every form, include not only the obvious information—such as a dollar amount<br />

and destination account number for bank transfers—but also a hidden field with a<br />

secret value that must match for the submission to be valid. That way, random<br />

POST requests that attackers generate with the dollar amount and destination<br />

account number will not work because they will lack the secret that would make<br />

the submission valid.<br />

While the possibilities for XSS are not, strictly speaking, problems or issues with the HTTP protocol<br />

itself, it helps to have a solid understanding <strong>of</strong> them when you are trying to write any program that<br />

operates safely on the World Wide Web.<br />

WebOb<br />

We have seen that HTTP requests and responses are each represented by ad-hoc objects in urllib2.<br />

Many <strong>Python</strong> programmers find its interface unwieldy, as well as incomplete! But, in their defense, the<br />

objects seem to have been created as minimal constructs, containing only what urllib2 needed to<br />

function.<br />

But a library called WebOb is also available for <strong>Python</strong> (and listed on the <strong>Python</strong> Package Index) that<br />

contains HTTP request and response classes that were designed from the other direction: that is, they<br />

were intended all along as general-purpose representations <strong>of</strong> HTTP in all <strong>of</strong> its low-level details. You<br />

can learn more about them at the WebOb project web page: http://pythonpaste.org/webob/<br />

This library’s objects are specifically designed to interface well with WSGI, which makes them useful<br />

when writing HTTP servers, as we will see in Chapter 11.<br />

Summary<br />

The HTTP protocol sounds simple enough: each request names a document (which can be an image or<br />

program or whatever), and responses are supposed to supply its content. But the reality, <strong>of</strong> course, is<br />

rather more complicated, as its main features to support the modern Web have driven its specification,<br />

RFC 2616, to nearly 60,000 words. In this chapter, we tried to capture its essence in around 10,000 words<br />

and obviously had to leave things out. Along the way, we discussed (and showed sample <strong>Python</strong> code)<br />

for the following concepts:<br />

161

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!