|
2 | 2 | title: "Week 6 - Little Bobby Tables"
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -View this classic XKCD cartoon: |
| 5 | +**Please use Canvas to return the assignments: <https://ucsb.instructure.com/courses/26293/assignments/361496>** |
| 6 | + |
| 7 | +In class we discussed how to parameterize a query and then insert values for the parameter(s): |
| 8 | + |
| 9 | +``` |
| 10 | +query_template = "SELECT ... WHERE Species = ? AND ageMethod = ?" |
| 11 | +species = "wolv" |
| 12 | +age_method = "float" |
| 13 | +cur.execute(query_template, [species, age_method]) |
| 14 | +``` |
| 15 | + |
| 16 | +The bare question marks in the template are placeholders. The database driver substitutes the supplied parameter values before submitting to the database, appropriately adding any quoting and character escaping as necessary. |
| 17 | + |
| 18 | +You may decide you want to use your own Python string substitution instead: |
| 19 | + |
| 20 | +``` |
| 21 | +query_template = "SELECT ... WHERE Species = '%s' AND ageMethod = '%s'" |
| 22 | +species = "wolv" |
| 23 | +age_method = "float" |
| 24 | +cur.execute(query_template % (species, age_method)) |
| 25 | +``` |
| 26 | + |
| 27 | +Before you do that, recognize that this practice continues to this day to be a **major** source of security exploits. To understand why, view this classic XKCD cartoon: |
6 | 28 |
|
7 | 29 | 
|
8 | 30 |
|
9 |
| -For the purposes of this problem you may assume that at some point the school's system performs the query |
| 31 | +To interpret the above, you may assume that at some point the school's system performs the query |
10 | 32 |
|
11 | 33 | ```
|
12 | 34 | SELECT *
|
13 | 35 | FROM Students
|
14 |
| - WHERE (name = '%s' AND year = 2024); |
| 36 | + WHERE (name = '%s' AND ...); |
15 | 37 | ```
|
16 | 38 |
|
17 |
| -where a student's name, as input by a user of the system, is directly substituted for the `%s`. Explain exactly how Little Bobby Tables' "name" can cause a catastrophe. Also, explain why his name has two dashes (`--`) at the end. |
| 39 | +where a student's name, as input by a user of the system, is directly substituted for the `%s`. |
| 40 | + |
| 41 | +## Part 1 |
| 42 | + |
| 43 | +Explain exactly how Little Bobby Tables' "name" can cause a catastrophe. Explain why his name has two hyphens (`--`) at the end. |
| 44 | + |
| 45 | +## Part 2 |
| 46 | + |
| 47 | +Suppose instead the school system executed the query |
| 48 | + |
| 49 | +``` |
| 50 | +SELECT * |
| 51 | + FROM Students WHERE name = '%s'; |
| 52 | +``` |
| 53 | + |
| 54 | +What "name" would Little Bobby Tables use to destroy things in that case? |
| 55 | + |
| 56 | +**Credit: 15 points** |
18 | 57 |
|
19 | 58 | ## Bonus problem!
|
20 | 59 |
|
|
0 commit comments