This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow IgnoreAntiForgeryToken applied on Razor Page models to work
Fixes #7795
- Loading branch information
Showing
12 changed files
with
283 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...icrosoft.AspNetCore.Mvc.RazorPages/AutoValidateAntiforgeryPageApplicationModelProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc.ApplicationModels; | ||
using Microsoft.AspNetCore.Mvc.ViewFeatures; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.RazorPages | ||
{ | ||
internal class AutoValidateAntiforgeryPageApplicationModelProvider : IPageApplicationModelProvider | ||
{ | ||
// The order is set to execute after the DefaultPageApplicationModelProvider. | ||
public int Order => -1000 + 10; | ||
|
||
public void OnProvidersExecuted(PageApplicationModelProviderContext context) | ||
{ | ||
} | ||
|
||
public void OnProvidersExecuting(PageApplicationModelProviderContext context) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
var pageApplicationModel = context.PageApplicationModel; | ||
|
||
// ValidateAntiforgeryTokenAttribute relies on order to determine if it's the effective policy. | ||
// When two antiforgery filters of the same order are added to the application model, the effective policy is determined | ||
// by whatever appears later in the list (closest to the action). This causes filters listed on the model to be pre-empted | ||
// by the one added here. We'll resolve this unusual behavior by skipping the addition of the AutoValidateAntiforgeryTokenAttribute | ||
// when another already exists. | ||
if (!pageApplicationModel.Filters.OfType<IAntiforgeryPolicy>().Any()) | ||
{ | ||
// Always require an antiforgery token on post | ||
pageApplicationModel.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); | ||
} | ||
} | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
...AspNetCore.Mvc.RazorPages/Internal/AutoValidateAntiforgeryPageApplicationModelProvider.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...AspNetCore.Mvc.RazorPages.Test/AutoValidateAntiforgeryPageApplicationModelProviderTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Reflection; | ||
using Microsoft.AspNetCore.Mvc.ApplicationModels; | ||
using Microsoft.AspNetCore.Mvc.ViewFeatures; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.RazorPages | ||
{ | ||
public class AutoValidateAntiforgeryPageApplicationModelProviderTest | ||
{ | ||
[Fact] | ||
public void OnProvidersExecuting_AddsFiltersToModel() | ||
{ | ||
// Arrange | ||
var actionDescriptor = new PageActionDescriptor(); | ||
var applicationModel = new PageApplicationModel( | ||
actionDescriptor, | ||
typeof(object).GetTypeInfo(), | ||
new object[0]); | ||
var applicationModelProvider = new AutoValidateAntiforgeryPageApplicationModelProvider(); | ||
var context = new PageApplicationModelProviderContext(new PageActionDescriptor(), typeof(object).GetTypeInfo()) | ||
{ | ||
PageApplicationModel = applicationModel, | ||
}; | ||
|
||
// Act | ||
applicationModelProvider.OnProvidersExecuting(context); | ||
|
||
// Assert | ||
Assert.Collection( | ||
applicationModel.Filters, | ||
filter => Assert.IsType<AutoValidateAntiforgeryTokenAttribute>(filter)); | ||
} | ||
|
||
[Fact] | ||
public void OnProvidersExecuting_DoesNotAddAutoValidateAntiforgeryTokenAttribute_IfIgnoreAntiforgeryTokenAttributeExists() | ||
{ | ||
// Arrange | ||
var expected = new IgnoreAntiforgeryTokenAttribute(); | ||
|
||
var descriptor = new PageActionDescriptor(); | ||
var provider = new AutoValidateAntiforgeryPageApplicationModelProvider(); | ||
var context = new PageApplicationModelProviderContext(descriptor, typeof(object).GetTypeInfo()) | ||
{ | ||
PageApplicationModel = new PageApplicationModel(descriptor, typeof(object).GetTypeInfo(), Array.Empty<object>()) | ||
{ | ||
Filters = { expected }, | ||
}, | ||
}; | ||
|
||
// Act | ||
provider.OnProvidersExecuting(context); | ||
|
||
// Assert | ||
Assert.Collection( | ||
context.PageApplicationModel.Filters, | ||
actual => Assert.Same(expected, actual)); | ||
} | ||
|
||
[Fact] | ||
public void OnProvidersExecuting_DoesNotAddAutoValidateAntiforgeryTokenAttribute_IfAntiforgeryPolicyExists() | ||
{ | ||
// Arrange | ||
var expected = Mock.Of<IAntiforgeryPolicy>(); | ||
|
||
var descriptor = new PageActionDescriptor(); | ||
var provider = new AutoValidateAntiforgeryPageApplicationModelProvider(); | ||
var context = new PageApplicationModelProviderContext(descriptor, typeof(object).GetTypeInfo()) | ||
{ | ||
PageApplicationModel = new PageApplicationModel(descriptor, typeof(object).GetTypeInfo(), Array.Empty<object>()) | ||
{ | ||
Filters = { expected }, | ||
}, | ||
}; | ||
|
||
// Act | ||
provider.OnProvidersExecuting(context); | ||
|
||
// Assert | ||
Assert.Collection( | ||
context.PageApplicationModel.Filters, | ||
actual => Assert.Same(expected, actual)); | ||
} | ||
} | ||
} |
36 changes: 0 additions & 36 deletions
36
...tCore.Mvc.RazorPages.Test/Internal/AutoValidateAntiforgeryPageApplicationModelProvider.cs
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
test/WebSites/RazorPagesWebSite/Pages/Antiforgery/AntiforgeryDefault.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@page | ||
@model AntiforgeryDefaultModel | ||
<form method="post"> | ||
|
||
</form> |
18 changes: 18 additions & 0 deletions
18
test/WebSites/RazorPagesWebSite/Pages/Antiforgery/AntiforgeryDefault.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace RazorPagesWebSite | ||
{ | ||
public class AntiforgeryDefaultModel : PageModel | ||
{ | ||
public void OnGet() | ||
{ | ||
} | ||
|
||
public void OnPost() | ||
{ | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
test/WebSites/RazorPagesWebSite/Pages/Antiforgery/IgnoreAntiforgery.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@page | ||
@model IgnoreAntiforgeryModel | ||
<form method="post"> | ||
|
||
</form> |
Oops, something went wrong.