Skip to content

Commit 336c7de

Browse files
committed
updates based on review
1 parent 6d3244d commit 336c7de

File tree

3 files changed

+6
-8
lines changed

3 files changed

+6
-8
lines changed

cpp/ql/src/Security/CWE/CWE-022/TaintedPath.qhelp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ is still contained within that directory.
6262
OWASP:
6363
<a href="https://owasp.org/www-community/attacks/Path_Traversal">Path Traversal</a>.
6464
</li>
65-
<li>Rails: <a href="https://api.rubyonrails.org/classes/ActiveStorage/Filename.html#method-i-sanitized">ActiveStorage::Filename#sanitized</a>.</li>
65+
<li>Linux man pages: <a href="https://man7.org/linux/man-pages/man3/realpath.3.html">realpath(3)</a>.</li>
6666

6767
</references>
6868
</qhelp>

cpp/ql/src/Security/CWE/CWE-022/examples/TaintedPathFolder.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@ int main(int argc, char** argv) {
55
char *userAndFile = argv[2];
66
char baseDir[PATH_MAX] = "/home/user/public/";
77
char fullPath[PATH_MAX];
8-
char resolvedPath[PATH_MAX];
98

109
// Attempt to concatenate the base directory and the user-supplied path
1110
snprintf(fullPath, sizeof(fullPath), "%s%s", baseDir, userAndFile);
1211

1312
// Resolve the absolute path, normalizing any ".." or "."
14-
if (realpath(fullPath, resolvedPath) == NULL) {
13+
char *resolvedPath = realpath(fullPath, NULL);
14+
if (resolvedPath == NULL) {
1515
perror("Error resolving path");
1616
return 1;
1717
}
1818

1919
// Check if the resolved path starts with the base directory
2020
if (strncmp(baseDir, resolvedPath, strlen(baseDir)) != 0) {
21+
free(resolvedPath);
2122
return 1;
2223
}
2324

2425
// GOOD: Path is within the intended directory
2526
FILE *file = fopen(resolvedPath, "wb+");
27+
free(resolvedPath);
2628
}

cpp/ql/src/Security/CWE/CWE-022/examples/TaintedPathNormalize.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,5 @@ int main(int argc, char** argv) {
1010
return 1;
1111
}
1212

13-
char fileBuffer[FILENAME_MAX] = "/home/user/files/";
14-
// Ensure buffer overflow is prevented
15-
strncat(fileBuffer, userAndFile, FILENAME_MAX - strlen(fileBuffer) - 1);
16-
// GOOD: We know that the filename is safe and stays within the public folder
17-
FILE *file = fopen(fileBuffer, "wb+");
13+
// use `userAndFile` as a safe filename
1814
}

0 commit comments

Comments
 (0)