【问题标题】:Is there a need for GWT SafeHtml when using UiBinder?使用 UiBinder 时是否需要 GWT SafeHtml?
【发布时间】:2012-10-29 05:31:41
【问题描述】:

假设您有以下MyPanel.ui.xml

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:gwt="urn:import:com.google.gwt.user.client.ui">
    <div>
        <span id="content">Some content</span>

        <gwt:RadioButton ...>
            ...
        </gwt:RadioButton>

        <!-- etc. -->
    </div>
</ui:UiBinder>

这个“映射”到MyPanel.java

public class MyPanel extends Composite {
    private RadioButton radioButton;
    // ...
}

那么,是否存在您想要/需要使用 SafeHtml 或 SafeHtmlBuilder 的用例,或者仅在使用 HTML 对象及其底层 DOM 结构时才需要“Safe*”API?

如果存在 UiBinder 支持的复合材料需要使用 Safe* 的用例,也许一个简单的代码示例可以帮助我连接这些点。提前致谢!

【问题讨论】:

    标签: java html gwt xss


    【解决方案1】:

    一个简单的例子,你应该将 SafeHTML 与 UiBinder 结合使用:

    <!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
    <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
        xmlns:gwt="urn:import:com.google.gwt.user.client.ui">
        <gwt:HTMLPanel>
            <gwt:HTML ui:field="myHtml"/>
        </g:HTMLPanel>
    </ui:UiBinder>
    
    public class MyPanel extends Composite {
        private HTML myHtml;
        // ...
    }
    

    这里你应该使用myHtml.setHTML(SafeHTML) [*]。原因是,这是示例中唯一可能出现用户提供的内容的地方。用户内容不能出现在 UiBinder 模板本身中(因为它是静态的:在编译时固定)。

    因此,是否需要 SafeHTML 之间的区别,等同于信任用户提供的内容与信任开发人员提供的内容之间的区别。

    [*] 在您自己的示例中,您应该使用 RadioButton 的 SafeHTML 构造函数之一

    【讨论】:

    • 感谢@Chris Lercher (+1) - 我想它的 开始 更有意义。我认为这是我困惑的根源:为什么 HTML myHTML 被认为是“用户提供的内容”,而不是 RadioButton?归根结底,这不就是 HTML 吗?我想如果你能帮助我理解用户提供的内容和开发者提供的内容之间的区别,我就会知道如何确定哪些用例适合 SafeHtml。再次感谢!
    • @orionTurtle:RadioButton 和 HTML 小部件之间没有基本区别 - 但我尝试尽可能简化示例(因为 RadioButton 有许多构造函数等)。为什么它被视为用户提供的内容?它不是。但它是用户提供的内容可能出现的唯一地方。
    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 2012-07-31
    相关资源
    最近更新 更多