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