8282730: LdapLoginModule throw NPE from logout method after login failure

Reviewed-by: mullan
This commit is contained in:
Weijun Wang 2022-08-01 22:28:30 +00:00
parent f714ac52bf
commit 554f44ecb1
11 changed files with 221 additions and 72 deletions

View file

@ -144,8 +144,8 @@ public final class Subject implements java.io.Serializable {
* has been set read-only before permitting subsequent modifications.
* The newly created Sets also prevent illegal modifications
* by ensuring that callers have sufficient permissions. These Sets
* also prohibit null elements, and attempts to add or query a null
* element will result in a {@code NullPointerException}.
* also prohibit null elements, and attempts to add, query, or remove
* a null element will result in a {@code NullPointerException}.
*
* <p> To modify the Principals Set, the caller must have
* {@code AuthPermission("modifyPrincipals")}.
@ -174,8 +174,8 @@ public final class Subject implements java.io.Serializable {
* has been set read-only before permitting subsequent modifications.
* The newly created Sets also prevent illegal modifications
* by ensuring that callers have sufficient permissions. These Sets
* also prohibit null elements, and attempts to add or query a null
* element will result in a {@code NullPointerException}.
* also prohibit null elements, and attempts to add, query, or remove
* a null element will result in a {@code NullPointerException}.
*
* <p> To modify the Principals Set, the caller must have
* {@code AuthPermission("modifyPrincipals")}.

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -691,13 +691,13 @@ public class LoginContext {
// - this can only be non-zero if methodName is LOGIN_METHOD
for (int i = moduleIndex; i < moduleStack.length; i++, moduleIndex++) {
String name = moduleStack[i].entry.getLoginModuleName();
try {
if (moduleStack[i].module == null) {
// locate and instantiate the LoginModule
//
String name = moduleStack[i].entry.getLoginModuleName();
Set<Provider<LoginModule>> lmProviders;
synchronized(providersCache){
lmProviders = providersCache.get(contextClassLoader);
@ -780,16 +780,16 @@ public class LoginContext {
clearState();
if (debug != null)
debug.println(methodName + " SUFFICIENT success");
debug.println(name + " " + methodName + " SUFFICIENT success");
return;
}
if (debug != null)
debug.println(methodName + " success");
debug.println(name + " " + methodName + " success");
success = true;
} else {
if (debug != null)
debug.println(methodName + " ignored");
debug.println(name + " " + methodName + " ignored");
}
} catch (Exception ite) {
@ -854,7 +854,7 @@ public class LoginContext {
AppConfigurationEntry.LoginModuleControlFlag.REQUISITE) {
if (debug != null)
debug.println(methodName + " REQUISITE failure");
debug.println(name + " " + methodName + " REQUISITE failure");
// if REQUISITE, then immediately throw an exception
if (methodName.equals(ABORT_METHOD) ||
@ -869,7 +869,7 @@ public class LoginContext {
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED) {
if (debug != null)
debug.println(methodName + " REQUIRED failure");
debug.println(name + " " + methodName + " REQUIRED failure");
// mark down that a REQUIRED module failed
if (firstRequiredError == null)
@ -878,7 +878,7 @@ public class LoginContext {
} else {
if (debug != null)
debug.println(methodName + " OPTIONAL failure");
debug.println(name + " " + methodName + " OPTIONAL failure");
// mark down that an OPTIONAL module failed
if (firstError == null)

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,7 +26,6 @@
package javax.security.auth.spi;
import javax.security.auth.Subject;
import javax.security.auth.AuthPermission;
import javax.security.auth.callback.*;
import javax.security.auth.login.*;
import java.util.Map;
@ -50,13 +49,13 @@ import java.util.Map;
* a {@code Subject}, a {@code CallbackHandler}, shared
* {@code LoginModule} state, and LoginModule-specific options.
*
* The {@code Subject} represents the
* <p> The {@code Subject} represents the
* {@code Subject} currently being authenticated and is updated
* with relevant Credentials if authentication succeeds.
* LoginModules use the {@code CallbackHandler} to
* communicate with users. The {@code CallbackHandler} may be
* used to prompt for usernames and passwords, for example.
* Note that the {@code CallbackHandler} may be null. LoginModules
* Note that the {@code CallbackHandler} may be {@code null}. LoginModules
* which absolutely require a {@code CallbackHandler} to authenticate
* the {@code Subject} may throw a {@code LoginException}.
* LoginModules optionally use the shared state to share information
@ -129,7 +128,7 @@ import java.util.Map;
public interface LoginModule {
/**
* Initialize this LoginModule.
* Initialize this {@code LoginModule}.
*
* <p> This method is called by the {@code LoginContext}
* after this {@code LoginModule} has been instantiated.
@ -163,12 +162,12 @@ public interface LoginModule {
* {@code Subject} information such
* as a username and password and then attempt to verify the password.
* This method saves the result of the authentication attempt
* as private state within the LoginModule.
* as private state within the {@code LoginModule}.
*
* @exception LoginException if the authentication fails
*
* @return true if the authentication succeeded, or false if this
* {@code LoginModule} should be ignored.
* @return {@code true} if the authentication succeeded, or {@code false}
* if this {@code LoginModule} should be ignored.
*/
boolean login() throws LoginException;
@ -190,8 +189,8 @@ public interface LoginModule {
*
* @exception LoginException if the commit fails
*
* @return true if this method succeeded, or false if this
* {@code LoginModule} should be ignored.
* @return {@code true} if this method succeeded, or {@code false}
* if this {@code LoginModule} should be ignored.
*/
boolean commit() throws LoginException;
@ -210,8 +209,8 @@ public interface LoginModule {
*
* @exception LoginException if the abort fails
*
* @return true if this method succeeded, or false if this
* {@code LoginModule} should be ignored.
* @return {@code true} if this method succeeded, or {@code false}
* if this {@code LoginModule} should be ignored.
*/
boolean abort() throws LoginException;
@ -223,8 +222,15 @@ public interface LoginModule {
*
* @exception LoginException if the logout fails
*
* @return true if this method succeeded, or false if this
* {@code LoginModule} should be ignored.
* @return {@code true} if this method succeeded, or {@code false}
* if this {@code LoginModule} should be ignored.
*
* @implSpec Implementations should check if a variable is {@code null}
* before removing it from the Principals or Credentials set
* of a {@code Subject}, otherwise a {@code NullPointerException}
* will be thrown as these sets {@linkplain Subject#Subject()
* prohibit null elements}. This is especially important if
* this method is called after a login failure.
*/
boolean logout() throws LoginException;
}