From be329447abdc05cf3e822bfc1d0d1935f0e3c57a Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Sun, 16 Jun 2024 09:20:32 +1000 Subject: [PATCH] Block AI bots --- Gemfile | 1 + Gemfile.lock | 3 +++ config.ru | 7 +++++++ 3 files changed, 11 insertions(+) diff --git a/Gemfile b/Gemfile index de97a9f..1abe236 100644 --- a/Gemfile +++ b/Gemfile @@ -38,6 +38,7 @@ gem "matrix" gem "rack-session" gem "rack-rewrite" gem "rack-contrib" +gem "rack-attack", "~> 6.7" gem "ruby-readability", require: "readability" gem "down" diff --git a/Gemfile.lock b/Gemfile.lock index 27cd766..9c5068c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -365,6 +365,8 @@ GEM que (2.3.0) racc (1.8.0) rack (2.2.9) + rack-attack (6.7.0) + rack (>= 1.0, < 4) rack-contrib (2.5.0) rack (< 4) rack-rewrite (1.5.1) @@ -579,6 +581,7 @@ DEPENDENCIES pg puma que + rack-attack (~> 6.7) rack-contrib rack-rewrite rack-session diff --git a/config.ru b/config.ru index 872d471..c71d8d2 100644 --- a/config.ru +++ b/config.ru @@ -23,6 +23,13 @@ use Rack::Rewrite do } end +require "rack/attack" +use Rack::Attack + +Rack::Attack.blocklist("block AI") do |req| + req.user_agent.match /AdsBot-Google|Amazonbot|anthropic-ai|Applebot|Applebot-Extended|AwarioRssBot|AwarioSmartBot|Bytespider|CCBot|ChatGPT-User|ClaudeBot|Claude-Web|cohere-ai|DataForSeoBot|Diffbot|FacebookBot|FriendlyCrawler|Google-Extended|GoogleOther|GPTBot|img2dataset|ImagesiftBot|magpie-crawler|Meltwater|omgili|omgilibot|peer39_crawler|peer39_crawler\/1.0|PerplexityBot|PiplBot|scoop.it|Seekr|YouBot/ +end + require "adamantium/middleware/header_fix" use Adamantium::Middleware::HeaderFix do |headers, env| unless headers["Content-Type"]&.downcase&.include?("xml") || headers["Content-Type"]&.downcase&.include?("json")