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

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.control.Control;
import org.parosproxy.paros.core.scanner.Alert;
import org.parosproxy.paros.db.DatabaseException;
import org.parosproxy.paros.network.HttpMalformedHeaderException;
import org.parosproxy.paros.network.HttpMessage;
import org.zaproxy.zap.extension.accessControl.AccessControlScannerThread.AccessControlNodeResult;
import org.zaproxy.zap.extension.accessControl.AccessControlScannerThread.AccessControlResultEntry;
import org.zaproxy.zap.extension.accessControl.AccessControlScannerThread.AccessControlScanStartOptions;
import org.zaproxy.zap.extension.alert.ExtensionAlert;
import org.zaproxy.zap.model.Vulnerabilities;
import org.zaproxy.zap.model.Vulnerability;

/**
 * The object that processes obtained scan results and raises {@link Alert Alerts}, if necessary.
 *
 * @author cosminstefanxp
 */
public class AccessControlAlertsProcessor {
    private static final Logger log = LogManager.getLogger(AccessControlAlertsProcessor.class);

    private static Vulnerability vulnerabilityAuthentication =
            Vulnerabilities.getVulnerability("wasc_1");
    private static Vulnerability vulnerabilityAuthorization =
            Vulnerabilities.getVulnerability("wasc_2");

    private static final String AUTHENTICATION_ALERT_TITLE =
            Constant.messages.getString("accessControl.alert.authentication.name");
    private static final String AUTHORIZATION_ALERT_TITLE =
            Constant.messages.getString("accessControl.alert.authorization.name");

    private ExtensionAlert alertExtension;
    private boolean shouldRun;
    private int alertRiskLevel;

    public AccessControlAlertsProcessor(AccessControlScanStartOptions scanOptions) {
        this.alertExtension =
                (ExtensionAlert)
                        Control.getSingleton()
                                .getExtensionLoader()
                                .getExtension(ExtensionAlert.NAME);
        this.shouldRun = alertExtension != null && scanOptions.raiseAlerts;
        this.alertRiskLevel = scanOptions.alertRiskLevel;
    }

    public void processScanResult(AccessControlResultEntry result) {
        if (!shouldRun || result.getResult() != AccessControlNodeResult.ILLEGAL) {
            return;
        }

        log.debug("Raising alert for: {}", result);

        if (result.getUser() == null) {
            raiseAuthenticationAlert(result);
        } else {
            raiseAuthorizationAlert(result);
        }
    }

    private void raiseAuthorizationAlert(AccessControlResultEntry result) {
        Alert alert =
                new Alert(10102, alertRiskLevel, Alert.CONFIDENCE_HIGH, AUTHORIZATION_ALERT_TITLE);

        HttpMessage msg = null;
        try {
            msg = result.getHistoryReference().getHttpMessage();
        } catch (HttpMalformedHeaderException | DatabaseException e) {
            log.debug(e);
        }
        // @formatter:off
        alert.setDetail(
                Vulnerabilities.getDescription(vulnerabilityAuthorization),
                result.getUri(),
                "", // Param
                "", // Attack
                Constant.messages.getString(
                        "accessControl.alert.authorization.otherinfo",
                        result.getUser().getName(),
                        result.isRequestAuthorized(),
                        result.getAccessRule()),
                Vulnerabilities.getSolution(vulnerabilityAuthorization), // Solution
                Vulnerabilities.getReference(vulnerabilityAuthorization), // Reference
                "", // Evidence
                285, // CWE Id
                2, // WASC Id
                msg);
        // @formatter:on

        alertExtension.alertFound(alert, result.getHistoryReference());
    }

    private void raiseAuthenticationAlert(AccessControlResultEntry result) {
        Alert alert =
                new Alert(10101, alertRiskLevel, Alert.CONFIDENCE_HIGH, AUTHENTICATION_ALERT_TITLE);

        HttpMessage msg = null;
        try {
            msg = result.getHistoryReference().getHttpMessage();
        } catch (HttpMalformedHeaderException | DatabaseException e) {
            log.debug(e);
        }
        // @formatter:off
        alert.setDetail(
                Vulnerabilities.getDescription(vulnerabilityAuthentication),
                result.getUri(),
                "", // Param
                "", // Attack
                Constant.messages.getString(
                        "accessControl.alert.authentication.otherinfo",
                        result.isRequestAuthorized(),
                        result.getAccessRule()),
                Vulnerabilities.getSolution(vulnerabilityAuthentication), // Solution
                Vulnerabilities.getReference(vulnerabilityAuthentication), // Reference
                "", // Evidence
                287, // CWE Id
                1, // WASC Id
                msg);
        // @formatter:on

        alertExtension.alertFound(alert, result.getHistoryReference());
    }
}

@@ -3802,34 +3802,8 @@
   %7D%0A
-        // @formatter:off%0A
     
@@ -3815,32 +3815,32 @@
 lert.setDetail(%0A
+
                 
@@ -4552,33 +4552,8 @@
 sg);
-%0A        // @formatter:on
 %0A%0A  
@@ -5052,34 +5052,8 @@
   %7D%0A
-        // @formatter:off%0A
     
@@ -5721,32 +5721,32 @@
   1, // WASC Id%0A
+
                 
@@ -5754,33 +5754,8 @@
 sg);
-%0A        // @formatter:on
 %0A%0A  

remove formatter related comments