1111import java .net .URLConnection ;
1212import java .util .Base64 ;
1313
14- public class InsecureBasicAuth {
14+ public class InsecureBasicAuthTest {
1515 /**
1616 * Test basic authentication with Apache HTTP POST request using string constructor.
1717 */
1818 public void testApacheHttpRequest (String username , String password ) {
1919 String host = "www.example.com" ;
20- HttpRequestBase post = new HttpPost ("http://" + host + "/rest/getuser.do?uid=abcdx" );
20+ HttpRequestBase post = new HttpPost ("http://" + host + "/rest/getuser.do?uid=abcdx" );
2121 post .setHeader ("Accept" , "application/json" );
2222 post .setHeader ("Content-type" , "application/json" );
23-
23+
2424 String authString = username + ":" + password ;
2525 byte [] authEncBytes = Base64 .getEncoder ().encode (authString .getBytes ());
2626 String authStringEnc = new String (authEncBytes );
2727
28- post .addHeader ("Authorization" , "Basic " + authStringEnc );
28+ post .addHeader ("Authorization" , "Basic " + authStringEnc ); // $hasInsecureBasicAuth
2929 }
3030
3131 /**
@@ -35,7 +35,8 @@ public void testApacheHttpRequest2(String url) throws java.io.IOException {
3535 String urlStr = "http://www.example.com:8000/payment/retrieve" ;
3636 HttpGet get = new HttpGet (urlStr );
3737 get .setHeader ("Accept" , "application/json" );
38- get .setHeader ("Authorization" , "Basic " + new String (Base64 .getEncoder ().encode ("admin:test" .getBytes ())));
38+ get .setHeader ("Authorization" , // $hasInsecureBasicAuth
39+ "Basic " + new String (Base64 .getEncoder ().encode ("admin:test" .getBytes ())));
3940 }
4041
4142 /**
@@ -46,44 +47,47 @@ public void testApacheHttpRequest3(String username, String password) {
4647 HttpRequestBase post = new HttpPost (URI .create (uriStr ));
4748 post .setHeader ("Accept" , "application/json" );
4849 post .setHeader ("Content-type" , "application/json" );
49-
50+
5051 String authString = username + ":" + password ;
5152 byte [] authEncBytes = Base64 .getEncoder ().encode (authString .getBytes ());
5253 String authStringEnc = new String (authEncBytes );
5354
54- post .addHeader ("Authorization" , "Basic " + authStringEnc );
55+ post .addHeader ("Authorization" , "Basic " + authStringEnc ); // $hasInsecureBasicAuth
5556 }
5657
5758 /**
58- * Test basic authentication with Apache HTTP POST request using the URI constructor with one argument.
59+ * Test basic authentication with Apache HTTP POST request using the URI constructor with one
60+ * argument.
5961 */
6062 public void testApacheHttpRequest4 (String username , String password ) throws Exception {
6163 String uriStr = "http://www.example.com/rest/getuser.do?uid=abcdx" ;
6264 URI uri = new URI (uriStr );
6365 HttpRequestBase post = new HttpPost (uri );
6466 post .setHeader ("Accept" , "application/json" );
6567 post .setHeader ("Content-type" , "application/json" );
66-
68+
6769 String authString = username + ":" + password ;
6870 byte [] authEncBytes = Base64 .getEncoder ().encode (authString .getBytes ());
6971 String authStringEnc = new String (authEncBytes );
7072
71- post .addHeader ("Authorization" , "Basic " + authStringEnc );
73+ post .addHeader ("Authorization" , "Basic " + authStringEnc ); // $hasInsecureBasicAuth
7274 }
7375
7476 /**
75- * Test basic authentication with Apache HTTP POST request using a URI constructor with multiple arguments.
77+ * Test basic authentication with Apache HTTP POST request using a URI constructor with multiple
78+ * arguments.
7679 */
7780 public void testApacheHttpRequest5 (String username , String password ) throws Exception {
78- HttpRequestBase post = new HttpPost (new URI ("http" , "www.example.com" , "/test" , "abc=123" , null ));
81+ HttpRequestBase post =
82+ new HttpPost (new URI ("http" , "www.example.com" , "/test" , "abc=123" , null ));
7983 post .setHeader ("Accept" , "application/json" );
8084 post .setHeader ("Content-type" , "application/json" );
81-
85+
8286 String authString = username + ":" + password ;
8387 byte [] authEncBytes = Base64 .getEncoder ().encode (authString .getBytes ());
8488 String authStringEnc = new String (authEncBytes );
8589
86- post .addHeader ("Authorization" , "Basic " + authStringEnc );
90+ post .addHeader ("Authorization" , "Basic " + authStringEnc ); // $hasInsecureBasicAuth
8791 }
8892
8993 /**
@@ -94,12 +98,12 @@ public void testApacheHttpRequest6(String username, String password) {
9498 BasicHttpRequest post = new BasicHttpRequest ("POST" , uriStr );
9599 post .setHeader ("Accept" , "application/json" );
96100 post .setHeader ("Content-type" , "application/json" );
97-
101+
98102 String authString = username + ":" + password ;
99103 byte [] authEncBytes = Base64 .getEncoder ().encode (authString .getBytes ());
100104 String authStringEnc = new String (authEncBytes );
101105
102- post .addHeader ("Authorization" , "Basic " + authStringEnc );
106+ post .addHeader ("Authorization" , "Basic " + authStringEnc ); // $hasInsecureBasicAuth
103107 }
104108
105109 /**
@@ -111,16 +115,17 @@ public void testApacheHttpRequest7(String username, String password) {
111115 BasicHttpRequest post = new BasicHttpRequest (requestLine );
112116 post .setHeader ("Accept" , "application/json" );
113117 post .setHeader ("Content-type" , "application/json" );
114-
118+
115119 String authString = username + ":" + password ;
116120 byte [] authEncBytes = Base64 .getEncoder ().encode (authString .getBytes ());
117121 String authStringEnc = new String (authEncBytes );
118122
119- post .addHeader ("Authorization" , "Basic " + authStringEnc );
123+ post .addHeader ("Authorization" , "Basic " + authStringEnc ); // $hasInsecureBasicAuth
120124 }
121125
122126 /**
123- * Test basic authentication with Java HTTP URL connection using the `URL(String spec)` constructor.
127+ * Test basic authentication with Java HTTP URL connection using the `URL(String spec)`
128+ * constructor.
124129 */
125130 public void testHttpUrlConnection (String username , String password ) throws Exception {
126131 String urlStr = "http://www.example.com/rest/getuser.do?uid=abcdx" ;
@@ -130,11 +135,12 @@ public void testHttpUrlConnection(String username, String password) throws Excep
130135 HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
131136 conn .setRequestMethod ("POST" );
132137 conn .setDoOutput (true );
133- conn .setRequestProperty ("Authorization" , "Basic " + encoding );
138+ conn .setRequestProperty ("Authorization" , "Basic " + encoding ); // $hasInsecureBasicAuth
134139 }
135140
136141 /**
137- * Test basic authentication with Java HTTP URL connection using the `URL(String protocol, String host, String file)` constructor.
142+ * Test basic authentication with Java HTTP URL connection using the `URL(String protocol,
143+ * String host, String file)` constructor.
138144 */
139145 public void testHttpUrlConnection2 (String username , String password ) throws Exception {
140146 String host = "www.example.com" ;
@@ -146,7 +152,7 @@ public void testHttpUrlConnection2(String username, String password) throws Exce
146152 HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
147153 conn .setRequestMethod ("POST" );
148154 conn .setDoOutput (true );
149- conn .setRequestProperty ("Authorization" , "Basic " + encoding );
155+ conn .setRequestProperty ("Authorization" , "Basic " + encoding ); // $hasInsecureBasicAuth
150156 }
151157
152158 /**
@@ -156,9 +162,10 @@ public void testHttpUrlConnection3(String username, String password) throws Exce
156162 String host = "LOCALHOST" ;
157163 String authString = username + ":" + password ;
158164 String encoding = Base64 .getEncoder ().encodeToString (authString .getBytes ("UTF-8" ));
159- HttpURLConnection conn = (HttpURLConnection ) new URL ("http://" +(((host +"/rest/getuser.do" )+"?uid=abcdx" ))).openConnection ();
165+ HttpURLConnection conn = (HttpURLConnection ) new URL (
166+ "http://" + (((host + "/rest/getuser.do" ) + "?uid=abcdx" ))).openConnection ();
160167 conn .setRequestMethod ("POST" );
161168 conn .setDoOutput (true );
162- conn .setRequestProperty ("Authorization" , "Basic " + encoding );
169+ conn .setRequestProperty ("Authorization" , "Basic " + encoding ); // Safe
163170 }
164171}
0 commit comments