2013-03-02 50 views
1

我正在使用MVC和KnockoutJS的項目,我想知道如何最好地爲網站提供安全性。使用模型綁定敲除JS/MVC安全

這是我的問題。

我允許使用Razor創建視圖,我使用Knockout通過JQuery對話框爲許多功能提供動力。我有如下所示的主視圖模型:

my.MasterViewModel = function() { 
    var 
    CatalogsViewModel = ko.observable(null) 
    BulkUploadViewModel = ko.observable(null), 
    ExploitationTypeViewModel = ko.observable(null), 
    ExploitationViewModel = ko.observable(null), 
    InviteSongWriterViewModel = ko.observable(null), 
    ManageCollaboratorsViewModel = ko.observable(null), 
    ManageSongwriterViewModel = ko.observable(null), 
    ManageSongViewModel = ko.observable(null), 
    ProViewModel = ko.observable(null), 
    SongsViewModel = ko.observable(null), 
    TagsViewModel = ko.observable(null), 
    TweaksViewModel = ko.observable(null), 
    NotificationsViewModel = ko.observable(new my.notificationsviewmodel()); 

    return { 
     CatalogsViewModel: CatalogsViewModel, 
     BulkUploadViewModel: BulkUploadViewModel, 
     ExploitationTypeViewModel: ExploitationTypeViewModel, 
     ExploitationViewModel: ExploitationViewModel, 
     InviteSongWriterViewModel: InviteSongWriterViewModel, 
     ManageCollaboratorsViewModel: ManageCollaboratorsViewModel, 
     ManageSongwriterViewModel: ManageSongwriterViewModel, 
     ManageSongViewModel: ManageSongViewModel, 
     ProViewModel: ProViewModel, 
     SongsViewModel: SongsViewModel, 
     TagsViewModel: TagsViewModel, 
     TweaksViewModel: TweaksViewModel, 
     NotificationsViewModel: NotificationsViewModel 
    }; 
}(); 

在我的點擊處理我然後實例化所需的視圖模式,一切都很好,這個偉大的工程!

是我遇到的問題是,我有事情在我的剃刀語法如下:

<li><a href="#" data-songid="@Model.Song.Id" class="icon-margin-left manage-song">Edit Song</a></li> 

在我的點擊處理我讀出的數據,songid值,並利用基因敲除從服務器加載數據。這也很好,但是,改變chrome,firebug或任何開發工具中的data-songid值並不是很難。一旦該值發生變化,用戶就可以點擊鏈接並編輯另一個實體...:S

有沒有更好的方法可以做到這一點?我非常擔心在我的應用程序中這是一個巨大的安全漏洞。

在此先感謝!

回答

1

更好的方法是檢查服務器端的權限。

你說的很對,很容易在瀏覽器開發工具中調整JS變量。僞造整個HTTP請求也很容易發送到服務器。

該規則是永遠不會相信用戶的輸入,並始終驗證此特定用戶在處理後端請求時是否有權編輯此特定歌曲。

+0

是的,我有安全構建在服務器端,但我不喜歡這是一切皆有可能。我想我必須忍受這樣一個事實,即如果用戶想要篡改他們自己的數據,那麼這個用戶就可以。 – mcottingham 2013-03-02 02:56:42

3

假設您已經在您的網站上應用了Asp.Net身份驗證,那麼來自Knockout的Ajax請求應該包含Asp.Net身份驗證Cookie(除非請求是跨域),以便在返回之前應用服務器端身份驗證/授權數據。正如Alex指出的那樣,這是必不可少的,也是確保用戶只能修改自己的數據的唯一方法。

正如你所說,雖然這並不阻止用戶更改ID,但它確實阻止他們修改他們不允許修改的數據。通常我認爲這已經足夠了,因爲用戶只能修改他們自己的數據,而這些數據大概是可以編輯的呢?

如果您確實需要確保用戶沒有更改該值,那麼您可以在ID頁面中生成一個服務器端摘要,然後在Razor頁面中輸出它。這可以用來驗證該值在Ajax請求中發回時未被修改。這篇文章給出了一個使用這種技術來防篡改URL查詢字符串的例子,但可以很容易地適用於像你這樣的場景:http://www.pradyblog.com/index.php/2012/08/20/creating-tamper-proof-url-in-asp-net-a-way-to-prevent-spoofing-and-forged-http-requests/