You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Fix lab format-strings
Add "hints:" (without the field name, it has no contents).
Remove an example of a password in source code.
That is a vulnerability all by itself, we do NOT want to
show how to write *vulnerable* code.
Signed-off-by: David A. Wheeler <[email protected]>
* Simplify format string example
We don't need to create lots of code, and in fact,
it's often better to switch to a constant string if we can.
Let's show doing that, instead of delving into complications
that are often unnecessary.
Signed-off-by: David A. Wheeler <[email protected]>
---------
Signed-off-by: David A. Wheeler <[email protected]>
@@ -61,36 +63,33 @@ <h1>Lab Exercise Format Strings and Templates</h1>
61
63
<p>
62
64
<h2>Task</h2>
63
65
<p>
64
-
<b>Practice using string templates in a secure way.</b>
66
+
<b>Practice eliminating string formatting vulnerabilities in Python.</b>
65
67
66
68
<p>
67
69
<h2>Background</h2>
68
70
<p>
69
71
In this exercise, we'll adjust our string formatting so that it doesn't allow a user to control
70
72
the <ahref="https://docs.python.org/3/tutorial/inputoutput.html#the-string-format-method"><tt>
71
-
format string</tt></a>. If a user can control the <tt>format string</tt> they can access
72
-
variables which they shouldn't. Particularly if those variable's values can be returned to the user
73
+
format string</tt></a>.
74
+
75
+
<p>If a user can control the <tt>format string</tt> in Python they can access
76
+
value which they shouldn't. Particularly if those variable's values can be returned to the user
73
77
as output, it could lead to information disclosure beyond what was intended by the developer.
74
78
75
79
<p>
76
80
<h2>Task Information</h2>
77
81
<p>
82
+
Please change the code below so the string formatting cannot disclose arbitrary
83
+
program values.
78
84
79
85
<p>
80
-
Please change the code below so the string formatting cannot disclose arbitrary
81
-
program values. The server-side program is written in Python and allows a user to specify a
82
-
<tt>format string</tt> to control the output format of an event.
86
+
The server-side program is written in Python and allows a user to specify a <tt>format string</tt> to control the output format of an event, shown here as <tt>user_format</tt>. The developer probably expected the user to provide a format string like <tt>'{event.level}'</tt> to control what is shown and where.
83
87
84
88
<p>
85
-
You could adjust the program so that it only formats the event, and does not include any user input,
86
-
However it is considered safe to include user input in the output as long as they cannot control
87
-
the <tt>format string</tt> itself.
89
+
However, in many programming languages, allowing an untrusted user to control a format sting is a vulnerability. Format strings are miniature programming languages; running code provided by an untrusted user is dangerous. In the case of Python, an attacker might be able to provide a sneaky format string value like <tt>'{event.__init__.__globals__[CONFIG][SECRET_KEY]}'</tt> and reveal a secret value like a password or secret key.
88
90
89
91
<p>
90
-
Adjust the value returned by the <tt>format_event</tt> function so the the user controlled
In this case, as in many, there is no need for an untrusted user to control the format string at all. Where we can, we should use a constant format that cannot be controlled by a potential attacker. For purposes of this lab, instead of letting the user control the formatting string, set the format to the fixed value <tt>'{event.level},{event.message}'</tt> and don't forget to remove the no-longer-needed format parameter.
94
93
95
94
<p>
96
95
Use the “hint” and “give up” buttons if necessary.
0 commit comments