Sub topic 7
Why robots.txt is not a security measure
This chapter assumes you can write User-agent, Disallow, and Allow rules, and that you know the difference between crawling and indexing from earlier chapters. You should also know basic HTTP status codes (200, 401, 403, 404). By the end you will be able to explain why robots.txt cannot protect content, spot the ways it leaks information, and pick a control that does the job.
What robots.txt actually is
robots.txt is a request. A crawler fetches /robots.txt, reads the rules, and decides whether to follow them. The Robots Exclusion Protocol (standardized as RFC 9309) describes this as a convention for well-behaved automated clients. Nothing in the protocol makes the server refuse a request.
When a crawler ignores your Disallow line, the server still answers with the page and a 200 status. The server has no idea the rule existed. Compliance depends entirely on the client.
Major search engines honor robots.txt because they want to be good citizens and avoid wasting your bandwidth. Scrapers, vulnerability scanners, and attackers have no such incentive. Some of them read robots.txt on purpose, because it tells them where to look.
Four ways robots.txt fails as protection
1. Compliance is voluntary
Any HTTP client can request a disallowed URL. A browser, curl, or a script that sets a Googlebot user-agent string will all get the content if the server serves it. The rule changes nothing about what the server returns.
2. The file is public and lists your sensitive paths
robots.txt must be readable by anyone, because crawlers need to fetch it without credentials. That means a line such as the following is visible to the world:
User-agent: *
Disallow: /internal-reports/
Disallow: /backup-2026/
Disallow: /admin/You have just published a short list of paths worth probing. Security testers check robots.txt early in a reconnaissance pass for this reason.
The next script shows how little effort that takes. It downloads a robots.txt file and prints every disallowed path.
import sys
from urllib.request import urlopen
def disallowed_paths(site):
url = site.rstrip("/") + "/robots.txt"
with urlopen(url, timeout=10) as response:
text = response.read().decode("utf-8", errors="replace")
paths = []
for line in text.splitlines():
line = line.split("#", 1)[0].strip()
if line.lower().startswith("disallow:"):
value = line.split(":", 1)[1].strip()
if value:
paths.append(value)
return paths
if __name__ == "__main__":
site = sys.argv[1] if len(sys.argv) > 1 else "https://www.example.com"
for path in disallowed_paths(site):
print(path)Run it as python list_disallowed.py https://your-site.example against a site you own. The output is the list of places you asked crawlers to avoid, in plain text.
3. Blocked URLs can still be indexed
Disallow stops a crawler from fetching a page. It does not stop a search engine from knowing the URL exists. If other pages link to a blocked URL, the engine can list it in results with no description, because it never fetched the content. The URL text and the anchor text of the links pointing to it may still be shown.
This is the crawl-versus-index distinction from the earlier chapter, now with privacy consequences. A page named /layoffs-draft-q4 that is blocked in robots.txt but linked from a public page can still appear as a bare URL in search results.
4. Old copies persist
Web archives, caches, and third-party datasets may keep copies of your robots.txt and of content crawled before you added a rule. Adding a Disallow line today does not recall anything already collected.
The sequence in practice
The diagram shows what happens when a compliant crawler and a non-compliant client both visit a protected-by-robots.txt page.
Controls that do protect content
Choose the control by what you are trying to achieve. The table maps common goals to tools that actually enforce them.
| Goal | Wrong tool | Right tool |
|---|---|---|
| Keep content private from everyone except staff | `Disallow` | Authentication and authorization (login, SSO, 401 or 403 responses) |
| Keep a public page out of search results | `Disallow` | `noindex` meta tag or `X-Robots-Tag` header, with the page left crawlable |
| Keep a staging site out of search and away from the public | `Disallow: /` | HTTP basic auth or an IP allowlist |
| Reduce load from an aggressive bot | `Disallow` | Rate limiting, a web application firewall, bot management |
| Stop a file type from being downloaded | `Disallow: /*.pdf$` | Server access rules, signed URLs, or login |
| Save crawl budget on low-value URLs | none needed | `Disallow` is appropriate here |
Notice the last row. robots.txt is a good tool for managing crawler traffic. It is the wrong tool for confidentiality.
Require authentication
If a resource must not be public, the server has to refuse unauthenticated requests. This nginx example protects an admin area with HTTP basic authentication:
location /admin/ {
auth_basic "Restricted area";
auth_basic_user_file /etc/nginx/.htpasswd;
}An unauthenticated request now gets a 401 response, whether or not the client reads robots.txt. Crawlers cannot index what they cannot see. For a real application, use your framework's login and role checks rather than basic auth, and send the request over HTTPS.
Use noindex for pages that are public but should not rank
To keep a reachable page out of search results, send a noindex signal and let crawlers fetch the page so they can see it:
X-Robots-Tag: noindexor, in the HTML head:
<meta name="robots" content="noindex">The second line is the contents of a tag you place in your page source. It is shown here as text for reference.
The Disallow plus noindex trap
Do not combine Disallow with noindex on the same URL. A crawler that is blocked from fetching the page never sees the noindex instruction, so the URL can stay in the index. If you added Disallow first and now want the page removed from results, remove the Disallow rule, let the crawler see the noindex, and re-add the block later if you still want it.
Handle bad bots at the server
For unwanted automated traffic, use measures the server enforces:
- Rate limits per IP address or per session.
- A web application firewall or CDN bot management rules.
- Verification of claimed crawler identity. A client that sends a Googlebot user-agent can be checked with a reverse DNS lookup followed by a forward lookup, or against the search engine's published IP ranges. User-agent strings alone are trivial to fake.
Writing a robots.txt that leaks less
Sometimes you must keep a Disallow rule for crawl-management reasons and the path name is itself revealing. You have options:
- Block a shorter prefix that covers many paths without naming each one, for example
Disallow: /intrather than a full list of internal folder names. Test that it does not catch public URLs. - Put the real protection (login) on the path, and leave it out of robots.txt altogether. A path behind a login that is never linked publicly will not be crawled.
- Never put secrets in robots.txt: no tokens, no staging hostnames, no backup file names. Treat every line as published.
Edge cases to know
- Status codes for robots.txt itself. Google treats a 4xx response (other than 429) for
/robots.txtas "no file", so everything is crawlable. A 5xx response is treated as a temporary full block, and if the error persists for a long time Google may fall back to a cached copy or treat the site as unrestricted. A broken robots.txt can open or close your whole site by accident. - Caching. Crawlers cache robots.txt, commonly for up to about 24 hours. A change you make now may not be picked up immediately, which matters if you are trying to block something quickly.
- Subdomains and protocols. Each host, protocol, and port has its own robots.txt. A file at
https://www.example.com/robots.txtdoes not apply tohttps://staging.example.com/. A forgotten staging host with no protection is a common leak. - Temporary removal. Search engines offer removal tools for urgent cases. These hide a URL for a limited time and do not replace fixing the underlying access control or adding
noindex.
Summary of the working model
robots.txt answers the question "which URLs should well-behaved crawlers request?". It does not answer "who may read this content?". The second question belongs to your server's authentication and authorization. Treat robots.txt as a public sign on the door and put a lock behind it where one is needed.
Exercise
Your robots.txt contains Disallow: /client-invoices/. The folder holds PDFs that are readable by anyone who knows the URL. A colleague says the Disallow rule keeps the invoices safe. Give two reasons the colleague is wrong, and name the fix.
Show answer
First, robots.txt is voluntary and public. Any client can ignore it and request the PDFs directly, and the Disallow line also tells everyone the folder name exists. Second, if any public page links to an invoice URL, a search engine can still list that URL in results without fetching it. The fix is to require authentication and authorization on /client-invoices/ so the server returns 401 or 403 to anyone not allowed, and to remove the path from robots.txt so it is not advertised.
Exercise
You want a public "thank you" page to stay out of search results. You add both Disallow: /thank-you in robots.txt and a noindex meta tag on the page. The page keeps appearing in results. Why, and what do you change?
Show answer
The Disallow rule stops the crawler from fetching the page, so it never sees the noindex tag, and the URL can remain in the index. Remove the Disallow rule so the page can be crawled, keep the noindex tag, and wait for the crawler to revisit the page and drop it.