Skip to content

Commit 8bae79d

Browse files
committed
Components file
1 parent f35fea3 commit 8bae79d

File tree

1 file changed

+136
-0
lines changed
  • csharp/ql/lib/semmle/code/csharp/frameworks/microsoft/aspnetcore

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/** Provides classes for working with `Microsoft.AspNetCore.Components` */
2+
3+
import csharp
4+
import semmle.code.csharp.frameworks.Microsoft
5+
import semmle.code.csharp.frameworks.microsoft.AspNetCore
6+
7+
/** The `Microsoft.AspNetCore.Components` namespace */
8+
class MicrosoftAspNetCoreComponentsNamespace extends Namespace {
9+
MicrosoftAspNetCoreComponentsNamespace() {
10+
this.getParentNamespace() instanceof MicrosoftAspNetCoreNamespace and
11+
this.hasName("Components")
12+
}
13+
}
14+
15+
/**
16+
* A class in the `Microsoft.AspNetCore.Components` namespace.
17+
*/
18+
private class MicrosoftAspNetCoreComponentsClass extends Class {
19+
MicrosoftAspNetCoreComponentsClass() {
20+
this.getNamespace() instanceof MicrosoftAspNetCoreComponentsNamespace
21+
}
22+
}
23+
24+
/** The `Microsoft.AspNetCore.Components.ParameterAttribute` class. */
25+
class MicrosoftAspNetCoreComponentsParamaterAttributeClass extends MicrosoftAspNetCoreComponentsClass
26+
{
27+
MicrosoftAspNetCoreComponentsParamaterAttributeClass() { this.hasName("ParameterAttribute") }
28+
}
29+
30+
/** The `Microsoft.AspNetCore.Components.CascadingParameterAttributeBase` class. */
31+
class MicrosoftAspNetCoreComponentsCascadingParameterAttributeBaseClass extends MicrosoftAspNetCoreComponentsClass
32+
{
33+
MicrosoftAspNetCoreComponentsCascadingParameterAttributeBaseClass() {
34+
this.hasName("CascadingParameterAttributeBase")
35+
}
36+
}
37+
38+
/** The `Microsoft.AspNetCore.Components.ComponentBase` class. */
39+
class MicrosoftAspNetCoreComponentsComponentBaseClass extends MicrosoftAspNetCoreComponentsClass {
40+
MicrosoftAspNetCoreComponentsComponentBaseClass() { this.hasName("ComponentBase") }
41+
}
42+
43+
/** The `Microsoft.AspNetCore.Components.IComponent` interface. */
44+
class MicrosoftAspNetCoreComponentsIComponentInterface extends Interface {
45+
MicrosoftAspNetCoreComponentsIComponentInterface() {
46+
this.getNamespace() instanceof MicrosoftAspNetCoreComponentsNamespace and
47+
this.hasName("IComponent")
48+
}
49+
}
50+
51+
/** The `Microsoft.AspNetCore.Components.RouteAttribute` attribute. */
52+
private class MicrosoftAspNetCoreComponentsRouteAttribute extends Attribute {
53+
MicrosoftAspNetCoreComponentsRouteAttribute() {
54+
this.getType().getNamespace() instanceof MicrosoftAspNetCoreComponentsNamespace and
55+
this.getType().hasName("RouteAttribute")
56+
}
57+
}
58+
59+
/** The `Microsoft.AspNetCore.Components.ParameterAttribute` attribute. */
60+
private class MicrosoftAspNetCoreComponentsParameterAttribute extends Attribute {
61+
MicrosoftAspNetCoreComponentsParameterAttribute() {
62+
this.getType().getNamespace() instanceof MicrosoftAspNetCoreComponentsNamespace and
63+
this.getType().hasName("ParameterAttribute")
64+
}
65+
}
66+
67+
/** An ASP.NET Core (Blazor) component. */
68+
class MicrosoftAspNetCoreComponentsComponent extends Class {
69+
MicrosoftAspNetCoreComponentsComponent() {
70+
this.getABaseType+() instanceof MicrosoftAspNetCoreComponentsComponentBaseClass or
71+
this.getABaseType+() instanceof MicrosoftAspNetCoreComponentsIComponentInterface
72+
}
73+
74+
/** Gets a property whose value cascades down the component hierarchy. */
75+
Property getACascadingParameterProperty() {
76+
result = this.getAProperty() and
77+
result.getAnAttribute().getType().getBaseClass() instanceof
78+
MicrosoftAspNetCoreComponentsCascadingParameterAttributeBaseClass
79+
}
80+
81+
/** Gets the url for the route from the `Microsoft.AspNetCore.Components.RouteAttribute` of the component. */
82+
private string getRouteAttributeUrl() {
83+
exists(MicrosoftAspNetCoreComponentsRouteAttribute a | a = this.getAnAttribute() |
84+
result = a.getArgument(0).getValue()
85+
)
86+
}
87+
88+
/**
89+
* Gets a route parameter from the `Microsoft.AspNetCore.Components.RouteAttribute` of the component.
90+
*
91+
* A route parameter is defined in the URL by wrapping its name in a pair of { braces } when adding a component's @page declaration.
92+
* There are various extensions that can be added next to the parameter name, such as `:int` or `?` to make the parameter optional.
93+
* Optionally, the parameter name can start with a `*` to make it a catch-all parameter.
94+
*
95+
* And example of a route parameter is `@page "/counter/{id:int}/{other?}/{*rest}"`, from this we're getting the `id`, `other` and `rest` parameters.
96+
*/
97+
private string getARouteParameter() {
98+
result = this.getRouteAttributeUrl().splitAt("{").regexpCapture("\\*?([^:?}]+)[:?}](.*)", 1)
99+
}
100+
101+
/** Gets a property attributed with `[Parameter]` attribute. */
102+
Property getAParameterProperty() {
103+
result = this.getAProperty() and
104+
result.getAnAttribute() instanceof MicrosoftAspNetCoreComponentsParameterAttribute
105+
}
106+
107+
/** Gets a property whose value is populated from route parameters. */
108+
Property getARouteParameterProperty() {
109+
result = this.getAParameterProperty() and
110+
exists(string urlParamName | urlParamName = this.getARouteParameter() |
111+
result.getName().toLowerCase() = urlParamName.toLowerCase()
112+
)
113+
}
114+
}
115+
116+
private module Sources {
117+
private import semmle.code.csharp.security.dataflow.flowsources.Remote
118+
119+
/**
120+
* A property with a `[Parameter]` attribute in an ASP.NET Core component which
121+
* is populated from a route parameter.
122+
*/
123+
private class AspNetCoreComponentRouteParameterFlowSource extends AspNetRemoteFlowSource,
124+
DataFlow::ExprNode
125+
{
126+
AspNetCoreComponentRouteParameterFlowSource() {
127+
exists(MicrosoftAspNetCoreComponentsComponent c, Property p |
128+
p = c.getARouteParameterProperty()
129+
|
130+
this.asExpr() = p.getGetter().getACall()
131+
)
132+
}
133+
134+
override string getSourceType() { result = "ASP.NET Core component route parameter" }
135+
}
136+
}

0 commit comments

Comments
 (0)