|
| 1 | +/* |
| 2 | + * This file is part of the Heritrix web crawler (crawler.archive.org). |
| 3 | + * |
| 4 | + * Licensed to the Internet Archive (IA) by one or more individual |
| 5 | + * contributors. |
| 6 | + * |
| 7 | + * The IA licenses this file to You under the Apache License, Version 2.0 |
| 8 | + * (the "License"); you may not use this file except in compliance with |
| 9 | + * the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.archive.modules.processor; |
| 20 | + |
| 21 | +import java.io.ByteArrayInputStream; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | +import org.archive.modules.CrawlURI; |
| 27 | +import org.archive.net.UURIFactory; |
| 28 | +import org.archive.util.Recorder; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.io.TempDir; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.*; |
| 33 | + |
| 34 | +class BotBlockDetectorTest { |
| 35 | + |
| 36 | + @TempDir |
| 37 | + Path tempDir; |
| 38 | + |
| 39 | + @Test |
| 40 | + void detectsAkamaiBlock() throws Exception { |
| 41 | + assertBlocked(akamaiBlock(), "akamai"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void detectsAnubisRedirectChallenge() throws Exception { |
| 46 | + CrawlURI curi = curi(307); |
| 47 | + curi.putHttpResponseHeader("Location", |
| 48 | + "/.within.website/?redir=https%3A%2F%2Fexample.com%2F"); |
| 49 | + |
| 50 | + assertBlocked(curi, "anubis"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void detectsAnubisCookieChallenge() throws Exception { |
| 55 | + CrawlURI curi = curi(200); |
| 56 | + curi.putHttpResponseHeader("Set-Cookie", |
| 57 | + "techaro.lol-anubis-auth=token; Path=/"); |
| 58 | + recordResponse(curi, "<script id=\"anubis_challenge\"></script>"); |
| 59 | + |
| 60 | + assertBlocked(curi, "anubis"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void ordinaryResponseDoesNotMatch() throws Exception { |
| 65 | + CrawlURI curi = curi(200); |
| 66 | + |
| 67 | + assertNotBlocked(curi); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void detectsCloudflareChallenge() throws Exception { |
| 72 | + CrawlURI curi = curi(403); |
| 73 | + curi.putHttpResponseHeader("Server", "cloudflare"); |
| 74 | + curi.putHttpResponseHeader("CF-Mitigated", "challenge"); |
| 75 | + |
| 76 | + assertBlocked(curi, "cloudflare"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void detectsCloudflareBlock() throws Exception { |
| 81 | + assertBlocked(cloudflareBlock(), "cloudflare"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void detectsIncapsulaChallenge() throws Exception { |
| 86 | + CrawlURI curi = curi(404); |
| 87 | + curi.putHttpResponseHeader("x-iinfo", "foo"); |
| 88 | + recordResponse(curi, |
| 89 | + "Request unsuccessful. Incapsula incident ID: 123456"); |
| 90 | + |
| 91 | + assertBlocked(curi, "incapsula"); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void unrelated403ResponseDoesNotMatch() throws Exception { |
| 96 | + CrawlURI curi = curi(403); |
| 97 | + curi.putHttpResponseHeader("Server", "example"); |
| 98 | + |
| 99 | + assertNotBlocked(curi); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void checkpointRoundTripPreservesBlockedRequestCounts() throws Exception { |
| 104 | + BotBlockDetector detector = new BotBlockDetector(); |
| 105 | + detector.process(akamaiBlock()); |
| 106 | + detector.process(cloudflareBlock()); |
| 107 | + |
| 108 | + BotBlockDetector restored = new BotBlockDetector(); |
| 109 | + restored.fromCheckpointJson(detector.toCheckpointJson()); |
| 110 | + |
| 111 | + assertEquals(1, restored.getCounts().get("akamai").get()); |
| 112 | + assertEquals(1, restored.getCounts().get("cloudflare").get()); |
| 113 | + assertEquals(2, restored.getURICount()); |
| 114 | + } |
| 115 | + |
| 116 | + private static void assertBlocked(CrawlURI curi, String service) |
| 117 | + throws InterruptedException { |
| 118 | + new BotBlockDetector().process(curi); |
| 119 | + assertEquals(Set.of("botblock:" + service), curi.getAnnotations()); |
| 120 | + } |
| 121 | + |
| 122 | + private static void assertNotBlocked(CrawlURI curi) |
| 123 | + throws InterruptedException { |
| 124 | + new BotBlockDetector().process(curi); |
| 125 | + assertTrue(curi.getAnnotations().isEmpty()); |
| 126 | + } |
| 127 | + |
| 128 | + private static CrawlURI akamaiBlock() throws Exception { |
| 129 | + CrawlURI curi = curi(403); |
| 130 | + curi.putHttpResponseHeader("Server", "AkamaiGHost"); |
| 131 | + return curi; |
| 132 | + } |
| 133 | + |
| 134 | + private CrawlURI cloudflareBlock() throws Exception { |
| 135 | + CrawlURI curi = curi(403); |
| 136 | + curi.putHttpResponseHeader("Server", "cloudflare"); |
| 137 | + recordResponse(curi, "<h1 data-translate=\"block_headline\">Sorry, you have been blocked</h1>"); |
| 138 | + return curi; |
| 139 | + } |
| 140 | + |
| 141 | + private static CrawlURI curi(int status) throws Exception { |
| 142 | + CrawlURI curi = new CrawlURI(UURIFactory.getInstance("https://example.com/")); |
| 143 | + curi.setFetchStatus(status); |
| 144 | + curi.setFetchType(CrawlURI.FetchType.HTTP_GET); |
| 145 | + curi.setContentType("text/html"); |
| 146 | + return curi; |
| 147 | + } |
| 148 | + |
| 149 | + private void recordResponse(CrawlURI curi, String body) throws Exception { |
| 150 | + byte[] response = ("HTTP/1.1 " + curi.getFetchStatus() + " Test\r\n" |
| 151 | + + "Content-Type: text/html\r\n" |
| 152 | + + "Content-Length: " + body.getBytes(StandardCharsets.UTF_8).length + "\r\n" |
| 153 | + + "\r\n" |
| 154 | + + body).getBytes(StandardCharsets.UTF_8); |
| 155 | + Recorder recorder = new Recorder(tempDir.toFile(), "bot-block-detector"); |
| 156 | + curi.setRecorder(recorder); |
| 157 | + recorder.inputWrap(new ByteArrayInputStream(response)); |
| 158 | + recorder.getRecordedInput().readFully(); |
| 159 | + recorder.close(); |
| 160 | + } |
| 161 | +} |
0 commit comments