Skip to content

Commit de4fe6d

Browse files
committed
Use inline expectations for query test
1 parent d69bcca commit de4fe6d

File tree

4 files changed

+40
-38
lines changed

4 files changed

+40
-38
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
query: Security/CWE/CWE-497/ExposedSystemData.ql
2-
postprocess: utils/test/PrettyPrintModels.ql
2+
postprocess:
3+
- utils/test/PrettyPrintModels.ql
4+
- utils/test/InlineExpectationsTestQuery.ql

cpp/ql/test/query-tests/Security/CWE/CWE-497/semmle/tests/tests2.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,23 @@ int val();
4747

4848
// --- test cases ---
4949

50-
const char *global1 = mysql_get_client_info();
50+
const char *global1 = mysql_get_client_info(); // $ Source
5151
const char *global2 = "abc";
5252

5353
void test7()
5454
{
5555
int sock = socket(val(), val(), val());
5656

5757
// tests for a strict implementation of CWE-497
58-
std::cout << getenv("HOME"); // BAD: outputs HOME environment variable [NOT DETECTED]
59-
std::cout << "PATH = " << getenv("PATH") << "."; // BAD: outputs PATH environment variable [NOT DETECTED]
58+
std::cout << getenv("HOME"); // $ MISSING: Alert // outputs HOME environment variable
59+
std::cout << "PATH = " << getenv("PATH") << "."; // $ MISSING: Alert // outputs PATH environment variable
6060
std::cout << "PATHPATHPATH"; // GOOD: not system data
6161

6262
// tests for a more pragmatic implementation of CWE-497
63-
send(sock, getenv("HOME"), val(), val()); // BAD
64-
send(sock, getenv("PATH"), val(), val()); // BAD
65-
send(sock, getenv("USERNAME"), val(), val()); // BAD
66-
send(sock, getenv("APP_PASSWORD"), val(), val()); // BAD
63+
send(sock, getenv("HOME"), val(), val()); // $ Alert
64+
send(sock, getenv("PATH"), val(), val()); // $ Alert
65+
send(sock, getenv("USERNAME"), val(), val()); // $ Alert
66+
send(sock, getenv("APP_PASSWORD"), val(), val()); // $ Alert
6767
send(sock, getenv("HARMLESS"), val(), val()); // GOOD: harmless information
6868
send(sock, "HOME", val(), val()); // GOOD: not system data
6969
send(sock, "PATH", val(), val()); // GOOD: not system data
@@ -75,11 +75,11 @@ void test7()
7575
{
7676
char buffer[256];
7777

78-
strcpy(buffer, mysql_get_client_info());
78+
strcpy(buffer, mysql_get_client_info()); // $ Source
7979

80-
send(sock, mysql_get_client_info(), val(), val()); // BAD
81-
send(sock, buffer, val(), val()); // BAD
82-
send(sock, global1, val(), val()); // BAD
80+
send(sock, mysql_get_client_info(), val(), val()); // $ Alert
81+
send(sock, buffer, val(), val()); // $ Alert
82+
send(sock, global1, val(), val()); // $ Alert
8383
send(sock, global2, val(), val()); // GOOD: not system data
8484
}
8585

@@ -88,27 +88,27 @@ void test7()
8888
const char *str1 = "123456";
8989
const char *str2 = "abcdef";
9090

91-
mysql_real_connect(sock, val(), val(), str1, val(), val(), val(), val());
91+
mysql_real_connect(sock, val(), val(), str1, val(), val(), val(), val()); // $ Source
9292

93-
send(sock, str1, val(), val()); // BAD
93+
send(sock, str1, val(), val()); // $ Alert
9494
send(sock, str2, val(), val()); // GOOD: not system data
9595
}
9696

9797
// tests for `getpwuid`
9898
{
9999
passwd *pw;
100100

101-
pw = getpwuid(val());
102-
send(sock, pw->pw_passwd, val(), val()); // BAD
101+
pw = getpwuid(val()); // $ Source
102+
send(sock, pw->pw_passwd, val(), val()); // $ Alert
103103
}
104104

105105
// tests for containers
106106
{
107107
container c1, c2;
108108

109-
c1.ptr = getenv("MY_SECRET_TOKEN");
109+
c1.ptr = getenv("MY_SECRET_TOKEN"); // $ Source
110110
c2.ptr = "";
111-
send(sock, c1.ptr, val(), val()); // BAD
111+
send(sock, c1.ptr, val(), val()); // $ Alert
112112
send(sock, c2.ptr, val(), val()); // GOOD: not system data
113113
}
114114
}
@@ -131,31 +131,31 @@ void test_zmq(void *remoteSocket)
131131
size_t message_len;
132132

133133
// prepare data
134-
message_data = getenv("HOME");
134+
message_data = getenv("HOME"); // $ Source
135135
message_len = strlen(message_data) + 1;
136136

