Skip to content

Commit 0d18ce4

Browse files
committed
C++: CWE-020/ExternalAPIs (+ add tests based on qhelp)
1 parent 4f9dc4b commit 0d18ce4

12 files changed

Lines changed: 62 additions & 2 deletions

cpp/ql/src/Security/CWE/CWE-020/ExternalAPIsSpecific.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private module UntrustedDataToExternalApiConfig implements DataFlow::ConfigSig {
5353
predicate isSink(DataFlow::Node sink) { sink instanceof ExternalApiDataNode }
5454

5555
predicate observeDiffInformedIncrementalMode() {
56-
any() // TODO: Make sure that the location overrides match the query's select clause: Flow call outside 'select' clause (/Users/d10c/src/semmle-code/ql/cpp/ql/src/Security/CWE/CWE-020/ExternalAPIs.qll@13:36:13:80), Flow call outside 'select' clause (/Users/d10c/src/semmle-code/ql/cpp/ql/src/Security/CWE/CWE-020/ExternalAPIs.qll@16:43:16:92)
56+
any() // normal use in UntrustedDataToExternalApi.ql; used via ExternalApiUsedWithUntrustedData (no location) in CountUntrustedDataToExternalAPI.ql
5757
}
5858
}
5959

cpp/ql/src/Security/CWE/CWE-020/ir/ExternalAPIsSpecific.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private module UntrustedDataToExternalApiConfig implements DataFlow::ConfigSig {
4848
predicate isSink(DataFlow::Node sink) { sink instanceof ExternalApiDataNode }
4949

5050
predicate observeDiffInformedIncrementalMode() {
51-
any() // TODO: Make sure that the location overrides match the query's select clause: Flow call outside 'select' clause (/Users/d10c/src/semmle-code/ql/cpp/ql/src/Security/CWE/CWE-020/ir/ExternalAPIs.qll@13:36:13:80), Flow call outside 'select' clause (/Users/d10c/src/semmle-code/ql/cpp/ql/src/Security/CWE/CWE-020/ir/ExternalAPIs.qll@16:43:16:92)
51+
any() // normal use in IRUntrustedDataToExternalApi.ql; used via ExternalApiUsedWithUntrustedData (no location) in IRCountUntrustedDataToExternalAPI.ql
5252
}
5353
}
5454

cpp/ql/test/query-tests/Security/CWE/CWE-020/CountUntrustedDataToExternalAPI.expected

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
query: Security/CWE/CWE-020/CountUntrustedDataToExternalAPI.ql
2+
postprocess:
3+
- utils/test/PrettyPrintModels.ql
4+
- utils/test/InlineExpectationsTestQuery.ql
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
typedef unsigned long size_t;
2+
typedef size_t FILE;
3+
4+
char *strcat(char *s1, const char *s2);
5+
char *fgets(char *s, int n, FILE *stream);
6+
char *fputs(const char *s, FILE *stream);
7+
8+
void do_get(FILE* request, FILE* response) {
9+
char page[1024];
10+
fgets(page, 1024, request);
11+
12+
char buffer[1024];
13+
strcat(buffer, "The page \"");
14+
strcat(buffer, page);
15+
strcat(buffer, "\" was not found.");
16+
17+
fputs(buffer, response);
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
typedef unsigned long size_t;
2+
typedef size_t FILE;
3+
4+
char *strcat(char *s1, const char *s2);
5+
char *fgets(char *s, int n, FILE *stream);
6+
char *fputs(const char *s, FILE *stream);
7+
8+
void do_get(FILE* request, FILE* response) {
9+
char user_id[1024];
10+
fgets(user_id, 1024, request);
11+
12+
char buffer[1024];
13+
strcat(buffer, "SELECT * FROM user WHERE user_id='");
14+
strcat(buffer, user_id);
15+
strcat(buffer, "'");
16+
17+
// ...
18+
}

cpp/ql/test/query-tests/Security/CWE/CWE-020/IRCountUntrustedDataToExternalAPI.expected

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
query: Security/CWE/CWE-020/IRCountUntrustedDataToExternalAPI.ql
2+
postprocess:
3+
- utils/test/PrettyPrintModels.ql
4+
- utils/test/InlineExpectationsTestQuery.ql
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#select
2+
edges
3+
nodes
4+
subpaths
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
query: Security/CWE/CWE-020/IRUntrustedDataToExternalAPI.ql
2+
postprocess:
3+
- utils/test/PrettyPrintModels.ql
4+
- utils/test/InlineExpectationsTestQuery.ql

0 commit comments

Comments
 (0)