Skip to content

Commit

Permalink
Merge pull request #4850 from thc202/commonlib/value-generator
Browse files Browse the repository at this point in the history
Provide core `ValueGenerator` through `commonlib`
  • Loading branch information
ricekot authored Aug 29, 2023
2 parents ed8f046 + fc98d3e commit 7ae219f
Show file tree
Hide file tree
Showing 28 changed files with 177 additions and 400 deletions.
1 change: 1 addition & 0 deletions addOns/commonlib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
### Added
- Provide Jackson datatype library for other add-ons (Issue 7961).
- Provide the Value Generator for other add-ons (Issue 8016).

## [1.16.0] - 2023-08-14
### Added
Expand Down
17 changes: 17 additions & 0 deletions addOns/commonlib/commonlib.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ zapAddOn {
baseName.set("help%LC%.helpset")
localeToken.set("%LC%")
}

extensions {
register("org.zaproxy.addon.commonlib.formhandler.ExtensionCommonlibFormHandler") {
classnames {
allowed.set(listOf("org.zaproxy.addon.commonlib.formhandler"))
}
dependencies {
addOns {
register("formhandler") {
version.set(">=6.0.0 & < 7.0.0")
}
}
}
}
}
}
}

Expand All @@ -25,6 +40,8 @@ crowdin {
}

