Skip to content

Commit

Permalink
kotlin addon in java
Browse files Browse the repository at this point in the history
Signed-off-by: kberg <[email protected]>
  • Loading branch information
kberg committed Jun 13, 2020
1 parent 4c99d2d commit 49db2b7
Show file tree
Hide file tree
Showing 13 changed files with 266 additions and 254 deletions.
2 changes: 1 addition & 1 deletion addOns/kotlin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ All notable changes to this add-on will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Alpha
## Unreleased

- Kotlin scripting for the JVM
6 changes: 3 additions & 3 deletions addOns/kotlin/kotlin.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ plugins {
kotlin("jvm") version "1.3.50"
}

version = "1"
description = "Allows Kotlin to be used for ZAP scripting - templates included"
version = "1.0.0"
description = "Allows Kotlin to be used for ZAP scripting - (some) templates included"

zapAddOn {
addOnName.set("Kotlin Scripting")
addOnName.set("Kotlin Support")
addOnStatus.set(AddOnStatus.ALPHA)
zapVersion.set("2.9.0")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Zed Attack Proxy (ZAP) and its related class files.
*
* ZAP is an HTTP/HTTPS proxy for assessing web application security.
*
* Copyright 2020 The ZAP Development Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.zaproxy.zap.extension.kotlin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.swing.*;
import org.apache.log4j.Logger;
import org.parosproxy.paros.control.Control;
import org.parosproxy.paros.extension.Extension;
import org.parosproxy.paros.extension.ExtensionAdaptor;
import org.parosproxy.paros.extension.ExtensionHook;
import org.parosproxy.paros.view.View;
import org.zaproxy.zap.ZAP;
import org.zaproxy.zap.control.AddOnLoader;
import org.zaproxy.zap.control.ExtensionFactory;
import org.zaproxy.zap.extension.script.ExtensionScript;

public class ExtensionKotlin extends ExtensionAdaptor {

public static final String NAME = "ExtensionKotlin";
public static final int EXTENSION_ORDER = 9999;
public static final ImageIcon KOTLIN_ICON;
private static final List<Class<? extends Extension>> EXTENSION_DEPENDENCIES;
private static final Logger LOGGER = Logger.getLogger(ExtensionKotlin.class);

static {
List<Class<? extends Extension>> dependencies = new ArrayList<>(1);
dependencies.add(ExtensionScript.class);
EXTENSION_DEPENDENCIES = Collections.unmodifiableList(dependencies);

KOTLIN_ICON =
View.isInitialised()
? new ImageIcon(
ExtensionKotlin.class.getResource(
"/org/zaproxy/zap/extension/kotlin/resources/kotlin.png"))
: null;
}

public ExtensionKotlin() {
super(NAME);
setOrder(EXTENSION_ORDER);
}

@Override
public void hook(ExtensionHook extensionHook) {
super.hook(extensionHook);

LOGGER.debug("Hooking Kotlin Scripting Extension");
String zapJar = ZAP.class.getProtectionDomain().getCodeSource().getLocation().getFile();

LOGGER.debug("Loading Kotlin engine...");
AddOnLoader addonLoader = ExtensionFactory.getAddOnLoader();
Arrays.stream(addonLoader.getURLs()).forEach(LOGGER::debug);
KotlinScriptEngineFactory factory = new KotlinScriptEngineFactory(addonLoader, zapJar);
getExtScript().registerScriptEngineWrapper(new KotlinEngineWrapper(factory));
LOGGER.debug("Kotlin engine loaded.");
}

public List<Class<? extends Extension>> getDependencies() {
return EXTENSION_DEPENDENCIES;
}

private ExtensionScript getExtScript() {
return Control.getSingleton().getExtensionLoader().getExtension(ExtensionScript.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Zed Attack Proxy (ZAP) and its related class files.
*
* ZAP is an HTTP/HTTPS proxy for assessing web application security.
*
* Copyright 2020 The ZAP Development Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.zaproxy.zap.extension.kotlin;

import java.util.Collections;
import java.util.List;
import javax.script.ScriptEngineFactory;
import javax.swing.*;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.zaproxy.zap.extension.script.DefaultEngineWrapper;

public class KotlinEngineWrapper extends DefaultEngineWrapper {

@Override
public ImageIcon getIcon() {
return ExtensionKotlin.KOTLIN_ICON;
}

@Override
public String getSyntaxStyle() {
return SyntaxConstants.SYNTAX_STYLE_NONE;
}

@Override
public boolean isRawEngine() {
return false;
}

@Override
public List<String> getExtensions() {
return Collections.singletonList("kts");
}

public KotlinEngineWrapper(ScriptEngineFactory factory) {
super(factory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Zed Attack Proxy (ZAP) and its related class files.
*
* ZAP is an HTTP/HTTPS proxy for assessing web application security.
*
* Copyright 2020 The ZAP Development Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.zaproxy.zap.extension.kotlin;

import java.io.File;
import java.util.List;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import kotlin.jvm.JvmClassMappingKt;
import kotlin.reflect.KClass;
import kotlin.script.experimental.jvm.util.JvmClasspathUtilKt;
import org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineFactoryBase;
import org.jetbrains.kotlin.cli.common.repl.ScriptArgsWithTypes;
import org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine;
import org.jetbrains.kotlin.script.jsr223.KotlinStandardJsr223ScriptTemplate;

public class KotlinScriptEngineFactory extends KotlinJsr223JvmScriptEngineFactoryBase {

private final List<File> jars;

public KotlinScriptEngineFactory(ClassLoader classLoader, String zapJar) {
List<File> clJars =
JvmClasspathUtilKt.scriptCompilationClasspathFromContextOrStdlib(
new String[] {"kotlin-stdlib"}, classLoader, true);
if (zapJar != null) {
clJars.add(new File(zapJar));
}
jars = clJars;
}

@Override
public ScriptEngine getScriptEngine() {
return new KotlinJsr223JvmLocalScriptEngine(
this,
jars,
KotlinStandardJsr223ScriptTemplate.class.getName(),
(ctx, types) ->
new ScriptArgsWithTypes(
new Bindings[] {ctx.getBindings(ScriptContext.ENGINE_SCOPE)},
types),
new KClass<?>[] {JvmClassMappingKt.getKotlinClass(Bindings.class)});
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 49db2b7

Please sign in to comment.