Skip to content

Instantly share code, notes, and snippets.

@hakanai
Created September 26, 2019 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakanai/d65c6ee361411c551d7939e7b9fb736e to your computer and use it in GitHub Desktop.
Save hakanai/d65c6ee361411c551d7939e7b9fb736e to your computer and use it in GitHub Desktop.
Demonstration of Java's HTTP client caching credentials when you don't want it
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
public class AuthenticationLeakBugDemo
{
public static void main(String[] args) throws Exception
{
// User 1
{
System.out.println("Simulating user 1 coming in with proper authentication.");
System.out.println("Expecting: 200");
Authenticator.setDefault(new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("a", "a".toCharArray());
}
});
try
{
URL url = new URL("http://httpbin.org/basic-auth/a/a");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
System.out.println("Got: " + connection.getResponseCode());
}
finally
{
Authenticator.setDefault(null);
}
}
// User 2
{
System.out.println("Simulating user 2 coming in with no authentication.");
System.out.println("Expecting: 401");
URL url = new URL("http://httpbin.org/basic-auth/a/a");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
System.out.println("Got: " + connection.getResponseCode());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment