2011-01-06 31 views
51

Lỗi này xuất hiện trong ứng dụng MVC của chúng tôi một cách ngẫu nhiên. Đôi khi làm điều tương tự chính xác nó sẽ không đôi khi, nó sẽ. Có ai biết nếu điều này đã làm với bất cứ điều gì mà có thể là một sửa chữa đơn giản, hoặc nếu điều này là một cái gì đó phổ biến mà rất nhiều bạn đã thấy?Json cho phép lỗi

System.InvalidOperationException: This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet. 
    at System.Web.Mvc.JsonResult.ExecuteResult(ControllerContext context) 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) 
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass14.b__11() 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) 
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass14.<>c__DisplayClass16.b__13() 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) 
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass14.<>c__DisplayClass16.b__13() 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) 
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass14.<>c__DisplayClass16.b__13() 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) 
    at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) 
    at System.Web.Mvc.Controller.ExecuteCore() 
    at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) 
    at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) 
    at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.b__4() 
    at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.b__0() 
    at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.b__7(IAsyncResult _) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End() 
    at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) 
    at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) 
    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 

Trả lời

118

Trả lời cho câu hỏi của bạn là theo dõi ngăn xếp. "JsonRequestBehavior để AllowGet"

Vì vậy, sử dụng nó trong điều khiển của bạn như:

return Json(data, JsonRequestBehavior.AllowGet) 
+27

tôi nghĩ rằng nó có thể là một sai lầm khi đi xung quanh và mù quáng nói mọi người bỏ qua lý do bảo mật cho phép AllowGet bị tắt. - Nó không phải là khó để làm một $ .post thay vì để có được dữ liệu và tránh toàn bộ vấn đề alltogether. – Yablargo

+11

Điểm tốt. Đối với những người có liên quan Haack có một bài viết tuyệt vời bao gồm: http://haacked.com/archive/2009/06/25/json-hijacking.aspx –

+2

Bài viết của Haack hiện có tại: http://haacked.com/archive/ 2009/06/24/json-hijacking.aspx/ –

4

Dường như bạn gọi đôi khi hành động điều khiển mỗi HTTP GET. Để có thể trở về JSON kết quả bạn nên sử dụng mã như

return Json(data, JsonRequestBehavior.AllowGet); 
22

Bạn nên đọc http://haacked.com/archive/2009/06/24/json-hijacking.aspx/ trước khi bỏ qua những kiểm soát an ninh.

Nếu bạn chỉ hiển thị dữ liệu JSON của mình để phản hồi cho POST POST, thì bạn không dễ bị tấn công này.

Bạn chỉ có thể chú thích hành động JSON của bạn với [HttpPost] và trong các khách hàng làm cái gì đó như

$.post('/blag/JSON', function (data) { 
     //do something with my json data object here 

}); 
+3

Hoàn toàn hợp lý khi chấp nhận yêu cầu GET khi cố gắng nhận dữ liệu JSON. BÀI ĐĂNG nên để tạo bản ghi mới hoặc ở trạng thái ít thay đổi nhất. Yêu cầu GET phải lấy dữ liệu không ảnh hưởng đến trạng thái. – ashurexm

+1

Lý do giải pháp JsonRequestBehavior.AllowGet ở trên tồn tại, là đảm bảo rằng nhà phát triển cố tình bỏ qua sự bảo vệ này. Đề xuất sử dụng bài đăng là để tránh một lỗ hổng trong thiết kế/triển khai cho phép dữ liệu này được hiển thị cho bên thứ ba. Điều này thậm chí được hiển thị trong dòng đầu tiên của dấu vết ngăn xếp: System.InvalidOperationException: Yêu cầu này ** đã bị chặn vì thông tin nhạy cảm có thể được tiết lộ cho các trang web của bên thứ ba khi điều này được sử dụng trong yêu cầu GET **. Để cho phép các yêu cầu GET, hãy đặt JsonRequestBehavior thành AllowGet – Yablargo

-1

return Json(PartialView("index").ToJsonObject(this), JsonRequestBehavior.AllowGet);

Các vấn đề liên quan