Skip to content

Commit 7fd462d

Browse files
thc202psiinon
authored andcommitted
zest: rely on script context writer (#1889)
Change ZestZapRunner to not set the script console writer as output writer if the script context writer is already set, while both end up in the script console the latter allows the core to intercept the writes. For the cases the context writer is not set the writes are not intercepted. Update changes in ZapAddOn.xml file. Related to zaproxy/zaproxy#5113 - Allow to differentiate scripts' output
1 parent 8794b82 commit 7fd462d

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/org/zaproxy/zap/extension/zest/ZapAddOn.xml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<url>https://github.com/zaproxy/zap-core-help/wiki/HelpAddonsZestZest</url>
88
<changes>
99
<![CDATA[
10+
Rely on script context writer for script output.<br>
1011
]]>
1112
</changes>
1213
<dependencies>

src/org/zaproxy/zap/extension/zest/ZestZapRunner.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package org.zaproxy.zap.extension.zest;
2222

2323
import java.io.IOException;
24+
import java.lang.reflect.Field;
2425
import java.util.ArrayList;
2526
import java.util.List;
2627
import java.util.Map;
@@ -69,6 +70,8 @@ public class ZestZapRunner extends ZestBasicRunner implements ScannerListener {
6970

7071
private static final int ZEST_HISTORY_REFERENCE_TYPE = HistoryReference.TYPE_ZEST_SCRIPT;
7172
private static final int FAIL_ACTION_PLUGIN_ID = 50004;
73+
74+
private static Field fieldOutputWriter;
7275

7376
private ExtensionZest extension;
7477
private ZestScriptWrapper wrapper = null;
@@ -126,24 +129,36 @@ public String run(ZestScript script, Map<String, String> params) throws ZestAsse
126129
this.target = null;
127130
if (wrapper.getWriter() != null) {
128131
super.setOutputWriter(wrapper.getWriter());
129-
} else if (extension.getExtScript().getScriptUI() != null) {
130-
super.setOutputWriter(extension.getExtScript().getScriptUI().getOutputWriter());
132+
} else if (scriptUI != null && !hasOutputWriter()) {
133+
super.setOutputWriter(scriptUI.getOutputWriter());
131134
}
132135
this.setDebug(this.wrapper.isDebug());
133136

134137
return super.run(script, params);
135138
}
136139
}
137140

141+
private boolean hasOutputWriter() {
142+
try {
143+
if (fieldOutputWriter == null) {
144+
fieldOutputWriter = ZestBasicRunner.class.getDeclaredField("outputWriter");
145+
fieldOutputWriter.setAccessible(true);
146+
}
147+
return fieldOutputWriter.get(this) != null;
148+
} catch (IllegalAccessException | NoSuchFieldException e) {
149+
return false;
150+
}
151+
}
152+
138153
@Override
139154
public String run (ZestScript script, ZestRequest target, Map<String, String> params)
140155
throws ZestAssertFailException, ZestActionFailException, IOException,
141156
ZestInvalidCommonTestException, ZestAssignFailException, ZestClientFailException {
142157
log.debug("Run script " + script.getTitle());
143158
if (wrapper.getWriter() != null) {
144159
super.setOutputWriter(wrapper.getWriter());
145-
} else if (extension.getExtScript().getScriptUI() != null) {
146-
super.setOutputWriter(extension.getExtScript().getScriptUI().getOutputWriter());
160+
} else if (scriptUI != null && !hasOutputWriter()) {
161+
super.setOutputWriter(scriptUI.getOutputWriter());
147162
}
148163
this.setDebug(this.wrapper.isDebug());
149164
String result = super.run(script, target, params);

0 commit comments

Comments
 (0)