137137
// send as data
138-
if (zmq_send(socket, message_data, message_len, 0) >= 0) { // BAD: outputs HOME environment variable
138+
if (zmq_send(socket, message_data, message_len, 0) >= 0) { // $ Alert: outputs HOME environment variable
139139
// ...
140140
}
141141

142142
// send as message
143143
if (zmq_msg_init_data(&message, message_data, message_len, 0, 0)) {
144-
if (zmq_sendmsg(remoteSocket, &message, message_len)) { // BAD: outputs HOME environment variable
144+
if (zmq_sendmsg(remoteSocket, &message, message_len)) { // $ Alert: outputs HOME environment variable
145145
// ...
146146
}
147-
if (zmq_msg_send(&message, remoteSocket, message_len)) { // BAD: outputs HOME environment variable
147+
if (zmq_msg_send(&message, remoteSocket, message_len)) { // $ Alert: outputs HOME environment variable
148148
// ...
149149
}
150150
}
151151

152152
// send as message (alternative path)
153153
if (zmq_msg_init_size(&message, message_len) == 0) {
154154
memcpy(zmq_msg_data(&message), message_data, message_len);
155-
if (zmq_sendmsg(remoteSocket,&message, message_len)) { // BAD: outputs HOME environment variable
155+
if (zmq_sendmsg(remoteSocket,&message, message_len)) { // $ Alert: outputs HOME environment variable
156156
// ...
157157
}
158-
if (zmq_msg_send(&message, remoteSocket, message_len)) { // BAD: outputs HOME environment variable
158+
if (zmq_msg_send(&message, remoteSocket, message_len)) { // $ Alert: outputs HOME environment variable
159159
// ...
160160
}
161161
}

cpp/ql/test/query-tests/Security/CWE/CWE-497/semmle/tests/tests_sockets.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void test_sockets1()
2323
int sockfd;
2424
sockaddr addr_remote;
2525
char *msg = "Hello, world!";
26-
char *path = getenv("PATH");
26+
char *path = getenv("PATH"); // $ Source
2727

2828
// create socket
2929
sockfd = socket(AF_INET, SOCK_STREAM, 0);
@@ -36,11 +36,11 @@ void test_sockets1()
3636

3737
// send something using 'send'
3838
if (send(sockfd, msg, strlen(msg) + 1, 0) < 0) return; // GOOD
39-
if (send(sockfd, path, strlen(path) + 1, 0) < 0) return; // BAD
40-
39+
if (send(sockfd, path, strlen(path) + 1, 0) < 0) return; // $ Alert
40+
4141
// send something using 'write'
4242
if (write(sockfd, msg, strlen(msg) + 1) < 0) return; // GOOD
43-
if (write(sockfd, path, strlen(path) + 1) < 0) return; // BAD
43+
if (write(sockfd, path, strlen(path) + 1) < 0) return; // $ Alert
4444

4545
// clean up
4646
// ...
@@ -49,9 +49,9 @@ void test_sockets1()
4949
int mksocket()
5050
{
5151
int fd;
52-
52+
5353
fd = socket(AF_INET, SOCK_STREAM, 0);
54-
54+
5555
return fd;
5656
}
5757

@@ -60,7 +60,7 @@ void test_sockets2()
6060
int sockfd;
6161
sockaddr addr_remote;
6262
char *msg = "Hello, world!";
63-
char *path = getenv("PATH");
63+
char *path = getenv("PATH"); // $ Source
6464

6565
// create socket
6666
sockfd = mksocket();
@@ -73,11 +73,11 @@ void test_sockets2()
7373

7474
// send something using 'send'
7575
if (send(sockfd, msg, strlen(msg) + 1, 0) < 0) return; // GOOD
76-
if (send(sockfd, path, strlen(path) + 1, 0) < 0) return; // BAD
77-
76+
if (send(sockfd, path, strlen(path) + 1, 0) < 0) return; // $ Alert
77+
7878
// send something using 'write'
7979
if (write(sockfd, msg, strlen(msg) + 1) < 0) return; // GOOD
80-
if (write(sockfd, path, strlen(path) + 1) < 0) return; // BAD
80+
if (write(sockfd, path, strlen(path) + 1) < 0) return; // $ Alert
8181

8282
// clean up
8383
// ...

cpp/ql/test/query-tests/Security/CWE/CWE-497/semmle/tests/tests_sysconf.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void test_sc_1()
2121
int value = sysconf(_SC_CHILD_MAX);
2222

2323
printf("_SC_CHILD_MAX = %i\n", _SC_CHILD_MAX); // GOOD
24-
printf("_SC_CHILD_MAX = %i\n", value); // BAD [NOT DETECTED]
24+
printf("_SC_CHILD_MAX = %i\n", value); // $ MISSING: Alert
2525
}
2626

2727
void test_sc_2()
@@ -33,9 +33,9 @@ void test_sc_2()
3333
pathbuf = (char *)malloc(n);
3434
if (pathbuf != NULL)
3535
{
36-
confstr(_CS_PATH, pathbuf, n);
36+
confstr(_CS_PATH, pathbuf, n); // $ Source
3737

38-
printf("path: %s", pathbuf); // BAD [NOT DETECTED]
39-
write(get_fd(), pathbuf, strlen(pathbuf)); // BAD
38+
printf("path: %s", pathbuf); // $ MISSING: Alert
39+
write(get_fd(), pathbuf, strlen(pathbuf)); // $ Alert
4040
}
4141
}

0 commit comments

Comments
 (0)