Skip to content

Commit bc61e89

Browse files
authored
Merge pull request #6954 from thc202/automation/delay-validation
automation: delay scan policy validation
2 parents fc88a6d + a3f98e2 commit bc61e89

File tree

3 files changed

+38
-41
lines changed

3 files changed

+38
-41
lines changed

addOns/automation/CHANGELOG.md

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

1414
### Fixed
1515
- Restore default standard output on absent `env` `parameters`.
16+
- Delay Scan Policy validation to runtime phase in the `activeScan` job, the Scan Policy might be created dynamically by other jobs.
1617

1718
## [0.56.0] - 2025-11-07
1819
### Added

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,7 @@ public void verifyParameters(AutomationProgress progress) {
118118
}
119119

120120
if (!StringUtils.isEmpty(getParameters().getPolicy())) {
121-
try {
122-
getExtAScan().getPolicyManager().getPolicy(getParameters().getPolicy());
123-
} catch (ConfigurationException e) {
124-
progress.error(
125-
Constant.messages.getString(
126-
"automation.error.ascan.policy.name",
127-
this.getName(),
128-
getParameters().getPolicy()));
129-
}
121+
// Validate the policy exists when running, it might be created dynamically.
130122

131123
if (!StringUtils.isEmpty(getParameters().getDefaultStrength())) {
132124
JobUtils.parseAttackStrength(
@@ -239,7 +231,12 @@ public void runJob(AutomationEnvironment env, AutomationProgress progress) {
239231
}
240232

241233
} catch (ConfigurationException e) {
242-
// Error already raised above
234+
progress.error(
235+
Constant.messages.getString(
236+
"automation.error.ascan.policy.name",
237+
this.getName(),
238+
getParameters().getPolicy()));
239+
return;
243240
}
244241
} else {
245242
scanPolicy =

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

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public ScannerParam getScannerParam() {
236236
}
237237

238238
@Test
239-
void shouldRunValidJob() throws MalformedURLException {
239+
void shouldRunValidJob() throws Exception {
240240
// Given
241241
Constant.messages = new I18N(Locale.ENGLISH);
242242
Context context = mock(Context.class);
@@ -254,8 +254,12 @@ void shouldRunValidJob() throws MalformedURLException {
254254
AutomationEnvironment env = mock(AutomationEnvironment.class);
255255
given(env.getDefaultContextWrapper()).willReturn(contextWrapper);
256256

257-
// When
257+
given(policyManager.getPolicy("policy1")).willReturn(mock(ScanPolicy.class));
258+
258259
ActiveScanJob job = new ActiveScanJob();
260+
job.getParameters().setPolicy("policy1");
261+
262+
// When
259263
job.runJob(env, progress);
260264

261265
// Then
@@ -288,6 +292,29 @@ void shouldFailIfUnknownContext() throws MalformedURLException {
288292
assertThat(progress.getErrors().get(0), is(equalTo("!automation.error.context.unknown!")));
289293
}
290294

295+
@Test
296+
void shouldFailIfUnknownPolicy() throws Exception {
297+
// Given
298+
given(policyManager.getPolicy("missingPolicy")).willThrow(ConfigurationException.class);
299+
Constant.messages = new I18N(Locale.ENGLISH);
300+
AutomationProgress progress = new AutomationProgress();
301+
AutomationEnvironment env = mock(AutomationEnvironment.class);
302+
303+
ContextWrapper contextWrapper = new ContextWrapper(mock(Context.class), env);
304+
given(env.getDefaultContextWrapper()).willReturn(contextWrapper);
305+
306+
ActiveScanJob job = new ActiveScanJob();
307+
job.getParameters().setPolicy("missingPolicy");
308+
309+
// When
310+
job.runJob(env, progress);
311+
312+
// Then
313+
assertThat(progress.hasWarnings(), is(equalTo(false)));
314+
assertThat(progress.hasErrors(), is(equalTo(true)));
315+
assertThat(progress.getErrors(), contains("!automation.error.ascan.policy.name!"));
316+
}
317+
291318
@Test
292319
void shouldUseSpecifiedContext() throws MalformedURLException {
293320
// Given
@@ -782,10 +809,8 @@ void shouldWarnOnInvalidIntThreshold() throws MalformedURLException {
782809
}
783810

784811
@Test
785-
void shouldVerifyParameters() throws Exception {
812+
void shouldVerifyParameters() {
786813
// Given
787-
given(policyManager.getPolicy("policy1")).willReturn(mock(ScanPolicy.class));
788-
789814
AutomationEnvironment env = mock(AutomationEnvironment.class);
790815
given(env.getAllUserNames()).willReturn(List.of("user0", "user1"));
791816
ActiveScanJob job = new ActiveScanJob();
@@ -833,30 +858,4 @@ void shouldVerifyParameters() throws Exception {
833858
assertThat(job.getParameters().getThreadPerHost(), is(equalTo(2)));
834859
assertThat(job.getParameters().getMaxAlertsPerRule(), is(equalTo(5)));
835860
}
836-
837-
@Test
838-
void shouldErrorOnUnknownPolicy() throws Exception {
839-
// Given
840-
given(policyManager.getPolicy("missingPolicy")).willThrow(ConfigurationException.class);
841-
842-
String yamlStr =
843-
"""
844-
parameters:
845-
policy: missingPolicy
846-
""";
847-
AutomationProgress progress = new AutomationProgress();
848-
Yaml yaml = new Yaml();
849-
Object data = yaml.load(yamlStr);
850-
851-
ActiveScanJob job = new ActiveScanJob();
852-
job.setJobData(((LinkedHashMap<?, ?>) data));
853-
854-
// When
855-
job.verifyParameters(progress);
856-
857-
// Then
858-
assertThat(progress.hasErrors(), is(equalTo(true)));
859-
assertThat(progress.hasWarnings(), is(equalTo(false)));
860-
assertThat(progress.getErrors(), contains("!automation.error.ascan.policy.name!"));
861-
}
862861
}

0 commit comments

Comments
 (0)