Least privilege
Read · 1 of 2
A file tool is a door
A read_file(path) tool should only reach the folder it's meant for. Checking that the path *starts with* docs/ sounds right, and is a classic hole: .. means "go up a folder".
Normalise the path first (resolve every . and ..), then check where it really points.
Read this lesson as text
Give each tool only the access it needs, and check paths properly.
A file tool is a door
A read_file(path) tool should only reach the folder it's meant for. Checking that the path *starts with* docs/ sounds right, and is a classic hole: .. means "go up a folder".
Normalise the path first (resolve every . and ..), then check where it really points.
Break it: Escape the folder
This check is meant to keep read_file inside docs/. Find a path it allows that actually points outside.
path = input()
print("allowed" if path.startswith("docs/") else "blocked")Input rules: A relative path made of letters, digits, dots, dashes, underscores and slashes, like docs/guide.md