Search in sources :

Example 1 with FilterResult

use of org.zaproxy.zap.spider.filters.ParseFilter.FilterResult in project zaproxy by zaproxy.

the class SpiderTask method runImpl.

private void runImpl() {
    // Check if the should stop
    if (parent.isStopped()) {
        log.debug("Spider process is stopped. Skipping crawling task...");
        deleteHistoryReference();
        return;
    }
    // Check if the crawling process is paused and do any "before execution" processing
    parent.preTaskExecution();
    // Fetch the resource
    HttpMessage msg;
    try {
        msg = prepareHttpMessage();
    } catch (Exception e) {
        log.error("Failed to prepare HTTP message: ", e);
        return;
    }
    try {
        fetchResource(msg);
    } catch (Exception e) {
        setErrorResponse(msg, e);
        parent.notifyListenersSpiderTaskResult(new SpiderTaskResult(msg, getSkippedMessage("ioerror")));
        return;
    }
    // Check if the should stop
    if (parent.isStopped()) {
        parent.notifyListenersSpiderTaskResult(new SpiderTaskResult(msg, getSkippedMessage("stopped")));
        log.debug("Spider process is stopped. Skipping crawling task...");
        return;
    }
    // Check if the crawling process is paused
    parent.checkPauseAndWait();
    // Check the parse filters to see if the resource should be skipped from parsing
    FilterResult filterResult = FilterResult.NOT_FILTERED;
    boolean wanted = false;
    for (ParseFilter filter : parent.getController().getParseFilters()) {
        filterResult = filter.filtered(msg);
        if (filterResult.isFiltered()) {
            break;
        } else if (filterResult == FilterResult.WANTED)
            wanted = true;
    }
    if (!wanted && !filterResult.isFiltered()) {
        filterResult = parent.getController().getDefaultParseFilter().filtered(msg);
    }
    if (filterResult.isFiltered()) {
        if (log.isDebugEnabled()) {
            log.debug("Resource [" + msg.getRequestHeader().getURI() + "] fetched, but will not be parsed due to a ParseFilter rule: " + filterResult.getReason());
        }
        parent.notifyListenersSpiderTaskResult(new SpiderTaskResult(msg, filterResult.getReason()));
        return;
    }
    // Check if the should stop
    if (parent.isStopped()) {
        parent.notifyListenersSpiderTaskResult(new SpiderTaskResult(msg, getSkippedMessage("stopped")));
        log.debug("Spider process is stopped. Skipping crawling task...");
        return;
    }
    // Check if the crawling process is paused
    parent.checkPauseAndWait();
    int maxDepth = parent.getSpiderParam().getMaxDepth();
    if (maxDepth == SpiderParam.UNLIMITED_DEPTH || resourceFound.getDepth() < maxDepth) {
        parent.notifyListenersSpiderTaskResult(new SpiderTaskResult(msg));
        processResource(msg);
    } else {
        parent.notifyListenersSpiderTaskResult(new SpiderTaskResult(msg, getSkippedMessage("maxdepth")));
    }
}
Also used : ParseFilter(org.zaproxy.zap.spider.filters.ParseFilter) FilterResult(org.zaproxy.zap.spider.filters.ParseFilter.FilterResult) HttpMessage(org.parosproxy.paros.network.HttpMessage) URIException(org.apache.commons.httpclient.URIException) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) DatabaseException(org.parosproxy.paros.db.DatabaseException) UnknownHostException(java.net.UnknownHostException) SSLException(javax.net.ssl.SSLException)

Example 2 with FilterResult

use of org.zaproxy.zap.spider.filters.ParseFilter.FilterResult in project zaproxy by zaproxy.

the class DefaultParseFilterUnitTest method shouldNotFilterHttpMessageWithTextResponse.