dependencies {
zapAddOn("formhandler")

api(platform("com.fasterxml.jackson:jackson-bom:2.15.2"))
api("com.fasterxml.jackson.core:jackson-databind")
api("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,54 @@
*/
package org.zaproxy.addon.commonlib;

import java.util.List;
import java.util.Map;
import org.apache.commons.httpclient.URI;
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.control.Control.Mode;
import org.parosproxy.paros.extension.ExtensionAdaptor;
import org.parosproxy.paros.extension.ExtensionHook;
import org.parosproxy.paros.extension.SessionChangedListener;
import org.parosproxy.paros.model.Session;
import org.zaproxy.addon.commonlib.ui.ProgressPanel;
import org.zaproxy.zap.model.DefaultValueGenerator;
import org.zaproxy.zap.model.ValueGenerator;

public class ExtensionCommonlib extends ExtensionAdaptor {

private static final ValueGenerator DEFAULT_VALUE_GENERATOR = new DefaultValueGenerator();

private ValueGenerator valueGeneratorImpl;

private final ValueGenerator valueGeneratorWrapper =
(URI uri,
String url,
String fieldId,
String defaultValue,
List<String> definedValues,
Map<String, String> envAttributes,
Map<String, String> fieldAttributes) -> {
var local = valueGeneratorImpl;
if (local != null) {
return local.getValue(
uri,
url,
fieldId,
defaultValue,
definedValues,
envAttributes,
fieldAttributes);
}
return DEFAULT_VALUE_GENERATOR.getValue(
uri,
url,
fieldId,
defaultValue,
definedValues,
envAttributes,
fieldAttributes);
};

private ProgressPanel progressPanel;

@Override
Expand Down Expand Up @@ -66,6 +104,21 @@ public String getUIName() {
return Constant.messages.getString("commonlib.name");
}

/**
* Gets the value generator.
*
* @return the value generator, never {@code null}.
* @since 2.17.0
*/
public ValueGenerator getValueGenerator() {
return valueGeneratorWrapper;
}

/** <strong>Note:</strong> Not part of the public API. */
public void setCustomValueGenerator(ValueGenerator generator) {
this.valueGeneratorImpl = generator;
}

private class SessionChangedListenerImpl implements SessionChangedListener {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.zaproxy.zap.extension.soap.formhandler;
package org.zaproxy.addon.commonlib.formhandler;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -27,24 +27,24 @@
import org.parosproxy.paros.extension.Extension;
import org.parosproxy.paros.extension.ExtensionAdaptor;
import org.parosproxy.paros.extension.ExtensionHook;
import org.zaproxy.addon.commonlib.ExtensionCommonlib;
import org.zaproxy.zap.extension.formhandler.ExtensionFormHandler;
import org.zaproxy.zap.extension.soap.ExtensionImportWSDL;
import org.zaproxy.zap.model.ValueGenerator;

public class ExtensionSoapFormHandler extends ExtensionAdaptor {
public class ExtensionCommonlibFormHandler extends ExtensionAdaptor {

private static final List<Class<? extends Extension>> DEPENDENCIES =
Collections.unmodifiableList(
Arrays.asList(ExtensionFormHandler.class, ExtensionImportWSDL.class));
Arrays.asList(ExtensionFormHandler.class, ExtensionCommonlib.class));

@Override
public String getUIName() {
return Constant.messages.getString("soap.formhandler.name");
return Constant.messages.getString("commonlib.formhandler.name");
}

@Override
public String getDescription() {
return Constant.messages.getString("soap.formhandler.desc");
return Constant.messages.getString("commonlib.formhandler.desc");
}

@Override
Expand All @@ -56,7 +56,11 @@ public List<Class<? extends Extension>> getDependencies() {
public void hook(ExtensionHook extensionHook) {
ValueGenerator valueGenerator =
getExtension(ExtensionFormHandler.class).getValueGenerator();
getExtension(ExtensionImportWSDL.class).setValueGenerator(valueGenerator);
setCustomValueGenerator(valueGenerator);
}

private static void setCustomValueGenerator(ValueGenerator valueGenerator) {
getExtension(ExtensionCommonlib.class).setCustomValueGenerator(valueGenerator);
}

private static <T extends Extension> T getExtension(Class<T> clazz) {
Expand All @@ -70,6 +74,6 @@ public boolean canUnload() {

@Override
public void unload() {
getExtension(ExtensionImportWSDL.class).setValueGenerator(null);
setCustomValueGenerator(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
commonlib.desc = A library of shared functionality

commonlib.formhandler.desc = Common Library Form Handler Integration
commonlib.formhandler.name = Common Library Form Handler

commonlib.name = Common Library

commonlib.progress.pane.completed = Completed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.zaproxy.addon.spider.formhandler;
package org.zaproxy.addon.commonlib.formhandler;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
Expand All @@ -39,36 +39,37 @@
import org.parosproxy.paros.extension.ExtensionHook;
import org.parosproxy.paros.extension.ExtensionLoader;
import org.parosproxy.paros.model.Model;
import org.zaproxy.addon.spider.ExtensionSpider2;
import org.zaproxy.addon.commonlib.ExtensionCommonlib;
import org.zaproxy.zap.extension.formhandler.ExtensionFormHandler;
import org.zaproxy.zap.model.ValueGenerator;
import org.zaproxy.zap.testutils.TestUtils;

/** Unit test for {@link ExtensionSpiderFormHandler}. */
class ExtensionSpiderFormHandlerUnitTest extends TestUtils {
/** Unit test for {@link ExtensionCommonlibFormHandler}. */
class ExtensionCommonlibFormHandlerUnitTest extends TestUtils {

private ExtensionSpider2 extensionSpider;
private ExtensionCommonlib extensionCommonlib;
private ExtensionFormHandler extensionFormHandler;
private ExtensionLoader extensionLoader;
private ExtensionSpiderFormHandler extension;
private ExtensionCommonlibFormHandler extension;

@BeforeEach
void setUp() {
extension = new ExtensionSpiderFormHandler();
mockMessages("org.zaproxy.addon.spider.resources." + Constant.MESSAGES_PREFIX, "spider");
extension = new ExtensionCommonlibFormHandler();
mockMessages(
"org.zaproxy.addon.commonlib.resources." + Constant.MESSAGES_PREFIX, "commonlib");

Model model = mock(Model.class, withSettings().strictness(Strictness.LENIENT));
Model.setSingletonForTesting(model);

extensionLoader =
ExtensionLoader extensionLoader =
mock(ExtensionLoader.class, withSettings().strictness(Strictness.LENIENT));
Control.initSingletonForTesting(model, extensionLoader);

extensionSpider = mockLoadedExtension(ExtensionSpider2.class);
extensionFormHandler = mockLoadedExtension(ExtensionFormHandler.class);
extensionCommonlib = mockLoadedExtension(extensionLoader, ExtensionCommonlib.class);
extensionFormHandler = mockLoadedExtension(extensionLoader, ExtensionFormHandler.class);
}

private <T extends Extension> T mockLoadedExtension(Class<T> clazz) {
private static <T extends Extension> T mockLoadedExtension(
ExtensionLoader extensionLoader, Class<T> clazz) {
T extension = mock(clazz);
given(extensionLoader.getExtension(clazz)).willReturn(extension);
return extension;
Expand All @@ -78,7 +79,9 @@ private <T extends Extension> T mockLoadedExtension(Class<T> clazz) {
void shouldHaveName() {
assertThat(
extension.getName(),
is(equalTo("org.zaproxy.addon.spider.formhandler.ExtensionSpiderFormHandler")));
is(
equalTo(
"org.zaproxy.addon.commonlib.formhandler.ExtensionCommonlibFormHandler")));
}

@Test
Expand All @@ -95,7 +98,7 @@ void shouldHaveDescription() {
void shouldHaveExpectedDependencies() {
assertThat(
extension.getDependencies(),
containsInAnyOrder(ExtensionFormHandler.class, ExtensionSpider2.class));
containsInAnyOrder(ExtensionFormHandler.class, ExtensionCommonlib.class));
}

@Test
Expand All @@ -107,7 +110,7 @@ void shouldSetValueGeneratorOnHook() {
// When
extension.hook(extensionHook);
// Then
verify(extensionSpider).setValueGenerator(valueGenerator);
verify(extensionCommonlib).setCustomValueGenerator(valueGenerator);
}

@Test
Expand All @@ -120,6 +123,6 @@ void shouldUnload() {
// Given / When
extension.unload();
// Then
verify(extensionSpider).setValueGenerator(null);
verify(extensionCommonlib).setCustomValueGenerator(null);
}
}
1 change: 1 addition & 0 deletions addOns/graphql/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Dependency updates.
- Maintenance changes.
- Depend on newer versions of Automation Framework and Common Library add-ons (Related to Issue 7961).
- Use Common Library add-on to obtain the Value Generator (Issue 8016).

## [0.18.0] - 2023-07-11
### Changed
Expand Down
14 changes: 0 additions & 14 deletions addOns/graphql/graphql.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@ zapAddOn {
}
}

register("org.zaproxy.addon.graphql.formhandler.ExtensionGraphQlFormHandler") {
classnames {
allowed.set(listOf("org.zaproxy.addon.graphql.formhandler"))
}
dependencies {
addOns {
register("formhandler") {
version.set(">=6.0.0 & < 7.0.0")
}
}
}
}

register("org.zaproxy.addon.graphql.spider.ExtensionGraphQlSpider") {
classnames {
allowed.set(listOf("org.zaproxy.addon.graphql.spider"))
Expand Down Expand Up @@ -73,7 +60,6 @@ crowdin {
dependencies {
zapAddOn("automation")
zapAddOn("commonlib")
zapAddOn("formhandler")
zapAddOn("spider")

implementation("com.graphql-java:graphql-java:21.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,25 @@
import org.parosproxy.paros.control.Control.Mode;
import org.parosproxy.paros.extension.CommandLineArgument;
import org.parosproxy.paros.extension.CommandLineListener;
import org.parosproxy.paros.extension.Extension;
import org.parosproxy.paros.extension.ExtensionAdaptor;
import org.parosproxy.paros.extension.ExtensionHook;
import org.parosproxy.paros.extension.SessionChangedListener;
import org.parosproxy.paros.model.Session;
import org.parosproxy.paros.network.HttpSender;
import org.zaproxy.addon.commonlib.ExtensionCommonlib;
import org.zaproxy.zap.extension.script.ExtensionScript;
import org.zaproxy.zap.model.DefaultValueGenerator;
import org.zaproxy.zap.model.ValueGenerator;
import org.zaproxy.zap.view.ZapMenuItem;

public class ExtensionGraphQl extends ExtensionAdaptor
implements CommandLineListener, SessionChangedListener {

public static final String NAME = "ExtensionGraphQl";

private static final List<Class<? extends Extension>> DEPENDENCIES =
List.of(ExtensionCommonlib.class);

static final int TOOL_ALERT_ID = 50007;
private static final Logger LOGGER = LogManager.getLogger(ExtensionGraphQl.class);

Expand All @@ -60,20 +65,20 @@ public class ExtensionGraphQl extends ExtensionAdaptor
private static final int ARG_IMPORT_URL_IDX = 1;
private static final int ARG_END_URL_IDX = 2;

private ValueGenerator valueGenerator;

public ExtensionGraphQl() {
super(NAME);

setValueGenerator(null);
}

public void setValueGenerator(ValueGenerator valueGenerator) {
this.valueGenerator = valueGenerator == null ? new DefaultValueGenerator() : valueGenerator;
@Override
public List<Class<? extends Extension>> getDependencies() {
return DEPENDENCIES;
}

ValueGenerator getValueGenerator() {
return valueGenerator;
return Control.getSingleton()
.getExtensionLoader()
.getExtension(ExtensionCommonlib.class)
.getValueGenerator();
}

@Override
Expand Down
Loading

0 comments on commit 7ae219f

Please sign in to comment.