Do I need to html-encode title attributes (tooltip

2019-04-10 19:01发布

In my markup I am using HTML title attributes which I set by the Tooltip property of various ASP.NET controls like an asp:Label. The content of those titles come from a database and I use data binding syntax, for instance:

<asp:Label ID="PersonLabel" runat="server" 
    Text='<%# HttpUtility.HtmlEncode(Eval("PersonShortName")) %>'
    ToolTip='<%# HttpUtility.HtmlEncode(Eval("PersonFullName")) %>' />

Now, tooltips seem to be displayed as plain text on Windows and in the browsers I have tested. So the HTML-encoding is not what I really want and I am inclined to remove the encoding.

Can this be dangerous in any way if the database fields may contain script tags for example? My question is basically: Is it always guaranteed that HTML-title attributes are displayed as plain text? Are they always displayed as tooltips at all, or is it possible that some browsers (or OSs) display them in another way and allow and render HTML content in the title attributes?

Edit:

Looking at some of the answers it seems I didn't phrase my question well, so here are some additions:

If I have in the code snippet above a PersonShortName of "PM" in my database and as the PersonFullName a name with non-ASCII characters in it like Umlauts in "Peter Müller" the browser displays in the tooltip Peter M&#252;ller when I apply HttpUtility.HtmlEncode like in the code example - which is ugly.

I've also tested a simple HTML fragment like:

<span title="<script>alert('Evil script')</script>" >Hello</span>

The script in the title attribute didn't run in a browser with enabled Javascript (tested with Firefox), instead it was displayed in the tooltip as plain text. Therefore my guess was that title attributes are always rendered as plain text.

But as Felipe Alsacreations answered below there exist "rich tooltip plugins" which may render the title attribute as HTML. So in this case encoding is a good thing. But how can I know that?

Perhaps HttpUtility.HtmlEncode isn't the right solution and I have to filter only HTML tags but not encode simple special characters to make sure that the plain text is displayed correctly and to protect "rich HTML tooltips" at the same time. But it looks like a costly work - only for a simple tooltip.

6条回答
贼婆χ
2楼-- · 2019-04-10 19:13

Always sanitize output to the browser.

If a value like "><script>blabla</script> is inserted as a value for your fields, a user can essentially take over your entire site. It will probably make a mess when it comes to validation and correct code, but the script will still be run.

So to answer your question: No, it is not guaranteed that HTML-title attributes are displayed as plain text if the user knows what he/she is doing.

查看更多
我命由我不由天
3楼-- · 2019-04-10 19:17

Surprisingly, still, no right answer in 5 years. The answer is: yes, you need to encode the title attribute, but not everything that is encoded in the innerText of the element.

The proper way to do it in asp.net if you do your own markup is:

string markup = string.Format("<div class='myClass' title='{0}'>{1}</div>",
   System.Web.HttpUtility.HtmlAttributeEncode(myText), 
   System.Web.HttpUtility.HtmlEncode(myText));

The above will set both innerText and title of the div to myText, which is customary for elements that may contain long text but are constrained in width (as I believe the question implies).

查看更多
孤傲高冷的网名
4楼-- · 2019-04-10 19:21

I think there may be some confusion going on with this thread.

Firstly <asp:Label> is an ASP.NET Web Control. The Text and ToolTip attributes are "abstractions" of the inline content and 'title' attributes of an HTML tag respectively.

For these particular two properties Microsoft will perform the HTML Encoding for you automatically so if you set ToolTip="H&S<" then the <span> tag will be rendered as <span title="H&amp;S&lt;"...>. The same goes for the Text property.

NOTE: Not all properties perform automatic encoding (HTML or InnerContent properties for example)

If however you are generating HTML tags directly (Response.Write("<span...") for example) then you MUST http encode the text content and tooltip attributes content if:

  • Those values originate from a user / external unsanitised source or
  • If there is a possibility that the content may contain characters that should be escaped (& < > etc.)

Usually this means that it is safe to to:

Hardcoded content with no http characters:

Response.Write("<span title='Book Reference'>The art of zen</span>"); // SAFE

Hardcoded content with http characters that you manualle encode:

Response.Write("<span title='Book &amp; Reference'>The art &amp; zen</span>"); // SAFE

Dynamically sourced content:

Response.Write("<span title='"+sTitle+"'>"+sText+"</span>"); // UNSAFE Response.Write("<span title='"+HttpUtility.HtmlEncode(sTitle)+"'>" +HttpUtility.HtmlEncode(sText)+"</span>"); // SAFE

查看更多
放荡不羁爱自由
5楼-- · 2019-04-10 19:29

Beside security reasons:

Title attributes should always be plain text but certain JS plugins misuse them to display 'rich' tooltips (i.e. HTML code with bold text, emphasis, links and so on).

As for browsers and AFAIK they are displayed as plain text and tooltips, never displayed to those who use tabbed navigation (keyboard) and scren readers give to their users (blind and partially sighted people) many options, like reading the longest between link title and its text or always title or never ...

查看更多
霸刀☆藐视天下
6楼-- · 2019-04-10 19:30

Another point:

Who cares how the title attribute is rendered by a browser, when it is the presence of malicious strings in the source code that could present an issue?

It doesn't matter how it is displayed, the question is: how does it appear in the source code?

(As already stated, if you're pumping strings to the client, do something to sanitize those strings.)

查看更多
啃猪蹄的小仙女
7楼-- · 2019-04-10 19:33

The ToolTip property of a ASP.NET control will auto encode the value on output/rendering.

This means it is safe to set the tooltip to plain text as the page will sanitize the text on rendering.

Label1.ToolTip = "Some encoded text < Tag >"

Renders HTML output as:

<span title="Some encoded text &lt; Tag >"></span>

If you need to use text that is already encoded, you can set the title attribute instead. The title attribute will not be automatically encoded on rendering:

Label1.Attributes("title") = "Some encoded text &lt; Tag &gt;"

Renders HTML output as:

<span title="Some encoded text &lt; Tag &gt;"></span>
查看更多
登录 后发表回答