Search code examples
javascriptsecuritygetwebmatrixurl-encoding

Should I worry that using GET in a form element doesn't automatically URL-encode angle brackets?


So I decided to use GET in my form element, point it to my cshtml page, and found (as expected) that it automatically URL encodes any passed form values.

I then, however, decided to test if it encodes angle brackets and surprisingly found that it did not when the WebMatrix validator threw a server error warning me about a potentially dangerous value being passed.

I said to myself, "Okay, then I guess I'll use Request.Unvalidated["searchText"] instead of Request.QueryString["searchText"]. Then, as any smart developer who uses Request.Unvalidated does, I tried to make sure that I was being extra careful, but I honestly don't know much about inserting JavaScript into URLs so I am not sure if I should worry about this or not. I have noticed that it encodes apostrophes, quotations, parenthesis, and many other JavaScript special characters (actually, I'm not even sure if an angle bracket even has special meaning in JavaScript OR URLs, but it probably does in one, if not both. I know it helps denote a List in C#, but in any event you can write script tags with it if you could find a way to get it on the HTML page, so I guess that's why WebMatrix's validator screams at me when it sees them).

Should I find another way to submit this form, whereas I can intercept and encode the user data myself, or is it okay to use Request.Unvalidated in this instance without any sense of worry?

Please note, as you have probably already noticed, my question comes from a WebMatrix C#.net environment.

Bonus question (if you feel like saving me some time and you already know the answer off the top of your head): If I use Request.Unvalidated will I have to URL-decode the value, or does it do that automatically like Request.QueryString does?

---------------------------UPDATE----------------------------

Since I know I want neither a YSOD nor a custom error page to appear simply because a user included angle brackets in their "searchText", I know I have to use Request.Unvalidated either way, and I know I can encode whatever I want once the value reaches the cshtml page.

So I guess the question really becomes: Should I worry about possible XSS attacks (or any other threat for that matter) inside the URL based on angle brackets alone?

Also, in case this is relevant:

Actually, the value I am using (i.e. "searchText") goes straight to a cshtml page where the value is ran through a (rather complex) SQL query that queries many tables in a database (using both JOINS and UNIONS, as well as Aliases and function-based calculations) to determine the number of matches found against "searchText" in each applicable field. Then I remember the page locations of all of these matches, determine a search results order based on relevance (determined by type and number of matches found) and finally use C# to write the search results (as links, of course) to a page.

And I guess it is important to note that the database values could easily contain angle brackets. I know it's safe so far (thanks to HTML encoding), but I suppose it may not be necessary to actually "search" against them. I am confused as to how to proceed to maximum security and functional expecations, but if I choose one way or the other, I may not know I chose the wrong decision until it is much too late...


Solution

  • URL and special caracters

    The url http://test.com/?param="><script>alert('xss')</script> is "benign" until it is read and ..

    1. print in a template : Hello @param. (Potential reflected/persisted XSS)

    2. or use in Javascript : divContent.innerHTML = '<a href="' + window.location.href + ... (Potential DOM XSS)

    Otherwise, the browser doesn't evaluate the query string as html/script.

    Request.Unvalidated/Request.QueryString

    You should use Request.Unvalidated["searchText"] if you are expecting to receive special caracters. For example : <b>User content</b><p>Some text...</p>

    If your application is working as expected with QueryString["searchText"], you should keep it since it validate for potential XSS.

    Ref: http://msdn.microsoft.com/en-us/library/system.web.httprequest.unvalidated.aspx