Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

3.0 KB
Raw Blame
package opensuite.target;

import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

import java.util.List;

/** Tests the link extraction behind the live passive crawl. */
public final class LinkFinderTest {

    public void run() {
        Harness.suite("link finder");

        Harness.test("href, src and action values are resolved against the page", () -> {
            List<String> l = LinkFinder.links("http://example.com/a/page.html",
                    "<a href=\"/x\">x</a><img src='img/y.png'><form action=../post><script src=//cdn.example.net/s.js></script>");
            Expect.equal(l, List.of("http://example.com/x", "http://example.com/a/img/y.png",
                    "http://example.com/post", "http://cdn.example.net/s.js"), "resolved links in document order");
        });

        Harness.test("fragments are dropped and repeats collapse", () -> {
            List<String> l = LinkFinder.links("https://example.com/", "<a href='/a#top'></a><a href=\"/a\"></a><a href='#here'></a>");
            Expect.equal(l, List.of("https://example.com/a"), "one link");
        });

        Harness.test("non-web schemes are ignored", () -> {
            List<String> l = LinkFinder.links("http://example.com/",
                    "<a href='javascript:void(0)'></a><a href='mailto:[email protected]'></a><img src='data:image/png;base64,AAAA'><a href='ftp://example.com/f'></a>");
            Expect.isTrue(l.isEmpty(), "nothing kept");
        });

        Harness.test("queries are kept, entities decoded and default ports omitted", () -> {
            List<String> l = LinkFinder.links("http://example.com:80/", "<a href='/s?q=1&amp;id=2'></a><a href='http://EXAMPLE.com:8080/z'></a>");
            Expect.equal(l, List.of("http://example.com/s?q=1&id=2", "http://example.com:8080/z"), "normalised");
        });

        Harness.test("an attribute name inside another word is not a link", () -> {
            List<String> l = LinkFinder.links("http://example.com/", "<div data-nosrc=\"/no\" xhref='/no2'><a href='/yes'></a></div>");
            Expect.equal(l, List.of("http://example.com/yes"), "only the real attribute");
        });

        Harness.test("pathOn gives the path only for the same origin", () -> {
            Expect.equal(LinkFinder.pathOn("http://example.com", "http://example.com/a?b=1"), "/a?b=1", "same site");
            Expect.equal(LinkFinder.pathOn("http://example.com", "http://example.com.evil.net/a"), null, "prefix of another host");
            Expect.equal(LinkFinder.pathOn("http://example.com", "https://example.com/a"), null, "other scheme");
            Expect.equal(LinkFinder.pathOn("http://example.com", "http://example.com:8080/a"), null, "other port");
        });

        Harness.test("bad input gives no links", () -> {
            Expect.isTrue(LinkFinder.links(null, "<a href='/x'>").isEmpty(), "no base");
            Expect.isTrue(LinkFinder.links("http://example.com/", null).isEmpty(), "no body");
            Expect.isTrue(LinkFinder.links("http://example.com/", "<a href='http://[bad'>").isEmpty(), "unparseable");
        });
    }
}