11
11
import java .net .URLConnection ;
12
12
import java .util .Base64 ;
13
13
14
- public class InsecureBasicAuth {
14
+ public class InsecureBasicAuthTest {
15
15
/**
16
16
* Test basic authentication with Apache HTTP POST request using string constructor.
17
17
*/
18
18
public void testApacheHttpRequest (String username , String password ) {
19
19
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" );
21
21
post .setHeader ("Accept" , "application/json" );
22
22
post .setHeader ("Content-type" , "application/json" );
23
-
23
+
24
24
String authString = username + ":" + password ;
25
25
byte [] authEncBytes = Base64 .getEncoder ().encode (authString .getBytes ());
26
26
String authStringEnc = new String (authEncBytes );
27
27
28
- post .addHeader ("Authorization" , "Basic " + authStringEnc );
28
+ post .addHeader ("Authorization" , "Basic " + authStringEnc ); // $hasInsecureBasicAuth
29
29
}
30
30
31
31
/**
@@ -35,7 +35,8 @@ public void testApacheHttpRequest2(String url) throws java.io.IOException {
35
35
String urlStr = "http://www.example.com:8000/payment/retrieve" ;
36
36
HttpGet get = new HttpGet (urlStr );
37
37
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 ())));
39
40
}
40
41
41
42
/**
@@ -46,44 +47,47 @@ public void testApacheHttpRequest3(String username, String password) {
46
47
HttpRequestBase post = new HttpPost (URI .create (uriStr ));
47
48
post .setHeader ("Accept" , "application/json" );
48
49
post .setHeader ("Content-type" , "application/json" );
49
-
50
+
50
51
String authString = username + ":" + password ;
51
52
byte [] authEncBytes = Base64 .getEncoder ().encode (authString .getBytes ());
52
53
String authStringEnc = new String (authEncBytes );
53
54
54
- post .addHeader ("Authorization" , "Basic " + authStringEnc );
55
+ post .addHeader ("Authorization" , "Basic " + authStringEnc ); // $hasInsecureBasicAuth
55
56
}
56
57
57
58
/**
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.
59
61
*/
60
62
public void testApacheHttpRequest4 (String username , String password ) throws Exception {
61
63
String uriStr = "http://www.example.com/rest/getuser.do?uid=abcdx" ;
62
64
URI uri = new URI (uriStr );
63
65
HttpRequestBase post = new HttpPost (uri );
64
66
post .setHeader ("Accept" , "application/json" );
65
67
post .setHeader ("Content-type" , "application/json" );
66
-
68
+
67
69
String authString = username + ":" + password ;
68
70
byte [] authEncBytes = Base64 .getEncoder ().encode (authString .getBytes ());
69
71
String authStringEnc = new String (authEncBytes );
70
72
71
- post .addHeader ("Authorization" , "Basic " + authStringEnc );
73
+ post .addHeader ("Authorization" , "Basic " + authStringEnc ); // $hasInsecureBasicAuth
72
74
}
73
75
74
76
/**
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.
76
79
*/
77
80
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 ));
79
83
post .setHeader ("Accept" , "application/json" );
80
84
post .setHeader ("Content-type" , "application/json" );
81
-
85
+
82
86
String authString = username + ":" + password ;
83
87
byte [] authEncBytes = Base64 .getEncoder ().encode (authString .getBytes ());
84
88
String authStringEnc = new String (authEncBytes );
85
89
86
- post .addHeader ("Authorization" , "Basic " + authStringEnc );
90
+ post .addHeader ("Authorization" , "Basic " + authStringEnc ); // $hasInsecureBasicAuth
87
91
}
88
92
89
93
/**
@@ -94,12 +98,12 @@ public void testApacheHttpRequest6(String username, String password) {
94
98
BasicHttpRequest post = new BasicHttpRequest ("POST" , uriStr );
95
99
post .setHeader ("Accept" , "application/json" );
96
100
post .setHeader ("Content-type" , "application/json" );
97
-
101
+
98
102
String authString = username + ":" + password ;
99
103
byte [] authEncBytes = Base64 .getEncoder ().encode (authString .getBytes ());
100
104
String authStringEnc = new String (authEncBytes );
101
105
102
- post .addHeader ("Authorization" , "Basic " + authStringEnc );
106
+ post .addHeader ("Authorization" , "Basic " + authStringEnc ); // $hasInsecureBasicAuth
103
107
}
104
108
105
109
/**
@@ -111,16 +115,17 @@ public void testApacheHttpRequest7(String username, String password) {
111
115
BasicHttpRequest post = new BasicHttpRequest (requestLine );
112
116
post .setHeader ("Accept" , "application/json" );
113
117
post .setHeader ("Content-type" , "application/json" );
114
-
118
+
115
119
String authString = username + ":" + password ;
116
120
byte [] authEncBytes = Base64 .getEncoder ().encode (authString .getBytes ());
117
121
String authStringEnc = new String (authEncBytes );
118
122
119
- post .addHeader ("Authorization" , "Basic " + authStringEnc );
123
+ post .addHeader ("Authorization" , "Basic " + authStringEnc ); // $hasInsecureBasicAuth
120
124
}
121
125
122
126
/**
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.
124
129
*/
125
130
public void testHttpUrlConnection (String username , String password ) throws Exception {
126
131
String urlStr = "http://www.example.com/rest/getuser.do?uid=abcdx" ;
@@ -130,11 +135,12 @@ public void testHttpUrlConnection(String username, String password) throws Excep
130
135
HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
131
136
conn .setRequestMethod ("POST" );
132
137
conn .setDoOutput (true );
133
- conn .setRequestProperty ("Authorization" , "Basic " + encoding );
138
+ conn .setRequestProperty ("Authorization" , "Basic " + encoding ); // $hasInsecureBasicAuth
134
139
}
135
140
136
141
/**
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.
138
144
*/
139
145
public void testHttpUrlConnection2 (String username , String password ) throws Exception {
140
146
String host = "www.example.com" ;
@@ -146,7 +152,7 @@ public void testHttpUrlConnection2(String username, String password) throws Exce
146
152
HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
147
153
conn .setRequestMethod ("POST" );
148
154
conn .setDoOutput (true );
149
- conn .setRequestProperty ("Authorization" , "Basic " + encoding );
155
+ conn .setRequestProperty ("Authorization" , "Basic " + encoding ); // $hasInsecureBasicAuth
150
156
}
151
157
152
158
/**
@@ -156,9 +162,10 @@ public void testHttpUrlConnection3(String username, String password) throws Exce
156
162
String host = "LOCALHOST" ;
157
163
String authString = username + ":" + password ;
158
164
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 ();
160
167
conn .setRequestMethod ("POST" );
161
168
conn .setDoOutput (true );
162
- conn .setRequestProperty ("Authorization" , "Basic " + encoding );
169
+ conn .setRequestProperty ("Authorization" , "Basic " + encoding ); // Safe
163
170
}
164
171
}
0 commit comments