Skip to content

Commit b00e312

Browse files
committed
C++: Replace placeholder key in the example for cpp/cleartext-storage-database.
1 parent f931dab commit b00e312

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

cpp/ql/src/Security/CWE/CWE-313/CleartextSqliteDatabase.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
void bad(void) {
3-
char *password = "cleartext password";
3+
const char *password = "cleartext password";
44
sqlite3 *credentialsDB;
55
sqlite3_stmt *stmt;
66

@@ -16,14 +16,15 @@ void bad(void) {
1616
}
1717
}
1818

19-
void good(void) {
20-
char *password = "cleartext password";
19+
void good(const char *secretKey) {
20+
const char *password = "cleartext password";
2121
sqlite3 *credentialsDB;
2222
sqlite3_stmt *stmt;
2323

2424
if (sqlite3_open("credentials.db", &credentialsDB) == SQLITE_OK) {
2525
// GOOD: database encryption enabled:
26-
sqlite3_exec(credentialsDB, "PRAGMA key = 'secretKey!'", NULL, NULL, NULL);
26+
std::string setKeyString = std::string("PRAGMA key = '") + secretKey + "'";
27+
sqlite3_exec(credentialsDB, setKeyString.c_str(), NULL, NULL, NULL);
2728
sqlite3_exec(credentialsDB, "CREATE TABLE IF NOT EXISTS creds (password TEXT);", NULL, NULL, NULL);
2829
if (sqlite3_prepare_v2(credentialsDB, "INSERT INTO creds(password) VALUES(?)", -1, &stmt, NULL) == SQLITE_OK) {
2930
sqlite3_bind_text(stmt, 1, password, -1, SQLITE_TRANSIENT);
@@ -33,4 +34,3 @@ void good(void) {
3334
}
3435
}
3536
}
36-

cpp/ql/src/Security/CWE/CWE-313/CleartextSqliteDatabase.qhelp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ In the 'GOOD' case, the database (and thus the credentials) are encrypted.</p>
2020

2121
<sample src="CleartextSqliteDatabase.c" />
2222

23+
<p>Note that for the good example to work we need to provide a secret key. Secure key generation and storage is required.</p>
24+
2325
</example>
2426
<references>
2527

26-
<li>M. Dowd, J. McDonald and J. Schuhm, <i>The Art of Software Security Assessment</i>, 1st Edition, Chapter 2 - 'Common Vulnerabilities of Encryption', p. 43. Addison Wesley, 2006.</li>
28+
<li>M. Dowd, J. McDonald and J. Schuhm, <i>The Art of Software Security Assessment</i>, 1st Edition, Chapter 2 - 'Common Vulnerabilities of Encryption', p. 43. Addison Wesley, 2006.</li>
2729
<li>M. Howard and D. LeBlanc, <i>Writing Secure Code</i>, 2nd Edition, Chapter 9 - 'Protecting Secret Data', p. 299. Microsoft, 2002.</li>
2830

2931

0 commit comments

Comments
 (0)