@Test
void shouldNotFilterHttpMessageWithTextResponse() throws Exception {
    // Given
    DefaultParseFilter filter = createDefaultParseFilter();
    HttpMessage httpMessage = createDefaultRequest();
    httpMessage.setResponseHeader("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n");
    // When
    FilterResult filterResult = filter.filtered(httpMessage);
    // Then
    assertThat(filterResult.isFiltered(), is(equalTo(false)));
}
Also used : FilterResult(org.zaproxy.zap.spider.filters.ParseFilter.FilterResult) HttpMessage(org.parosproxy.paros.network.HttpMessage) Test(org.junit.jupiter.api.Test)

Example 3 with FilterResult

use of org.zaproxy.zap.spider.filters.ParseFilter.FilterResult in project zaproxy by zaproxy.

the class DefaultParseFilterUnitTest method shouldNotFilterHttpMessageWithResponseEqualToMaxParseSize.

@Test
void shouldNotFilterHttpMessageWithResponseEqualToMaxParseSize() throws Exception {
    // Given
    int maxParseSizeBytes = 2;
    DefaultParseFilter filter = new DefaultParseFilter(createSpiderParam(maxParseSizeBytes), resourceBundle);
    HttpMessage httpMessage = createHttpMessageWithResponseBody("AB");
    // When
    FilterResult filterResult = filter.filtered(httpMessage);
    // Then
    assertThat(filterResult.isFiltered(), is(equalTo(false)));
}
Also used : FilterResult(org.zaproxy.zap.spider.filters.ParseFilter.FilterResult) HttpMessage(org.parosproxy.paros.network.HttpMessage) Test(org.junit.jupiter.api.Test)

Example 4 with FilterResult

use of org.zaproxy.zap.spider.filters.ParseFilter.FilterResult in project zaproxy by zaproxy.

the class DefaultParseFilterUnitTest method shouldNotFilterHttpMessageWithRobotsTxtRequestEvenWithContentType.

@Test
void shouldNotFilterHttpMessageWithRobotsTxtRequestEvenWithContentType() throws Exception {
    // Given
    DefaultParseFilter filter = createDefaultParseFilter();
    HttpMessage httpMessage = createHttpMessageWithRequestUri("/robots.txt");
    httpMessage.getResponseHeader().setHeader(HttpHeader.CONTENT_TYPE, "text/plain");
    // When
    FilterResult filterResult = filter.filtered(httpMessage);
    // Then
    assertThat(filterResult.isFiltered(), is(equalTo(false)));
}
Also used : FilterResult(org.zaproxy.zap.spider.filters.ParseFilter.FilterResult) HttpMessage(org.parosproxy.paros.network.HttpMessage) Test(org.junit.jupiter.api.Test)

Example 5 with FilterResult

use of org.zaproxy.zap.spider.filters.ParseFilter.FilterResult in project zaproxy by zaproxy.

the class DefaultParseFilterUnitTest method shouldNotFilterHttpMessageWithRedirectResponse.

@Test
void shouldNotFilterHttpMessageWithRedirectResponse() throws Exception {
    // Given
    DefaultParseFilter filter = createDefaultParseFilter();
    HttpMessage httpMessage = createDefaultRequest();
    httpMessage.setResponseHeader("HTTP/1.1 303 See Other\r\n");
    // When
    FilterResult filterResult = filter.filtered(httpMessage);
    // Then
    assertThat(filterResult.isFiltered(), is(equalTo(false)));
}
Also used : FilterResult(org.zaproxy.zap.spider.filters.ParseFilter.FilterResult) HttpMessage(org.parosproxy.paros.network.HttpMessage) Test(org.junit.jupiter.api.Test)

Aggregations

HttpMessage (org.parosproxy.paros.network.HttpMessage)17 FilterResult (org.zaproxy.zap.spider.filters.ParseFilter.FilterResult)17 Test (org.junit.jupiter.api.Test)16 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UnknownHostException (java.net.UnknownHostException)1 SSLException (javax.net.ssl.SSLException)1 URIException (org.apache.commons.httpclient.URIException)1 DatabaseException (org.parosproxy.paros.db.DatabaseException)1 HttpMalformedHeaderException (org.parosproxy.paros.network.HttpMalformedHeaderException)1 ParseFilter (org.zaproxy.zap.spider.filters.ParseFilter)1