Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion zap/src/main/java/org/zaproxy/zap/extension/alert/AlertNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,31 @@ public class AlertNode extends DefaultMutableTreeNode {

private final Comparator<TreeNode> childComparator;
private String nodeName = null;
private String alertRef;
private int risk = -1;
private Alert alert;
private boolean systemic;

public AlertNode(int risk, String nodeName) {
this(risk, nodeName, null);
this(risk, nodeName, null, null);
}

public AlertNode(int risk, String nodeName, Comparator<AlertNode> childComparator) {
this(risk, nodeName, null, childComparator);
}

AlertNode(int risk, String nodeName, String alertRef, Comparator<AlertNode> childComparator) {
super();
this.nodeName = nodeName;
this.alertRef = alertRef;
this.setRisk(risk);
this.childComparator = new AlertNodeComparatorWrapper(childComparator);
}

String getAlertRef() {
return alertRef;
}

/** Sets an alert for this node. The {@link #setAlert(Alert)} method should be used instead. */
@Override
public void setUserObject(Object userObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ public AlertNode getAlertNode(Alert alert) {
risk = -1;
}

AlertNode needle = new AlertNode(risk, alert.getName(), GROUP_ALERT_CHILD_COMPARATOR);
AlertNode needle =
new AlertNode(
risk, alert.getName(), alert.getAlertRef(), GROUP_ALERT_CHILD_COMPARATOR);
needle.setAlert(alert);
int idx = parent.findIndex(needle);
if (idx < 0) {
Expand Down Expand Up @@ -194,7 +196,7 @@ private AlertNode findAndAddGroup(AlertNode parent, String nodeName, Alert alert
risk = -1;
}

AlertNode node = new AlertNode(risk, nodeName, ALERT_CHILD_COMPARATOR);
AlertNode node = new AlertNode(risk, nodeName, alert.getAlertRef(), ALERT_CHILD_COMPARATOR);
int idx = parent.findIndex(node);
if (idx < 0) {
idx = -(idx + 1);
Expand All @@ -214,7 +216,8 @@ private AlertNode addLeaf(AlertNode parent, String nodeName, Alert alert) {
risk = -1;
}

AlertNode needle = new AlertNode(risk, nodeName, ALERT_CHILD_COMPARATOR);
AlertNode needle =
new AlertNode(risk, nodeName, alert.getAlertRef(), ALERT_CHILD_COMPARATOR);
needle.setAlert(alert);
int idx = parent.findIndex(needle);
if (idx < 0) {
Expand Down Expand Up @@ -268,7 +271,11 @@ public int compare(AlertNode alertNode, AlertNode anotherAlertNode) {
return -1;
}

return alertNode.getNodeName().compareTo(anotherAlertNode.getNodeName());
int res = alertNode.getNodeName().compareTo(anotherAlertNode.getNodeName());
if (res == 0) {
return alertNode.getAlertRef().compareTo(anotherAlertNode.getAlertRef());
}
return res;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,22 @@ void shouldAddUniqueAlerts() {
"https://www.example.com",
Alert.RISK_MEDIUM,
Alert.CONFIDENCE_MEDIUM);
Alert a4 =
newAlert(
1,
"1-2",
3,
"Alert A",
"https://www.example.com",
"https://www.example.com",
Alert.RISK_MEDIUM,
Alert.CONFIDENCE_MEDIUM);

// When
atModel.addPath(a1);
atModel.addPath(a3);
atModel.addPath(a2);
atModel.addPath(a4);

// Then

Expand All @@ -89,12 +100,15 @@ void shouldAddUniqueAlerts() {
- Medium: Alert A
- GET:https://www.example.com
- GET:https://www.example.net
- Medium: Alert A
- GET:https://www.example.com
""",
TextAlertTree.toString(atModel));

assertEquals(a1, atModel.getRoot().getChildAt(0).getChildAt(0).getAlert());
assertEquals(a3, atModel.getRoot().getChildAt(1).getChildAt(0).getAlert());
assertEquals(a2, atModel.getRoot().getChildAt(1).getChildAt(1).getAlert());
assertEquals(a4, atModel.getRoot().getChildAt(2).getChildAt(0).getAlert());
}

@Test
Expand Down Expand Up @@ -177,11 +191,22 @@ void shouldFindDuplicateAlerts() {
"https://www.example.com?a=3",
Alert.RISK_MEDIUM,
Alert.CONFIDENCE_MEDIUM);
Alert a4 =
newAlert(
1,
"1-2",
3,
"Alert A",
"https://www.example.com(a)",
"https://www.example.com?a=4",
Alert.RISK_MEDIUM,
Alert.CONFIDENCE_MEDIUM);

// When
atModel.addPath(a1);
atModel.addPath(a2);
atModel.addPath(a3);
atModel.addPath(a4);

AlertNode an1 = atModel.getAlertNode(a1);
AlertNode an2 = atModel.getAlertNode(a2);
Expand All @@ -191,6 +216,7 @@ void shouldFindDuplicateAlerts() {
assertEquals("GET:https://www.example.com(a)", an1.getNodeName());
assertEquals("GET:https://www.example.com(a)", an2.getNodeName());
assertEquals("GET:https://www.example.com(a)", an3.getNodeName());
assertEquals("GET:https://www.example.com(a)", atModel.getAlertNode(a4).getNodeName());
}

@Test
Expand Down Expand Up @@ -225,16 +251,27 @@ void shouldChangeDuplicateAlerts() {
"https://www.example.com?a=3",
Alert.RISK_MEDIUM,
Alert.CONFIDENCE_MEDIUM);
Alert a4 =
newAlert(
1,
"1-2",
3,
"Alert A",
"https://www.example.com(a)",
"https://www.example.com?a=4",
Alert.RISK_MEDIUM,
Alert.CONFIDENCE_MEDIUM);

// When
atModel.addPath(a1);
atModel.addPath(a2);
atModel.addPath(a3);
atModel.addPath(a4);
a1.setRisk(Alert.RISK_HIGH);
atModel.updatePath(a1);

// Then
assertEquals(1, atModel.getRoot().getChildCount());
assertEquals(2, atModel.getRoot().getChildCount());

// Only child - Medium risk
assertEquals("Alert A", atModel.getRoot().getChildAt(0).getNodeName());
Expand All @@ -245,6 +282,11 @@ void shouldChangeDuplicateAlerts() {
"GET:https://www.example.com(a)",
atModel.getRoot().getChildAt(0).getChildAt(0).getNodeName());
assertEquals(Alert.RISK_HIGH, atModel.getRoot().getChildAt(0).getChildAt(0).getRisk());

assertEquals(
"GET:https://www.example.com(a)",
atModel.getRoot().getChildAt(1).getChildAt(0).getNodeName());
assertEquals(Alert.RISK_MEDIUM, atModel.getRoot().getChildAt(1).getChildAt(0).getRisk());
}

@Test
Expand Down Expand Up @@ -280,28 +322,42 @@ void shouldDeleteUniqueAlert() {
"https://www.example.net",
Alert.RISK_MEDIUM,
Alert.CONFIDENCE_MEDIUM);
Alert a4 =
newAlert(
1,
"1-2",
3,
"Alert A",
"https://www.example.com/a1",
"https://www.example.com/a1",
Alert.RISK_MEDIUM,
Alert.CONFIDENCE_MEDIUM);

// When
atModel.addPath(a1);
atModel.addPath(a2);
atModel.addPath(a3);
atModel.addPath(a4);

atModel.deletePath(a1);

// Then
assertEquals(1, atModel.getRoot().getChildCount());
assertEquals(2, atModel.getRoot().getChildCount());

assertEquals(
"""
- Alerts
- Medium: Alert A
- GET:https://www.example.com/a2
- GET:https://www.example.net
- Medium: Alert A
- GET:https://www.example.com/a1
""",
TextAlertTree.toString(atModel));

assertEquals(a2, atModel.getRoot().getChildAt(0).getChildAt(0).getAlert());
assertEquals(a3, atModel.getRoot().getChildAt(0).getChildAt(1).getAlert());
assertEquals(a4, atModel.getRoot().getChildAt(1).getChildAt(0).getAlert());
}

@Test
Expand Down Expand Up @@ -337,11 +393,22 @@ void shouldChangeUniqueAlert() {
"https://www.example.com/a2",
Alert.RISK_MEDIUM,
Alert.CONFIDENCE_MEDIUM);
Alert a4 =
newAlert(
1,
"1-2",
3,
"Alert A",
"https://www.example.com/a1",
"https://www.example.com/a1",
Alert.RISK_MEDIUM,
Alert.CONFIDENCE_MEDIUM);

// When
atModel.addPath(a1);
atModel.addPath(a2);
atModel.addPath(a3);
atModel.addPath(a4);

a1.setRisk(Alert.RISK_HIGH);
atModel.updatePath(a1);
Expand All @@ -355,12 +422,15 @@ void shouldChangeUniqueAlert() {
- Medium: Alert A
- GET:https://www.example.com/a2
- GET:https://www.example.net
- Medium: Alert A
- GET:https://www.example.com/a1
""",
TextAlertTree.toString(atModel));

assertEquals(a1, atModel.getRoot().getChildAt(0).getChildAt(0).getAlert());
assertEquals(a3, atModel.getRoot().getChildAt(1).getChildAt(0).getAlert());
assertEquals(a2, atModel.getRoot().getChildAt(1).getChildAt(1).getAlert());
assertEquals(a4, atModel.getRoot().getChildAt(2).getChildAt(0).getAlert());
}

@Test
Expand Down Expand Up @@ -393,18 +463,30 @@ void shouldDeleteNodeWhenNoAlertsLeft() {
"https://www.example.com?a=3",
Alert.RISK_MEDIUM,
Alert.CONFIDENCE_MEDIUM);
Alert a4 =
newAlert(
1,
"1-2",
3,
"Alert A",
"https://www.example.com(a)",
"https://www.example.com?a=4",
Alert.RISK_MEDIUM,
Alert.CONFIDENCE_MEDIUM);

// When
atModel.addPath(a1);
atModel.addPath(a2);
atModel.addPath(a3);
atModel.addPath(a4);

atModel.deletePath(a1);
atModel.deletePath(a3);
atModel.deletePath(a2);

// Then
assertEquals(0, atModel.getRoot().getChildCount());
assertEquals(1, atModel.getRoot().getChildCount());
assertEquals(a4, atModel.getRoot().getChildAt(0).getChildAt(0).getAlert());
}

private static Alert newAlert(
Expand All @@ -415,7 +497,22 @@ private static Alert newAlert(
String uri,
int risk,
int confidence) {
return newAlert(pluginId, null, id, name, nodeName, uri, risk, confidence);
}

private static Alert newAlert(
int pluginId,
String alertRef,
int id,
String name,
String nodeName,
String uri,
int risk,
int confidence) {
Alert alert = new Alert(pluginId, risk, confidence, name);
if (alertRef != null) {
alert.setAlertRef(alertRef);
}
alert.setUri(uri);
alert.setAlertId(id);
alert.setNodeName(nodeName);
Expand Down
Loading