Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dibs 202 posts 991 karma points
    Mar 04, 2017 @ 23:54
    Dibs
    0

    handle malicious input with Razor in template

    HI Umbraco Team

    I have set up a simple search feature via the following link: http://www.codeshare.co.uk/blog/simple-umbraco-search-example/ . I would like to handle malicious input from the user, currently if i input a <script> tag i get error about partial view not able to be rendered and log entry.

    System.Web.HttpRequestValidationException (0x80004005): A potentially dangerous Request.QueryString value was detected from the client (keywords="**")

    How can i handle this with Razor ? Debugging catches the exception in code at

    string searchTerm = Request.QueryString["keywords"];
    

    Thanks Dibs

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Mar 06, 2017 @ 07:31
    Dennis Adolfi
    0

    Hi Dibs.

    Maybe you could use Regex? Not on the querystring after post but instead on the form input, preventing the user from posting malicious input. See example (This example only allows letters and numbers):

    <input type="text" placeholder="Search..." name="query" pattern="^[a-zA-Z0-9]+$">
    

    Live example: https://jsfiddle.net/411pk92u/

    Regex used: http://regexlib.com/REDetails.aspx?regexp_id=1014

    Best of luck!

  • Stefano 61 posts 313 karma points c-trib
    Mar 06, 2017 @ 09:21
    Stefano
    101

    That seems like a good idea! Maybe the regex could be more permissive!

    The alternative would be to allow for "potentially dangerous" request string and then handle them correctly.

    Look here https://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx#Anchor_0 under , skip the first part about ASP.NET Web Forms.

  • Dibs 202 posts 991 karma points
    Mar 07, 2017 @ 16:45
    Dibs
    1

    Thanks for your input guys,

    I'll give both a try, maybe go with Microsoft link

    Dibs

Please Sign in or register to post replies

Write your reply to:

Draft