2

If the name of a link is pulled from the database, should you be calling the Html.Encode method to clean the name?

For example:

Html.ActionLink(Model.PersonFromDB.FirstName,
                "Action",
                "Controller",
                new RouteValueDictionary { { "id", Model.PersonFromDB.Id } },
                null)

or:

Html.ActionLink(Html.Encode(Model.PersonFromDB.FirstName),
                "Action",
                "Controller",
                new RouteValueDictionary { { "id", Model.PersonFromDB.Id } },
                null)

It would make sense that you would want to do this to ensure that there are no dangerous strings injected into the page between <a> and </a> tags, but are scripts and such executable between anchor tags?

1
  • I came across this site after posting the question and marking the answer, but I figure it's helpful to others who stumble onto this question: owasp.org/index.php/… Commented Feb 18, 2010 at 20:36

3 Answers 3

6

No, since according to this thread on SO HtmlAction.Link() already HTML encodes values, so you'd end up doing it twice.

Sign up to request clarification or add additional context in comments.

1 Comment

Correct; see GenerateLinkInternal in HtmlHelper.cs in the MVC 2 source code.
0

It's certainly a good idea, but you should probably be preventing users from entering in potentially malicious strings as their first name.

2 Comments

ASP.NET MVC has a feature that checks all input to see if you are attempting to input script or malicious text, so that's already in place.
I generally agree, but keep in mind that "potentially malicious" depends a lot on where the data will be displayed. For instance, "</form>" is not at all damaging when displayed on a paper report or a raw DB report, it's only an issue when displayed in HTML. Thus, it's ultimately the job of the presentation layer to format the data according to its specific needs.
-1

Yes, absolutely. As a general rule, for any HTML that you are going to output that was originally obtained from an untrusted source, assuming the format wasn't HTML already (and sufficiently vetted), you should always HTML encode the string to protect against injection attacks.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.