-
Notifications
You must be signed in to change notification settings - Fork 1.9k
C#: Blazor: Add route parameters as remote flow sources #18664
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
egregius313
merged 12 commits into
github:main
from
egregius313:egregius313/csharp/blazor/url-param-sources
Feb 7, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8bae79d
Components file
egregius313 6ae7ede
Add Blazor components file to Remote sources module
egregius313 aaefa0f
Add remote flow source test
egregius313 414c0a6
Fix test results
egregius313 5236a40
Remove unnecessary class
egregius313 eb25c76
Change note
egregius313 07aad61
Typo
egregius313 12ebfa6
Change join order of Property/Name matching
egregius313 a783ac1
Add QL tests for remoteFlowSource
egregius313 274a2d8
Remove remoteFlowSource integration test
egregius313 0a817eb
Fix test expectations
egregius313 29d03db
Remove unneeded disjunction
egregius313 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
csharp/ql/lib/change-notes/2025-02-03-blazor-routing-parameters.md
This file contains hidden or 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,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Blazor `[Parameter]` fields bound to a variable from the route specified in the `@page` directive are now modeled as remote flow sources. |
135 changes: 135 additions & 0 deletions
135
csharp/ql/lib/semmle/code/csharp/frameworks/microsoft/aspnetcore/Components.qll
This file contains hidden or 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,135 @@ | ||
| /** Provides classes for working with `Microsoft.AspNetCore.Components` */ | ||
|
|
||
| import csharp | ||
| import semmle.code.csharp.frameworks.Microsoft | ||
| import semmle.code.csharp.frameworks.microsoft.AspNetCore | ||
|
|
||
| /** The `Microsoft.AspNetCore.Components` namespace */ | ||
| class MicrosoftAspNetCoreComponentsNamespace extends Namespace { | ||
| MicrosoftAspNetCoreComponentsNamespace() { | ||
| this.getParentNamespace() instanceof MicrosoftAspNetCoreNamespace and | ||
| this.hasName("Components") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A class in the `Microsoft.AspNetCore.Components` namespace. | ||
| */ | ||
| private class MicrosoftAspNetCoreComponentsClass extends Class { | ||
| MicrosoftAspNetCoreComponentsClass() { | ||
| this.getNamespace() instanceof MicrosoftAspNetCoreComponentsNamespace | ||
| } | ||
| } | ||
|
|
||
| /** The `Microsoft.AspNetCore.Components.CascadingParameterAttributeBase` class. */ | ||
| class MicrosoftAspNetCoreComponentsCascadingParameterAttributeBaseClass extends MicrosoftAspNetCoreComponentsClass | ||
| { | ||
| MicrosoftAspNetCoreComponentsCascadingParameterAttributeBaseClass() { | ||
| this.hasName("CascadingParameterAttributeBase") | ||
| } | ||
| } | ||
|
|
||
| /** The `Microsoft.AspNetCore.Components.ComponentBase` class. */ | ||
| class MicrosoftAspNetCoreComponentsComponentBaseClass extends MicrosoftAspNetCoreComponentsClass { | ||
| MicrosoftAspNetCoreComponentsComponentBaseClass() { this.hasName("ComponentBase") } | ||
| } | ||
|
|
||
| /** The `Microsoft.AspNetCore.Components.IComponent` interface. */ | ||
| class MicrosoftAspNetCoreComponentsIComponentInterface extends Interface { | ||
| MicrosoftAspNetCoreComponentsIComponentInterface() { | ||
| this.getNamespace() instanceof MicrosoftAspNetCoreComponentsNamespace and | ||
| this.hasName("IComponent") | ||
| } | ||
| } | ||
|
|
||
| /** The `Microsoft.AspNetCore.Components.RouteAttribute` attribute. */ | ||
| private class MicrosoftAspNetCoreComponentsRouteAttribute extends Attribute { | ||
| MicrosoftAspNetCoreComponentsRouteAttribute() { | ||
| this.getType().getNamespace() instanceof MicrosoftAspNetCoreComponentsNamespace and | ||
| this.getType().hasName("RouteAttribute") | ||
| } | ||
| } | ||
|
|
||
| /** The `Microsoft.AspNetCore.Components.ParameterAttribute` attribute. */ | ||
| private class MicrosoftAspNetCoreComponentsParameterAttribute extends Attribute { | ||
| MicrosoftAspNetCoreComponentsParameterAttribute() { | ||
| this.getType().getNamespace() instanceof MicrosoftAspNetCoreComponentsNamespace and | ||
| this.getType().hasName("ParameterAttribute") | ||
| } | ||
| } | ||
|
|
||
| /** An ASP.NET Core (Blazor) component. */ | ||
| class MicrosoftAspNetCoreComponentsComponent extends Class { | ||
| MicrosoftAspNetCoreComponentsComponent() { | ||
| this.getABaseType+() instanceof MicrosoftAspNetCoreComponentsComponentBaseClass or | ||
| this.getABaseType+() instanceof MicrosoftAspNetCoreComponentsIComponentInterface | ||
| } | ||
|
|
||
| /** Gets a property whose value cascades down the component hierarchy. */ | ||
| Property getACascadingParameterProperty() { | ||
| result = this.getAProperty() and | ||
| result.getAnAttribute().getType().getBaseClass() instanceof | ||
| MicrosoftAspNetCoreComponentsCascadingParameterAttributeBaseClass | ||
| } | ||
|
|
||
| /** Gets the url for the route from the `Microsoft.AspNetCore.Components.RouteAttribute` of the component. */ | ||
| private string getRouteAttributeUrl() { | ||
| exists(MicrosoftAspNetCoreComponentsRouteAttribute a | a = this.getAnAttribute() | | ||
| result = a.getArgument(0).getValue() | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Gets a route parameter from the `Microsoft.AspNetCore.Components.RouteAttribute` of the component. | ||
| * | ||
| * A route parameter is defined in the URL by wrapping its name in a pair of { braces } when adding a component's @page declaration. | ||
| * There are various extensions that can be added next to the parameter name, such as `:int` or `?` to make the parameter optional. | ||
| * Optionally, the parameter name can start with a `*` to make it a catch-all parameter. | ||
| * | ||
| * An example of a route parameter is `@page "/counter/{id:int}/{other?}/{*rest}"`, from this we're getting the `id`, `other` and `rest` parameters. | ||
| */ | ||
| pragma[nomagic] | ||
| private string getARouteParameter() { | ||
| exists(string s | | ||
| s = this.getRouteAttributeUrl().splitAt("{").regexpCapture("\\*?([^:?}]+)[:?}](.*)", 1) and | ||
| result = s.toLowerCase() | ||
| ) | ||
| } | ||
|
|
||
| /** Gets a property attributed with `[Parameter]` attribute. */ | ||
| pragma[nomagic] | ||
| private Property getAParameterProperty(string name) { | ||
| result = this.getAProperty() and | ||
| result.getAnAttribute() instanceof MicrosoftAspNetCoreComponentsParameterAttribute and | ||
| name = result.getName().toLowerCase() | ||
| } | ||
|
|
||
| /** Gets a property whose value is populated from route parameters. */ | ||
| Property getARouteParameterProperty() { | ||
| exists(string name | name = this.getARouteParameter() | | ||
| result = this.getAParameterProperty(name) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private module Sources { | ||
| private import semmle.code.csharp.security.dataflow.flowsources.Remote | ||
|
|
||
| /** | ||
| * A property with a `[Parameter]` attribute in an ASP.NET Core component which | ||
| * is populated from a route parameter. | ||
| */ | ||
| private class AspNetCoreComponentRouteParameterFlowSource extends AspNetRemoteFlowSource, | ||
| DataFlow::ExprNode | ||
| { | ||
| AspNetCoreComponentRouteParameterFlowSource() { | ||
| exists(MicrosoftAspNetCoreComponentsComponent c, Property p | | ||
| p = c.getARouteParameterProperty() | ||
| | | ||
| this.asExpr() = p.getGetter().getACall() | ||
| ) | ||
| } | ||
|
|
||
| override string getSourceType() { result = "ASP.NET Core component route parameter" } | ||
| } | ||
| } | ||
This file contains hidden or 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
124 changes: 124 additions & 0 deletions
124
...l/test/library-tests/frameworks/microsoft/aspnetcore/blazor/Components_MyInput_razor.g.cs
This file contains hidden or 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,124 @@ | ||
| // <auto-generated/> | ||
| #pragma warning disable 1591 | ||
| namespace BlazorTest.Components | ||
| { | ||
| #line default | ||
| using global::System; | ||
| using global::System.Collections.Generic; | ||
| using global::System.Linq; | ||
| using global::System.Threading.Tasks; | ||
| using global::Microsoft.AspNetCore.Components; | ||
| #nullable restore | ||
| using System.Net.Http | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using System.Net.Http.Json | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using Microsoft.AspNetCore.Components.Forms | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using Microsoft.AspNetCore.Components.Routing | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using Microsoft.AspNetCore.Components.Web | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using static Microsoft.AspNetCore.Components.Web.RenderMode | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using Microsoft.AspNetCore.Components.Web.Virtualization | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using Microsoft.JSInterop | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using BlazorTest | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using BlazorTest.Components | ||
|
|
||
| #line default | ||
| #line hidden | ||
| #nullable disable | ||
| ; | ||
| [global::BlazorTest.Components.MyInput.__PrivateComponentRenderModeAttribute] | ||
| #nullable restore | ||
| public partial class MyInput : global::Microsoft.AspNetCore.Components.ComponentBase | ||
| #nullable disable | ||
| { | ||
| #pragma warning disable 1998 | ||
| protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) | ||
| { | ||
| __builder.OpenElement(0, "input"); | ||
| __builder.AddAttribute(1, "value", global::Microsoft.AspNetCore.Components.BindConverter.FormatValue( | ||
| #nullable restore | ||
| Param1 | ||
|
|
||
| #line default | ||
| #line hidden | ||
| #nullable disable | ||
| )); | ||
| __builder.AddAttribute(2, "onchange", global::Microsoft.AspNetCore.Components.EventCallback.Factory.CreateBinder(this, global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredBindSetter(callback: __value => | ||
| { | ||
| Param1 = __value; return global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.InvokeAsynchronousDelegate(callback: | ||
| #nullable restore | ||
| Fire | ||
|
|
||
| #line default | ||
| #line hidden | ||
| #nullable disable | ||
| ); | ||
| }, value: Param1), Param1)); | ||
| __builder.SetUpdatesAttributeName("value"); | ||
| __builder.CloseElement(); | ||
| } | ||
| #pragma warning restore 1998 | ||
| #nullable restore | ||
|
|
||
| [Parameter] | ||
| public string? Param1 { get; set; } = ""; | ||
|
|
||
| [Parameter] | ||
| public EventCallback<string?> ValueChanged { get; set; } | ||
|
|
||
| [Parameter] | ||
| public EventCallback<string?> Param1Changed { get; set; } | ||
|
|
||
| private void Fire() | ||
| { | ||
| ValueChanged.InvokeAsync(Param1); | ||
| Param1Changed.InvokeAsync(Param1); | ||
| } | ||
|
|
||
| #line default | ||
| #line hidden | ||
| #nullable disable | ||
|
|
||
| private sealed class __PrivateComponentRenderModeAttribute : global::Microsoft.AspNetCore.Components.RenderModeAttribute | ||
| { | ||
| private static global::Microsoft.AspNetCore.Components.IComponentRenderMode ModeImpl => InteractiveServer | ||
| ; | ||
| public override global::Microsoft.AspNetCore.Components.IComponentRenderMode Mode => ModeImpl; | ||
| } | ||
| } | ||
| } | ||
| #pragma warning restore 1591 |
115 changes: 115 additions & 0 deletions
115
.../test/library-tests/frameworks/microsoft/aspnetcore/blazor/Components_MyOutput_razor.g.cs
This file contains hidden or 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,115 @@ | ||
| // <auto-generated/> | ||
| #pragma warning disable 1591 | ||
| namespace BlazorTest.Components | ||
| { | ||
| #line default | ||
| using global::System; | ||
| using global::System.Collections.Generic; | ||
| using global::System.Linq; | ||
| using global::System.Threading.Tasks; | ||
| using global::Microsoft.AspNetCore.Components; | ||
| #nullable restore | ||
| using System.Net.Http | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using System.Net.Http.Json | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using Microsoft.AspNetCore.Components.Forms | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using Microsoft.AspNetCore.Components.Routing | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using Microsoft.AspNetCore.Components.Web | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using static Microsoft.AspNetCore.Components.Web.RenderMode | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using Microsoft.AspNetCore.Components.Web.Virtualization | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using Microsoft.JSInterop | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using BlazorTest | ||
|
|
||
| #nullable disable | ||
| ; | ||
| #nullable restore | ||
| using BlazorTest.Components | ||
|
|
||
| #line default | ||
| #line hidden | ||
| #nullable disable | ||
| ; | ||
| [global::BlazorTest.Components.MyOutput.__PrivateComponentRenderModeAttribute] | ||
| #nullable restore | ||
| public partial class MyOutput : global::Microsoft.AspNetCore.Components.ComponentBase | ||
| #nullable disable | ||
| { | ||
| #pragma warning disable 1998 | ||
| protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) | ||
| { | ||
| __builder.OpenElement(0, "div"); | ||
| __builder.OpenElement(1, "p"); | ||
| __builder.AddContent(2, "Value from InputText: "); | ||
| __builder.AddContent(3, | ||
| #nullable restore | ||
| Value | ||
|
|
||
| #line default | ||
| #line hidden | ||
| #nullable disable | ||
| ); | ||
| __builder.CloseElement(); | ||
| __builder.AddMarkupContent(4, "\n "); | ||
| __builder.OpenElement(5, "p"); | ||
| __builder.AddContent(6, "Raw value from InputText: "); | ||
| __builder.AddContent(7, | ||
| #nullable restore | ||
| new MarkupString(Value) | ||
|
|
||
| #line default | ||
| #line hidden | ||
| #nullable disable | ||
| ); | ||
| __builder.CloseElement(); | ||
| __builder.CloseElement(); | ||
| } | ||
| #pragma warning restore 1998 | ||
| #nullable restore | ||
|
|
||
| [Parameter] | ||
| public string Value { get; set; } = ""; | ||
|
|
||
| #line default | ||
| #line hidden | ||
| #nullable disable | ||
|
|
||
| private sealed class __PrivateComponentRenderModeAttribute : global::Microsoft.AspNetCore.Components.RenderModeAttribute | ||
| { | ||
| private static global::Microsoft.AspNetCore.Components.IComponentRenderMode ModeImpl => InteractiveServer | ||
| ; | ||
| public override global::Microsoft.AspNetCore.Components.IComponentRenderMode Mode => ModeImpl; | ||
| } | ||
| } | ||
| } | ||
| #pragma warning restore 1591 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Redundant import Warning