Skip to content

Commit 24b9939

Browse files
authored
Merge pull request #6676 from thc202/client/pscan-tree-href
client: use Sites tree for history references
2 parents 4eb1d69 + fa564b1 commit 24b9939

3 files changed

Lines changed: 45 additions & 18 deletions

File tree

addOns/client/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
99

1010
### Changed
1111
- Updated Chrome and Firefox extensions to v0.1.5.
12+
- Reduce warnings when passive scanning.
1213

1314
### Fixed
1415
- Error logs to always include stack trace.

addOns/client/src/main/java/org/zaproxy/addon/client/pscan/ClientPassiveScanHelper.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,22 @@
2222
import java.awt.event.KeyEvent;
2323
import java.nio.charset.StandardCharsets;
2424
import java.util.Base64;
25+
import org.apache.commons.httpclient.URI;
26+
import org.apache.commons.httpclient.URIException;
2527
import org.apache.commons.lang3.StringUtils;
2628
import org.apache.logging.log4j.LogManager;
2729
import org.apache.logging.log4j.Logger;
2830
import org.parosproxy.paros.core.scanner.Alert;
2931
import org.parosproxy.paros.extension.history.ExtensionHistory;
3032
import org.parosproxy.paros.model.HistoryReference;
33+
import org.parosproxy.paros.model.SiteNode;
3134
import org.zaproxy.addon.client.ClientUtils;
3235
import org.zaproxy.zap.extension.alert.ExtensionAlert;
3336
import org.zaproxy.zap.utils.Stats;
3437

3538
public class ClientPassiveScanHelper {
3639

3740
private static final Logger LOGGER = LogManager.getLogger(ClientPassiveScanHelper.class);
38-
private static final int MAX_HREFS_TO_CHECK = 1000;
3941
private ExtensionAlert extAlert;
4042
private ExtensionHistory extHistory;
4143

@@ -46,21 +48,20 @@ public ClientPassiveScanHelper(ExtensionAlert extAlert, ExtensionHistory extHist
4648

4749
public HistoryReference findHistoryRef(String url) {
4850
url = ClientUtils.stripUrlFragment(url);
49-
int lastId = extHistory.getLastHistoryId();
5051

51-
// We don't expect to have to go too far back..
52-
int limit = Math.max(lastId - MAX_HREFS_TO_CHECK, 0);
53-
LOGGER.debug("Searching for history reference for {}", url);
54-
for (int i = lastId; i >= limit; i--) {
55-
HistoryReference hr = extHistory.getHistoryReference(i);
56-
if (hr != null && url.equals(hr.getURI().toString())) {
57-
LOGGER.debug("Found history reference {} for {}", hr.getHistoryId(), url);
52+
try {
53+
SiteNode node =
54+
extHistory.getModel().getSession().getSiteTree().findNode(new URI(url, true));
55+
if (node != null) {
56+
HistoryReference hr = node.getHistoryReference();
5857
Stats.incCounter("stats.client.pscan.href.found");
58+
LOGGER.debug("Found history reference {} for {}", hr.getHistoryId(), url);
5959
return hr;
6060
}
61+
} catch (URIException e) {
62+
LOGGER.warn("Failed to create URI from: {} Cause: {}", url, e.getMessage());
6163
}
62-
// Include the limit in case we change it in the future
63-
Stats.incCounter("stats.client.pscan.href.missing." + MAX_HREFS_TO_CHECK);
64+
Stats.incCounter("stats.client.pscan.href.missing");
6465
return null;
6566
}
6667

addOns/client/src/test/java/org/zaproxy/addon/client/pscan/ClientPassiveScanHelperUnitTest.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static org.hamcrest.Matchers.equalTo;
2424
import static org.hamcrest.Matchers.is;
2525
import static org.hamcrest.Matchers.nullValue;
26-
import static org.mockito.ArgumentMatchers.anyInt;
2726
import static org.mockito.BDDMockito.given;
2827
import static org.mockito.Mockito.mock;
2928
import static org.mockito.Mockito.withSettings;
@@ -40,6 +39,10 @@
4039
import org.mockito.quality.Strictness;
4140
import org.parosproxy.paros.extension.history.ExtensionHistory;
4241
import org.parosproxy.paros.model.HistoryReference;
42+
import org.parosproxy.paros.model.Model;
43+
import org.parosproxy.paros.model.Session;
44+
import org.parosproxy.paros.model.SiteMap;
45+
import org.parosproxy.paros.model.SiteNode;
4346
import org.zaproxy.addon.client.ExtensionClientIntegration;
4447
import org.zaproxy.zap.extension.alert.ExtensionAlert;
4548
import org.zaproxy.zap.testutils.TestUtils;
@@ -66,16 +69,38 @@ void setup() {
6669
@Test
6770
void shouldFindHistoryRef() throws Exception {
6871
// Given
69-
given(extHistory.getLastHistoryId()).willReturn(3);
72+
Model model = mock();
73+
given(extHistory.getModel()).willReturn(model);
74+
Session session = mock();
75+
given(model.getSession()).willReturn(session);
76+
SiteMap siteTree = mock();
77+
given(session.getSiteTree()).willReturn(siteTree);
78+
SiteNode siteNode = mock();
7079
String url = "http://example.com/";
71-
HistoryReference href1 = mockHistoryReference(url);
72-
HistoryReference href2Deleted = null;
73-
HistoryReference href3 = mockHistoryReference("http://not.example.com/");
74-
given(extHistory.getHistoryReference(anyInt())).willReturn(href3, href2Deleted, href1);
80+
given(siteTree.findNode(new URI(url, true))).willReturn(siteNode);
81+
HistoryReference href = mockHistoryReference(url);
82+
given(siteNode.getHistoryReference()).willReturn(href);
7583
// When
7684
HistoryReference foundHref = helper.findHistoryRef(url);
7785
// Then
78-
assertThat(foundHref, is(equalTo(href1)));
86+
assertThat(foundHref, is(equalTo(href)));
87+
}
88+
89+
@Test
90+
void shouldNotFindHistoryRefIfNotPresent() throws Exception {
91+
// Given
92+
Model model = mock();
93+
given(extHistory.getModel()).willReturn(model);
94+
Session session = mock();
95+
given(model.getSession()).willReturn(session);
96+
SiteMap siteTree = mock();
97+
given(session.getSiteTree()).willReturn(siteTree);
98+
String url = "http://example.com/";
99+
given(siteTree.findNode(new URI(url, true))).willReturn(null);
100+
// When
101+
HistoryReference foundHref = helper.findHistoryRef(url);
102+
// Then
103+
assertThat(foundHref, is(nullValue()));
79104
}
80105

81106
private static HistoryReference mockHistoryReference(String url) throws URIException {

0 commit comments

Comments
 (0)