Skip to content

Commit 7a3f9c7

Browse files
committed
C++: Add a test (cleaned up) that was previously in the internal repo.
1 parent 7f25efd commit 7a3f9c7

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
edges
2+
| test.c:14:20:14:23 | argv | test.c:19:18:19:23 | (const char *)... |
3+
| test.c:14:20:14:23 | argv | test.c:19:18:19:23 | (const char *)... |
4+
| test.c:14:20:14:23 | argv | test.c:19:18:19:23 | query1 |
5+
| test.c:14:20:14:23 | argv | test.c:19:18:19:23 | query1 |
6+
nodes
7+
| test.c:14:20:14:23 | argv | semmle.label | argv |
8+
| test.c:14:20:14:23 | argv | semmle.label | argv |
9+
| test.c:19:18:19:23 | (const char *)... | semmle.label | (const char *)... |
10+
| test.c:19:18:19:23 | (const char *)... | semmle.label | (const char *)... |
11+
| test.c:19:18:19:23 | query1 | semmle.label | query1 |
12+
#select
13+
| test.c:19:18:19:23 | query1 | test.c:14:20:14:23 | argv | test.c:19:18:19:23 | query1 | This argument to a SQL query function is derived from $@ and then passed to mysql_query(sqlArg) | test.c:14:20:14:23 | argv | user input (argv) |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE/CWE-089/SqlTainted.ql
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Semmle test case for rule SprintfToSqlQuery.ql (Uncontrolled sprintf for SQL query)
2+
// Associated with CWE-089: SQL injection. http://cwe.mitre.org/data/definitions/89.html
3+
4+
///// Library routines /////
5+
6+
typedef unsigned long size_t;
7+
int snprintf(char *s, size_t n, const char *format, ...);
8+
void sanitizeString(char *stringOut, size_t len, const char *strIn);
9+
int mysql_query(int arg1, const char *sqlArg);
10+
11+
///// Test code /////
12+
13+
int main(int argc, char** argv) {
14+
char *userName = argv[2];
15+
16+
// a string from the user is injected directly into an SQL query.
17+
char query1[1000] = {0};
18+
snprintf(query1, 1000, "SELECT UID FROM USERS where name = \"%s\"", userName);
19+
mysql_query(0, query1); // BAD
20+
21+
// the user string is encoded by a library routine.
22+
char userNameSanitized[1000] = {0};
23+
sanitizeString(userNameSanitized, 1000, userName);
24+
char query2[1000] = {0};
25+
snprintf(query2, 1000, "SELECT UID FROM USERS where name = \"%s\"", userNameSanitized);
26+
mysql_query(0, query2); // GOOD
27+
}

0 commit comments

Comments
 (0)