Skip to content

Commit c37f1a7

Browse files
authored
Merge pull request #6780 from thc202/automation/ascan-policy-validation
automation: reinstate scan policy validation
2 parents e1c0bda + 92edc82 commit c37f1a7

File tree

3 files changed

+55
-28
lines changed

3 files changed

+55
-28
lines changed

addOns/automation/CHANGELOG.md

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

66
## Unreleased
77
### Changed
8+
- Reinstate the validation of the Scan Policy in the `activeScan` job.
89
- Adjust the text for the plan load warning/error dialog text to be clear which output panel it's referring to.
910

1011
## [0.53.0] - 2025-09-18

addOns/automation/src/main/java/org/zaproxy/addon/automation/jobs/ActiveScanJob.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ public void verifyParameters(AutomationProgress progress) {
115115
break;
116116
}
117117
}
118+
119+
if (!StringUtils.isEmpty(getParameters().getPolicy())) {
120+
try {
121+
getExtAScan().getPolicyManager().getPolicy(getParameters().getPolicy());
122+
} catch (ConfigurationException e) {
123+
progress.error(
124+
Constant.messages.getString(
125+
"automation.error.ascan.policy.name",
126+
this.getName(),
127+
getParameters().getPolicy()));
128+
}
129+
}
130+
118131
policyDefinition.parsePolicyDefinition(
119132
jobData.get("policyDefinition"), this.getName(), progress);
120133
this.verifyUser(this.getParameters().getUser(), progress);

addOns/automation/src/test/java/org/zaproxy/addon/automation/jobs/ActiveScanJobUnitTest.java

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.zaproxy.addon.automation.jobs;
2121

2222
import static org.hamcrest.MatcherAssert.assertThat;
23+
import static org.hamcrest.Matchers.contains;
2324
import static org.hamcrest.Matchers.equalTo;
2425
import static org.hamcrest.Matchers.is;
2526
import static org.hamcrest.Matchers.notNullValue;
@@ -43,6 +44,7 @@
4344
import java.util.Locale;
4445
import java.util.Map;
4546
import java.util.Objects;
47+
import org.apache.commons.configuration.ConfigurationException;
4648
import org.junit.jupiter.api.AfterAll;
4749
import org.junit.jupiter.api.BeforeAll;
4850
import org.junit.jupiter.api.BeforeEach;
@@ -74,6 +76,7 @@
7476
import org.zaproxy.addon.automation.ContextWrapper;
7577
import org.zaproxy.zap.extension.ascan.ActiveScan;
7678
import org.zaproxy.zap.extension.ascan.ExtensionActiveScan;
79+
import org.zaproxy.zap.extension.ascan.PolicyManager;
7780
import org.zaproxy.zap.extension.ascan.ScanPolicy;
7881
import org.zaproxy.zap.model.Context;
7982
import org.zaproxy.zap.model.Target;
@@ -83,6 +86,7 @@
8386
class ActiveScanJobUnitTest {
8487

8588
private static MockedStatic<CommandLine> mockedCmdLine;
89+
private PolicyManager policyManager;
8690
private ExtensionActiveScan extAScan;
8791
private static AbstractPlugin plugin;
8892

@@ -120,6 +124,9 @@ void setUp() throws Exception {
120124
extAScan = mock(ExtensionActiveScan.class, withSettings().strictness(Strictness.LENIENT));
121125
given(extensionLoader.getExtension(ExtensionActiveScan.class)).willReturn(extAScan);
122126

127+
policyManager = mock();
128+
given(extAScan.getPolicyManager()).willReturn(policyManager);
129+
123130
Control.initSingletonForTesting(Model.getSingleton(), extensionLoader);
124131
Model.getSingleton().getOptionsParam().load(new ZapXmlConfiguration());
125132
}
@@ -152,36 +159,14 @@ void shouldReturnCustomConfigParams() {
152159
assertThat(params.get("context"), is(equalTo("")));
153160
}
154161

155-
@Test
156-
void shouldApplyCustomConfigParams() {
157-
// Given
158-
String yamlStr =
159-
"parameters:\n"
160-
+ " maxScanDurationInMins: 12\n"
161-
+ " maxAlertsPerRule: 5\n"
162-
+ " policy: testPolicy";
163-
AutomationProgress progress = new AutomationProgress();
164-
Yaml yaml = new Yaml();
165-
Object data = yaml.load(yamlStr);
166-
167-
ActiveScanJob job = new ActiveScanJob();
168-
job.setJobData(((LinkedHashMap<?, ?>) data));
169-
170-
// When
171-
job.verifyParameters(progress);
172-
173-
// Then
174-
assertThat(job.getParameters().getMaxScanDurationInMins(), is(equalTo(12)));
175-
assertThat(job.getParameters().getMaxAlertsPerRule(), is(equalTo(5)));
176-
assertThat(job.getParameters().getPolicy(), is(equalTo("testPolicy")));
177-
assertThat(progress.hasErrors(), is(equalTo(false)));
178-
assertThat(progress.hasWarnings(), is(equalTo(false)));
179-
}
180-
181162
@Test
182163
void shouldFailWithUnknownConfigParam() {
183164
// Given
184-
String yamlStr = "parameters:\n" + " blah: 12\n" + " policy: testPolicy";
165+
String yamlStr =
166+
"""
167+
parameters:
168+
blah: 12
169+
""";
185170
AutomationProgress progress = new AutomationProgress();
186171
Yaml yaml = new Yaml();
187172
Object data = yaml.load(yamlStr);
@@ -795,8 +780,10 @@ void shouldWarnOnInvalidIntThreshold() throws MalformedURLException {
795780
}
796781

797782
@Test
798-
void shouldVerifyParameters() {
783+
void shouldVerifyParameters() throws Exception {
799784
// Given
785+
given(policyManager.getPolicy("policy1")).willReturn(mock(ScanPolicy.class));
786+
800787
AutomationEnvironment env = mock(AutomationEnvironment.class);
801788
given(env.getAllUserNames()).willReturn(List.of("user0", "user1"));
802789
ActiveScanJob job = new ActiveScanJob();
@@ -844,4 +831,30 @@ void shouldVerifyParameters() {
844831
assertThat(job.getParameters().getThreadPerHost(), is(equalTo(2)));
845832
assertThat(job.getParameters().getMaxAlertsPerRule(), is(equalTo(5)));
846833
}
834+
835+
@Test
836+
void shouldErrorOnUnknownPolicy() throws Exception {
837+
// Given
838+
given(policyManager.getPolicy("missingPolicy")).willThrow(ConfigurationException.class);
839+
840+
String yamlStr =
841+
"""
842+
parameters:
843+
policy: missingPolicy
844+
""";
845+
AutomationProgress progress = new AutomationProgress();
846+
Yaml yaml = new Yaml();
847+
Object data = yaml.load(yamlStr);
848+
849+
ActiveScanJob job = new ActiveScanJob();
850+
job.setJobData(((LinkedHashMap<?, ?>) data));
851+
852+
// When
853+
job.verifyParameters(progress);
854+
855+
// Then
856+
assertThat(progress.hasErrors(), is(equalTo(true)));
857+
assertThat(progress.hasWarnings(), is(equalTo(false)));
858+
assertThat(progress.getErrors(), contains("!automation.error.ascan.policy.name!"));
859+
}
847860
}

0 commit comments

Comments
 (0)