Sub topic 7
Why robots.txt is not a security measure
This chapter assumes you know how a robots.txt file is laid out and how Disallow rules work. It also assumes you know basic HTTP status codes. The point here is narrow: robots.txt asks crawlers to stay away from URLs, but it does not stop anyone from requesting them.
A request, not a lock
Robots.txt is a convention. A well-behaved crawler fetches the file, reads the rules, and chooses to follow them. Nothing on the server enforces the rules. A scraper, a vulnerability scanner, or a person with a browser can ignore the file and request any URL directly.
Compare it with real access control:
| Mechanism | Who it stops | Enforced by |
|---|---|---|
| robots.txt `Disallow` | Nothing. It only asks cooperative crawlers to skip the URL. | The crawler's own choice |
| Login or session check | Anonymous visitors and bots | The server, with a 401 or 403 response |
| IP allowlist or firewall | Requests from other networks | The network or server |
The file advertises what you hide
Anyone can open https://www.example.com/robots.txt. If you list a sensitive path, you tell every reader where it is.
User-agent: *
Disallow: /admin/
Disallow: /backups/
Disallow: /internal-reports/This file gives an attacker a short list of directories worth probing. Security teams often check robots.txt first for exactly this reason.
Blocked URLs can still appear in search
A Disallow rule stops crawling, not indexing. If other sites link to a blocked URL, a search engine can list the bare address without fetching the page. Blocking also prevents the crawler from seeing a noindex tag on that page. The earlier chapter on noindex versus Disallow covers this in more detail.
Private data therefore fails twice. It can be fetched by anyone who ignores the file, and its URL can still show up in results.
What to use instead
Choose the control that matches the goal.
- Private content, such as admin tools, customer data, or drafts: require authentication and return 401 or 403 to unauthorized requests. Add network restrictions where you can.
- Pages that may be fetched but should not appear in search: allow crawling and send
noindex, either in a meta tag or in anX-Robots-Tagresponse header. - Low-value URLs that waste crawl effort, such as internal search or session-ID URLs: use
Disallow. This is the job robots.txt is good at, and nothing sensitive is at stake.
You can confirm that a path is really protected by requesting it without credentials. The check below reports the status code, and a protected path should return 401 or 403, not 200.
import urllib.request
import urllib.error
def status_of(url):
try:
with urllib.request.urlopen(url, timeout=10) as response:
return response.status
except urllib.error.HTTPError as err:
return err.code
for path in ["/admin/", "/backups/"]:
url = "https://www.example.com" + path
code = status_of(url)
verdict = "protected" if code in (401, 403) else "CHECK THIS"
print(url, code, verdict)Replace the domain with your own site before running it. A 404 is also acceptable if the path should not exist, but the script flags it for review so you can decide.
Common mistakes
- Treating a
Disallowline as a substitute for a login. - Listing the exact secret directory name in robots.txt.
- Blocking a page in robots.txt and expecting it to disappear from search.
- Assuming that all bots obey the file. Reputable search engines do, but others often do not.
Exercise
A site owner adds Disallow: /client-invoices/ to robots.txt to keep invoices private. Give two reasons this fails, and name the control that should be used.
Show answer
First, robots.txt is only a request, so any client that ignores it can fetch the URLs directly. Second, the file is public and reveals the directory name, and a blocked URL can still appear in search results if other sites link to it. The invoices should be protected with authentication (return 401 or 403 to unauthorized requests), optionally with network restrictions.