FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codeql/java/ql/src/Security/CWE/CWE-522/InsecureBasicAuth.java at main · github/codeql · GitHub
github
codeql
Repository navigation
Code
Issues
1k
(1k)
Pull requests
478
(478)
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
codeql
/
java
/
ql
/
src
/
Security
/
CWE
/
CWE-522
/
InsecureBasicAuth.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (34 loc) · 1.51 KB
Breadcrumbs
codeql
/
java
/
ql
/
src
/
Security
/
CWE
/
CWE-522
/
InsecureBasicAuth.java
Copy path
File metadata and controls
43 lines (34 loc) · 1.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public
class
InsecureBasicAuth
{
/**
* Test basic authentication with Apache HTTP request.
*/
public
void
testApacheHttpRequest
(
String
username
,
String
password
) {
// BAD: basic authentication over HTTP
String
url
=
"http://www.example.com/rest/getuser.do?uid=abcdx"
;
// GOOD: basic authentication over HTTPS
url
=
"https://www.example.com/rest/getuser.do?uid=abcdx"
;
HttpPost
post
=
new
HttpPost
(
url
);
post
.
setHeader
(
"Accept"
,
"application/json"
);
post
.
setHeader
(
"Content-type"
,
"application/json"
);
String
authString
=
username
+
":"
+
password
;
byte
[]
authEncBytes
=
Base64
.
getEncoder
().
encode
(
authString
.
getBytes
());
String
authStringEnc
=
new
String
(
authEncBytes
);
post
.
addHeader
(
"Authorization"
,
"Basic "
+
authStringEnc
);
}
/**
* Test basic authentication with Java HTTP URL connection.
*/
public
void
testHttpUrlConnection
(
String
username
,
String
password
) {
// BAD: basic authentication over HTTP
String
urlStr
=
"http://www.example.com/rest/getuser.do?uid=abcdx"
;
// GOOD: basic authentication over HTTPS
urlStr
=
"https://www.example.com/rest/getuser.do?uid=abcdx"
;
String
authString
=
username
+
":"
+
password
;
String
encoding
=
Base64
.
getEncoder
().
encodeToString
(
authString
.
getBytes
(
"UTF-8"
));
URL
url
=
new
URL
(
urlStr
);
HttpURLConnection
conn
= (
HttpURLConnection
)
url
.
openConnection
();
conn
.
setRequestMethod
(
"POST"
);
conn
.
setDoOutput
(
true
);
conn
.
setRequestProperty
(
"Authorization"
,
"Basic "
+
encoding
);
}
}
Back
|
FazBrowse Home
|
New Git URL