Python traps 2 · Chapter 11 of 16

shell=True and a name with an apostrophe

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Libraries
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python traps 2 are free. This one comes with the all labs plan.

Why you need this

Nothing checks your data before the shell reads it, so a name is not only able to break the command: it can add to it. A value that closes the quote can run anything after a ;:

dish = "thali'; echo this ran; echo '"
run = subprocess.run(f"echo '{dish}'", shell=True, capture_output=True, text=True)
print(run.stdout)
thali
this ran

That is command injection. Swapping single quotes for double ones only moves the problem: inside double quotes the shell still expands $ and runs backticks and $(...):

price = "₹40, not $40"
run = subprocess.run(f'echo "{price}"', shell=True, capture_output=True, text=True)
print(run.stdout.strip())
₹40, not 0

The fix is to not use a shell: a list of arguments, so every value reaches the program as one argument, whatever characters it holds. When a shell really cannot be avoided, shlex.quote(value) quotes a value safely for it.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.py: Neel's label, failing on purpose

    Write reproduce() in repro.py. Call print_label from printer.py with Neel's order from the ticket, {"id": "D3170", "name": "Neel D'Souza", "note": "Ring twice"}, and return the label line it gave. If it raises RuntimeError, catch it and return the error's message, str(error), instead.

    The check runs your reproduce() twice: beside the original printer.py it must return "label printer failed: /bin/sh: 1: Syntax error: Unterminated quoted string", and beside a fixed printer.py it must return "D3170 | Neel D'Souza | Ring twice". So it must really print Neel's label. No real waiting and no input(): reproduce() must finish within 3 seconds.

  2. Task 2

    printer.py: every label prints, whatever the name

    Fix print_label in printer.py so the order's id, name and note reach label_printer.py exactly as they are, whatever they contain: apostrophes, double quotes, $, ;, backticks and double spaces are all printed as they are, and a note with $(...) in it is printed, never run. It still runs label_printer.py with sys.executable and returns the line it printed, "<id> | <name> | <note>", without the newline. When the printer exits with an error (it refuses an empty name), print_label still raises RuntimeError("label printer failed: <last line of its error output>").

    Keep PRINTER, and do not change label_printer.py: it is the printer's own tool, given. The check imports a fresh copy of your printer.py and prints its own labels, with every kind of quote and sign in names and notes, and checks that nothing in a note was ever run: a fix that works for D'Souza but breaks on a double quote is as wrong as the bug.

  3. Task 3

    test_printer.py: a test that would have caught it

    Write a pytest file, test_printer.py, that would have caught this bug before Neel's labels went missing. The check runs your file three times: beside the original printer.py, where at least one test must fail; beside the fixed printer.py, where every test must pass; and beside a wrong fix that puts double quotes round the name and the note instead of single ones, where at least one test must fail.

    So test more than the apostrophe: a name with ' in it, and a note with a double quote or a $ in it, each printed exactly as it is (print_label runs the real label_printer.py beside your test). No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: Neel's label, failing on purpose
  • printer.py: every label prints, whatever the name
  • test_printer.py: a test that would have caught it

More in Libraries

Build "shell=True and a name with an apostrophe" yourself

Monsoon season at DabbaDrop brings a new pile of tickets from Kunal. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python traps 2 are free. This one comes with the all labs plan.