Skip to content

Commit

Permalink
only notify as foundURI when a seed is added neither from addFileSeed…
Browse files Browse the repository at this point in the history
…() nor addRootFileSeed()

Signed-off-by: Jeremy Choi <[email protected]>
  • Loading branch information
jeremychoi committed Feb 27, 2024
1 parent a24aa33 commit 6765ec2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
20 changes: 15 additions & 5 deletions addOns/spider/src/main/java/org/zaproxy/addon/spider/Spider.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public void addSeed(URI uri, String httpVersion) {
}
// Add the seed to the list -- it will be added to the task list only when the spider is
// started
this.seedList.add(new Seed(uri, httpVersion));
this.seedList.add(new Seed(uri, httpVersion, false));
// Add the appropriate 'robots.txt' as a seed
if (getSpiderParam().isParseRobotsTxt()) {
addRootFileSeed(uri, "robots.txt", httpVersion);
Expand Down Expand Up @@ -275,7 +275,7 @@ private void addRootFileSeed(URI baseUri, String fileName, String httpVersion) {
baseUri.getPort(),
"/" + fileName);
try {
this.seedList.add(new Seed(new URI(seed, true), httpVersion));
this.seedList.add(new Seed(new URI(seed, true), httpVersion, true));
} catch (Exception e) {
LOGGER.warn("Error while creating [{}] seed: {}", fileName, seed, e);
}
Expand Down Expand Up @@ -343,7 +343,7 @@ private void addFileSeed(URI baseUri, String fileName, Pattern condition, String
baseUri.getPort(),
pathminusfilename + fileName);
try {
this.seedList.add(new Seed(new URI(uri, true), httpVersion));
this.seedList.add(new Seed(new URI(uri, true), httpVersion, true));
} catch (Exception e) {
LOGGER.warn(
"Error while creating a seed URI for file [{}] from [{}] using [{}]:",
Expand Down Expand Up @@ -528,7 +528,11 @@ public void start() {
// Add the seeds
for (Seed seed : seedList) {
LOGGER.debug("Adding seed for spider: {}", seed);
controller.addSeed(seed.getUri(), HttpRequestHeader.GET, seed.getHttpVersion());
controller.addSeed(
seed.getUri(),
HttpRequestHeader.GET,
seed.getHttpVersion(),
seed.getFromFileSeed());
}
// Mark the process as completely initialized
initialized = true;
Expand Down Expand Up @@ -858,10 +862,16 @@ public Thread newThread(Runnable r) {
private static class Seed {
private final URI uri;
private final String httpVersion;
private final Boolean fromFileSeed;

Seed(URI uri, String httpVersion) {
public Boolean getFromFileSeed() {
return fromFileSeed;
}

Seed(URI uri, String httpVersion, Boolean fromFileSeed) {
this.uri = uri;
this.httpVersion = httpVersion;
this.fromFileSeed = fromFileSeed;
}

URI getUri() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private void prepareDefaultParsers() {
* @param method the http method used for fetching the resource
* @param httpVersion the HTTP version for fetching the resource.
*/
protected void addSeed(URI uri, String method, String httpVersion) {
protected void addSeed(URI uri, String method, String httpVersion, Boolean fromFileSeed) {
SpiderResourceFound resourceFound =
SpiderResourceFound.builder()
.setUri(uri.toString())
Expand All @@ -195,6 +195,12 @@ protected void addSeed(URI uri, String method, String httpVersion) {
// Create and submit the new task
SpiderTask task = new SpiderTask(spider, resourceFound, uri);
spider.submitTask(task);

// Add the uri to the found list

if (!fromFileSeed) {
spider.notifyListenersFoundURI(uri.toString(), method, FetchStatus.SEED);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,23 @@ void shouldCreateTaskWithHttpVersionFromAddedSeed(String httpVersion) throws Exc
// Given
URI uri = new URI("http://127.0.0.1", true);
// When
spiderController.addSeed(uri, HttpRequestHeader.GET, httpVersion);
spiderController.addSeed(uri, HttpRequestHeader.GET, httpVersion, false);
// Then
HttpMessage msg = messageWrittenToSession();
assertThat(msg.getRequestHeader().getVersion(), is(equalTo(httpVersion)));

verify(spider, times(1))
.notifyListenersFoundURI(
"http://127.0.0.1", HttpRequestHeader.GET, FetchStatus.SEED);
}

@Test
void shouldNotNotifySeedAsFoundUri() throws Exception {
String testURI = "http://127.0.0.1";
void shouldNotNotifyFileSeedAsFoundUri() throws Exception {
String testURI = "http://127.0.0.1/test.xml";
// Given
URI seed = new URI(testURI, true);
// When
spiderController.addSeed(seed, HttpRequestHeader.GET, "HTTP/2");
spiderController.addSeed(seed, HttpRequestHeader.GET, "HTTP/2", true);
// Then
verify(spider, times(0))
.notifyListenersFoundURI(testURI, HttpRequestHeader.GET, FetchStatus.SEED);
Expand Down

0 comments on commit 6765ec2

Please sign in to comment.