Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Update how FormTagHelper handles get method attributes. #6207

Merged
merged 1 commit into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions src/Microsoft.AspNetCore.Mvc.TagHelpers/FormTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.CopyHtmlAttribute(nameof(Method), context);
}
else
{
Method = "get";
}

var antiforgeryDefault = true;

Expand Down Expand Up @@ -205,9 +201,16 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
if (string.IsNullOrEmpty(attributeValue))
{
// User is using the FormTagHelper like a normal <form> tag that has an empty or complex IHtmlContent action attribute.
// e.g. <form action="" method="post"> or <form action="@CustomUrlIHtmlContent" method="post">
// e.g. <form action="" method="..."> or <form action="@CustomUrlIHtmlContent" method="...">

// Antiforgery default is already set to true
if (string.Equals(Method ?? "get", "get", StringComparison.OrdinalIgnoreCase))
{
antiforgeryDefault = false;
}
else
{
// Antiforgery default is already set to true
}
}
else
{
Expand Down Expand Up @@ -251,17 +254,18 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
}

TagBuilder tagBuilder = null;
if (Action == null &&
Controller == null &&
Route == null &&
_routeValues == null &&
Fragment == null &&
Area == null &&
if (Action == null &&
Controller == null &&
Route == null &&
_routeValues == null &&
Fragment == null &&
Area == null &&
Page == null &&
// Antiforgery will sometime be set globally via TagHelper Initializers, verify it was provided in the cshtml.
!context.AllAttributes.ContainsName(AntiforgeryAttributeName))
{
// Empty form tag such as <form></form>. Let it flow to the output as-is and only handle anti-forgery.
// A <form> tag that doesn't utilize asp-* attributes. Let it flow to the output.
Method = Method ?? "get";
}
else if (pageLink)
{
Expand Down Expand Up @@ -304,11 +308,11 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
output.PostContent.AppendHtml(tagBuilder.InnerHtml);
}
}
}

if (string.Equals(Method, "get", StringComparison.OrdinalIgnoreCase))
{
antiforgeryDefault = false;
if (string.Equals(Method, "get", StringComparison.OrdinalIgnoreCase))
{
antiforgeryDefault = false;
}
}

if (Antiforgery ?? antiforgeryDefault)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<form method="post"><input name="__RequestVerificationToken" type="hidden" value="{0}" /></form>
<form action="" method="post"><input name="__RequestVerificationToken" type="hidden" value="{0}" /></form>
<form action="/Foo/Bar/Baz.html" method="get"></form>
<form action="/Foo/Bar/Baz.html" method="post"></form>
<form action="/Foo/Bar/Baz.html" method="post"></form>
<form action="/RedirectToPage" method="post"><input name="__RequestVerificationToken" type="hidden" value="{0}" /></form>
57 changes: 57 additions & 0 deletions test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/FormTagHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,63 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
{
public class FormTagHelperTest
{
[Fact]
public async Task ProcessAsync_ActionAndControllerGenerateAntiforgery()
{
// Arrange
var expectedTagName = "form";
var metadataProvider = new TestModelMetadataProvider();
var tagHelperContext = new TagHelperContext(
tagName: "form",
allAttributes: new TagHelperAttributeList()
{
{ "asp-action", "index" },
{ "asp-controller", "home" }
},
items: new Dictionary<object, object>(),
uniqueId: "test");
var output = new TagHelperOutput(
expectedTagName,
attributes: new TagHelperAttributeList(),
getChildContentAsync: (useCachedResult, encoder) =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var urlHelper = new Mock<IUrlHelper>();
urlHelper
.Setup(mock => mock.Action(It.IsAny<UrlActionContext>())).Returns("home/index");
var htmlGenerator = new TestableHtmlGenerator(metadataProvider, urlHelper.Object);
var viewContext = TestableHtmlGenerator.GetViewContext(
model: null,
htmlGenerator: htmlGenerator,
metadataProvider: metadataProvider);
var expectedPostContent = HtmlContentUtilities.HtmlContentToString(
htmlGenerator.GenerateAntiforgery(viewContext),
HtmlEncoder.Default);
var formTagHelper = new FormTagHelper(htmlGenerator)
{
ViewContext = viewContext,
Action = "index",
Controller = "home",
};

// Act
await formTagHelper.ProcessAsync(tagHelperContext, output);

// Assert
Assert.Equal(2, output.Attributes.Count);
var attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("method"));
Assert.Equal("post", attribute.Value);
attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("action"));
Assert.Equal("home/index", attribute.Value);
Assert.Empty(output.PreContent.GetContent());
Assert.True(output.Content.GetContent().Length == 0);
Assert.Equal(expectedPostContent, output.PostContent.GetContent());
Assert.Equal(expectedTagName, output.TagName);
}

[Fact]
public async Task ProcessAsync_AspAntiforgeryAloneGeneratesProperFormTag()
{
Expand Down
3 changes: 2 additions & 1 deletion test/WebSites/RazorPagesWebSite/SimpleForms.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<form method="post"></form>
<form action="" method="post"></form>
<form action="/Foo/Bar/Baz.html" method="get"></form>
<form action="/Foo/Bar/Baz.html" method="post"></form>
<form action="/Foo/Bar/Baz.html" method="post"></form>
<form asp-controller="Redirect" asp-action="RedirectToPageAction"></form>