Die besten Muster für die von AllowUnsafeUpdates

Bisher in meiner Forschung habe ich gesehen, dass es unklug ist, zu setzen, von AllowUnsafeUpdates auf GET-request-operation zu vermeiden, cross-site-scripting. Aber, wenn es erforderlich ist, um das zu erlauben, was ist der richtige Weg, um mit der situation zu mindern, eine Exposition?

Hier ist meine beste erste Vermutung auf eine zuverlässige Muster, wenn Sie absolut müssen es erlauben, web-oder Website-updates auf einen GET-request.

Best Practice?

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

        }
    }

Feedback auf die besten Muster geschätzt wird.

InformationsquelleAutor der Frage webwires | 2008-10-17

Schreibe einen Kommentar