Skip to content

Commit 3b0220d

Browse files
authored
Merge pull request #13501 from adrienpessu/main
JS: Add another example the Hardcoded credential help
2 parents eca3df2 + 5541fe7 commit 3b0220d

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

javascript/ql/src/Security/CWE-798/HardcodedCredentials.qhelp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@
2121
</p>
2222
</recommendation>
2323

24+
<example>
25+
<p>
26+
The following code example connects to an HTTP request using an hard-codes authentication header:
27+
</p>
28+
29+
<sample src="examples/HardcodedCredentialsHttpRequest.js"/>
30+
31+
<p>
32+
Instead, user name and password can be supplied through the environment variables
33+
<code>username</code> and <code>password</code>, which can be set externally without hard-coding
34+
credentials in the source code.
35+
</p>
36+
37+
<sample src="examples/HardcodedCredentialsHttpRequestFixed.js"/>
38+
39+
</example>
40+
2441
<example>
2542
<p>
2643
The following code example connects to a Postgres database using the <code>pg</code> package
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
let base64 = require('base-64');
2+
3+
let url = 'http://example.org/auth';
4+
let username = 'user';
5+
let password = 'passwd';
6+
7+
let headers = new Headers();
8+
9+
headers.append('Content-Type', 'text/json');
10+
headers.append('Authorization', 'Basic' + base64.encode(username + ":" + password));
11+
12+
fetch(url, {
13+
method:'GET',
14+
headers: headers
15+
})
16+
.then(response => response.json())
17+
.then(json => console.log(json))
18+
.done();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
let base64 = require('base-64');
2+
3+
let url = 'http://example.org/auth';
4+
let username = process.env.USERNAME;
5+
let password = process.env.PASSWORD;
6+
7+
let headers = new Headers();
8+
9+
headers.append('Content-Type', 'text/json');
10+
headers.append('Authorization', 'Basic' + base64.encode(username + ":" + password));
11+
12+
fetch(url, {
13+
method:'GET',
14+
headers: headers
15+
})
16+
.then(response => response.json())
17+
.then(json => console.log(json))
18+
.done();

0 commit comments

Comments
 (0)