Drill 21 ยท
AP Cybersecurity Drill 21: Web Form and the Inventory Database is a practice drill. It contains 5 original questions created by Brian Stewart, a Barron's test prep author with over 20 years of tutoring experience.
A developer at an online garden-supply store reviews how a public search box talks to the product database; this drill uses an invented company and original figures.
Brackwell Garden Supply runs an online store. A public product-search box sends whatever a visitor types straight into a database query. A developer pulls one line of the server code and one suspicious entry from the search log.
The query the server builds (server code):
query = "SELECT name, price FROM products WHERE name = '" + userInput + "';"
One entry from the search log:
| Time | Field | Value typed by visitor |
|---|---|---|
| 09:14 | search box | rose' OR '1'='1 |
The visitor's text is inserted into the query exactly as typed, with no checking or escaping.
Question 1. When the logged value is inserted into the query, what condition does the database actually evaluate in the WHERE clause?
Explanation: Choice A is correct. The single quote after rose closes the string, OR begins a second condition, and '1'='1' is always true; because the WHERE clause is now name='rose' OR true, every row satisfies it. Choice B is incorrect because the quote is treated as code, not as part of a literal value, so the input is not matched character-for-character. Choice C is incorrect because the database does not stop reading at a space; it parses the full string as SQL. Choice D is incorrect because nothing in the input refers to price or null; that condition is invented.
Question 2. What class of attack does this input represent?
Explanation: Choice B is correct. Untrusted input is placed inside an SQL statement and executed as code, which is the definition of SQL injection. Choice A is incorrect because cross-site scripting targets a browser with injected script, not the database query. Choice C is incorrect because directory traversal manipulates file paths to escape a folder, which is not what this input does. Choice D is incorrect because a buffer overflow overruns a memory buffer; this attack abuses query syntax, not memory.
Question 3. A second entry typed into the search box is: x'; DROP TABLE products; -- (with a trailing space). Which clue in this entry most strongly signals an attempted injection rather than a normal search?
Explanation: Choice D is correct. The combination of a closing quote, a statement-ending semicolon, a destructive DROP TABLE command, and a comment marker to neutralize the trailing code is SQL syntax, not search text, and is the clearest tell. Choice A is incorrect because a search naturally can mention products; the word alone is not suspicious. Choice B is incorrect because length by itself is weak evidence; long legitimate searches exist. Choice C is incorrect because an apostrophe alone is common in real names and is not conclusive on its own.
Question 4. Why does building the query by joining text create the risk, regardless of what the visitor types?
Explanation: Choice B is correct. When input is concatenated into the statement, the data and the code share the same string, so characters the visitor supplies can be read as SQL and alter the query's meaning. Choice A is incorrect because query length and run time are not the security problem. Choice C is incorrect because caching old results is not how this risk arises. Choice D is incorrect because plaintext logging of prices is unrelated to why concatenation is dangerous.
Question 5. Which change best removes the vulnerability at its source?
Explanation: Choice A is correct. Parameterized queries (prepared statements) send user input strictly as data bound to a placeholder, so it is never interpreted as SQL; this fixes the root cause. Choice B is incorrect because hiding the form is obscurity, not a fix; the endpoint still accepts the same input. Choice C is incorrect because weakening the password reduces security and does nothing about injection. Choice D is incorrect because a length cap is brittle and does not separate data from command text; attackers can often craft shorter payloads, and truncation tends to create errors rather than safety.