Skip to content

Commit a9fbb70

Browse files
committed
Added null checks and tests to constructors
RequestKey, JaasGrantedAuthority, and SwitchUserGrantedAuthority assume certain final members are non-null. Issue: spring-projectsgh-6892
1 parent e159223 commit a9fbb70

File tree

6 files changed

+123
-4
lines changed

6 files changed

+123
-4
lines changed

core/src/main/java/org/springframework/security/authentication/jaas/JaasGrantedAuthority.java

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.security.core.GrantedAuthority;
2020
import org.springframework.security.core.SpringSecurityCoreVersion;
21+
import org.springframework.util.Assert;
2122

2223
import java.security.Principal;
2324

@@ -37,6 +38,8 @@ public final class JaasGrantedAuthority implements GrantedAuthority {
3738
private final Principal principal;
3839

3940
public JaasGrantedAuthority(String role, Principal principal) {
41+
Assert.notNull(role, "role cannot be null");
42+
Assert.notNull(principal, "principal cannot be null");
4043
this.role = role;
4144
this.principal = principal;
4245
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2002-2019 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+
* https://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+
17+
package org.springframework.security.authentication.jaas;
18+
19+
import org.junit.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
22+
import org.springframework.security.authentication.jaas.JaasGrantedAuthority;
23+
24+
/**
25+
*
26+
* @author Clement Ng
27+
*
28+
*/
29+
public class JaasGrantedAuthorityTests {
30+
31+
/**
32+
* @throws Exception
33+
*/
34+
@Test
35+
public void authorityWithNullRoleFailsAssertion() throws Exception {
36+
assertThatThrownBy(() -> new JaasGrantedAuthority(null, null))
37+
.isInstanceOf(IllegalArgumentException.class)
38+
.hasMessageContaining("role cannot be null");
39+
}
40+
41+
/**
42+
* @throws Exception
43+
*/
44+
@Test
45+
public void authorityWithNullPrincipleFailsAssertion() throws Exception {
46+
assertThatThrownBy(() -> new JaasGrantedAuthority("role", null))
47+
.isInstanceOf(IllegalArgumentException.class)
48+
.hasMessageContaining("principal cannot be null");
49+
}
50+
}

web/src/main/java/org/springframework/security/web/access/intercept/RequestKey.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.security.web.access.intercept;
1717

18+
import org.springframework.util.Assert;
19+
1820
/**
1921
* @author Luke Taylor
2022
* @since 2.0
@@ -28,6 +30,7 @@ public RequestKey(String url) {
2830
}
2931

3032
public RequestKey(String url, String method) {
33+
Assert.notNull(url, "url cannot be null");
3134
this.url = url;
3235
this.method = method;
3336
}

web/src/main/java/org/springframework/security/web/authentication/switchuser/SwitchUserGrantedAuthority.java

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.security.core.Authentication;
2020
import org.springframework.security.core.GrantedAuthority;
2121
import org.springframework.security.core.SpringSecurityCoreVersion;
22+
import org.springframework.util.Assert;
2223

2324
/**
2425
* Custom {@code GrantedAuthority} used by
@@ -44,6 +45,8 @@ public final class SwitchUserGrantedAuthority implements GrantedAuthority {
4445
// ===================================================================================================
4546

4647
public SwitchUserGrantedAuthority(String role, Authentication source) {
48+
Assert.notNull(role, "role cannot be null");
49+
Assert.notNull(source, "source cannot be null");
4750
this.role = role;
4851
this.source = source;
4952
}

web/src/test/java/org/springframework/security/web/access/intercept/RequestKeyTests.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,9 +15,10 @@
1515
*/
1616
package org.springframework.security.web.access.intercept;
1717

18-
import static org.assertj.core.api.Assertions.*;
19-
2018
import org.junit.Test;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2122
import org.springframework.security.web.access.intercept.RequestKey;
2223

2324
/**
@@ -63,4 +64,14 @@ public void keysWithDifferentUrlsAreNotEquals() {
6364
assertThat(key1.equals(key2)).isFalse();
6465
assertThat(key2.equals(key1)).isFalse();
6566
}
67+
68+
/**
69+
* @throws Exception
70+
*/
71+
@Test
72+
public void keysWithNullUrlFailsAssertion() throws Exception {
73+
assertThatThrownBy(() -> new RequestKey(null, null))
74+
.isInstanceOf(IllegalArgumentException.class)
75+
.hasMessage("url cannot be null");
76+
}
6677
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2002-2019 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+
* https://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.web.authentication.switchuser;
17+
18+
import org.junit.Test;
19+
20+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
21+
import org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority;
22+
23+
/**
24+
*
25+
* @author Clement Ng
26+
*
27+
*/
28+
public class SwitchUserGrantedAuthorityTests {
29+
30+
/**
31+
* @throws Exception
32+
*/
33+
@Test
34+
public void authorityWithNullRoleFailsAssertion() throws Exception {
35+
assertThatThrownBy(() -> new SwitchUserGrantedAuthority(null, null))
36+
.isInstanceOf(IllegalArgumentException.class)
37+
.hasMessage("role cannot be null");
38+
}
39+
40+
/**
41+
* @throws Exception
42+
*/
43+
@Test
44+
public void authorityWithNullSourceFailsAssertion() throws Exception {
45+
assertThatThrownBy(() -> new SwitchUserGrantedAuthority("role", null))
46+
.isInstanceOf(IllegalArgumentException.class)
47+
.hasMessage("source cannot be null");
48+
}
49+
}

0 commit comments

Comments
 (0)