...
Wiki Markup |
---|
There are several national variants of ASCII. Therefore, the original ASCII is often referred as *US-ASCII*. The international standard _ISO 646_ defines a character set similar to US-ASCII, but with code positions corresponding to US-ASCII characters {{@\[\]\{\|\}}} as "national use positions". It also gives some liberties with characters {{\#$^`\~}}. In _ISO 646_, several "national variants of ASCII" have been defined, assigning different letters and symbols to the "national use" positions. Thus, the characters that appear in those positions - including those in *US-ASCII* are somewhat "unsafe" in international data transfer. ThusConsequently, due to the "national variants," some characters are less "_safe"_ than others--they might be transferred or interpreted incorrectly. |
In addition to the letters of the English alphabet ("A" through "Z" and "a" through "z"), the digits ("0" through "9"), and the space, only the following characters can be regarded as really " safe:"
No Format |
---|
% & + , - . : = _ |
When naming files, variables, etc., only these characters should be considered for use. This recommendation is related to STR02-A. Sanitize data passed to complex subsystems.
File Names
File names containing particular characters can be troublesome and can cause unexpected behavior leading to potential vulnerabilities. If a program allows the user to specify a filename in the creation or renaming of a file, certain checks should be made to disallow the following characters and patterns:
- Leading dashes
- Control characters such as newlines, carriage returns, and escape
- Spaces
- Invalid character encodings
- Any characters other than letters, numbers, and
punctuation designated above as ' safe'.
Most of these characters or patterns are primarily a problem to scripts or automated parsing, but since because they are not commonly used very commonly anyway, it is best to disallow their use to reduce potential problems. Interoperability concerns also exist because different operating systems handle filenames of this sort in different ways. Leading dashes can cause programs when programs are called with this filename as a parameter, the first character or characters of the file might be taken to mean that its an option switch. Control characters in a filename can cause unexpected results from shell scripts and in logging. Spaces can again cause problems with scripts and anytime double quotes aren't used to surround the filename. Character encodings can be a huge issue and are also discussed in MSC10-A. Character Encoding - UTF8 Related Issues. Other special characters are included in this recommendation because they are commonly used as seperators separators and having them in a filename can cause unexpected and potentially insecure behavior.
...
Code Block | ||
---|---|---|
| ||
char myFilename[1000]; char const elimNewLn[] = "\n"; fgets(myFilename, sizeof(myFilename)-1, stdin); myFilename[sizeof(myFilename)-1] = '\0'; myFilename[strcspn(myFilename, elimNewLn)] = '\0'; |
This example is borrowed in spirit derived from FIO30-C. Exclude user input from format strings except that we remove a newline assuming is removed on the assumption that fgets()
will include it. No checks are performed on the filename to prevent troublesome characters. If an attacker knew this code was in a program used to create or rename files that would later be used in a script or automated process of some sort, they could choose particular characters in the output filename to confuse the later process for malicious purposes.
...
The LDRA tool suite V 7.6.0 is able to detect violations of this recommendation.
Related Rules and Recommendations
STR02-A. Sanitize data passed to complex subsystems
Reference
Wiki Markup |
---|
\[[Kuhn 06|AA. C References#Kuhn 06]\] UTF-8 and Unicode FAQ for Unix/Linux \[[ISO/IEC 646-1991|AA. C References#ISO/IEC 646-1991]\] ISO 7-bit coded character set for information interchange \[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 5.2.1, "Character sets" \[[MISRA 04|AA. C References#MISRA 04]\] Rule 3.2, "The character set and the corresponding encoding shall be documented," and Rule 4.1, "Only those escape sequences that are defined in the ISO C standard shall be used" \[[Wheeler 03|AA. C References#Wheeler03]\] 5.4 File Names |
...