Skip to content

Commit a97a404

Browse files
tumbl3w33dmichael-doubez
authored andcommitted
Add test for redirect url validation
1 parent c04ea98 commit a97a404

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/main/java/org/jenkinsci/plugins/oic/OicSecurityRealm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ protected AuthorizationCodeFlow buildAuthorizationCodeFlow() {
730730
return builder.build();
731731
}
732732

733-
private String getValidRedirectUrl(String url) {
733+
protected String getValidRedirectUrl(String url) {
734734
if (url != null && !url.isEmpty()) {
735735
// Check if the URL is relative and starts with a slash
736736
if (url.startsWith("/")) {

src/test/java/org/jenkinsci/plugins/oic/OicSecurityRealmTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.github.tomakehurst.wiremock.junit.WireMockRule;
55
import hudson.util.Secret;
66
import java.io.IOException;
7+
import java.net.MalformedURLException;
8+
79
import org.acegisecurity.AuthenticationManager;
810
import org.acegisecurity.BadCredentialsException;
911
import org.acegisecurity.GrantedAuthority;
@@ -16,6 +18,7 @@
1618

1719
import static org.junit.Assert.assertEquals;
1820
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertThrows;
1922

2023
public class OicSecurityRealmTest {
2124

@@ -83,4 +86,17 @@ public void testShouldSetNullClientSecretWhenSecretIsNone() throws IOException {
8386
.build();
8487
assertEquals("none", Secret.toString(realm.getClientSecret()));
8588
}
89+
90+
@Test
91+
public void testGetValidRedirectUrl() throws IOException {
92+
String rootUrl = "http://localhost:" + wireMockRule.port() + "/jenkins/";
93+
94+
TestRealm realm = new TestRealm.Builder(wireMockRule)
95+
.WithMinimalDefaults().build();
96+
assertEquals(rootUrl + "foo", realm.getValidRedirectUrl("/foo"));
97+
assertEquals(rootUrl + "bar", realm.getValidRedirectUrl(rootUrl + "bar"));
98+
assertEquals(rootUrl, realm.getValidRedirectUrl(null));
99+
assertEquals(rootUrl, realm.getValidRedirectUrl(""));
100+
assertThrows(MalformedURLException.class, () -> realm.getValidRedirectUrl("foobar"));
101+
}
86102
}

0 commit comments

Comments
 (0)