You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Updated the TestDataProvider to ensure we remove duplicate routes from the EndpointRoute collection
(%release-note:
- Updated the EndpointTestDataProvider to ensure we remove duplicate routes from the EndpointRoute collection
- Updated the readme.md file
%)
@@ -82,11 +82,15 @@ You can create custom factory customizations and use them like the following exa
82
82
This demonstrates how you can test your queries and database context interactions using a custom web application factory and test claims.
83
83
84
84
85
-
###Authorization and Endpoint Security Testing Framework
85
+
## Authorization and Endpoint and Page Security Testing Framework
86
86
87
-
The **Endpoint Security Testing Framework** is a library designed to help you verify that all your API endpoints have the expected security configurations.
87
+
The **Endpoint and Page Security Testing Framework** is a library designed to help you verify that all your API endpoints have the expected security configurations.
88
88
It ensures that each controller and action has the appropriate authorization attributes and that your application's security policies are consistently enforced.
89
89
90
+
## Endpoint Security Validator
91
+
92
+
**Endpoint Security Validator** allows you to validate that endpoint in your .NET API has the correct security settings. The validator uses reflection along with a configuration file to enforce expected security requirements.
93
+
90
94
## Usage
91
95
92
96
To utilize the framework, follow these steps:
@@ -95,32 +99,34 @@ To utilize the framework, follow these steps:
95
99
96
100
Create a JSON file (e.g., `ExpectedSecurity.json`) that defines the expected security for each endpoint in your application. This file should include all controllers and actions.
97
101
98
-
```json
102
+
103
+
```json
104
+
{
105
+
"Endpoints": [
99
106
{
100
-
"Endpoints": [
101
-
{
102
-
"Controller": "SchoolsController",
103
-
"Action": "GetPrincipalBySchoolAsync",
104
-
"ExpectedSecurity": "Authorize: Policy=API.Read"
105
-
},
106
-
{
107
-
"Controller": "SchoolsController",
108
-
"Action": "GetPrincipalsBySchoolsAsync",
109
-
"ExpectedSecurity": "Authorize: Policy=API.Read"
110
-
},
111
-
{
112
-
"Controller": "SchoolsController",
113
-
"Action": "CreateSchoolAsync",
114
-
"ExpectedSecurity": "Authorize: Policy=API.Write"
115
-
},
116
-
{
117
-
"Controller": "SchoolsController",
118
-
"Action": "CreateReportAsync",
119
-
"ExpectedSecurity": "AllowAnonymous"
120
-
}
121
-
]
107
+
"Controller": "SchoolsController",
108
+
"Action": "GetPrincipalBySchoolAsync",
109
+
"ExpectedSecurity": "Authorize: Policy=API.Read"
110
+
},
111
+
{
112
+
"Controller": "SchoolsController",
113
+
"Action": "GetPrincipalsBySchoolsAsync",
114
+
"ExpectedSecurity": "Authorize: Policy=API.Read"
115
+
},
116
+
{
117
+
"Controller": "SchoolsController",
118
+
"Action": "CreateSchoolAsync",
119
+
"ExpectedSecurity": "Authorize: Policy=API.Write"
120
+
},
121
+
{
122
+
"Controller": "SchoolsController",
123
+
"Action": "CreateReportAsync",
124
+
"ExpectedSecurity": "AllowAnonymous"
122
125
}
123
-
```
126
+
]
127
+
}
128
+
```
129
+
124
130
125
131
### 2\. Write the Test Class
126
132
@@ -148,6 +154,124 @@ Create a test class in your test project that uses the framework to validate you
148
154
149
155
The above test will run a test per endpoint and ensures the expected security policy is applied to thje endpoint or the controller.
150
156
157
+
## Page Security Validator
158
+
159
+
**Page Security Validator** allows you to validate that each page in your ASP.NET Core application has the correct security settings. The validator uses route metadata along with a configuration file to enforce expected security requirements, including global authorization settings or route-specific configurations.
This configuration file should be set to always copy to the output directory by setting `Copy to Output Directory` to `Copy always` in your project settings.
185
+
186
+
187
+
### Understanding `_globalAuthorizationEnabled`
188
+
189
+
190
+
***When `_globalAuthorizationEnabled` is `true`:**
191
+
* This setting assumes **global security enforcement** is applied in `Startup.cs` (e.g., `AuthorizeFolder("/")`).
192
+
* By default, **all pages are expected to have the `Authorize` attribute**.
193
+
* The configuration file can specify exceptions to global authorization, such as `AllowAnonymous` or specific authorization policies or roles.
194
+
***When `_globalAuthorizationEnabled` is `false`:**
195
+
* Only the routes explicitly listed in the configuration file are validated.
196
+
* No global assumptions are made about other pages.
197
+
198
+
199
+
### 2\. Test Setup
200
+
201
+
The test setup includes:
202
+
203
+
* Instantiating the `AuthorizationTester`.
204
+
* Using `InitializeEndpoints` to retrieve all relevant endpoints.
205
+
* Loading security expectations from the JSON configuration file.
0 commit comments