Skip to content

Commit

Permalink
client: add more stats to the spider
Browse files Browse the repository at this point in the history
Add more stats and correct the prefix for the existing ones.

Signed-off-by: thc202 <thc202@gmail.com>
  • Loading branch information
thc202 committed Jan 10, 2025
1 parent 54845ad commit 17cb5f7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import org.zaproxy.zap.model.ScanListenner2;
import org.zaproxy.zap.network.HttpResponseBody;
import org.zaproxy.zap.users.User;
import org.zaproxy.zap.utils.Stats;
import org.zaproxy.zap.utils.ThreadUtils;

public class ClientSpider implements EventConsumer, GenericScanner2 {
Expand Down Expand Up @@ -367,23 +368,27 @@ public void eventReceived(Event event) {
this.lastEventReceivedtime = System.currentTimeMillis();
if (maxTime > 0 && this.lastEventReceivedtime > maxTime) {
LOGGER.debug("Exceeded max time, stopping");
Stats.incCounter("stats.client.spider.event.max.time");
this.stopScan();
return;
}

Map<String, String> parameters = event.getParameters();
String url = parameters.get(ClientMap.URL_KEY);
if (!isUrlInScope(url)) {
Stats.incCounter("stats.client.spider.event.scope.out");
return;
}

Stats.incCounter("stats.client.spider.event.scope.in");
addUriToAddedNodesModel(url);

if (options.getMaxDepth() > 0) {
int depth = Integer.parseInt(parameters.get(ClientMap.DEPTH_KEY));
if (depth > options.getMaxDepth()) {
LOGGER.debug(
"Ignoring URL - too deep {} > {} : {}", depth, options.getMaxDepth(), url);
Stats.incCounter("stats.client.spider.event.max.depth");
return;
}
}
Expand All @@ -395,12 +400,15 @@ public void eventReceived(Event event) {
siblings,
options.getMaxChildren(),
url);
Stats.incCounter("stats.client.spider.event.max.children");
return;
}
}

if (ClientMap.MAP_COMPONENT_ADDED_EVENT.equals(event.getEventType())) {
Stats.incCounter("stats.client.spider.event.component");
if (ClickElement.isSupported(this::isUrlInScope, parameters)) {
Stats.incCounter("stats.client.spider.event.component.click");
addTask(
url,
openAction(
Expand All @@ -409,6 +417,7 @@ url, new ClickElement(valueProvider, createUri(url), parameters)),
Constant.messages.getString("client.spider.panel.table.action.click"),
paramsToString(parameters));
} else if (SubmitForm.isSupported(parameters)) {
Stats.incCounter("stats.client.spider.event.component.form");
addTask(
url,
openAction(url, new SubmitForm(valueProvider, createUri(url), parameters)),
Expand All @@ -417,6 +426,7 @@ url, new ClickElement(valueProvider, createUri(url), parameters)),
paramsToString(parameters));
}
} else {
Stats.incCounter("stats.client.spider.event.url");
addOpenUrlTask(url, options.getPageLoadTimeInSecs());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openqa.selenium.WebDriver;
import org.parosproxy.paros.Constant;
import org.zaproxy.addon.client.spider.ClientSpider.WebDriverProcess;
import org.zaproxy.zap.utils.Stats;

public class ClientSpiderTask implements Runnable {

Expand Down Expand Up @@ -119,7 +120,9 @@ private void runImpl() {
ok = true;
this.status = Status.FINISHED;
this.clientSpider.taskStateChange(this);
Stats.incCounter("stats.client.spider.task.finished");
} catch (Exception e) {
Stats.incCounter("stats.client.spider.task.failed");
LOGGER.warn("Task {} failed {}", id, e.getMessage(), e);
this.status = Status.FAILED;
this.error = e.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ClickElement extends BaseElementAction {

private static final Logger LOGGER = LogManager.getLogger(ClickElement.class);

private static final String STATS_PREFIX = "client.spider.click";
private static final String STATS_PREFIX = "stats.client.spider.click";

private final Map<String, String> elementData;
private final String tagName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class OpenUrl implements SpiderAction {

private static final String STATS_PREFIX = "client.spider.url";
private static final String STATS_PREFIX = "stats.client.spider.url";

private final String url;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class SubmitForm extends BaseElementAction {

private static final Logger LOGGER = LogManager.getLogger(SubmitForm.class);

private static final String STATS_PREFIX = "client.spider.form";
private static final String STATS_PREFIX = "stats.client.spider.form";

private final String tagName;
private final int formIndex;
Expand Down

0 comments on commit 17cb5f7

Please sign in to comment.