Skip to content

Commit 7d437a4

Browse files
committed
fixes setting paramName only when it is not null
Fixes: gh-6223
1 parent 1a02caf commit 7d437a4

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

core/src/main/java/org/springframework/security/access/expression/method/MethodSecurityEvaluationContext.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ private void addArgumentsAsVariables() {
112112
}
113113

114114
for (int i = 0; i < args.length; i++) {
115-
super.setVariable(paramNames[i], args[i]);
115+
if (paramNames[i] != null) {
116+
setVariable(paramNames[i], args[i]);
117+
}
116118
}
117119
}
118120

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.access.expression.method;
17+
18+
import java.lang.reflect.Method;
19+
20+
import org.aopalliance.intercept.MethodInvocation;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.mockito.Mock;
24+
import org.mockito.junit.MockitoJUnitRunner;
25+
26+
import org.springframework.core.ParameterNameDiscoverer;
27+
import org.springframework.lang.Nullable;
28+
import org.springframework.security.core.Authentication;
29+
import org.springframework.util.ReflectionUtils;
30+
31+
import static org.mockito.Mockito.doReturn;
32+
33+
/**
34+
* @author shabarijonnalagadda
35+
*
36+
*/
37+
@RunWith(MockitoJUnitRunner.class)
38+
public class MethodSecurityEvaluationContextTests {
39+
@Mock
40+
private ParameterNameDiscoverer paramNameDiscoverer;
41+
@Mock
42+
private Authentication authentication;
43+
@Mock
44+
private MethodInvocation methodInvocation;
45+
46+
@Test
47+
public void lookupVariableWhenParameterNameNullThenNotSet() {
48+
Class<String> type = String.class;
49+
Method method = ReflectionUtils.findMethod(String.class, "contains", CharSequence.class);
50+
doReturn(new String[] {null}).when(paramNameDiscoverer).getParameterNames(method);
51+
doReturn(new Object[]{null}).when(methodInvocation).getArguments();
52+
doReturn(type).when(methodInvocation).getThis();
53+
doReturn(method).when(methodInvocation).getMethod();
54+
NotNullVariableMethodSecurityEvaluationContext context= new NotNullVariableMethodSecurityEvaluationContext(authentication, methodInvocation, paramNameDiscoverer);
55+
context.lookupVariable("testVariable");
56+
}
57+
58+
private static class NotNullVariableMethodSecurityEvaluationContext
59+
extends MethodSecurityEvaluationContext {
60+
61+
public NotNullVariableMethodSecurityEvaluationContext(Authentication auth, MethodInvocation mi,
62+
ParameterNameDiscoverer parameterNameDiscoverer) {
63+
super(auth, mi, parameterNameDiscoverer);
64+
}
65+
66+
@Override
67+
public void setVariable(String name, @Nullable Object value) {
68+
if ( name == null ) {
69+
throw new IllegalArgumentException("name should not be null");
70+
}
71+
else {
72+
super.setVariable(name, value);
73+
}
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)