When code pastes input into SQL, the database receives one string and reads all of it as SQL. Seeing that string is the first step to spotting injection.
What to build
Print the SQL that string concatenation builds for each login attempt.
Requirements
- Each line has a name and a PIN separated by a tab character.
- Print exactly
SELECT * FROM users WHERE name = 'NAME' AND pin = 'PIN', with the inputs pasted in place of NAME and PIN, unchanged.
Input
One name<TAB>pin per line.
Output
One query per line.
Constraints
1 to 30 lines. Names and PINs contain no tab characters.
Example
Input:
rahim 4821
Output:
SELECT * FROM users WHERE name = 'rahim' AND pin = '4821'
Why it works
Concatenation copies every character of the input into the query, quotes included, which is exactly what lets a quote end the string and turn the rest into SQL.
Examples
Example 1 · Input
rahim 4821
Expected output
SELECT * FROM users WHERE name = 'rahim' AND pin = '4821'
Example 2 · Input
' OR '1'='1' -- anything
Expected output
SELECT * FROM users WHERE name = '' OR '1'='1' --' AND pin = 'anything'
Hints
Hint 1
line.split("\t") separates the two parts.
Solution
Show one correct solution (try it yourself first)
let line = prompt();
while (line !== null) {
const [name, pin = ""] = line.split("\t");
console.log("SELECT * FROM users WHERE name = '" + name + "' AND pin = '" + pin + "'");
line = prompt();
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step