Skip to content

Commit

Permalink
Merge pull request #5963 from thc202/pscan/mngr-api
Browse files Browse the repository at this point in the history
pscan: use scan rule manager in the API
  • Loading branch information
kingthorin authored Nov 29, 2024
2 parents 0f34317 + 185a196 commit 05af5da
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public void hook(ExtensionHook extensionHook) {

if (org.zaproxy.zap.extension.pscan.PassiveScanAPI.class.getAnnotation(Deprecated.class)
!= null) {
extensionHook.addApiImplementor(new PassiveScanApi(getExtPscan()));
extensionHook.addApiImplementor(new PassiveScanApi(getExtPscan(), scanRuleManager));
}

if (loadScanRules) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.logging.log4j.Logger;
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.core.scanner.Plugin;
import org.zaproxy.addon.pscan.internal.ScanRuleManager;
import org.zaproxy.zap.extension.api.ApiAction;
import org.zaproxy.zap.extension.api.ApiException;
import org.zaproxy.zap.extension.api.ApiImplementor;
Expand Down Expand Up @@ -76,14 +77,16 @@ public class PassiveScanApi extends ApiImplementor {
private static final String PARAM_MAX_ALERTS = "maxAlerts";

private ExtensionPassiveScan extension;
private final ScanRuleManager scanRuleManager;
private Method setPassiveScanEnabledMethod;

public PassiveScanApi() {
this(null);
this(null, null);
}

public PassiveScanApi(ExtensionPassiveScan extension) {
public PassiveScanApi(ExtensionPassiveScan extension, ScanRuleManager scanRuleManager) {
this.extension = extension;
this.scanRuleManager = scanRuleManager;

this.addApiAction(new ApiAction(ACTION_SET_ENABLED, new String[] {PARAM_ENABLED}));
this.addApiAction(
Expand Down Expand Up @@ -226,7 +229,14 @@ private void setPluginPassiveScannersEnabled(JSONObject params, boolean enabled)
* @return {@code true} if the scanner exist, {@code false} otherwise.
*/
private boolean hasPluginPassiveScanner(int pluginId) {
return extension.getPluginPassiveScanner(pluginId) != null;
return getScanRule(pluginId) != null;
}

private PluginPassiveScanner getScanRule(int pluginId) {
if (scanRuleManager != null) {
return (PluginPassiveScanner) scanRuleManager.getScanRule(pluginId);
}
return extension.getPluginPassiveScanner(pluginId);
}

/**
Expand All @@ -235,12 +245,19 @@ private boolean hasPluginPassiveScanner(int pluginId) {
* @param enabled {@code true} if the scanners should be enabled, {@code false} otherwise
*/
private void setAllPluginPassiveScannersEnabled(boolean enabled) {
for (PluginPassiveScanner scanner : extension.getPluginPassiveScanners()) {
for (PluginPassiveScanner scanner : getPluginScanRules()) {
scanner.setEnabled(enabled);
scanner.save();
}
}

private List<PluginPassiveScanner> getPluginScanRules() {
if (scanRuleManager != null) {
return scanRuleManager.getPluginScanRules();
}
return extension.getPluginPassiveScanners();
}

/**
* Sets whether or not the plug-in passive scanner with the given {@code pluginId} is {@code
* enabled}.
Expand All @@ -249,7 +266,7 @@ private void setAllPluginPassiveScannersEnabled(boolean enabled) {
* @param enabled {@code true} if the scanner should be enabled, {@code false} otherwise
*/
private void setPluginPassiveScannerEnabled(int pluginId, boolean enabled) {
PluginPassiveScanner scanner = extension.getPluginPassiveScanner(pluginId);
PluginPassiveScanner scanner = getScanRule(pluginId);
if (scanner != null) {
scanner.setEnabled(enabled);
scanner.save();
Expand Down Expand Up @@ -280,7 +297,7 @@ private static Plugin.AlertThreshold getAlertThresholdFromParamAlertThreshold(JS
*/
private void setPluginPassiveScannerAlertThreshold(
int pluginId, Plugin.AlertThreshold alertThreshold) {
PluginPassiveScanner scanner = extension.getPluginPassiveScanner(pluginId);
PluginPassiveScanner scanner = getScanRule(pluginId);
if (scanner != null) {
scanner.setAlertThreshold(alertThreshold);
scanner.setEnabled(!Plugin.AlertThreshold.OFF.equals(alertThreshold));
Expand All @@ -302,7 +319,7 @@ public ApiResponse handleApiView(String name, JSONObject params) throws ApiExcep
result = new ApiResponseElement(name, String.valueOf(extension.getRecordsToScan()));
break;
case VIEW_SCANNERS:
List<PluginPassiveScanner> scanners = extension.getPluginPassiveScanners();
List<PluginPassiveScanner> scanners = getPluginScanRules();

ApiResponseList resultList = new ApiResponseList(name);
for (PluginPassiveScanner scanner : scanners) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.junit.jupiter.params.provider.ValueSource;
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.network.HttpMessage;
import org.zaproxy.addon.pscan.internal.ScanRuleManager;
import org.zaproxy.zap.extension.api.API;
import org.zaproxy.zap.extension.api.API.RequestType;
import org.zaproxy.zap.extension.api.ApiElement;
Expand All @@ -53,12 +54,13 @@
class PassiveScanApiUnitTest extends TestUtils {

private PassiveScanApi pscanApi;
private ScanRuleManager scanRuleManager;
private ExtensionPassiveScan extension;

@BeforeEach
void setUp() {
mockMessages(new ExtensionPassiveScan2());
pscanApi = new PassiveScanApi(extension);
pscanApi = new PassiveScanApi(extension, scanRuleManager);
}

@AfterAll
Expand All @@ -77,7 +79,7 @@ void shouldHavePrefix() throws Exception {
@Test
void shouldAddApiElements() {
// Given / When
pscanApi = new PassiveScanApi(extension);
pscanApi = new PassiveScanApi(extension, scanRuleManager);
// Then
assertThat(pscanApi.getApiActions(), hasSize(11));
assertThat(pscanApi.getApiViews(), hasSize(6));
Expand Down Expand Up @@ -140,7 +142,7 @@ void shouldThrowApiExceptionForUnknownView(String name) throws Exception {

@Test
void shouldHaveDescriptionsForAllApiElements() {
pscanApi = new PassiveScanApi(extension);
pscanApi = new PassiveScanApi(extension, scanRuleManager);
List<String> issues = new ArrayList<>();
checkKey(pscanApi.getDescriptionKey(), issues);
checkApiElements(pscanApi, pscanApi.getApiActions(), API.RequestType.action, issues);
Expand Down

0 comments on commit 05af5da

Please sign in to comment.