Insufficiently restricted file uploads can allow a file to be uploaded that runs malicious code. For example, a website that doesn't check the file extension of an image can be exploited by uploading a script with an extension, such as .php
or .asp
, that can be run on the server.
1#include <stdio.h>
2
3void unsafeFileExtensionCompliant() {
4 // Compliant: Safe extension used with fopen example
5 FILE* fileFopen = fopen("example.txt", "r");
6 if (fileFopen != NULL) {
7 printf("File opened successfully using fopen.\n");
8 fclose(fileFopen);
9 } else {
10 printf("Error: Failed to open the file using fopen.\n");
11 }
12}
1#include <stdio.h>
2
3void unsafeFileExtensionNonCompliant() {
4 // Noncompliant: Unsafe file extension used with fopen
5 FILE* fileFopen = fopen("example.bat", "rb");
6 if (fileFopen != NULL) {
7 printf("File opened successfully using fopen.\n");
8 fclose(fileFopen);
9 } else {
10 printf("Error: Failed to open the file using fopen.\n");
11 }
12}