Skip to content

Commit

Permalink
zest: rely on script context writer (#1889)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
thc202 authored and psiinon committed Nov 15, 2018
1 parent 8794b82 commit 7fd462d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/org/zaproxy/zap/extension/zest/ZapAddOn.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<url>https://github.com/zaproxy/zap-core-help/wiki/HelpAddonsZestZest</url>
<changes>
<![CDATA[
Rely on script context writer for script output.<br>
]]>
</changes>
<dependencies>
Expand Down
23 changes: 19 additions & 4 deletions src/org/zaproxy/zap/extension/zest/ZestZapRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.zaproxy.zap.extension.zest;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -69,6 +70,8 @@ public class ZestZapRunner extends ZestBasicRunner implements ScannerListener {

private static final int ZEST_HISTORY_REFERENCE_TYPE = HistoryReference.TYPE_ZEST_SCRIPT;
private static final int FAIL_ACTION_PLUGIN_ID = 50004;

private static Field fieldOutputWriter;

private ExtensionZest extension;
private ZestScriptWrapper wrapper = null;
Expand Down Expand Up @@ -126,24 +129,36 @@ public String run(ZestScript script, Map<String, String> params) throws ZestAsse
this.target = null;
if (wrapper.getWriter() != null) {
super.setOutputWriter(wrapper.getWriter());
} else if (extension.getExtScript().getScriptUI() != null) {
super.setOutputWriter(extension.getExtScript().getScriptUI().getOutputWriter());
} else if (scriptUI != null && !hasOutputWriter()) {
super.setOutputWriter(scriptUI.getOutputWriter());
}
this.setDebug(this.wrapper.isDebug());

return super.run(script, params);
}
}

private boolean hasOutputWriter() {
try {
if (fieldOutputWriter == null) {
fieldOutputWriter = ZestBasicRunner.class.getDeclaredField("outputWriter");
fieldOutputWriter.setAccessible(true);
}
return fieldOutputWriter.get(this) != null;
} catch (IllegalAccessException | NoSuchFieldException e) {
return false;
}
}

@Override
public String run (ZestScript script, ZestRequest target, Map<String, String> params)
throws ZestAssertFailException, ZestActionFailException, IOException,
ZestInvalidCommonTestException, ZestAssignFailException, ZestClientFailException {
log.debug("Run script " + script.getTitle());
if (wrapper.getWriter() != null) {
super.setOutputWriter(wrapper.getWriter());
} else if (extension.getExtScript().getScriptUI() != null) {
super.setOutputWriter(extension.getExtScript().getScriptUI().getOutputWriter());
} else if (scriptUI != null && !hasOutputWriter()) {
super.setOutputWriter(scriptUI.getOutputWriter());
}
this.setDebug(this.wrapper.isDebug());
String result = super.run(script, target, params);
Expand Down

0 comments on commit 7fd462d

Please sign in to comment.