- AntiForgerFilter now opt-in for Orchard extensions such as modules and areas.

- "antiforgery: enabled" in your module.txt will enable the filter to do the antiforgery check on posts.

--HG--
branch : dev
This commit is contained in:
Suha Can
2010-03-04 15:15:06 -08:00
parent 3f0c7e73dc
commit 71f302277e
22 changed files with 53 additions and 10 deletions

View File

@@ -1 +1,2 @@
Name: Common
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: Dashboard
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: Feeds
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: HomePage
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: Navigation
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: Scheduling
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: Settings
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: Themes
antiforgery: enabled

View File

@@ -1 +1,2 @@
Name: XmlRpc
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: Widgets
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: Blogs
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: Comments
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: Media
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: Pages
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: Roles
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: Setup
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: Tags
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: Users
antiforgery: enabled

View File

@@ -1 +1,2 @@
name: TinyMce
antiforgery: enabled

View File

@@ -22,5 +22,6 @@
public string Author { get; set; }
public string HomePage { get; set; }
public string Tags { get; set; }
public string AntiForgery { get; set; }
}
}

View File

@@ -57,7 +57,8 @@ namespace Orchard.Extensions {
Version = GetValue(fields, "version"),
Author = GetValue(fields, "author"),
HomePage = GetValue(fields, "homepage"),
Tags = GetValue(fields, "tags")
Tags = GetValue(fields, "tags"),
AntiForgery = GetValue(fields, "antiforgery")
};
}

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.Mvc;
using JetBrains.Annotations;
using Orchard.Extensions;
using Orchard.Mvc.Filters;
using Orchard.Security;
using Orchard.Settings;
@@ -11,26 +13,46 @@ namespace Orchard.Mvc.AntiForgery {
public class AntiForgeryAuthorizationFilter : FilterProvider, IAuthorizationFilter {
private readonly ISiteService _siteService;
private readonly IAuthenticationService _authenticationService;
private readonly IExtensionManager _extensionManager;
public AntiForgeryAuthorizationFilter(ISiteService siteService, IAuthenticationService authenticationService) {
public AntiForgeryAuthorizationFilter(ISiteService siteService, IAuthenticationService authenticationService, IExtensionManager extensionManager) {
_siteService = siteService;
_authenticationService = authenticationService;
_extensionManager = extensionManager;
}
public void OnAuthorization(AuthorizationContext filterContext) {
#if false
if ((filterContext.HttpContext.Request.HttpMethod != "POST" ||
_authenticationService.GetAuthenticatedUser() == null) && !ShouldValidateGet(filterContext)) {
return;
}
if (!IsAntiForgeryProtectionEnabled(filterContext)) {
return;
}
var siteSalt = _siteService.GetSiteSettings().SiteSalt;
var validator = new ValidateAntiForgeryTokenAttribute {Salt = siteSalt};
validator.OnAuthorization(filterContext);
if (filterContext.HttpContext is HackHttpContext)
filterContext.HttpContext = ((HackHttpContext)filterContext.HttpContext).OriginalHttpContextBase;
#endif
}
private bool IsAntiForgeryProtectionEnabled(ControllerContext context) {
string currentModule = context.RouteData.Values["area"].ToString();
if (!String.IsNullOrEmpty(currentModule)) {
foreach (var descriptor in _extensionManager.AvailableExtensions()) {
if (String.Equals(descriptor.Name, currentModule, StringComparison.OrdinalIgnoreCase)) {
if (String.Equals(descriptor.AntiForgery, "enabled", StringComparison.OrdinalIgnoreCase)) {
return true;
}
return false;
}
}
}
return false;
}
private static bool ShouldValidateGet(AuthorizationContext context) {