use of org.zaproxy.zap.extension.script.ScriptWrapper in project zaproxy by zaproxy.
the class ScriptsActiveScannerUnitTest method shouldStopScanningParamsWithActiveScriptWhenScanStopped.
@Test
@SuppressWarnings("unchecked")
void shouldStopScanningParamsWithActiveScriptWhenScanStopped() throws Exception {
// Given
ActiveScript script1 = mock(ActiveScript.class);
doAnswer(stopScan()).when(script1).scan(any(), any(), any(), any());
ScriptWrapper scriptWrapper1 = createScriptWrapper(script1, ActiveScript.class);
ActiveScript script2 = mock(ActiveScript.class);
ScriptWrapper scriptWrapper2 = createScriptWrapper(script2, ActiveScript.class);
given(extensionScript.getScripts(SCRIPT_TYPE)).willReturn(asList(scriptWrapper1, scriptWrapper2));
ScriptsCache<ActiveScript> scriptsCache = createScriptsCache(createCachedScript(script1, scriptWrapper1), createCachedScript(script2, scriptWrapper2));
given(extensionScript.<ActiveScript>createScriptsCache(any())).willReturn(scriptsCache);
given(parent.getScannerParam()).willReturn(mock(ScannerParam.class));
String name1 = "Name1";
String value1 = "Value1";
NameValuePair param1 = param(name1, value1);
String name2 = "Name2";
String value2 = "Value2";
NameValuePair param2 = param(name2, value2);
Variant variant = mock(Variant.class);
given(variant.getParamList()).willReturn(asList(param1, param2));
VariantFactory variantFactory = mock(VariantFactory.class);
given(variantFactory.createVariants(any(), any())).willReturn(asList(variant));
given(model.getVariantFactory()).willReturn(variantFactory);
ScriptsActiveScanner scriptsActiveScanner = new ScriptsActiveScanner();
scriptsActiveScanner.init(message, parent);
// When
scriptsActiveScanner.scan();
// Then
verify(scriptsCache, times(1)).refresh();
verify(scriptsCache, times(1)).getCachedScripts();
verify(script1, times(1)).scan(scriptsActiveScanner, message, name1, value1);
verify(script1, times(0)).scan(scriptsActiveScanner, message, name2, value2);
verify(script2, times(0)).scan(any(), any(), any(), any());
}
use of org.zaproxy.zap.extension.script.ScriptWrapper in project zaproxy by zaproxy.
the class ScriptsActiveScanner method scan.
@Override
public void scan() {
List<ScriptWrapper> scripts = this.getActiveScripts();
for (Iterator<ScriptWrapper> it = scripts.iterator(); it.hasNext() && !isStop(); ) {
ScriptWrapper script = it.next();
try {
if (script.isEnabled()) {
ActiveScript2 s = extension.getInterface(script, ActiveScript2.class);
if (s != null) {
HttpMessage msg = this.getNewMsg();
logger.debug("Calling script " + script.getName() + " scanNode for " + msg.getRequestHeader().getURI());
s.scanNode(this, msg);
} else {
scriptsNoInterface.add(script);
}
}
} catch (Exception e) {
extension.handleScriptException(script, e);
}
}
if (!isStop()) {
InterfaceProvider<ActiveScript> interfaceProvider = (scriptWrapper, targetInterface) -> {
ActiveScript s = extension.getInterface(scriptWrapper, targetInterface);
if (s != null) {
return s;
}
if (scriptsNoInterface.contains(scriptWrapper)) {
extension.handleFailedScriptInterface(scriptWrapper, Constant.messages.getString("ascan.scripts.interface.active.error", scriptWrapper.getName()));
}
return null;
};
cachedScripts = getExtension().createScriptsCache(Configuration.<ActiveScript>builder().setScriptType(ExtensionActiveScan.SCRIPT_TYPE_ACTIVE).setTargetInterface(ActiveScript.class).setInterfaceProvider(interfaceProvider).build());
super.scan();
}
scriptsNoInterface.clear();
}
use of org.zaproxy.zap.extension.script.ScriptWrapper in project zaproxy by zaproxy.
the class ScriptsActiveScanner method scan.
@Override
public void scan(HttpMessage msg, String param, String value) {
cachedScripts.refresh();
for (CachedScript<ActiveScript> cachedScript : cachedScripts.getCachedScripts()) {
if (isStop()) {
return;
}
ScriptWrapper script = cachedScript.getScriptWrapper();
try {
logger.debug("Calling script " + script.getName() + " scan for " + msg.getRequestHeader().getURI() + "param=" + param + " value=" + value);
cachedScript.getScript().scan(this, msg, param, value);
} catch (Exception e) {
extension.handleScriptException(script, e);
}
}
}
use of org.zaproxy.zap.extension.script.ScriptWrapper in project zaproxy by zaproxy.
the class ScriptsActiveScannerUnitTest method shouldHandleExceptionsThrownByActiveScript.
@Test
@SuppressWarnings("unchecked")
void shouldHandleExceptionsThrownByActiveScript() throws Exception {
// Given
ActiveScript script1 = mock(ActiveScript.class);
ScriptWrapper scriptWrapper1 = createScriptWrapper(script1, ActiveScript.class);
ActiveScript script2 = mock(ActiveScript.class);
ScriptWrapper scriptWrapper2 = createScriptWrapper(script2, ActiveScript.class);
given(extensionScript.getScripts(SCRIPT_TYPE)).willReturn(asList(scriptWrapper1, scriptWrapper2));
ScriptsCache<ActiveScript> scriptsCache = createScriptsCache(createCachedScript(script1, scriptWrapper1), createCachedScript(script2, scriptWrapper2));
given(extensionScript.<ActiveScript>createScriptsCache(any())).willReturn(scriptsCache);
given(parent.getScannerParam()).willReturn(mock(ScannerParam.class));
String name1 = "Name1";
String value1 = "Value1";
NameValuePair param1 = param(name1, value1);
ScriptException exception = mock(ScriptException.class);
doThrow(exception).when(script1).scan(any(), any(), eq(name1), eq(value1));
String name2 = "Name2";
String value2 = "Value2";
NameValuePair param2 = param(name2, value2);
Variant variant = mock(Variant.class);
given(variant.getParamList()).willReturn(asList(param1, param2));
VariantFactory variantFactory = mock(VariantFactory.class);
given(variantFactory.createVariants(any(), any())).willReturn(asList(variant));
given(model.getVariantFactory()).willReturn(variantFactory);
ScriptsActiveScanner scriptsActiveScanner = new ScriptsActiveScanner();
scriptsActiveScanner.init(message, parent);
// When
scriptsActiveScanner.scan();
// Then
verify(scriptsCache, times(2)).refresh();
verify(scriptsCache, times(2)).getCachedScripts();
verify(script1, times(1)).scan(scriptsActiveScanner, message, name1, value1);
verify(extensionScript, times(1)).handleScriptException(scriptWrapper1, exception);
verify(script2, times(1)).scan(scriptsActiveScanner, message, name1, value1);
verify(script2, times(1)).scan(scriptsActiveScanner, message, name2, value2);
}
use of org.zaproxy.zap.extension.script.ScriptWrapper in project zaproxy by zaproxy.
the class ScriptsActiveScannerUnitTest method createScriptWrapper.
private <T> ScriptWrapper createScriptWrapper(T script, Class<T> scriptClass) throws Exception {
ScriptWrapper scriptWrapper = mock(ScriptWrapper.class);
given(scriptWrapper.isEnabled()).willReturn(true);
given(extensionScript.getInterface(scriptWrapper, scriptClass)).willReturn(script);
return scriptWrapper;
}
Aggregations