The Loop16 of 37
In Tools That Touch Files you saw how mini keeps its file tools inside one folder. Here you'll write that check, and the tests try the tricky paths a model might send.
The first line names the working folder, then one path per line, as a model might give them to a tool:
root /home/leila/project
notes.txt
../secrets.txt
/etc/passwd
/home/leila/project-old/notes.txtinside_folder(root, path) returns True if the path stays inside the folder and False if it would leave it. The code below it prints allowed or blocked before each path. For the example above:
allowed notes.txt
blocked ../secrets.txt
blocked /etc/passwd
blocked /home/leila/project-old/notes.txtThe rules, the same as mini's inside:
/, in which case it is used as it is. posixpath.join(root, path) does exactly this.posixpath.normpath, which turns reports/../notes.txt into notes.txt. Normalise root the same way first, since it may end with a slash./. Without the slash, /home/leila/project-old would look like it was inside /home/leila/project.~. To a person, ~ means a home folder; to join it is just a folder called ~. Either way it isn't what a tool should guess about.Look only at the text of the path, never at the disk: posixpath doesn't touch files. (That is also this check's limit: a link inside the folder that points outside it would get through. The lesson says what covers that.)
Sign in to try this exercise.