2
2
// See LICENSE for more details.
3
3
4
4
using System ;
5
- using System . Collections . Generic ;
6
5
using System . Security . Claims ;
7
- using System . Threading . Tasks ;
8
6
using Microsoft . AspNetCore . Authorization ;
9
7
using Microsoft . AspNetCore . Http ;
10
8
using Microsoft . Extensions . Primitives ;
@@ -35,7 +33,7 @@ public class ContentPermissionsQueryStringHandlerTests
35
33
public async Task Node_Id_From_Requirement_With_Permission_Is_Authorized ( )
36
34
{
37
35
var authHandlerContext = CreateAuthorizationHandlerContext ( NodeId ) ;
38
- var mockHttpContextAccessor = CreateMockHttpContextAccessor ( ) ;
36
+ var mockHttpContextAccessor = CreateMockHttpContextAccessorWithQueryStringValue ( ) ;
39
37
var sut = CreateHandler ( mockHttpContextAccessor . Object , NodeId , new [ ] { "A" } ) ;
40
38
41
39
await sut . HandleAsync ( authHandlerContext ) ;
@@ -47,7 +45,7 @@ public async Task Node_Id_From_Requirement_With_Permission_Is_Authorized()
47
45
public async Task Node_Id_From_Requirement_Without_Permission_Is_Not_Authorized ( )
48
46
{
49
47
var authHandlerContext = CreateAuthorizationHandlerContext ( NodeId ) ;
50
- var mockHttpContextAccessor = CreateMockHttpContextAccessor ( ) ;
48
+ var mockHttpContextAccessor = CreateMockHttpContextAccessorWithQueryStringValue ( ) ;
51
49
var sut = CreateHandler ( mockHttpContextAccessor . Object , NodeId , new [ ] { "B" } ) ;
52
50
53
51
await sut . HandleAsync ( authHandlerContext ) ;
@@ -60,7 +58,7 @@ public async Task Node_Id_From_Requirement_Without_Permission_Is_Not_Authorized(
60
58
public async Task Node_Id_Missing_From_Requirement_And_QueryString_Is_Authorized ( )
61
59
{
62
60
var authHandlerContext = CreateAuthorizationHandlerContext ( ) ;
63
- var mockHttpContextAccessor = CreateMockHttpContextAccessor ( "xxx" ) ;
61
+ var mockHttpContextAccessor = CreateMockHttpContextAccessorWithQueryStringValue ( "xxx" ) ;
64
62
var sut = CreateHandler ( mockHttpContextAccessor . Object , NodeId , new [ ] { "A" } ) ;
65
63
66
64
await sut . HandleAsync ( authHandlerContext ) ;
@@ -72,7 +70,7 @@ public async Task Node_Id_Missing_From_Requirement_And_QueryString_Is_Authorized
72
70
public async Task Node_Integer_Id_From_QueryString_With_Permission_Is_Authorized ( )
73
71
{
74
72
var authHandlerContext = CreateAuthorizationHandlerContext ( ) ;
75
- var mockHttpContextAccessor = CreateMockHttpContextAccessor ( queryStringValue : NodeId . ToString ( ) ) ;
73
+ var mockHttpContextAccessor = CreateMockHttpContextAccessorWithQueryStringValue ( queryStringValue : NodeId . ToString ( ) ) ;
76
74
var sut = CreateHandler ( mockHttpContextAccessor . Object , NodeId , new [ ] { "A" } ) ;
77
75
78
76
await sut . HandleAsync ( authHandlerContext ) ;
@@ -85,7 +83,21 @@ public async Task Node_Integer_Id_From_QueryString_With_Permission_Is_Authorized
85
83
public async Task Node_Integer_Id_From_QueryString_Without_Permission_Is_Not_Authorized ( )
86
84
{
87
85
var authHandlerContext = CreateAuthorizationHandlerContext ( ) ;
88
- var mockHttpContextAccessor = CreateMockHttpContextAccessor ( queryStringValue : NodeId . ToString ( ) ) ;
86
+ var mockHttpContextAccessor = CreateMockHttpContextAccessorWithQueryStringValue ( queryStringValue : NodeId . ToString ( ) ) ;
87
+ var sut = CreateHandler ( mockHttpContextAccessor . Object , NodeId , new [ ] { "B" } ) ;
88
+
89
+ await sut . HandleAsync ( authHandlerContext ) ;
90
+
91
+ Assert . IsFalse ( authHandlerContext . HasSucceeded ) ;
92
+ AssertContentCached ( mockHttpContextAccessor ) ;
93
+ }
94
+
95
+ [ Test ]
96
+ public async Task Node_Integer_Id_From_QueryString_Without_Permission_Is_Not_Authorized_Even_When_Additional_Parameter_For_Id_With_Permission_Is_Provided ( )
97
+ {
98
+ // Provides initially failing test and verifies fix for advisory https://github.com/umbraco/Umbraco-CMS/security/advisories/GHSA-wx5h-wqfq-v698
99
+ var authHandlerContext = CreateAuthorizationHandlerContext ( ) ;
100
+ var mockHttpContextAccessor = CreateMockHttpContextAccessorWithQueryStringValues ( queryStringValues : new [ ] { NodeId . ToString ( ) , 1001 . ToString ( ) } ) ;
89
101
var sut = CreateHandler ( mockHttpContextAccessor . Object , NodeId , new [ ] { "B" } ) ;
90
102
91
103
await sut . HandleAsync ( authHandlerContext ) ;
@@ -98,7 +110,7 @@ public async Task Node_Integer_Id_From_QueryString_Without_Permission_Is_Not_Aut
98
110
public async Task Node_Udi_Id_From_QueryString_With_Permission_Is_Authorized ( )
99
111
{
100
112
var authHandlerContext = CreateAuthorizationHandlerContext ( ) ;
101
- var mockHttpContextAccessor = CreateMockHttpContextAccessor ( queryStringValue : s_nodeUdi . ToString ( ) ) ;
113
+ var mockHttpContextAccessor = CreateMockHttpContextAccessorWithQueryStringValue ( queryStringValue : s_nodeUdi . ToString ( ) ) ;
102
114
var sut = CreateHandler ( mockHttpContextAccessor . Object , NodeId , new [ ] { "A" } ) ;
103
115
104
116
await sut . HandleAsync ( authHandlerContext ) ;
@@ -111,7 +123,7 @@ public async Task Node_Udi_Id_From_QueryString_With_Permission_Is_Authorized()
111
123
public async Task Node_Udi_Id_From_QueryString_Without_Permission_Is_Not_Authorized ( )
112
124
{
113
125
var authHandlerContext = CreateAuthorizationHandlerContext ( ) ;
114
- var mockHttpContextAccessor = CreateMockHttpContextAccessor ( queryStringValue : s_nodeUdi . ToString ( ) ) ;
126
+ var mockHttpContextAccessor = CreateMockHttpContextAccessorWithQueryStringValue ( queryStringValue : s_nodeUdi . ToString ( ) ) ;
115
127
var sut = CreateHandler ( mockHttpContextAccessor . Object , NodeId , new [ ] { "B" } ) ;
116
128
117
129
await sut . HandleAsync ( authHandlerContext ) ;
@@ -124,7 +136,7 @@ public async Task Node_Udi_Id_From_QueryString_Without_Permission_Is_Not_Authori
124
136
public async Task Node_Guid_Id_From_QueryString_With_Permission_Is_Authorized ( )
125
137
{
126
138
var authHandlerContext = CreateAuthorizationHandlerContext ( ) ;
127
- var mockHttpContextAccessor = CreateMockHttpContextAccessor ( queryStringValue : s_nodeGuid . ToString ( ) ) ;
139
+ var mockHttpContextAccessor = CreateMockHttpContextAccessorWithQueryStringValue ( queryStringValue : s_nodeGuid . ToString ( ) ) ;
128
140
var sut = CreateHandler ( mockHttpContextAccessor . Object , NodeId , new [ ] { "A" } ) ;
129
141
130
142
await sut . HandleAsync ( authHandlerContext ) ;
@@ -137,7 +149,7 @@ public async Task Node_Guid_Id_From_QueryString_With_Permission_Is_Authorized()
137
149
public async Task Node_Guid_Id_From_QueryString_Without_Permission_Is_Not_Authorized ( )
138
150
{
139
151
var authHandlerContext = CreateAuthorizationHandlerContext ( ) ;
140
- var mockHttpContextAccessor = CreateMockHttpContextAccessor ( queryStringValue : s_nodeGuid . ToString ( ) ) ;
152
+ var mockHttpContextAccessor = CreateMockHttpContextAccessorWithQueryStringValue ( queryStringValue : s_nodeGuid . ToString ( ) ) ;
141
153
var sut = CreateHandler ( mockHttpContextAccessor . Object , NodeId , new [ ] { "B" } ) ;
142
154
143
155
await sut . HandleAsync ( authHandlerContext ) ;
@@ -150,7 +162,7 @@ public async Task Node_Guid_Id_From_QueryString_Without_Permission_Is_Not_Author
150
162
public async Task Node_Invalid_Id_From_QueryString_Is_Authorized ( )
151
163
{
152
164
var authHandlerContext = CreateAuthorizationHandlerContext ( ) ;
153
- var mockHttpContextAccessor = CreateMockHttpContextAccessor ( queryStringValue : "invalid" ) ;
165
+ var mockHttpContextAccessor = CreateMockHttpContextAccessorWithQueryStringValue ( queryStringValue : "invalid" ) ;
154
166
var sut = CreateHandler ( mockHttpContextAccessor . Object , NodeId , new [ ] { "A" } ) ;
155
167
156
168
await sut . HandleAsync ( authHandlerContext ) ;
@@ -169,14 +181,20 @@ private static AuthorizationHandlerContext CreateAuthorizationHandlerContext(int
169
181
return new AuthorizationHandlerContext ( new List < IAuthorizationRequirement > { requirement } , user , resource ) ;
170
182
}
171
183
172
- private static Mock < IHttpContextAccessor > CreateMockHttpContextAccessor (
184
+ private static Mock < IHttpContextAccessor > CreateMockHttpContextAccessorWithQueryStringValue (
173
185
string queryStringName = QueryStringName ,
174
186
string queryStringValue = "" )
187
+ => CreateMockHttpContextAccessorWithQueryStringValues ( queryStringName , new [ ] { queryStringValue } ) ;
188
+
189
+ private static Mock < IHttpContextAccessor > CreateMockHttpContextAccessorWithQueryStringValues (
190
+ string queryStringName = QueryStringName ,
191
+ string [ ] ? queryStringValues = null )
175
192
{
193
+ queryStringValues ??= Array . Empty < string > ( ) ;
176
194
var mockHttpContextAccessor = new Mock < IHttpContextAccessor > ( ) ;
177
195
var mockHttpContext = new Mock < HttpContext > ( ) ;
178
196
var mockHttpRequest = new Mock < HttpRequest > ( ) ;
179
- var queryParams = new Dictionary < string , StringValues > { { queryStringName , queryStringValue } } ;
197
+ var queryParams = new Dictionary < string , StringValues > { { queryStringName , new StringValues ( queryStringValues ) } } ;
180
198
mockHttpRequest . SetupGet ( x => x . Query ) . Returns ( new QueryCollection ( queryParams ) ) ;
181
199
mockHttpContext . SetupGet ( x => x . Request ) . Returns ( mockHttpRequest . Object ) ;
182
200
mockHttpContext . SetupGet ( x => x . Items ) . Returns ( new Dictionary < object , object > ( ) ) ;
0 commit comments