web*_*res 14 sharepoint spweb spsite
到目前为止,在我的研究中,我已经看到在GET请求操作上设置AllowUnsafeUpdates是不明智的,以避免跨站点脚本.但是,如果要求允许这样做,处理这种情况以减轻任何暴露的正确方法是什么?
如果您绝对需要在GET请求上允许Web或站点更新,这是我对可靠模式的最佳猜测.
最佳实践?
protected override void OnLoad(System.EventArgs e)
{
if(Request.HttpMethod == "POST")
{
SPUtility.ValidateFormDigest();
// will automatically set AllowSafeUpdates to true
}
// If not a POST then AllowUnsafeUpdates should be used only
// at the point of update and reset immediately after finished
// NOTE: Is this true? How is cross-site scripting used on GET
// and what mitigates the vulnerability?
}
// Point of item update
using(SPSite site = new SPSite(SPContext.Current.Site.Url, SPContext.Current.Site.SystemAccount.UserToken))
{
using (SPWeb web = site.RootWeb)
{
bool allowUpdates = web.AllowUnsafeUpdates; //store original value
web.AllowUnsafeUpdates = true;
//... Do something and call Update() ...
web.AllowUnsafeUpdates = allowUpdates; //restore original value
}
}
Run Code Online (Sandbox Code Playgroud)
对最佳模式的反馈表示赞赏.
如果您正在执行任何修改某些操作的操作,那么任何可以说服用户单击链接的操作都可以执行该操作.例如,假设您有一个页面的GET请求,该页面允许用户将管理员添加到站点,并且用户单击指向执行Response.Redirect的页面的链接(" http:// yourserver/_layouts/admin.aspx?operation = addAdministrator&username = attackerNameHere ").
虽然通常POST没有提供太多保护(没有什么能阻止某人使用<form method ="post"action ="http://yourserver/_layouts/admin.aspx">),但SharePoint有一个表单的概念摘要,其中包含有关生成回发的先前请求的信息(包括用户的名称).这显着减少了这种攻击的足迹.
如果您没有从用户那里获取输入,那么唯一一次在GET上对AllowUnsafeUpdates不是安全问题.例如,如果您的Web部件也记录了对列表的访问,则不会暴露任何安全漏洞.
编辑:如果您要使用AllowUnsafeUpdates,则无需将其重置为以前的值.它不会持久化.在从GET(或其他情况)执行更新之前,您只需要在SPWeb对象上设置它
我会稍微修改Trent的委托以接受网页更新:
public static void DoUnsafeUpdate(this SPWeb web, Action<SPWeb> action)
{
try
{
web.AllowUnsafeUpdates = true;
action(web);
}
finally
{
web.AllowUnsafeUpdates = false;
}
}
Run Code Online (Sandbox Code Playgroud)
然后扩展HttpContext以封装表单摘要的验证,并提供使用此处描述的技术提升的选项:
public static void DoUnsafeUpdate(this HttpContext context, Action<SPWeb> action, bool elevated)
{
SPWeb web = SPControl.GetContextWeb(context);
if (!context.Request.HttpMethod.Equals("POST", StringComparison.Ordinal)
|| web.ValidateFormDigest())
throw new SPException("Error validating postback digest");
if (elevated)
web.RunAsSystem(w => w.DoUnsafeUpdate(action));
else
web.DoUnsafeUpdate(action);
}
Run Code Online (Sandbox Code Playgroud)
用法:
protected override void OnLoad(System.EventArgs e)
{
Context.DoUnsafeUpdate(web =>
{
// Update elevated web
}, true);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21770 次 |
| 最近记录: |