Skip to content

Conversation

@github-actions
Copy link
Contributor

You can preview this rule here (updated a few minutes after each push).

Review

A dedicated reviewer checked the rule description successfully for:

  • logical errors and incorrect information
  • information gaps and missing content
  • text style and tone
  • PR summary and labels follow the guidelines

@martin-strecker-sonarsource martin-strecker-sonarsource changed the title Create rule S6932 Create rule S6932: Use model binding instead of reading raw request data Feb 21, 2024
@zsolt-kolbay-sonarsource
Copy link
Contributor

zsolt-kolbay-sonarsource commented Feb 22, 2024

This can be extended to Razor Pages (despite not being in the scope of the sprint, the concept is the same).
The PageModel class also has a Request property, the same type as the Request property in MVC Controllers.
So the description will be pretty much the same. The later implementation as well. The only difference will be that instead of Controllers the rule will look through PageModel classes.

public class HomePageModel: PageModel
{
    public IActionResult OnPost()
    {
        var name = Request.Form["name"];   // Noncompliant
        ...
    }
}

Copy link
Contributor

@antonioaversa antonioaversa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have done a first round mainly analyzing code examples, for both ASP.NET Core and Framework.
Once all the comments on those are addressed, and the necessary information are included into the RSPEC, I will move to review the v2 of the RSPEC.

@antonioaversa
Copy link
Contributor

@martin-strecker-sonarsource Please check the CI:

Rule csharp:S6932 has a "How to fix it" section for an unsupported framework: "ASP.NET Core"

The framework mentioned in the "How to fix it" section should be included in the list of frameworks here.

Copy link
Contributor

@antonioaversa antonioaversa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are close to be done on code examples. I will then move the resulting RSPEC.
Please have a look at the message that @zsolt-kolbay-sonarsource has written here.

Copy link
Contributor

@antonioaversa antonioaversa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reviewed the table in the "How to fix it for ASP.NET Core".
It's great work: the table looks very nice and comprehensive!

I think it will help a lot the user to get basic understanding of what to do, when faced with this issue.

I think you can proceed to make something similar for ASP.NET Framework.

I wouldn't worry if you don't get to have the same level of detail for ASP.NET Framework. What is important to me is that we have at least the Replace column and a second column with the [FromX] attribute or the ISomething type. The rest is nice but not strictly necessary IMO.


== How to fix it in ASP.NET Core

`Request.Form`, `Request.Form.Files`, `Request.Headers`, `Request.Query` and `Request.RouteValues` are key/value collections that expose data from the incoming HTPP request:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, Request.Form.Files is not a key/value collection. It is an IEnumerable of IFormFile, that is not a KeyValuePair.
However, IFormFile has content, as well as Name, that can be considered as a "key" of sort.
So you either:

  • remove "key/value" and only talk about "collections"
  • or keep it as is, assuming a broader definition of "key/value collection"

Both are fine with me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTPP -> HTTP

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about "keyed collection"? This is a more generic term and a little bit further away from KeyValuePair<,> than "key/value collection".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good for me. I don't consider this a blocking change. I let you make the change, if you prefer so.


To replace the key/value collection access, you can:

[options="header"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remark for the implementation phase
The table looks incorrectly rendered on https://sonarsource.github.io/rspec and correctly rendered by the Visual Studio Code plugin:
image
image
The rendering of the table will need to be checked on Peach, once the rule is embedded into the analyzer.

"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, 5 minutes may not be far off the actual time.
Let's keep this for the time being. We may change it later based on Denis' opinion.
I keep this conversation open for Denis.

Copy link
Contributor

@antonioaversa antonioaversa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very close to completion. Remaining discussions are not blocking.
Can you please check Zsolt's comment here and provide an answer?
I think it makes sense to also support Razor pages.
If you would prefer to have a dedicated rule for that, creating a new Rule Idea and linking here would be enough.


protected override void Initialize(RequestContext requestContext)
{
_ = requestContext.HttpContext.Request.Form["id"]; // Compliant. Model binding is not supported here
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all other compliant/non-compliant examples you use : as a separator (that is also in line with the results of this poll), but here, and in all following examples . is used. I would be coherent throughout the entire file, and use : everywhere.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed sending some pending comments. They might be outdated by now.

_ = Request.Form.Get(@"key"); // Noncompliant
_ = Request.Form.GetValues(@"key"); // Noncompliant

const string key = "id";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will not handle the VariableDeclarationSyntax. We will call semanticModel.GetConstantValue(argument.Expression) instead. Do you want me to handle the whole constant expressions section of the spec?

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#1223-constant-expressions

@antonioaversa
Copy link
Contributor

We will not handle the VariableDeclarationSyntax. We will call semanticModel.GetConstantValue(argument.Expression) instead. Do you want me to handle the whole constant expressions section of the spec?

No, knowing a bit how the implementation will look like, I think cover all constant expressions would be an overkill that doesn't bring much value. It's good as it is IMO.

@antonioaversa
Copy link
Contributor

antonioaversa commented Feb 29, 2024

@martin-strecker-sonarsource

I missed sending some pending comments. They might be outdated by now.

I went through all resolved conversations and checked whether there was something new.
It should be all good now.

@sonarqube-next
Copy link

Quality Gate passed Quality Gate passed for 'rspec-frontend'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

@sonarqube-next
Copy link

Quality Gate passed Quality Gate passed for 'rspec-tools'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

@martin-strecker-sonarsource
Copy link
Contributor

@zsolt-kolbay-sonarsource Thank you for bringing this up. I did not have the time to look into this in more detail, but you are right that this looks like a low-hanging fruit. I added comments next to the parameterized tests, that we can extend these to PageModel 2015820. Such an addition needs more tests and should result in an update of the RSpec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants