<commit_before>/*
 * Zed Attack Proxy (ZAP) and its related class files.
 *
 * ZAP is an HTTP/HTTPS proxy for assessing web application security.
 *
 * Copyright 2021 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.spiderAjax.automation;

import java.net.URI;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.control.Control;
import org.parosproxy.paros.model.HistoryReference;
import org.parosproxy.paros.model.Model;
import org.parosproxy.paros.network.HttpMessage;
import org.zaproxy.addon.automation.AutomationEnvironment;
import org.zaproxy.addon.automation.AutomationJob;
import org.zaproxy.addon.automation.AutomationProgress;
import org.zaproxy.addon.automation.ContextWrapper;
import org.zaproxy.zap.extension.spiderAjax.AjaxSpiderTarget;
import org.zaproxy.zap.extension.spiderAjax.ExtensionAjax;
import org.zaproxy.zap.extension.spiderAjax.SpiderListener;
import org.zaproxy.zap.extension.spiderAjax.SpiderThread;

public class AjaxSpiderJob extends AutomationJob {

    private static final String JOB_NAME = "spiderAjax";
    private static final String OPTIONS_METHOD_NAME = "getAjaxSpiderParam";

    private static final String PARAM_CONTEXT = "context";
    private static final String PARAM_URL = "url";
    private static final String PARAM_FAIL_IF_LESS_URLS = "failIfFoundUrlsLessThan";
    private static final String PARAM_WARN_IF_LESS_URLS = "warnIfFoundUrlsLessThan";
    private static final String PARAM_MAX_DURATION = "maxDuration";

    private ExtensionAjax extSpider;

    private int failIfFoundUrlsLessThan = 0;
    private int warnIfFoundUrlsLessThan = 0;

    // Local copy
    private int maxDuration = 0;

    private String contextName;
    private String url;

    private boolean inScopeOnly = true;

    public AjaxSpiderJob() {}

    private ExtensionAjax getExtSpider() {
        if (extSpider == null) {
            extSpider =
                    Control.getSingleton().getExtensionLoader().getExtension(ExtensionAjax.class);
        }
        return extSpider;
    }

    public boolean applyCustomParameter(String name, String value) {
        switch (name) {
            case PARAM_CONTEXT:
                contextName = value;
                return true;
            case PARAM_URL:
                url = value;
                return true;
            case PARAM_FAIL_IF_LESS_URLS:
                failIfFoundUrlsLessThan = Integer.parseInt(value);
                return true;
            case PARAM_WARN_IF_LESS_URLS:
                warnIfFoundUrlsLessThan = Integer.parseInt(value);
                return true;
            case PARAM_MAX_DURATION:
                maxDuration = Integer.parseInt(value);
                // Don't consume this as we still want it to be applied to the spider params
                return false;
            default:
                // Ignore
                break;
        }
        return false;
    }

    @Override
    public Map<String, String> getCustomConfigParameters() {
        Map<String, String> map = super.getCustomConfigParameters();
        map.put(PARAM_CONTEXT, "");
        map.put(PARAM_URL, "");
        map.put(PARAM_FAIL_IF_LESS_URLS, "0");
        map.put(PARAM_WARN_IF_LESS_URLS, "0");
        return map;
    }

    @Override
    public void runJob(
            AutomationEnvironment env, LinkedHashMap<?, ?> jobData, AutomationProgress progress) {

        ContextWrapper context;
        if (contextName != null) {
            context = env.getContextWrapper(contextName);
            if (context == null) {
                progress.error(
                        Constant.messages.getString(
                                "automation.error.context.unknown", contextName));
                return;
            }
        } else {
            context = env.getDefaultContextWrapper();
        }

        String uriStr = url;
        if (uriStr == null) {
            uriStr = context.getUrls().get(0);
        }
        URI uri = null;
        try {
            uri = new URI(url);
        } catch (Exception e1) {
            progress.error(Constant.messages.getString("automation.error.context.badurl", uriStr));
            return;
        }

        AjaxSpiderTarget.Builder targetBuilder =
                AjaxSpiderTarget.newBuilder(Model.getSingleton().getSession())
                        .setContext(context.getContext())
                        .setInScopeOnly(inScopeOnly)
                        .setOptions(getExtSpider().getAjaxSpiderParam())
                        .setStartUri(uri)
                        .setSubtreeOnly(false);

        AjaxSpiderTarget target = targetBuilder.build();
        JobSpiderListener listener = new JobSpiderListener();

        SpiderThread spiderThread =
                getExtSpider()
                        .createSpiderThread(
                                "Auto - " + getExtSpider().createDisplayName(target),<commit_msg>Ajax Spider - fix automation context url<commit_after>/*
 * Zed Attack Proxy (ZAP) and its related class files.
 *
 * ZAP is an HTTP/HTTPS proxy for assessing web application security.
 *
 * Copyright 2021 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.spiderAjax.automation;

import java.net.URI;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.control.Control;
import org.parosproxy.paros.model.HistoryReference;
import org.parosproxy.paros.model.Model;
import org.parosproxy.paros.network.HttpMessage;
import org.zaproxy.addon.automation.AutomationEnvironment;
import org.zaproxy.addon.automation.AutomationJob;
import org.zaproxy.addon.automation.AutomationProgress;
import org.zaproxy.addon.automation.ContextWrapper;
import org.zaproxy.zap.extension.spiderAjax.AjaxSpiderTarget;
import org.zaproxy.zap.extension.spiderAjax.ExtensionAjax;
import org.zaproxy.zap.extension.spiderAjax.SpiderListener;
import org.zaproxy.zap.extension.spiderAjax.SpiderThread;

public class AjaxSpiderJob extends AutomationJob {

    private static final String JOB_NAME = "spiderAjax";
    private static final String OPTIONS_METHOD_NAME = "getAjaxSpiderParam";

    private static final String PARAM_CONTEXT = "context";
    private static final String PARAM_URL = "url";
    private static final String PARAM_FAIL_IF_LESS_URLS = "failIfFoundUrlsLessThan";
    private static final String PARAM_WARN_IF_LESS_URLS = "warnIfFoundUrlsLessThan";
    private static final String PARAM_MAX_DURATION = "maxDuration";

    private ExtensionAjax extSpider;

    private int failIfFoundUrlsLessThan = 0;
    private int warnIfFoundUrlsLessThan = 0;

    // Local copy
    private int maxDuration = 0;

    private String contextName;
    private String url;

    private boolean inScopeOnly = true;

    public AjaxSpiderJob() {}

    private ExtensionAjax getExtSpider() {
        if (extSpider == null) {
            extSpider =
                    Control.getSingleton().getExtensionLoader().getExtension(ExtensionAjax.class);
        }
        return extSpider;
    }

    public boolean applyCustomParameter(String name, String value) {
        switch (name) {
            case PARAM_CONTEXT:
                contextName = value;
                return true;
            case PARAM_URL:
                url = value;
                return true;
            case PARAM_FAIL_IF_LESS_URLS:
                failIfFoundUrlsLessThan = Integer.parseInt(value);
                return true;
            case PARAM_WARN_IF_LESS_URLS:
                warnIfFoundUrlsLessThan = Integer.parseInt(value);
                return true;
            case PARAM_MAX_DURATION:
                maxDuration = Integer.parseInt(value);
                // Don't consume this as we still want it to be applied to the spider params
                return false;
            default:
                // Ignore
                break;
        }
        return false;
    }

    @Override
    public Map<String, String> getCustomConfigParameters() {
        Map<String, String> map = super.getCustomConfigParameters();
        map.put(PARAM_CONTEXT, "");
        map.put(PARAM_URL, "");
        map.put(PARAM_FAIL_IF_LESS_URLS, "0");
        map.put(PARAM_WARN_IF_LESS_URLS, "0");
        return map;
    }

    @Override
    public void runJob(
            AutomationEnvironment env, LinkedHashMap<?, ?> jobData, AutomationProgress progress) {

        ContextWrapper context;
        if (contextName != null) {
            context = env.getContextWrapper(contextName);
            if (context == null) {
                progress.error(
                        Constant.messages.getString(
                                "automation.error.context.unknown", contextName));
                return;
            }
        } else {
            context = env.getDefaultContextWrapper();
        }

        String uriStr = url;
        if (uriStr == null) {
            uriStr = context.getUrls().get(0);
        }
        URI uri = null;
        try {
            uri = new URI(uriStr);
        } catch (Exception e1) {
            progress.error(Constant.messages.getString("automation.error.context.badurl", uriStr));
            return;
        }

        AjaxSpiderTarget.Builder targetBuilder =
                AjaxSpiderTarget.newBuilder(Model.getSingleton().getSession())
                        .setContext(context.getContext())
                        .setInScopeOnly(inScopeOnly)
                        .setOptions(getExtSpider().getAjaxSpiderParam())
                        .setStartUri(uri)
                        .setSubtreeOnly(false);

        AjaxSpiderTarget target = targetBuilder.build();
        JobSpiderListener listener = new JobSpiderListener();

        SpiderThread spiderThread =
                getExtSpider()
                        .createSpiderThread(
                                "Auto - " + getExtSpider().createDisplayName(target),
{'fraction_non_alphanumeric': 0.08115604867573371, 'fraction_numerical': 0.002684323550465283, 'mean_word_length': 2.354441776710684, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1094881', 'n_tokens_mistral': 3111, 'n_tokens_neox': 3014, 'n_words': 746}