mirror of
https://github.com/internetarchive/heritrix3.git
synced 2026-11-22 21:57:17 +00:00
BrowserProcessor: reject proxy requests that don't belong to an open page
This commit is contained in:
1 parent
21b808f9c2
commit
74d236ee57
3 files changed
+28
-7
No files matched your search
@@ -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?
|
||||
|
||||
@@ -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<CrawlURI> subrequests;
|
||||
private static CrawlController crawlController;
|
||||
private final Set<Recorder> 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";
|
||||
|
||||
Reference in new issue
Block a user