How a web request is handled

A request is text, and every character in it came from the client. This set checks that you can read a request line, and that you can confine the path it carries to the directory the server meant to serve.

4 questions 1 task Read the lesson

Questions

Each one is marked as you go, and the explanation arrives straight after, which is the part worth reading.

4 questions, marked as you go.

1 In the request line GET /hello HTTP/1.1, what is /hello?
2 Why is curl --path-as-is needed to demonstrate a path traversal?
3 The flawed server returned 404 for /%2e%2e/secret.txt but 200 for /../secret.txt. Why?
4 Why does os.path.realpath make the path check reliable?

Tasks

These need your own device. Run the command in Termux, then paste what it printed. The marking looks at what the output means, so spacing, version numbers and ordering do not matter. If you have not installed anything yet, the lesson covers the setup.

Show a path escaping its root, and the check that stops it

This writes a small web root with a public file, then a secret file just outside it. It joins the request /../secret.txt onto the root the way the flawed server does, prints what the join points at, then resolves the path and asks whether it is still inside the root. Run it and paste what comes back.

Run this in Termux On your own device. Nothing on this page executes it. The command runs in your terminal, and this box only reads what you paste back.

mkdir -p /tmp/wr; echo public > /tmp/wr/index.txt; echo secret > /tmp/secret.txt; python3 -c "import os; root='/tmp/wr'; path=os.path.join(root,'../secret.txt'); print('served:', open(path).read().strip()); fixed=os.path.realpath(path); print('fixed path:', fixed); print('allowed :', fixed.startswith(root))"
Marking your own work Both halves of this are checked in your browser, with no server involved, so nothing stops you from pasting output you did not run. The tasks are written so that the easiest way to pass one is to actually do it and read the result, which is the whole intention.

All sets Read the lesson