Search in sources :

Example 26 with Source

use of net.htmlparser.jericho.Source in project zaproxy by zaproxy.

the class SpiderHtmlFormParserUnitTest method shouldUseBaseHtmlUrlWhenParsingPostForm.

@Test
public void shouldUseBaseHtmlUrlWhenParsingPostForm() {
    // Given
    SpiderHtmlFormParser htmlParser = createSpiderHtmlFormParser();
    TestSpiderParserListener listener = createTestSpiderParserListener();
    htmlParser.addSpiderParserListener(listener);
    HttpMessage msg = createMessageWith("POST", "FormWithHtmlBase.html", "search", "http://base.example.com/");
    Source source = createSource(msg);
    // When
    boolean completelyParsed = htmlParser.parseResource(msg, source, BASE_DEPTH);
    // Then
    assertThat(completelyParsed, is(equalTo(false)));
    assertThat(listener.getNumberOfResourcesFound(), is(equalTo(1)));
    assertThat(listener.getResourcesFound(), contains(postResource(msg, 1, "http://base.example.com/search", "q=Search&submit=Submit")));
}
Also used : HttpMessage(org.parosproxy.paros.network.HttpMessage) Source(net.htmlparser.jericho.Source) Test(org.junit.Test)

Example 27 with Source

use of net.htmlparser.jericho.Source in project zaproxy by zaproxy.

the class SpiderHtmlFormParserUnitTest method shouldParseGetFormWithMultipleSubmitFields.

@Test
public void shouldParseGetFormWithMultipleSubmitFields() {
    // Given
    SpiderHtmlFormParser htmlParser = createSpiderHtmlFormParser();
    TestSpiderParserListener listener = createTestSpiderParserListener();
    htmlParser.addSpiderParserListener(listener);
    HttpMessage messageHtmlResponse = createMessageWith("GET", "FormMultipleSubmitFields.html");
    Source source = createSource(messageHtmlResponse);
    // When
    boolean completelyParsed = htmlParser.parseResource(messageHtmlResponse, source, BASE_DEPTH);
    // Then
    assertThat(completelyParsed, is(equalTo(false)));
    assertThat(listener.getNumberOfUrlsFound(), is(equalTo(5)));
    assertThat(listener.getUrlsFound(), contains("http://example.org/?field1=Text+1&field2=Text+2&submit1=Submit+1", "http://example.org/?field1=Text+1&field2=Text+2&submit2=Submit+2", "http://example.org/?field1=Text+1&field2=Text+2&submit3=Submit+3", "http://example.org/?field1=Text+1&field2=Text+2&submit=Submit+4", "http://example.org/?field1=Text+1&field2=Text+2&submit=Submit+5"));
}
Also used : HttpMessage(org.parosproxy.paros.network.HttpMessage) Source(net.htmlparser.jericho.Source) Test(org.junit.Test)

Example 28 with Source

use of net.htmlparser.jericho.Source in project zaproxy by zaproxy.

the class SpiderTextParserUnitTest method shouldFailToParseAnUndefinedMessage.

@Test(expected = NullPointerException.class)
public void shouldFailToParseAnUndefinedMessage() {
    // Given
    HttpMessage undefinedMessage = null;
    SpiderTextParser spiderParser = new SpiderTextParser();
    Source source = createSource(createMessageWith(EMPTY_BODY));
    // When
    spiderParser.parseResource(undefinedMessage, source, BASE_DEPTH);
// Then = NullPointerException
}
Also used : HttpMessage(org.parosproxy.paros.network.HttpMessage) Source(net.htmlparser.jericho.Source) Test(org.junit.Test)

Example 29 with Source

use of net.htmlparser.jericho.Source in project zaproxy by zaproxy.

the class SpiderHtmlParser method parseResource.

/**
	 * @throws NullPointerException if {@code message} is null.
	 */
@Override
public boolean parseResource(HttpMessage message, Source source, int depth) {
    // Prepare the source, if not provided
    if (source == null) {
        source = new Source(message.getResponseBody().toString());
    }
    // Get the context (base url)
    String baseURL = message.getRequestHeader().getURI().toString();
    // Try to see if there's any BASE tag that could change the base URL
    Element base = source.getFirstElement(HTMLElementName.BASE);
    if (base != null) {
        if (log.isDebugEnabled()) {
            log.debug("Base tag was found in HTML: " + base.getDebugInfo());
        }
        String href = base.getAttributeValue("href");
        if (href != null && !href.isEmpty()) {
            baseURL = URLCanonicalizer.getCanonicalURL(href, baseURL);
        }
    }
    // Parse the source
    parseSource(message, source, depth, baseURL);
    // Parse the comments
    if (params.isParseComments()) {
        List<StartTag> comments = source.getAllStartTags(StartTagType.COMMENT);
        for (StartTag comment : comments) {
            Source s = new Source(comment.getTagContent());
            if (!parseSource(message, s, depth, baseURL)) {
                Matcher matcher = PLAIN_COMMENTS_URL_PATTERN.matcher(s.toString());
                while (matcher.find()) {
                    processURL(message, depth, matcher.group(), baseURL);
                }
            }
        }
    }
    return false;
}
Also used : Matcher(java.util.regex.Matcher) Element(net.htmlparser.jericho.Element) Source(net.htmlparser.jericho.Source) StartTag(net.htmlparser.jericho.StartTag)

Example 30 with Source

use of net.htmlparser.jericho.Source in project zaproxy by zaproxy.

the class SpiderHtmlFormParserUnitTest method shouldNeverConsiderCompletelyParsed.

@Test
public void shouldNeverConsiderCompletelyParsed() {
    // Given
    SpiderHtmlFormParser htmlParser = createSpiderHtmlFormParser();
    HttpMessage messageHtmlResponse = createMessageWith("NoForms.html");
    Source source = createSource(messageHtmlResponse);
    // When
    boolean completelyParsed = htmlParser.parseResource(messageHtmlResponse, source, BASE_DEPTH);
    // Then
    assertThat(completelyParsed, is(equalTo(false)));
}
Also used : HttpMessage(org.parosproxy.paros.network.HttpMessage) Source(net.htmlparser.jericho.Source) Test(org.junit.Test)

Aggregations

Source (net.htmlparser.jericho.Source)77 HttpMessage (org.parosproxy.paros.network.HttpMessage)73 Test (org.junit.Test)71 SpiderParam (org.zaproxy.zap.spider.SpiderParam)24 DefaultValueGenerator (org.zaproxy.zap.model.DefaultValueGenerator)6 Element (net.htmlparser.jericho.Element)3 Date (java.util.Date)2 DatabaseException (org.parosproxy.paros.db.DatabaseException)2 HttpMalformedHeaderException (org.parosproxy.paros.network.HttpMalformedHeaderException)2 Matcher (java.util.regex.Matcher)1 Attribute (net.htmlparser.jericho.Attribute)1 StartTag (net.htmlparser.jericho.StartTag)1 URIException (org.apache.commons.httpclient.URIException)1 HistoryFilter (org.parosproxy.paros.extension.history.HistoryFilter)1 HistoryReference (org.parosproxy.paros.model.HistoryReference)1 HtmlParameter (org.parosproxy.paros.network.HtmlParameter)1 SpiderParser (org.zaproxy.zap.spider.parser.SpiderParser)1