Re: Escape html tags and other dangerous input

From: Nicole Calinoiu (ngcalinoiu)
Date: 09/30/04


Date: Thu, 30 Sep 2004 10:35:09 -0400

If this is an ASP.NET application, you can take advantage of the UrlEncode
and HtmlEncode methods of the System.Web.HttpServerUtilityClass. Some
methods of some ASP.NET controls will HTML-encode for you, but the
System.Web.UI.WebControls.HyperLink control doesn't. Assuming you are using
the HyperLink control to display the link, you should URL-encode the hobby
value and HTML-encode the display text. e.g. (assuming that you've already
verified that the hobbyName string is not null):

yourHyperLink.NavigateUrl = "http://www.domain.com/search.aspx?hobby=" +
Server.UrlEncode(hobbyName);
yourHyperLink.Text = Server.HtmlEncode(hobbyName);

In the search page, you can simply read the value from the query string
items collection since it will already have been URL-decoded by the
underlying .NET objects. e.g.:

hobbyName = Request.QueryString["hobby"];

Of course, if you are going to display this value to the user in the search
page, you should URL- or HTML-encode it, as appropriate.

Also, you should keep in mind that, while validation of the user-provided
values is certainly a good idea, it is highly unlikely to eliminate all
display problems (particularly those involving client-side encoding of
non-low ASCII characters) unless it is sufficiently strict to potentially
interfere with intended functionality. Even if your initial validation
screens out all characters that could potentially interfere with HTML
rendering or functionality, later changes may re-introduce the acceptance of
such characters, and this will cause problems unless the application can
accomodate them via appropriate render-time encoding. In addition, the web
user interface is rarely the only mechanism via which data can be provided
(e.g.: direct entry into the underlying db or files is usually possible),
and your code should probably accomodate even innocent mistakes at that
level.

HTH,
Nicole

"Shabam" <blislecp@hotmail.com> wrote in message
news:1_CdnUexgrlxtsTcRVn-rw@adelphia.com...
>I have an application that stores user input via a text box. The text box
> lets user enter their hobby, which can then be viewed in their page by
> others. This hobby is linked by the application so that it searches the
> database for other users who have the same hobby. The search string is
> displayed as:
> WHATEVER
>
> The problem comes when the users inputs something invalid like "&". This
> is
> obviously interpreted by the server as another variable, and can mess
> things
> up when a viewing user clicks on the link, since the search engine does
> take
> on other fields too.
>
> Another would be if the user tries to inject html tags like "><THEIR
> HTML>".
> This would mess up the remainder of the page obviously.
>
> What is the best way to filter out such nasty input? There may also be
> others that I'm unaware of. I'm taking care of SQL injection by using a
> parametized sql queries.
>
> Are there library functions I can just use or do I have to do this
> manually?
>
>
>



Relevant Pages

  • Re: Validating ComboBox Entries
    ... To prevent a user from entering any data, it can't be in the rowsource. ... still need to see it in the employees historical entries. ... the project name is displayed in the Combo control. ... I would have to display a Message Box or something similar to ...
    (microsoft.public.access.formscoding)
  • RE: VS2005 DataList with Nested GridView using ObjectDataSource
    ... you're wantting to display some data on the ASP.NET ... web page through the DataList control, and since those data records can ... category items in the DataList and in each DataList item, use a GridView ... | customer number in a grid (call it QUERY A). ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: Synchronizing the Main form and six (6) subforms?
    ... you're going to need to control which controls/which subform to ... display. ... > shipment to the correct main record. ...
    (microsoft.public.access.formscoding)
  • Object creation
    ... I thought that I could easily copy a single Excel file from ... I've built my userform and got some of the code running. ... display the link names accordingly. ... range of 3 columns and then show that in my control. ...
    (microsoft.public.excel.programming)
  • Re: Can I load a picture form an URL into a Picturebox?
    ... :>> control is based on IE, ... I am telling the control what to display, ... Only if they know it's a web browser control, ... :>the picture data and load it into the picture box from there, ...
    (microsoft.public.vb.general.discussion)