[FIXED SECURITY-243] Prefer resolving users by IDs first and foremost.

When looking up users we must always try to use the id first and fallback
to other methods only if this is unsucessful.
This commit is contained in:
James Nord committed 2016-04-20 18:54:07 +01:00
1 parent f4172ebb56
commit e073d331fd
1 file changed
+45
+45
View File
@@ -52,6 +52,8 @@ import org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.springframework.dao.DataAccessException;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;
@@ -996,6 +998,49 @@ public class User extends AbstractModelObject implements AccessControlled, Descr
}
}
/**
* Resolve user ID from id.
* @since TODO
*/
@Extension
@Restricted(NoExternalUse.class)
public static class UserIDCanonicalIdResolver extends User.CanonicalIdResolver {
@Override
public String resolveCanonicalId(String idOrFullName, Map<String, ?> context) {
// use the same as DefaultUserCanonicalIdResolver
String _id = idOrFullName.replace('\\', '_').replace('/', '_').replace('<','_')
.replace('>', '_'); // 4 replace() still faster than regex
Jenkins j = Jenkins.getInstance();
if (j == null) {
return null;
}
IdStrategy userIdStrategy = j.getSecurityRealm().getUserIdStrategy();
Collection<User> users = User.getAll();
for (User u : users) {
if (userIdStrategy.equals(u.getId(), _id)) {
return u.getId();
}
}
// XXX I have seen files created in AD that are in the form DOMAIN\User so not sure why they get through but lets also try those...
for (User u : users) {
if (userIdStrategy.equals(u.getId(), idOrFullName)) {
return u.getId();
}
}
return null;
}
@Override
public int getPriority() {
// should always come first so that ID that are ids get mapped correctly
return Integer.MAX_VALUE;
}
}
/**
* Jenkins now refuses to let the user login if he/she doesn't exist in {@link SecurityRealm},
* which was necessary to make sure users removed from the backend will get removed from the frontend.