Who kept the bots out? Stopping content being harvested by AI
Tags: ai, scraping, http
Published: 2023-06-22 09:57:18

AI-powered content generation has exploded in popularity recently, with bots like [ChatGPT](https://chat.openai.com) and [Bard](https://bard.google.com/), but the giant amounts of data these bots require comes from harvesting the web. What if you don’t want your content feeding the bots? Some respect [robots.txt](https://developers.google.com/search/docs/crawling-indexing/robots/intro), others notice a new ‘noai’ header tag.

An [article in Vice](https://www.vice.com/en/article/dy3vmx/an-ai-scraping-tool-is-overwhelming-websites-with-traffic) recently drew attention to the way AI bots are harvesting the web, in some cases quite aggressively. Site owners quite reasonably want to protect the originality of their creative works, assert their copyrights, and also not have to deal with traffic surges from ill-configured or badly-behaving scraper bots.

The particular tool mentioned in the Vice article is [Img2dataset](https://github.com/rom1504/img2dataset), and right now, it doesn't pay attention to the robots.txt file, the normal mechanism you can use to dissuade well behaved bots from indexing your content.  However, it does [respect a new HTTP header directive](https://github.com/rom1504/img2dataset/issues/293), `X-Robots-Tag: noai` (and also `noindex`, though that's an existing and already well-known part of the robots.txt standard).

A lot of Fastly customers have multiple websites running through Fastly, and in many cases multiple backend servers feeding a single public domain.  Managing the addition of HTTP metadata at the origin application can be tedious and error prone, but fortunately applying it at the edge is pretty simple.

If you have a [VCL powered Fastly service](https://developer.fastly.com/learning/vcl/), you can add a header in a deliver snippet or in the vcl_deliver subroutine of your custom VCL code:

```vcl
set resp.http.x-robots-tag = "noai";
```

Here's [a fiddle you can play with](https://fiddle.fastly.dev/fiddle/68cdc262) demonstrating that code.

If you are using our new [Compute@Edge platform](https://developer.fastly.com/learning/compute/), setting a header varies from one language to another.  There's a [generic example in our developer hub](https://developer.fastly.com/solutions/examples/add-remove-or-change-http-headers) covering all languages we support, but as an example, here's how to do it in JavaScript:

```js
resp.headers.set("x-robots-tag", "noai");
```

And again, [here's a fiddle with that code](https://fiddle.fastly.dev/fiddle/fa0d0b36) you can remix and try for yourself.

Of course this just fixes the issue for one bot, and while others might pick up on signals from robots.txt, some are just going to be stubbornly insistent on crawling and downloading your content regardless.  **These bots should be considered bad actors and blocked.**  Fastly has lots of ways to do this - manual logic can be written in both VCL and all Compute languages to block based on signals such as:

- ASN (Autonomous system number): the block of IPs owned by a single network operator
- Explicit IP addresses or ranges
- HTTP headers such as `User-Agent` or `Accept`
- Originating traffic location

Here's [an example of banning by IP address](https://developer.fastly.com/solutions/examples/dictionary-based-ip-block-list).

You might like to use challenges to separate out bot traffic, such as a [CAPTCHA](https://developer.fastly.com/solutions/examples/captcha-challenge) or proof of work, or use [DNS lookups to authenticate good crawlers](https://developer.fastly.com/solutions/examples/verify-if-a-web-crawler-accessing-your-server-really-is-googlebot).

Finally, if you want more of a magic bullet, consider enabling our [Next-gen Web Application Firewall](https://docs.fastly.com/products/fastly-next-gen-waf) on your service, which will detect anomalous behaviour, alert and block attacks automatically.

If you want to get inspiration for what else you could be doing with edge compute technologies, check out the [solutions area on the Fastly developer hub](https://developer.fastly.com/solutions/).  Happy coding!