How can I use template.HTML inside a tag?

xuanbao · · 425 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m trying to do something like this: <a href="https://play.golang.org/p/4300MheGPS" rel="nofollow">https://play.golang.org/p/4300MheGPS</a></p> <p>For some reason <code>template.HTML</code> will remove tags if you put the template variable as the attribute of a tag - how can I stop this? I want the output to be <code>&lt;input value=&#34;&lt;b&gt;text&lt;/b&gt;&#34;&gt;</code> in that example above.</p> <hr/>**评论:**<br/><br/>SourLemon15: <pre><p>Try using the <code>template.HTMLAttr</code> type instead, like this: <a href="https://play.golang.org/p/TU5CX9Kstj" rel="nofollow">https://play.golang.org/p/TU5CX9Kstj</a></p> <p>You just need to remember to set the entire attribute, like <code>value=&#34;&lt;b&gt;text&lt;/b&gt;&#34;</code> instead of only the value (<code>&lt;b&gt;text&lt;/b&gt;</code>).</p> <p><strong>Edit:</strong></p> <p>Also, before using this method just remember that this is a security risk and could very easily introduce a vector of attack into your application that would allow for XSS.</p> <p>The <a href="https://golang.org/pkg/html/template/#HTMLAttr" rel="nofollow">Go documentation here</a> also warns about the inherent security risk this introduces, so only use it with 100% trusted input if you have no other choice.</p></pre>qrv3w: <pre><p>I think this solution will work for me, thanks! The HTML is coming from <a href="https://trix-editor.org/" rel="nofollow">https://trix-editor.org/</a> which will escape tags on the input and only adds HTML internally. Also I will also be sanitizing it with bluemonday.</p></pre>earthboundkid: <pre><p><a href="http://xyproblem.info/" rel="nofollow">http://xyproblem.info/</a></p> <p>The thing you are trying to do will produce invalid HTML. </p> <p>What do you actually want to do?</p></pre>SourLemon15: <pre><p>As far as I can tell <code>&lt;input value=&#34;&lt;b&gt;text&lt;/b&gt;&#34;&gt;</code> isn&#39;t actually invalid HTML, at least in HTML5. You just need to escape any <code>&#34;</code> and <code>&amp;</code> characters inside the quotes from what I remember.</p> <p>I haven&#39;t found anything myself to show that this is invalid, and all HTML5 validators I&#39;ve tried have no problem with it, so is there anything you can point me to that says this is invalid? Because if it is then I would really like to know so I can stop doing it in my own code for things like tooltips etc.</p> <p>That said I don&#39;t know how well Go&#39;s templating will handle stuff like this. There is <code>template.HTMLAttr</code> which is what I suggested to use, but I tend to stay away from Go templating so I don&#39;t know how well <code>template.HTMLAttr</code> works in cases like this.</p> <p>Good point about the XY problem though :)</p></pre>jerf: <pre><p>It is likely what <a href="/u/qrv3w" rel="nofollow">/u/qrv3w</a> really needs to do is <a href="https://play.golang.org/p/s_jkHt9kWl" rel="nofollow">this</a>, which is the original post with the <code>template.HTML</code> wrapper removed from the string.</p> <p>Tags within attributes may not be invalid in HTML5, but it&#39;s still asking for trouble. For one thing, if the contents of <code>template.HTML</code> end up with user input in them, this will, despite the html/template&#39;s author&#39;s best efforts, end up permitting cross-site scripting attacks and such. It&#39;s just better to learn to &#34;color within the lines&#34; with HTML rather than use the escape hatches. <code>template.HTML</code> is for when you have a block of pre-encoded content, like a blog post stored as literal HTML.</p> <p>The only conceivable reason I can imagine to want literal <code>&lt;b&gt;</code> within the value attribute is &#34;I like the way it looks better than all that <code>&amp;lt;b&amp;gt;</code> stuff&#34;, and the solution to that is just to let that desire go. :)</p></pre>SourLemon15: <pre><p>I agree it&#39;s definitely asking for trouble assuming you can&#39;t 100% guarantee the contents of the HTML you want to output, and the Go documentation of course makes it abundantly clear that it is a security risk and should only be used on trusted sources.</p> <p>In my own experience I have had to do what the OP is asking for so I could pass HTML formatted tooltips to a JS library that was just printing out any form of escaping as the literal values, which is why I just gave a straightforward answer. But of course I wouldn&#39;t inject any user input using this method.</p> <p>My own answer to the question just assumed that things like XSS were obvious to be careful of, but I&#39;ll edit it to point directly to the documentation and mention the security issues, just in case anyone takes my answer at face value and just dumps it in their code.</p></pre>qrv3w: <pre><p>I am using the <a href="https://trix-editor.org" rel="nofollow">trix editor</a> which is HTML based. To repopulate the editor with saved content, the trix editor requires putting in HTML into the <code>value</code> attribute of an <code>input</code> tag (documented <a href="https://github.com/basecamp/trix#populating-with-stored-content" rel="nofollow">here</a>, and see the source of <a href="https://trix-editor.org" rel="nofollow">https://trix-editor.org</a> for another example). Therefore, I would most like to have code like <code>&lt;input id=&#34;content&#34; value=&#34;{{ .HTML }}&#34;&gt;</code> to repopulate the trix editor with the HTML when processing the template.</p> <p>The editor can also be repopulated by setting it with Javascript, so my current workaround is to set set it a Javascript variable in the template, and then I have Javascript that unescapes it and then uses the <a href="https://github.com/basecamp/trix#inserting-html" rel="nofollow">trix API to update</a> it. Its not very elegant, so I was curious if I could directly just put it into the <code>value</code> attribute.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

425 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传