Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Rename AppendEncoded() to AppendHtml() and SetContentEncoded() to SetHtmlContent() #3225

Closed
kichalla opened this issue Sep 29, 2015 · 11 comments
Assignees
Milestone

Comments

@kichalla
Copy link
Member

In the following tag helper, I am using AppendEncoded to append <b> even though its not really encoded and is just raw content.

@NTaylorMullen suggested AppendRaw which seems like a good name to indicate what it does.

[HtmlTargetElement("greeting")]
public class GreetingTagHelper : TagHelper
{
    public string text { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = null;
        output.Content.AppendEncoded("<b>");
        output.Content.Append(text);
        output.Content.AppendEncoded("</b>");
    }
}

@rynowak @dougbu

@dougbu
Copy link
Member

dougbu commented Sep 29, 2015

Note <b> is just as fully-encoded as &lt;b&gt;. The only difference is you actually want to be bold in the first case.

@dougbu
Copy link
Member

dougbu commented Sep 29, 2015

I suspect there's only three choices here but would love to hear something better:

  1. Leave the name as-is.
  2. Change to AppendRaw() which will be meaningful primarily to those familiar with @Html.Raw() and not necessarily to new MVC users.
  3. Name the method to say exactly what it does AppendWithoutFurtherEncoding().

@Eilon
Copy link
Member

Eilon commented Sep 29, 2015

AppendRaw() would indeed match up nicely with Html.Raw() - I would like that.

@danroth27 danroth27 added this to the 6.0.0-rc1 milestone Sep 29, 2015
@Eilon
Copy link
Member

Eilon commented Sep 30, 2015

I chatted with @dougbu and we both like AppendHtml. That makes it explicit that you can pass in HTML into it, and how you encoded the content in there (if at all), is totally up to the caller.

@Eilon Eilon changed the title Provide an extension method like 'AppendRaw' on IHtmlContent Provide an extension method like 'AppendHtml' on IHtmlContent Sep 30, 2015
@NTaylorMullen
Copy link
Contributor

Like it, 👍

@rynowak
Copy link
Member

rynowak commented Oct 1, 2015

👍 love it!

@kichalla
Copy link
Member Author

kichalla commented Oct 1, 2015

I like the name but wondering if it would make it a more attractive extension to be used when probably for most scenarios you want the content to be encoded?

@Eilon
Copy link
Member

Eilon commented Oct 1, 2015

Oh it could certainly be an extension method if that simplifies the interface. The interface should have the minimum required. However, as far as how frequently it's used: I think HTML is just as common as non-HTML. Tag helpers, HTML helpers, etc. often have to render a lot of their own HTML plus page-generated stuff that needs to be encoded.

@rynowak
Copy link
Member

rynowak commented Oct 1, 2015

Oh it could certainly be an extension method if that simplifies the interface.

We might not want to simplify the interface any further. An IHtmlContentBuilder needs to decide how it wants to represent the two kinds of string (encoded, unencoded). Making this an extension method means that you have to choose one of those as canonical, which is going to have a BAD perf impact on half the scenarios. It used to be this way, and I changed it based on the data.

However, as far as how frequently it's used: I think HTML is just as common as non-HTML. Tag helpers, HTML helpers, etc. often have to render a lot of their own HTML plus page-generated stuff that needs to be encoded.

The point about this that I was making earlier in the week is that you don't want to accidentally use AppendHtml when you mean Append. If it's not absolutely clear what the various methods do, then it's good that the unsafe one is scary 👻

  • If you mistakenly use AppendHtml with unencoded text, you probably have an XSS vulnerability.
  • If you mistakenly use Append with encoded text, you will have a double-encode - which is probably apparent just by looking at the site in a browser, and won't get your b0x0rs H4X0red

I think AppendHtml is better than AppendEncoded because it's clearer. Some others were saying that they found AppendEncoded confusing, and didn't know if it meant either:

  • I want to encode this text and append it
  • I want to append this encoded text

@Eilon
Copy link
Member

Eilon commented Oct 1, 2015

Ok interface is fine then, and we'll go with AppendHtml.

dougbu added a commit to aspnet/HttpAbstractions that referenced this issue Oct 21, 2015
dougbu added a commit to aspnet/Razor that referenced this issue Oct 21, 2015
dougbu added a commit that referenced this issue Oct 21, 2015
dougbu added a commit to aspnet/HttpAbstractions that referenced this issue Oct 21, 2015
dougbu added a commit to aspnet/HttpAbstractions that referenced this issue Oct 22, 2015
dougbu added a commit to aspnet/Razor that referenced this issue Oct 22, 2015
dougbu added a commit to aspnet/Razor that referenced this issue Oct 22, 2015
dougbu added a commit that referenced this issue Oct 22, 2015
dougbu added a commit to aspnet/HttpAbstractions that referenced this issue Oct 22, 2015
…to `SetHtmlContent()`

- aspnet/Mvc#3225, 1 of 3

Also correct parameter names and doc comments
- add `xml-docs-test` to ensure this doc comments remain valid in the future
dougbu added a commit to aspnet/Razor that referenced this issue Oct 22, 2015
…to `SetHtmlContent()`

- aspnet/Mvc#3225, 2 of 3
- also correct parameter names
dougbu added a commit that referenced this issue Oct 22, 2015
@dougbu dougbu changed the title Provide an extension method like 'AppendHtml' on IHtmlContent Rename AppendEncoded() to AppendHtml() and SetContentEncoded() to SetHtmlContent() Oct 23, 2015
@dougbu
Copy link
Member

dougbu commented Oct 23, 2015

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants