public class ExtensionScript extends ExtensionAdaptor implements CommandLineListener
Modifier and Type | Field and Description |
---|---|
static Charset |
DEFAULT_CHARSET
The
Charset used to load/save the scripts from/to the file. |
static int |
EXTENSION_ORDER |
static ImageIcon |
ICON
Deprecated.
(2.7.0) Use
getScriptIcon() instead. |
static String |
NAME |
protected static String |
SCRIPT_CONSOLE_HOME_PAGE
Deprecated.
|
protected static String |
SCRIPT_NAME_ATT |
static String |
SCRIPTS_DIR |
static String |
TEMPLATES_DIR |
static String |
TYPE_HTTP_SENDER |
static String |
TYPE_PROXY |
static String |
TYPE_STANDALONE |
static String |
TYPE_TARGETED |
Constructor and Description |
---|
ExtensionScript() |
Modifier and Type | Method and Description |
---|---|
void |
addListener(ScriptEventListener listener) |
ScriptNode |
addScript(ScriptWrapper script) |
ScriptNode |
addScript(ScriptWrapper script,
boolean display) |
void |
addScriptOutputListener(ScriptOutputListener listener)
Adds the given script output listener.
|
int |
addScriptsFromDir(File dir)
Adds the scripts from the given directory and starts tracking it.
|
ScriptNode |
addTemplate(ScriptWrapper template) |
ScriptNode |
addTemplate(ScriptWrapper template,
boolean display) |
void |
addWriter(Writer writer)
Adds the given writer.
|
<T> ScriptsCache<T> |
createScriptsCache(ScriptsCache.Configuration<T> config)
Creates a scripts cache.
|
void |
execute(CommandLineArgument[] args)
execute the command line using the argument provided.
|
String |
getAuthor()
Gets the author of the extension.
|
String |
getDescription()
Returns the description of the extension, to be shown in UI components.
|
String |
getEngineNameForExtension(String ext) |
ScriptEngineWrapper |
getEngineWrapper(String name) |
List<String> |
getHandledExtensions()
Get the list of extensions this listener can handle
|
<T> T |
getInterface(ScriptWrapper script,
Class<T> class1)
Gets the interface
class1 from the given script . |
<T> T |
getInterfaceWithOutAddOnLoader(ScriptWrapper script,
Class<T> clazz)
Gets the interface
clazz from the given script . |
ScriptWrapper |
getScript(String name) |
int |
getScriptCount(File dir)
Gets the numbers of scripts for the given directory for the currently registered script
engines and types.
|
static ImageIcon |
getScriptIcon()
Gets the script icon.
|
List<String> |
getScriptingEngines() |
protected ScriptParam |
getScriptParam() |
List<ScriptWrapper> |
getScripts(ScriptType type) |
List<ScriptWrapper> |
getScripts(String type) |
ScriptType |
getScriptType(String name) |
Collection<ScriptType> |
getScriptTypes() |
ScriptUI |
getScriptUI() |
List<ScriptWrapper> |
getTemplates(ScriptType type) |
ScriptTreeModel |
getTreeModel() |
String |
getUIName()
By default returns the name returned by
getName() . |
List<String> |
getUnsavedResources()
Extensions should return the user friendly names of any unsaved resources - if there are any
the user will be given the option not to exit ZAP or, if the extension is bundled in an
add-on that's being updated or uninstalled, not to continue with the changes.
|
void |
handleFailedScriptInterface(ScriptWrapper script,
String errorMessage)
Handles a failed attempt to convert a script into an interface.
|
boolean |
handleFile(File file)
Handle the specified file (in whatever way is appropriate).
|
void |
handleScriptError(ScriptWrapper script,
String error)
Handles errors caused by scripts.
|
void |
handleScriptException(ScriptWrapper script,
Exception exception)
Handles exceptions thrown by scripts.
|
static boolean |
hasSameScriptEngine(ScriptWrapper scriptWrapper,
ScriptEngineWrapper engineWrapper)
Tells whether or not the given
scriptWrapper has the given engineWrapper . |
void |
hook(ExtensionHook extensionHook)
Called during extension's initialisation to allow to add new functionality to core
components.
|
boolean |
invokeProxyScript(ScriptWrapper script,
HttpMessage msg,
boolean request)
Invokes the given
script , synchronously, as a ProxyScript , handling any
Exception thrown during the invocation. |
Invocable |
invokeScript(ScriptWrapper script)
Invokes the given
script , synchronously, handling any Exception thrown during
the invocation. |
Invocable |
invokeScriptWithOutAddOnLoader(ScriptWrapper script)
Invokes the given
script , synchronously, handling any Exception thrown during
the invocation. |
void |
invokeSenderScript(ScriptWrapper script,
HttpMessage msg,
int initiator,
HttpSender sender,
boolean request)
Invokes the given
script , synchronously, as a HttpSenderScript , handling any
Exception thrown during the invocation. |
void |
invokeTargetedScript(ScriptWrapper script,
HttpMessage msg)
Invokes the given
script , synchronously, as a TargetedScript , handling any
Exception thrown during the invocation. |
static boolean |
isSameScriptEngine(String name,
String engineName,
String engineLanguage)
Tells whether or not the given
name matches the given engineName and engineLanguage . |
ScriptWrapper |
loadScript(ScriptWrapper script)
Loads the script from the file, using
DEFAULT_CHARSET . |
ScriptWrapper |
loadScript(ScriptWrapper script,
Charset charset)
Loads the script from the file, using the given charset.
|
void |
postInit()
Implement this method to perform tasks after all extensions/add-ons have been initialised.
|
static void |
recordScriptCalledStats(ScriptWrapper sw) |
static void |
recordScriptFailedStats(ScriptWrapper sw) |
void |
registerScriptEngineWrapper(ScriptEngineWrapper wrapper)
Registers a new script engine wrapper.
|
void |
registerScriptType(ScriptType type)
Registers a new type of script.
|
void |
removeListener(ScriptEventListener listener) |
void |
removeScript(ScriptWrapper script) |
void |
removeScriptEngineWrapper(ScriptEngineWrapper wrapper)
Removes the given script engine wrapper.
|
void |
removeScriptOutputListener(ScriptOutputListener listener)
Removes the given script output listener.
|
int |
removeScriptsFromDir(File dir)
Removes the scripts added from the given directory and stops tracking it.
|
void |
removeScriptType(ScriptType type)
Removes the given script type.
|
void |
removeScriptUI() |
void |
removeScripType(ScriptType type)
Deprecated.
(2.9.0) Use
removeScriptType(ScriptType) instead. |
void |
removeTemplate(ScriptWrapper template) |
void |
removeWriter(Writer writer)
Removes the given writer.
|
void |
saveScript(ScriptWrapper script) |
void |
setChanged(ScriptWrapper script,
boolean changed) |
void |
setEnabled(ScriptWrapper script,
boolean enabled) |
void |
setError(ScriptWrapper script,
Exception e) |
void |
setError(ScriptWrapper script,
String details) |
void |
setScriptUI(ScriptUI scriptUI) |
boolean |
supportsDb(String type)
No database tables used, so all supported
|
canUnload, databaseOpen, destroy, getActiveActions, getAddOn, getDependencies, getExtensionMenu, getExtensionView, getI18nPrefix, getMessages, getModel, getName, getOrder, getView, hasView, init, initModel, initView, initXML, isCore, isDepreciated, isEnabled, optionsLoaded, postInstall, setAddOn, setDescription, setEnabled, setI18nPrefix, setMessages, setName, setOrder, start, stop, supportsLowMemory, unload
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getURL, getVersion
public static final int EXTENSION_ORDER
public static final String NAME
@Deprecated public static final ImageIcon ICON
getScriptIcon()
instead.public static final Charset DEFAULT_CHARSET
Charset
used to load/save the scripts from/to the file.
While the scripts can be loaded with any Charset
(defaulting to this one) they are
always saved with this Charset
.
loadScript(ScriptWrapper)
,
loadScript(ScriptWrapper, Charset)
,
saveScript(ScriptWrapper)
public static final String SCRIPTS_DIR
public static final String TEMPLATES_DIR
@Deprecated protected static final String SCRIPT_CONSOLE_HOME_PAGE
protected static final String SCRIPT_NAME_ATT
public static final String TYPE_HTTP_SENDER
public static final String TYPE_PROXY
public static final String TYPE_STANDALONE
public static final String TYPE_TARGETED
public String getUIName()
ExtensionAdaptor
getName()
.getUIName
in interface Extension
getUIName
in class ExtensionAdaptor
null
ExtensionAdaptor.getName()
public void hook(ExtensionHook extensionHook)
Extension
hook
in interface Extension
hook
in class ExtensionAdaptor
extensionHook
- the hook to add the components.public void registerScriptEngineWrapper(ScriptEngineWrapper wrapper)
The templates of the wrapped script engine are loaded, if any.
The engine is set to existing scripts targeting the given engine.
wrapper
- the script engine wrapper that will be added, must not be null
removeScriptEngineWrapper(ScriptEngineWrapper)
,
ScriptWrapper.setEngine(ScriptEngineWrapper)
public static boolean hasSameScriptEngine(ScriptWrapper scriptWrapper, ScriptEngineWrapper engineWrapper)
scriptWrapper
has the given engineWrapper
.
If the given scriptWrapper
has an engine set it's checked by reference (operator
==
), otherwise it's used the engine names.
scriptWrapper
- the script wrapper that will be checkedengineWrapper
- the engine that will be checked against the engine of the scripttrue
if the given script has the given engine, false
otherwise.isSameScriptEngine(String, String, String)
public static boolean isSameScriptEngine(String name, String engineName, String engineLanguage)
name
matches the given engineName
and engineLanguage
.name
- the name that will be checked against the given engineName
and engineLanguage
.engineName
- the name of the script engine.engineLanguage
- the language of the script engine.true
if the name
matches the given engine's name and language, false
otherwise.hasSameScriptEngine(ScriptWrapper, ScriptEngineWrapper)
public void removeScriptEngineWrapper(ScriptEngineWrapper wrapper)
The user's templates and scripts associated with the given engine are not removed but its
engine is set to null
. Default templates are removed.
The call to this method has no effect if the given type is not registered.
wrapper
- the script engine wrapper that will be removed, must not be null
registerScriptEngineWrapper(ScriptEngineWrapper)
,
ScriptWrapper.setEngine(ScriptEngineWrapper)
public ScriptEngineWrapper getEngineWrapper(String name)
protected ScriptParam getScriptParam()
public ScriptTreeModel getTreeModel()
public void registerScriptType(ScriptType type)
The script is added to the tree of scripts and its scripts/templates loaded, if any.
type
- the new type of scriptInvalidParameterException
- if a script type with same name is already registeredremoveScriptType(ScriptType)
@Deprecated public void removeScripType(ScriptType type)
removeScriptType(ScriptType)
instead.The templates and scripts associated with the given type are also removed, if any.
The call to this method has no effect if the given type is not registered.
type
- the script type that will be removedregisterScriptType(ScriptType)
public void removeScriptType(ScriptType type)
The templates and scripts associated with the given type are also removed, if any.
The call to this method has no effect if the given type is not registered.
type
- the script type that will be removedregisterScriptType(ScriptType)
public ScriptType getScriptType(String name)
public Collection<ScriptType> getScriptTypes()
public String getAuthor()
Extension
Since 2.9.0 defaults to the author of the add-on, if set, otherwise an empty string.
public String getDescription()
Extension
getDescription
in interface Extension
getDescription
in class ExtensionAdaptor
null
public ScriptWrapper getScript(String name)
public ScriptNode addScript(ScriptWrapper script)
public ScriptNode addScript(ScriptWrapper script, boolean display)
public void saveScript(ScriptWrapper script) throws IOException
IOException
public void removeScript(ScriptWrapper script)
public void removeTemplate(ScriptWrapper template)
public ScriptNode addTemplate(ScriptWrapper template)
public ScriptNode addTemplate(ScriptWrapper template, boolean display)
public void postInit()
Extension
postInit
in interface Extension
postInit
in class ExtensionAdaptor
Extension.postInstall()
public int addScriptsFromDir(File dir)
The directory will be tracked to add or remove its scripts when a new script engine/type is added or removed.
The scripts are expected to be under directories of the corresponding script type, for example:
(dir specified)
├── active
│ ├── gof_lite.js
│ ├── TestInsecureHTTPVerbs.py
│ └── User defined attacks.js
├── extender
│ └── HTTP Message Logger.js
├── httpfuzzerprocessor
│ ├── add_msgs_sites_tree.js
│ ├── http_status_code_filter.py
│ └── showDifferences.js
├── httpsender
│ ├── add_header_request.py
│ └── Capture and Replace Anti CSRF Token.js
└── variant
└── JsonStrings.js
where active
, extender
, httpfuzzerprocessor
, httpsender
, and
variant
are the script types.dir
- the directory from where to add the scripts.removeScriptsFromDir(File)
public int removeScriptsFromDir(File dir)
The number of scripts removed might be different than the number of scripts initially added, as a script engine or type might have been added or removed in the meantime.
dir
- the directory previously added.addScriptsFromDir(File)
public int getScriptCount(File dir)
dir
- the directory to check.public ScriptWrapper loadScript(ScriptWrapper script) throws IOException
DEFAULT_CHARSET
.
If the file contains invalid byte sequences (for DEFAULT_CHARSET
) it will be
loaded again using the (JVM) default charset
, to load
scripts saved with older ZAP versions (which relied on default charset).
script
- the ScriptWrapper to be loaded (read script from file).ScriptWrapper
with the actual script read from the file.IOException
- if an error occurred while reading the script from the file.IllegalArgumentException
- if the script
is null
.loadScript(ScriptWrapper, Charset)
public ScriptWrapper loadScript(ScriptWrapper script, Charset charset) throws IOException
script
- the ScriptWrapper to be loaded (read script from file).charset
- the charset to use when reading the script from the file.ScriptWrapper
with the actual script read from the file.IOException
- if an error occurred while reading the script from the file.IllegalArgumentException
- if the script
or the charset
is null
.loadScript(ScriptWrapper)
public List<ScriptWrapper> getScripts(String type)
public List<ScriptWrapper> getScripts(ScriptType type)
public List<ScriptWrapper> getTemplates(ScriptType type)
public Invocable invokeScript(ScriptWrapper script) throws ScriptException
script
, synchronously, handling any Exception
thrown during
the invocation.
The context class loader of caller thread is replaced with the class loader AddOnLoader
to allow the script to access classes of add-ons. If this behaviour is not
desired call the method invokeScriptWithOutAddOnLoader
instead.
script
- the script that will be invokedInvocable
for the script
, or null
if none.ScriptException
- if the engine of the given script
was not found.invokeScriptWithOutAddOnLoader(ScriptWrapper)
,
Invocable
public Invocable invokeScriptWithOutAddOnLoader(ScriptWrapper script) throws ScriptException
script
, synchronously, handling any Exception
thrown during
the invocation.script
- the script that will be invoked/evaluatedInvocable
for the script
, or null
if none.ScriptException
- if the engine of the given script
was not found.invokeScript(ScriptWrapper)
,
Invocable
public void handleScriptException(ScriptWrapper script, Exception exception)
The given exception
(if of type ScriptException
the cause will be used
instead) will be written to the writer(s) associated with the given script
, moreover
the script will be disabled and flagged that has an error.
script
- the script that resulted in an exception, must not be null
exception
- the exception thrown, must not be null
setEnabled(ScriptWrapper, boolean)
,
setError(ScriptWrapper, Exception)
,
handleFailedScriptInterface(ScriptWrapper, String)
,
handleScriptError(ScriptWrapper, String)
,
ScriptException
public void handleScriptError(ScriptWrapper script, String error)
The given error
will be written to the writer(s) associated with the given script
, moreover the script will be disabled and flagged that has an error.
script
- the script that caused the error, must not be null
.error
- the error caused by the script, must not be null
.setEnabled(ScriptWrapper, boolean)
,
setError(ScriptWrapper, String)
,
handleScriptException(ScriptWrapper, Exception)
public void invokeTargetedScript(ScriptWrapper script, HttpMessage msg)
script
, synchronously, as a TargetedScript
, handling any
Exception
thrown during the invocation.
The context class loader of caller thread is replaced with the class loader AddOnLoader
to allow the script to access classes of add-ons.
script
- the script to invoke.msg
- the HTTP message to process.getInterface(ScriptWrapper, Class)
public void handleFailedScriptInterface(ScriptWrapper script, String errorMessage)
The given errorMessage
will be written to the writer(s) associated with the given
script
, moreover it will be disabled and flagged that has an error.
script
- the script that resulted in an exception, must not be null
errorMessage
- the message that will be written to the writer(s)setEnabled(ScriptWrapper, boolean)
,
setError(ScriptWrapper, Exception)
,
handleScriptException(ScriptWrapper, Exception)
public boolean invokeProxyScript(ScriptWrapper script, HttpMessage msg, boolean request)
script
, synchronously, as a ProxyScript
, handling any
Exception
thrown during the invocation.
The context class loader of caller thread is replaced with the class loader AddOnLoader
to allow the script to access classes of add-ons.
script
- the script to invoke.msg
- the HTTP message being proxied.request
- true
if processing the request, false
otherwise.true
if the request should be forward to the server, false
otherwise.getInterface(ScriptWrapper, Class)
public void invokeSenderScript(ScriptWrapper script, HttpMessage msg, int initiator, HttpSender sender, boolean request)
script
, synchronously, as a HttpSenderScript
, handling any
Exception
thrown during the invocation.
The context class loader of caller thread is replaced with the class loader AddOnLoader
to allow the script to access classes of add-ons.
script
- the script to invoke.msg
- the HTTP message being sent/received.initiator
- the initiator of the request.sender
- the sender of the given HttpMessage
.request
- true
if processing the request, false
otherwise.getInterface(ScriptWrapper, Class)
public void setChanged(ScriptWrapper script, boolean changed)
public void setEnabled(ScriptWrapper script, boolean enabled)
public void setError(ScriptWrapper script, String details)
public void setError(ScriptWrapper script, Exception e)
public void addListener(ScriptEventListener listener)
public void removeListener(ScriptEventListener listener)
public void addWriter(Writer writer)
It will be written to each time a script writes some output.
writer
- the writer to add.removeWriter(Writer)
,
addScriptOutputListener(ScriptOutputListener)
public void removeWriter(Writer writer)
writer
- the writer to remove.addWriter(Writer)
public void addScriptOutputListener(ScriptOutputListener listener)
listener
- the listener to add.NullPointerException
- if the given listener is null
.removeScriptOutputListener(ScriptOutputListener)
public void removeScriptOutputListener(ScriptOutputListener listener)
listener
- the listener to remove.NullPointerException
- if the given listener is null
.addScriptOutputListener(ScriptOutputListener)
public ScriptUI getScriptUI()
public void setScriptUI(ScriptUI scriptUI)
public void removeScriptUI()
public <T> ScriptsCache<T> createScriptsCache(ScriptsCache.Configuration<T> config)
T
- the target interface.config
- the cache configurationpublic <T> T getInterface(ScriptWrapper script, Class<T> class1) throws ScriptException, IOException
class1
from the given script
. Might return null
if
the script
does not implement the interface.
First tries to get the interface directly from the script
by calling the method
ScriptWrapper.getInterface(Class)
, if it returns null
the interface will be
extracted from the script after invoking it, using the method Invocable.getInterface(Class)
.
The context class loader of caller thread is replaced with the class loader AddOnLoader
to allow the script to access classes of add-ons. If this behaviour is not
desired call the method getInterfaceWithOutAddOnLoader(
instead.
script
- the script that will be invokedclass1
- the interface that will be obtained from the scriptnull
if the script
does
not implement the interface.ScriptException
- if the engine of the given script
was not found.IOException
- if an error occurred while obtaining the interface directly from the
script ( ScriptWrapper.getInterface(Class)
)getInterfaceWithOutAddOnLoader(ScriptWrapper, Class)
,
ScriptWrapper.getInterface(Class)
,
Invocable.getInterface(Class)
public <T> T getInterfaceWithOutAddOnLoader(ScriptWrapper script, Class<T> clazz) throws ScriptException, IOException
clazz
from the given script
. Might return null
if
the script
does not implement the interface.
First tries to get the interface directly from the script
by calling the method
ScriptWrapper.getInterface(Class)
, if it returns null
the interface will be
extracted from the script after invoking it, using the method Invocable.getInterface(Class)
.
script
- the script that will be invokedclazz
- the interface that will be obtained from the scriptnull
if the script
does
not implement the interface.ScriptException
- if the engine of the given script
was not found.IOException
- if an error occurred while obtaining the interface directly from the
script ( ScriptWrapper.getInterface(Class)
)getInterface(ScriptWrapper, Class)
,
ScriptWrapper.getInterface(Class)
,
Invocable.getInterface(Class)
public List<String> getUnsavedResources()
Extension
getUnsavedResources
in interface Extension
getUnsavedResources
in class ExtensionAdaptor
List
containing the unsaved resources or null
if noneExtension.getActiveActions()
public void execute(CommandLineArgument[] args)
CommandLineListener
execute
in interface CommandLineListener
args
- the command line argumentspublic boolean handleFile(File file)
CommandLineListener
handleFile
in interface CommandLineListener
file
- the file provided through the command linepublic List<String> getHandledExtensions()
CommandLineListener
getHandledExtensions
in interface CommandLineListener
List
with the handled extensionspublic boolean supportsDb(String type)
supportsDb
in interface Extension
supportsDb
in class ExtensionAdaptor
type
- the db typeDatabase.getType()
public static ImageIcon getScriptIcon()
Should be called/used only when in view mode.
null
.public static void recordScriptCalledStats(ScriptWrapper sw)
public static void recordScriptFailedStats(ScriptWrapper sw)