2017-09-26 35 views
0

我正在使用BreezeJS,並對如何保存數據有疑問。這裏是我的代碼和註釋BreezeJS SaveChanges()安全問題

[Authorize] 
    /* 
    * I want to point out the security hole here. Any Authorized user is able to pass to this method 
    * a saveBundle which will be saved to the DB. This saveBundle can contain anything, for any user, 
    * or any table. 
    * 
    * This cannot be stopped at the client level as this method can be called from Postman, curl, or whatever. 
    * 
    * The only way I can see to subvert this attack would be to examine the saveBundle and verify 
    * no data is being impacted that is not owned or related directly to the calling user. 
    * 
    * Brute force could be applied here because SaveResult contains Errors and impacted Entities. 
    * 
    */ 

    [HttpPost] 
    public SaveResult SaveChanges(JObject saveBundle) 
    { 
     return _efContext.SaveChanges(saveBundle); 
    } 

限制訪問來獲取數據我首先從ACCESS_TOKEN user_ID的提取和限制我所有的問題,包括這在where子句中一個呼叫者的能力,使得它有些用戶不可能檢索另一個用戶數據。

但是,這不會阻止具有有效access_token的流氓用戶在增量對象id的強力循環中調用SaveChanges()。

我在這一個路上?也許我錯過了一些東西。

感謝您的任何幫助。

麥克

+0

不熟悉微風,但你聽起來像你正在實施適當的訪問控制。在生成對象標識符時使用加密隨機性也是一種很好的做法,但實際上這對於實施適當的訪問控制來說是次要的。在SaveChanges()方面,您可能希望限制用戶可以執行的保存操作的數量 - 否則他可以用垃圾填充數據庫。 – TheGreatContini

回答

2

客戶端傳遞給SaveChanges方法是不透明的,並且很難使用的JObject saveBundle。微風ContextProvider將其轉換爲實體圖並將其傳遞給BeforeSaveEntities方法。 BeforeSaveEntities是一種方法,你能實現你的ContextProvider子類,或在你連接到ContextProvider代表,如:

var cp = new MyContextProvider(); 
    cp.BeforeSaveEntitiesDelegate += MySaveValidator; 

在你BeforeSaveEntities或委託方法,你會檢查是否實體可以保存由當前用戶提供。如果您發現不應該被保存的實體,您可以從變更集刪除它,或者拋出一個錯誤並中止保存:

protected override Dictionary<Type, List<EntityInfo>> BeforeSaveEntities(
       Dictionary<Type, List<EntityInfo>> saveMap) 
{ 
    var user = GetCurrentUser(); 
    var entityErrors = new List<EFEntityError>(); 
    foreach (Type type in saveMap.Keys) 
    { 
    foreach (EntityInfo entityInfo in saveMap[type]) 
    { 
     if (!UserCanSave(entityInfo, user)) 
     { 
     throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden) 
      { ReasonPhrase = "Not authorized to make these changes" }); 
     } 
    } 
    } 
    return saveMap; 
} 

您需要確定用戶是否應該被允許保存一個特定的實體。這可以基於用戶的角色和/或一些其他屬性,例如, Sales角色中的用戶只能保存屬於其SalesRegion的客戶端記錄。

+0

謝謝!這正是我需要知道的。我感謝幫助! – Mike