/*
 * Copyright (C) 2015 Dominik Schadow, dominikschadow@gmail.com
 *
 * This file is part of the Application Intrusion Detection project.
 *
 * 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 de.dominikschadow.duke.encounters.validators;

import de.dominikschadow.duke.encounters.Constants;
import de.dominikschadow.duke.encounters.domain.Likelihood;
import de.dominikschadow.duke.encounters.domain.SearchFilter;
import de.dominikschadow.duke.encounters.services.SecurityValidationService;
import de.dominikschadow.duke.encounters.services.UserService;
import de.dominikschadow.duke.encounters.spring.Loggable;
import org.owasp.appsensor.core.DetectionPoint;
import org.owasp.appsensor.core.DetectionSystem;
import org.owasp.appsensor.core.Event;
import org.owasp.appsensor.core.event.EventManager;
import org.owasp.security.logging.SecurityMarkers;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.SpringValidatorAdapter;

import javax.inject.Named;

import static org.owasp.appsensor.core.DetectionPoint.Category.*;

/**
 * Validates a search filter: scans for basic Cross-Site Scripting and SQL Injection payload.
 *
 * @author Dominik Schadow
 */
@Named
public class SearchFilterValidator implements Validator {
    @Loggable
    private Logger logger;

    @Autowired
    private SpringValidatorAdapter validator;
    @Autowired
    private EventManager ids;
    @Autowired
    private UserService userService;
    @Autowired
    private SecurityValidationService securityValidationService;
    @Autowired
    private DetectionSystem detectionSystem;

    @Override
    public boolean supports(Class<?> clazz) {
        return SearchFilter.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        validator.validate(target, errors);

        SearchFilter filter = (SearchFilter) target;

        if (securityValidationService.hasXssPayload(filter.getEvent())) {
            fireXssEvent();
            errors.rejectValue("event", Constants.XSS_ERROR_CODE);
        } else if (securityValidationService.hasSqlIPayload(filter.getEvent())) {
            fireSqlIEvent();
            errors.rejectValue("event", Constants.SQLI_ERROR_CODE);
        }

        if (securityValidationService.hasXssPayload(filter.getLocation())) {
            fireXssEvent();
            errors.rejectValue("location", Constants.XSS_ERROR_CODE);
        } else if (securityValidationService.hasSqlIPayload(filter.getLocation())) {
            fireSqlIEvent();
            errors.rejectValue("location", Constants.SQLI_ERROR_CODE);
        }

        if (securityValidationService.hasXssPayload(filter.getCountry())) {
            fireXssEvent();
            errors.rejectValue("country", Constants.XSS_ERROR_CODE);
        } else if (securityValidationService.hasSqlIPayload(filter.getCountry())) {
            fireSqlIEvent();
            errors.rejectValue("country", Constants.SQLI_ERROR_CODE);
        }

        if (securityValidationService.hasXssPayload(filter.getYear())) {
            fireXssEvent();
            errors.rejectValue("year", Constants.XSS_ERROR_CODE);
        } else if (securityValidationService.hasSqlIPayload(filter.getYear())) {
            fireSqlIEvent();
            errors.rejectValue("year", Constants.SQLI_ERROR_CODE);
        } else {
            try {
                int year = Integer.parseInt(filter.getYear());

                if (year < 1995) {
                    logger.info(SecurityMarkers.SECURITY_FAILURE, "Requested {} as event year - possible typo", filter
                            .getYear());
                    errors.rejectValue("year", Constants.INVALID_YEAR_ERROR_CODE);
                }
            } catch (NumberFormatException ex) {
                errors.rejectValue("year", Constants.INVALID_YEAR_ERROR_CODE);
            }
        }

        try {
            Likelihood.fromString(filter.getLikelihood());
        } catch (IllegalArgumentException ex) {
            logger.info(SecurityMarkers.SECURITY_FAILURE, "Requested {} as likelihood - out of configured range",
                    filter.getLikelihood());
            fireInvalidValueEvent();
            errors.rejectValue("likelihood", Constants.ATTACK_ERROR_CODE);
        }

        if (filter.getConfirmations() < 0 || filter.getConfirmations() > 10) {
            logger.info(SecurityMarkers.SECURITY_FAILURE, "Requested {} confirmations - out of configured range",
                    filter.getConfirmations());
            fireInvalidValueEvent();
            errors.rejectValue("confirmations", Constants.ATTACK_ERROR_CODE);
        }
    }

    private void fireInvalidValueEvent() {
        DetectionPoint detectionPoint = new DetectionPoint(ACCESS_CONTROL, "ACE2-001");
        ids.addEvent(new Event(userService.getUser(), detectionPoint, detectionSystem));
    }

    private void fireXssEvent() {
        DetectionPoint detectionPoint = new DetectionPoint(INPUT_VALIDATION, "IE1-001");
        ids.addEvent(new Event(userService.getUser(), detectionPoint, detectionSystem));
    }

    private void fireSqlIEvent() {
        DetectionPoint detectionPoint = new DetectionPoint(COMMAND_INJECTION, "CIE1-001");
        ids.addEvent(new Event(userService.getUser(), detectionPoint, detectionSystem));
    }
}

@@ -746,16 +746,55 @@
 ators;%0A%0A
+import com.google.common.base.Strings;%0A
 import d
@@ -4034,16 +4034,80 @@
  else %7B%0A
+            if (!Strings.isNullOrEmpty(filter.getYear())) %7B%0A    
         
@@ -4132,16 +4132,20 @@
         
+    
 int year
@@ -4196,24 +4196,28 @@
             
+    
 if (year %3C 1
@@ -4219,24 +4219,28 @@
 r %3C 1995) %7B%0A
+    
             
@@ -4338,23 +4338,16 @@
 e typo%22,
- filter
 %0A       
@@ -4367,16 +4367,26 @@
         
+    filter
 .getYear
@@ -4402,32 +4402,36 @@
                 
+   
+ 
 errors.rejectVal
@@ -4485,34 +4485,41 @@
                 
-%7D%0A
+    %7D%0A   
             %7D ca
@@ -4506,32 +4506,33 @@
 %0A               
+ 
 %7D catch (NumberF
@@ -4544,32 +4544,36 @@
 Exception ex) %7B%0A
+    
                 
@@ -4627,32 +4627,50 @@
 AR_ERROR_CODE);%0A
+                %7D%0A
             %7D%0A  

Ignore year if empty