Skip to content

C#: Add flow steps for View calls refering to Razor pages #14343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Nov 23, 2023

Conversation

joefarebrother
Copy link
Contributor

Adds a flow step from the model parameter of a View call to the Razor page it refers to using this view discovery system, when this can be determined statically.

@joefarebrother joefarebrother marked this pull request as ready for review October 24, 2023 16:26
@joefarebrother joefarebrother requested a review from a team as a code owner October 24, 2023 16:26
@joefarebrother joefarebrother changed the title [Draft] C#: Add XSS flow steps for View calls refering to Razor pages C#: Add XSS flow steps for View calls refering to Razor pages Oct 24, 2023
@joefarebrother joefarebrother changed the title C#: Add XSS flow steps for View calls refering to Razor pages C#: Add flow steps for View calls refering to Razor pages Oct 26, 2023
@michaelnebel michaelnebel self-requested a review October 27, 2023 07:21
Copy link
Contributor

@michaelnebel michaelnebel left a comment

Choose a reason for hiding this comment

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

Very nice work @joefarebrother !

result = this.getNameArgument()
or
not exists(this.getNameArgument()) and
result = this.getActionMethod().getName()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can action methods be generic? If so, we will probably need to strip away the <,...,> suffix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to this SO question, action methods cannot be generic.

Copy link
Contributor

@tamasvajk tamasvajk left a comment

Choose a reason for hiding this comment

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

Added some comments/questions mostly related to "MVC" vs "Razor Pages".

Also, a more general question:
Wouldn't we need to model flow from View("path", model) to an ExecuteAsync invocation of an instance of the page that has "path" after its Model property has been set to model?

Comment on lines 77 to 90
/**
* Gets the filepath of the source file that this class was generated from,
* relative to the application root.
*/
string getSourceFilepath() { result = attr.getArgument(2).(StringLiteral).getValue() }
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should remove the "relative to the application root" from the QLDoc. In case of standalone extraction the path is absolute. This probably also means that some improvements would be needed for matching the relative path in a View call and the absolute path in RazorCompiledItemAttribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you have an example of a database for which the generated files contain absolute paths?

Copy link
Contributor

Choose a reason for hiding this comment

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

Any standalone extracted web project will have that. So running the following produces one:

dotnet new mvc --name abc
CODEQL_EXTRACTOR_CSHARP_STANDALONE_EXTRACT_WEB_VIEWS=true codeql database create DB_standalone --language=csharp --source-root=abc -Obuildless=true

The DB contains A/DB_standalone/[...]/DB_standalone/working/razor/907F0B60B9944B50BE8465FB6805A19A/Microsoft.NET.Sdk.Razor.SourceGenerators/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/[...]_abc_Views_Home_Index_cshtml.g.cs which has the attribute with the absolute path:

[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCoreGeneratedDocument.[...]_abc_Views_Home_Index), @"mvc.1.0.view", @"[...]/abc/Views/Home/Index.cshtml")]

---
category: minorAnalysis
---
* Modelled additional flow steps to track flow from a `View` call in an MVC controller to the corresponding Razor page, which may result in additional results for queries such as `cs/web/xss`.
Copy link
Contributor

Choose a reason for hiding this comment

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

I would avoid using the term "Razor page". "Razor Pages" is a product name within the ASP.NET Core world. See https://learn.microsoft.com/en-us/aspnet/core/razor-pages/.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What term should be used instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

How about "Razor view", "Razor view file", or simply "view file"? The latter seems to be used in the docs.

}

/** A compiler-generated Razor page. */
class RazorPage extends Class {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as below, I would avoid using "Razor page". Also, I would call a .cshtml or .razor file a RazorPage, which is not the case here. Should we make the naming a bit more explicit to show that this is the generated RazorPageClass?

Also, should we follow model the class hierarchy used by ASP.NET Core? See the comment on the charpred.

Comment on lines 75 to 79
attr.getFile() = this.getFile() and
attr.getType()
.hasQualifiedName("Microsoft.AspNetCore.Razor.Hosting", "RazorCompiledItemAttribute")
Copy link
Contributor

Choose a reason for hiding this comment

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

Would looking for an RazorCompiledItemMetadataAttribute targeting the class instead work? So something like

attr.getTarget() = this and
attr.getType()
  .hasQualifiedName("Microsoft.AspNetCore.Razor.Hosting", "RazorCompiledItemMetadataAttribute")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have tried that at some point, and it seemed to not work due to that attribute not appearing in (non-standalone) DBs.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's odd and unfortunate. I guess it could also depend on the dotnet version being used. I'm somewhat worried that #14792 doesn't fix the assembly attribute extraction, and then we wouldn't find the RazorCompiledItemAttribute attributes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In my integration tests, the RazorCompiledItemAttribute attributes are successfully found.

// import PathGraph // exclude query predicates with output dependant on the absolute filepath the tests are run in
from XssNode source, XssNode sink, string message
where xssFlow(source, sink, message)
select sink, source, sink, "$@ flows to here and " + message, source, "User-provided value"

Check warning

Code scanning / CodeQL

Alert message style violation

Try to use a descriptive phrase instead of "here". Use "this location" if you can't get around mentioning the current location.
Copy link
Contributor

@tamasvajk tamasvajk left a comment

Choose a reason for hiding this comment

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

LGTM. Let's wait for feedback from the others too.

Have you checked the new alerts in tobyash86/WebGoat.NET, are those true positive new findings?

@joefarebrother
Copy link
Contributor Author

Yes, the new results in tobyash86/WebGoat.NET look like TPs.

Copy link
Contributor

@michaelnebel michaelnebel left a comment

Choose a reason for hiding this comment

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

LGTM! 👍

@joefarebrother
Copy link
Contributor Author

Should I worry about the failing integration tests? They don't appear to be related to these changes, but I've tied rerunning them a few times.

@michaelnebel
Copy link
Contributor

Should I worry about the failing integration tests? They don't appear to be related to these changes, but I've tied rerunning them a few times.

Yeah, we need them to pass.
Try and rebase again - they were just fixed a couple of days ago.

@joefarebrother joefarebrother merged commit 561b769 into github:main Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants