21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

* These are HTML tags that do not take arguments. We special-case the tag<br />

* since it takes an argument. We will allow the tag as-is, or we will allow a<br />

* closing tag (e.g., ). Additionally, we process tags in a case-<br />

* insensitive way. Only letters and numbers are allowed in tags we can allow.<br />

* Note that we do a linear search of the tags. A binary search is more<br />

* efficient (log n time instead of linear), but more complex to implement.<br />

* The efficiency hit shouldn’t matter in practice.<br />

*/<br />

static unsigned char *allowed_formatters[] = {<br />

"b", "big", "blink", "i", "s", "small", "strike", "sub", "sup", "tt", "u",<br />

"abbr", "acronym", "cite", "code", "del", "dfn", "em", "ins", "kbd", "samp",<br />

"strong", "var", "dir", "li", "dl", "dd", "dt", "menu", "ol", "ul", "hr",<br />

"br", "p", "h1", "h2", "h3", "h4", "h5", "h6", "center", "bdo", "blockquote",<br />

"nobr", "plaintext", "pre", "q", "spacer",<br />

/* include "a" here so that will work */<br />

"a"<br />

};<br />

#define SKIP_WHITESPACE(p) while (isspace(*p)) p++<br />

static int spc_is_valid_link(const char *input) {<br />

static const char *href = "href";<br />

static const char *http = "http://";<br />

int quoted_string = 0, seen_whitespace = 0;<br />

if (!isspace(*input)) return 0;<br />

SKIP_WHITESPACE(input);<br />

if (strncasecmp(href, input, strlen(href))) return 0;<br />

input += strlen(href);<br />

SKIP_WHITESPACE(input);<br />

if (*input++ != ’=’) return 0;<br />

SKIP_WHITESPACE(input);<br />

if (*input == ’"’) {<br />

quoted_string = 1;<br />

input++;<br />

}<br />

if (strncasecmp(http, input, strlen(http))) return 0;<br />

for (input += strlen(http); *input && *input != ’>’; input++) {<br />

switch (*input) {<br />

case ’.’: case ’/’: case ’-’: case ’_’:<br />

break;<br />

case ’"’:<br />

if (!quoted_string) return 0;<br />

SKIP_WHITESPACE(input);<br />

if (*input != ’>’) return 0;<br />

return 1;<br />

default:<br />

if (isspace(*input)) {<br />

if (seen_whitespace && !quoted_string) return 0;<br />

SKIP_WHITESPACE(input);<br />

seen_whitespace = 1;<br />

break;<br />

}<br />

Preventing Cross-Site Scripting | 105<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

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

Saved successfully!

Ooh no, something went wrong!