Repository: zaproxy/zaproxy
Issue: Proxy script return value misinterpreted in 2.10.0

**Describe the bug**
Hi,
Not sure if it's related to the version, but since the update to 2.10.0, I have this issue.

If I enable a proxy script, I have the error "Secure Connection Failed" in the browser when I try to connect to any url.
- it was working fine before the update (not 100% sure when it start to fail)
- I'm using dynamic SSL certificates, and SSL is working fine if I use proxy without any script enabled
- I tested with Jython and  Zest script, even with basic provided templates (so it's not related to the script himself)
- I tried to regenerate SSL certs, but doesn't help.
- no error in logs

From the browser side, it looks like the SSL is not validated when script is enabled.

Any advice to solve this issue ? 

Thanks

**To Reproduce**
Steps to reproduce the behavior:
1. Setup dynamic SSL certificates and browser proxy to connect through zap
2. Create new proxy script (jython or zest) and choose any default template
3. Enable the script
4. Connect to any url from browser

**Expected behavior**
Connection should be done and the script should be executed.

**Software versions**
 - ZAP: 2.10.0
 - Browser: Tried with Firefox & Chrome
 - OS : Kali linux

**Errors from the zap.log file**
No error in zap log.

**Would you like to help fix this issue?**
sure

Changed files:
zap/src/test/java/org/zaproxy/zap/extension/script/ProxyListenerScriptUnitTest.java
zap/src/main/java/org/zaproxy/zap/extension/script/ProxyListenerScript.java

diff --git a/zap/src/main/java/org/zaproxy/zap/extension/script/ProxyListenerScript.java b/zap/src/main/java/org/zaproxy/zap/extension/script/ProxyListenerScript.java
index 2d5bb8e1c..1a66066ed 100644
--- a/zap/src/main/java/org/zaproxy/zap/extension/script/ProxyListenerScript.java
+++ b/zap/src/main/java/org/zaproxy/zap/extension/script/ProxyListenerScript.java
@@ -65,9 +65,9 @@ public class ProxyListenerScript implements ProxyListener {
         for (CachedScript<ProxyScript> cachedScript : scripts.getCachedScripts()) {
             ProxyScript script = cachedScript.getScript();
             try {
-                boolean dropMessage =
+                boolean forwardMessage =
                         request ? script.proxyRequest(msg) : script.proxyResponse(msg);
-                if (dropMessage) {
+                if (!forwardMessage) {
                     return false;
                 }
             } catch (Exception e) {
diff --git a/zap/src/test/java/org/zaproxy/zap/extension/script/ProxyListenerScriptUnitTest.java b/zap/src/test/java/org/zaproxy/zap/extension/script/ProxyListenerScriptUnitTest.java
index c1c8b0930..0ecbc285c 100644
--- a/zap/src/test/java/org/zaproxy/zap/extension/script/ProxyListenerScriptUnitTest.java
+++ b/zap/src/test/java/org/zaproxy/zap/extension/script/ProxyListenerScriptUnitTest.java
@@ -99,7 +99,7 @@ class ProxyListenerScriptUnitTest extends WithConfigsTest {
     @SuppressWarnings("unchecked")
     void shouldRefreshScriptsAndCallOnHttpRequestSend() throws Exception {
         // Given
-        ProxyScript script = mock(TARGET_INTERFACE);
+        ProxyScript script = mockProxyScript();
         CachedScript<ProxyScript> cachedScript = createCachedScript(script);
         ScriptsCache<ProxyScript> scriptsCache = createScriptsCache(cachedScript);
         given(extensionScript.<ProxyScript>createScriptsCache(any())).willReturn(scriptsCache);
@@ -117,7 +117,7 @@ class ProxyListenerScriptUnitTest extends WithConfigsTest {
     @SuppressWarnings("unchecked")
     void shouldHandleExceptionsThrownByScriptsOnHttpRequestSend() throws Exception {
         // Given
-        ProxyScript script = mock(TARGET_INTERFACE);
+        ProxyScript script = mockProxyScript();
         Exception exception = mock(ScriptException.class);
         given(script.proxyRequest(any())).willThrow(exception);
         ScriptWrapper scriptWrapper = mock(ScriptWrapper.class);
@@ -136,10 +136,10 @@ class ProxyListenerScriptUnitTest extends WithConfigsTest {
     @SuppressWarnings("unchecked")
     void shouldDropMessageOnHttpRequestSend() throws Exception {
         // Given
-        ProxyScript script1 = mock(TARGET_INTERFACE);
-        given(script1.proxyRequest(any())).willReturn(true);
+        ProxyScript script1 = mockProxyScript();
+        given(script1.proxyRequest(any())).willReturn(false);
         CachedScript<ProxyScript> cachedScript1 = createCachedScript(script1);
-        ProxyScript script2 = mock(TARGET_INTERFACE);
+        ProxyScript script2 = mockProxyScript();
         CachedScript<ProxyScript> cachedScript2 = createCachedScript(script2);
         ScriptsCache<ProxyScript> scriptsCache = createScriptsCache(cachedScript1, cachedScript2);
         given(extensionScript.<ProxyScript>createScriptsCache(any())).willReturn(scriptsCache);
@@ -156,7 +156,7 @@ class ProxyListenerScriptUnitTest extends WithConfigsTest {
     @SuppressWarnings("unchecked")
     void shouldCallOnHttpResponseReceive() throws Exception {
         // Given
-        ProxyScript script = mock(TARGET_INTERFACE);
+        ProxyScript script = mockProxyScript();
         CachedScript<ProxyScript> cachedScript = createCachedScript(script);
         ScriptsCache<ProxyScript> scriptsCache = createScriptsCache(cachedScript);
         given(extensionScript.<ProxyScript>createScriptsCache(any())).willReturn(scriptsCache);
@@ -174,7 +174,7 @@ class ProxyListenerScriptUnitTest extends WithConfigsTest {
     @SuppressWarnings("unchecked")
     void shouldHandleExceptionsThrownByScriptsOnHttpResponseReceive() throws Exception {
         // Given
-        ProxyScript script = mock(TARGET_INTERFACE);
+        ProxyScript script = mockProxyScript();
         Exception exception = mock(ScriptException.class);
         given(script.proxyResponse(any())).willThrow(exception);
         ScriptWrapper scriptWrapper = mock(ScriptWrapper.class);
@@ -193,10 +193,10 @@ class ProxyListenerScriptUnitTest extends WithConfigsTest {
     @SuppressWarnings("unchecked")
     void shouldDropMessageOnHttpResponseReceive() throws Exception {
         // Given
-        ProxyScript script1 = mock(TARGET_INTERFACE);
-        given(script1.proxyResponse(any())).willReturn(true);
+        ProxyScript script1 = mockProxyScript();
+        given(script1.proxyResponse(any())).willReturn(false);
         CachedScript<ProxyScript> cachedScript1 = createCachedScript(script1);
-        ProxyScript script2 = mock(TARGET_INTERFACE);
+        ProxyScript script2 = mockProxyScript();
         CachedScript<ProxyScript> cachedScript2 = createCachedScript(script2);
         ScriptsCache<ProxyScript> scriptsCache = createScriptsCache(cachedScript1, cachedScript2);
         given(extensionScript.<ProxyScript>createScriptsCache(any())).willReturn(scriptsCache);
@@ -209,6 +209,13 @@ class ProxyListenerScriptUnitTest extends WithConfigsTest {
         verify(script2, times(0)).proxyResponse(message);
     }
 
+    private static ProxyScript mockProxyScript() throws ScriptException {
+        ProxyScript script = mock(TARGET_INTERFACE, withSettings().lenient());
+        given(script.proxyRequest(any())).willReturn(true);
+        given(script.proxyResponse(any())).willReturn(true);
+        return script;
+    }
+
     private static <T> CachedScript<T> createCachedScript(T script) {
         return createCachedScript(script, null);
     }