Skip to content

Commit 2b61f26

Browse files
committed
Swift: Add doc.
1 parent 32c4728 commit 2b61f26

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>
7+
Passing untrusted format strings to functions that use <code>printf</code> style formatting can lead to buffer overflows and data representation problems. An attacker can exploit this weakness to crash the program or obtain sensitive information from its internal state.</p>
8+
</p>
9+
10+
</overview>
11+
<recommendation>
12+
13+
<p>Use a string literal for the format string to prevent the possibility of data flow from
14+
an untrusted source. This also helps to prevent errors where the format arguments do not match the format string.</p>
15+
16+
<p>If the format string cannot be constant, ensure that it comes from a secure data source or is compiled into the source code. If you need to include a value from the user, use the <code>%s</code> specifier in the format string and include that value as a format argument.
17+
</p>
18+
19+
</recommendation>
20+
<example>
21+
22+
<p>In this example, the format string includes a user-controlled <code>inputString</code>:</p>
23+
24+
<sample src="UncontrolledFormatStringBad.cs" />
25+
26+
<p>To fix it, make <code>inputString</code> a format argument rather than part of the format string, as in the following code:</p>
27+
28+
<sample src="UncontrolledFormatStringGood.cs" />
29+
30+
</example>
31+
<references>
32+
33+
<li>
34+
OWASP:
35+
<a href="https://owasp.org/www-community/attacks/Format_string_attack">Format string attack</a>.
36+
</li>
37+
38+
</references>
39+
</qhelp>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
print(String(format: "User input: " + inputString)) // vulnerable
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
print(String(format: "User input: %s", inputString)) // fixed

0 commit comments

Comments
 (0)