【问题标题】:Prevent user to inject html to edit control value - server side防止用户注入 html 来编辑控件值 - 服务器端
【发布时间】:2019-11-09 08:42:56
【问题描述】:

我正在开发包含安全性的自定义控件。当用户无权访问该控件时,该控件会使其自身禁用但也将变为不可见。该控件未呈现,也未出现在页面上。在这一点上,一切都很好。

我的问题是如何保护控件以防止用户更改值?

我已经使用 Chrome HTML Inspector 在我的表单中注入了一个输入,因为就像它应该的那样,该字段没有呈现,当我提交带有新值的注入输入的表单时,服务器在控件中具有新值价值属性。

public enum UserRole {
   Standard,
   Administrator,
   [...]
}

//For this example, my custom control is derived from HtmlInputText.  [ToolboxData("<{0}:MyCustomControl runat=\"server\"></{0}:MyCustomControl>")]
public class MyCustomControl: System.Web.UI.HtmlControls.HtmlInputText
{
    public UserRole? MinimumRoleRequired { get; set; }

    protected override void OnLoad(EventArgs e)
    {
        //Simplified version
        if (this.Page.CurrentUser.Role < this.MinimumRoleRequired) 
        {
            this.Visible = false;
            this.Disabled = true;
            return;
        }
        [...]
    }

    protected override void Render(HtmlTextWriter writer)
    {
        if (!this.Visible || this.Disabled)
        {
            return;
        }
        [...]
    }
[...]
}
//My page who contain the control:

//HTML (MyPage.aspx)
<Controls:MyCustomControl ID="tbAdminOnly"runat="server"></Controls:MyCustomControl>


//C# (MyPage.aspx.cs)
public partial class UserEdit : Page
{
    protected override void OnInit(EventArgs e)
    {
       this.tbAdminOnly.MinimumRoleRequired = UserRole.Administrator;  
       [...]
    }

    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);
        if (this.IsPostBack)
        {
            string postBackValue = tbAdminOnly.Value;
            return;
        }
        tbAdminOnly.Value = "Hello world!";
    }
}

当我以标准用户身份加载页面时,不会呈现控件。但是如果我在 html 页面中注入输入

//Note, i need to know the valid name/id but it could be done.
<input type="text" name="tbAdminOnly" id="tbAdminOnly" value="Damn shit">

postBackValue 现在是来自注入输入的新值。我怎样才能防止这种情况?

谢谢。

【问题讨论】:

    标签: c# security webforms custom-controls


    【解决方案1】:

    为了防止用户注入 html 控件,您需要清理输入。有类似的帖子。 How to use C# to sanitize input on an html page?

    【讨论】:

    • 感谢您的快速回复,但我已阅读有关消毒剂的信息,但我不确定。我知道这个库可以“清理” html,但我不明白如何在我的上下文中应用它。你能给我一个简单的例子,你将如何在我的上下文中使用消毒剂吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多