-
Notifications
You must be signed in to change notification settings - Fork 2.1k
ASP.NET Core MVC 2.0 non asp-* attribute form elements now auto-insert anti-forgery tokens. #6204
Comments
Is there a better approach to this? Technically (meaning: tools) speaking this is no longer
This one causes invalid markup as the '!form' is not a valid tag or custom element tag name: |
@peterblazejewicz It's the first I have seen of it, but I imagine that the |
Can you explain why this change was made? Why is there an antiforgery token added when the target clearly isn’t handled by MVC (otherwise a It’s really not uncommon for HTML forms to exist without having an actual target and action, as a way to group form elements that are handled differently. For what it’s worth, form submission is not mandatory for a form element, so it does not make sense to default to that behavior with this tag helper. Especially not when neither I also don’t like this |
I agree 100% with @poke concerns. Also want to know the reasons behind this. |
Hey @NTaylorMullen, There was a prime directive at some point that tag helpers should only apply when you want them to be applied so as to not introduce magic. This sounds like magic to me. Opt-out is generally bad because people are not aware of it or will have them digging for documentation. What prompted this addition? I get it if it's security. I, and the rest of the community, just want to know the history of this change so we can rationalize it better. Thanks, |
This is a reaction to patterns that fall out of using Razor Pages. It is idiomatic in those cases to use a This is something we can certainly tweak in subsequent pre-releases before we finalize 2.0, so I encourage people to try it out in their apps with their use-cases and report back thoughts and findings to help us land on the best (and hopefully least compromised) design. |
Thanks for the quick reply @DamianEdwards |
Good to know @DamianEdwards |
@Bartmax it works for any tag, it basically forcefully opts an element out of tag helper processing. |
@DamianEdwards In my opinion, the razor pages feature shouldn’t be a reason to change how tag helpers are designed (to only run when explicitly requested by the author). I would like you to consider adding something for the razor pages use case that makes it clear to those using the razor pages that this also involves a tag helper. If posting to self is such a common use case, maybe we can just add an attribute for that then to make this behavior still explicit (e.g.
In my opinion, the logic should be something like this:
This would enable the token for the following examples: <form asp-antiforgery="true">
<form asp-controller="Account" asp-action="Login" method="post">
<form method="post"> But it would not be enabled for the following examples: <form asp-antiforgery="false" (ignoring all other attributes)>
<form action="/some/other/route" method="post">
<form> Since the tag helper now runs for every |
@poke I don't think this is significantly changing the design of Tag Helpers. Tag helpers already run on tags without explicitly asking for it. That's how the things like |
Target html tags without explicit telling to be processed by a tag helpers existed since v1. Anyone can author a tag helper to target html tags without any explicit parameters. This is not a change on how Tag Helpers are designed. @poke I think what's confusing is that there are 2 things going on.
For 1, as of right now, opting out of taghelpers without an explicit option provided by the author (like For 2, I think it's a valid reason to make the form tag helper targets the I don't see many scenarios when you will need to actually use If there's any scenario I am missing when you don't want an antiforgerytoken on a form post please share. |
Why not fix this particular thing with some sort of analyzer-like behavior? Is it really important that the The mentioned conditions imho should simply issue a warning that can be removed by adding either The downside is when you're not using VS or VS Code, you'll get - what is it? 403? - which you know how to fix - people that use notepad do, right!? |
Wouldn't this be about a two-minute fix for anyone running into the problem you described as a result of the "idiom?" They write |
@SolderedMushroom it doesn't give them a specific error actually, it simply replies with a 400 Bad Request, which makes it kinda hard to diagnose. |
Doesn't the developer exception page report the specifics of any 400 or 500 responses? |
Only 500 |
We are closing this issue because no further action is planned for this issue. If you still have any issues or questions, please log a new issue with any additional details that you have. |
The change
In ASP.NET Core MVC 2.0 the
FormTagHelper
from theMicrosoft.AspNetCore.Mvc.TagHelpers
assembly now injects anti-forgery tokens for plain form elements (elements that don't haveasp-*
). This happens under the following conditions:method="post"
attributeaction
attribute is empty. e.g.action=""
action
attribute is not suppliedWorkarounds
Explicitly turn off anti-forgery
Opt your form element out of TagHelperification by using the
!
symbol.Remove the
FormTagHelper
entirely from the view.@removeTagHelper Microsoft.AspNetCore.Mvc.TagHelpers.FormTagHelper, Microsoft.AspNetCore.Mvc.TagHelpers
See announcement: aspnet/Announcements#233
The text was updated successfully, but these errors were encountered: