<commit_before>package org.javalite.activeweb.controller_filters;

import org.javalite.activeweb.CSRF;
import org.javalite.activeweb.HttpMethod;

public class CSRFFilter extends HttpSupportFilter {


    public CSRFFilter() {
        CSRF.enableVerification();
    }

    @Override
    public void before() {
        HttpMethod method = getRoute().getMethod();
        if (method == HttpMethod.POST || method == HttpMethod.DELETE || method == HttpMethod.PUT) {
            verify();
        }
    }

    private void verify() {
        String sessionToken = sessionString(CSRF.PARAMETER_NAME);
        if (sessionToken != null) {
            String token = param(CSRF.PARAMETER_NAME);
            if (token == null) {
                token = header(CSRF.HTTP_HEADER_NAME);
            }
            if (token == null) {
                throw new SecurityException("CSRF attack detected! Token not found!");
            }
            if (!sessionToken.equals(token)) {
                throw new SecurityException("CSRF attack detected! Request token is not valid!");
            }
        } else {
            throw new SecurityException("CSRF protection, session token missing!");
        }
    }
}<commit_msg>copy changes<commit_after>package org.javalite.activeweb.controller_filters;

import org.javalite.activeweb.CSRF;
import org.javalite.activeweb.HttpMethod;

public class CSRFFilter extends HttpSupportFilter {


    public CSRFFilter() {
        CSRF.enableVerification();
    }

    @Override
    public void before() {
        HttpMethod method = getRoute().getMethod();
        if (method == HttpMethod.POST || method == HttpMethod.DELETE || method == HttpMethod.PUT) {
            verify();
        }
    }

    private void verify() {
        String sessionToken = sessionString(CSRF.PARAMETER_NAME);
        if (sessionToken != null) {
            String token = param(CSRF.PARAMETER_NAME);
            if (token == null) {
                token = header(CSRF.HTTP_HEADER_NAME);
            }
            if (token == null) {
                throw new SecurityException("CSRF attack detected! Token not found!");
            }
            if (!sessionToken.equals(token)) {
                throw new SecurityException("CSRF attack detected! Request token is not valid!");
            }
        } else {
            throw new SecurityException("CSRF attack detected! Session token missing!");
        }
    }
}
{'fraction_non_alphanumeric': 0.10313531353135313, 'fraction_numerical': 0.0, 'mean_word_length': 2.17824377457405, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3144102', 'n_tokens_mistral': 637, 'n_tokens_neox': 627, 'n_words': 156}