diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fe5e3dc..e0784454 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ [Full Changelog](https://github.com/internetarchive/heritrix3/compare/3.18.0...HEAD) +### Security + +* **BrowserProcessor:** The recording proxy now listens on the loopback interface only and rejects requests that + don't belong to a page the crawler has open. Previously anyone who could reach the crawler's host could use it as + an open forward proxy. + ## [3.18.0](https://github.com/internetarchive/heritrix3/releases/tag/3.18.0) (2026-10-06) [Download distribution zip](https://github.com/internetarchive/heritrix3/releases/download/3.18.0/heritrix-3.18.0-dist.zip) (or [tar.gz](https://github.com/internetarchive/heritrix3/releases/download/3.18.0/heritrix-3.18.0-dist.tar.gz)) diff --git a/engine/src/main/java/org/archive/crawler/processor/BrowserProcessor.java b/engine/src/main/java/org/archive/crawler/processor/BrowserProcessor.java index ff911499..560950e6 100644 --- a/engine/src/main/java/org/archive/crawler/processor/BrowserProcessor.java +++ b/engine/src/main/java/org/archive/crawler/processor/BrowserProcessor.java @@ -50,8 +50,8 @@ import org.json.JSONException; import org.json.JSONObject; import org.springframework.context.ApplicationEventPublisher; +import java.io.ByteArrayInputStream; import java.io.IOException; -import java.io.InputStream; import java.io.UncheckedIOException; import java.nio.ByteBuffer; import java.nio.channels.Channels; @@ -375,14 +375,14 @@ public class BrowserProcessor extends Processor { private void handleProxyRequest(MitmProxy.Request proxyRequest) throws IOException { logger.log(System.Logger.Level.DEBUG, "Handling proxy request {0}", proxyRequest); - if (proxyRequest.url().startsWith("https://firefox.settings.services.mozilla.com/")) { - proxyRequest.sendResponse(403, Map.of(), InputStream.nullInputStream()); + String pageId = proxyRequest.request().getHeaders().get(PAGE_ID_HEADER); + BrowserPage page = pageId == null ? null : pages.get(pageId); + if (page == null) { + String message = "Missing or invalid " + PAGE_ID_HEADER + " header\n"; + proxyRequest.sendResponse(403, Map.of("Content-Type", "text/plain"), + new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8))); return; } - String pageId = proxyRequest.request().getHeaders().get(PAGE_ID_HEADER); - if (pageId == null) return; - BrowserPage page = pages.get(pageId); - if (page == null) return; CrawlURI curi = page.curi(); // FIXME: more accurate to annotate with another header indicating this is the main request? diff --git a/engine/src/test/java/org/archive/crawler/processor/BrowserProcessorTest.java b/engine/src/test/java/org/archive/crawler/processor/BrowserProcessorTest.java index 0d639423..e88b4516 100644 --- a/engine/src/test/java/org/archive/crawler/processor/BrowserProcessorTest.java +++ b/engine/src/test/java/org/archive/crawler/processor/BrowserProcessorTest.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.net.Inet4Address; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.Socket; import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.util.*; @@ -38,6 +39,7 @@ class BrowserProcessorTest { private static FetchHTTP2 fetcher; private static BrowserProcessor browserProcessor; private static String baseUrl; + private static volatile int serverRequests; private static ArrayList subrequests; private static CrawlController crawlController; private final Set recorders = new HashSet<>(); @@ -185,6 +187,18 @@ class BrowserProcessorTest { } } + @Test + public void testProxyRejectsRequestWithoutPage() throws IOException { + int requestsBefore = serverRequests; + try (var socket = new Socket(InetAddress.getLoopbackAddress(), browserProcessor.proxy.getPort())) { + socket.getOutputStream().write(("GET " + baseUrl + " HTTP/1.0\r\n\r\n").getBytes(StandardCharsets.US_ASCII)); + String response = new String(socket.getInputStream().readAllBytes(), StandardCharsets.US_ASCII); + assertTrue(response.startsWith("HTTP/1.1 403 "), response); + assertTrue(response.endsWith("Missing or invalid Heritrix-Request-ID header\n"), response); + } + assertEquals(requestsBefore, serverRequests, "request should not have been forwarded"); + } + private CrawlURI newCrawlURI(String uri) throws URIException { CrawlURI curi = new CrawlURI(UURIFactory.getInstance(uri)); Recorder recorder = new Recorder(tempDir.toFile(), "fetcher"); @@ -216,6 +230,7 @@ class BrowserProcessorTest { httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), -1); httpServer.createContext("/", exchange -> { logger.log(DEBUG, "Server received request: {0} {1}", exchange.getRequestMethod(), exchange.getRequestURI()); + serverRequests++; String userAgent = exchange.getRequestHeaders().getFirst("User-Agent"); if (userAgent != null) exchange.getResponseHeaders().add("Reflected-User-Agent", userAgent); String contentType = "text/html";