- 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 Name: Common
antiforgery: enabled

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -22,5 +22,6 @@
public string Author { get; set; } public string Author { get; set; }
public string HomePage { get; set; } public string HomePage { get; set; }
public string Tags { 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"), Version = GetValue(fields, "version"),
Author = GetValue(fields, "author"), Author = GetValue(fields, "author"),
HomePage = GetValue(fields, "homepage"), 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.Collections.Specialized;
using System.Web; using System.Web;
using System.Web.Mvc; using System.Web.Mvc;
using JetBrains.Annotations; using JetBrains.Annotations;
using Orchard.Extensions;
using Orchard.Mvc.Filters; using Orchard.Mvc.Filters;
using Orchard.Security; using Orchard.Security;
using Orchard.Settings; using Orchard.Settings;
@@ -11,26 +13,46 @@ namespace Orchard.Mvc.AntiForgery {
public class AntiForgeryAuthorizationFilter : FilterProvider, IAuthorizationFilter { public class AntiForgeryAuthorizationFilter : FilterProvider, IAuthorizationFilter {
private readonly ISiteService _siteService; private readonly ISiteService _siteService;
private readonly IAuthenticationService _authenticationService; private readonly IAuthenticationService _authenticationService;
private readonly IExtensionManager _extensionManager;
public AntiForgeryAuthorizationFilter(ISiteService siteService, IAuthenticationService authenticationService) { public AntiForgeryAuthorizationFilter(ISiteService siteService, IAuthenticationService authenticationService, IExtensionManager extensionManager) {
_siteService = siteService; _siteService = siteService;
_authenticationService = authenticationService; _authenticationService = authenticationService;
_extensionManager = extensionManager;
} }
public void OnAuthorization(AuthorizationContext filterContext) { public void OnAuthorization(AuthorizationContext filterContext) {
#if false
if ((filterContext.HttpContext.Request.HttpMethod != "POST" || if ((filterContext.HttpContext.Request.HttpMethod != "POST" ||
_authenticationService.GetAuthenticatedUser() == null) && !ShouldValidateGet(filterContext)) { _authenticationService.GetAuthenticatedUser() == null) && !ShouldValidateGet(filterContext)) {
return; return;
} }
if (!IsAntiForgeryProtectionEnabled(filterContext)) {
return;
}
var siteSalt = _siteService.GetSiteSettings().SiteSalt; var siteSalt = _siteService.GetSiteSettings().SiteSalt;
var validator = new ValidateAntiForgeryTokenAttribute {Salt = siteSalt}; var validator = new ValidateAntiForgeryTokenAttribute {Salt = siteSalt};
validator.OnAuthorization(filterContext); validator.OnAuthorization(filterContext);
if (filterContext.HttpContext is HackHttpContext) if (filterContext.HttpContext is HackHttpContext)
filterContext.HttpContext = ((HackHttpContext)filterContext.HttpContext).OriginalHttpContextBase; 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) { private static bool ShouldValidateGet(AuthorizationContext context) {