<commit_before>from flask import Flask, redirect, render_template, request, Response, url_for
from flask.globals import session
import functools
import random
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import joinedload
from sqlalchemy.orm.exc import NoResultFound
import string
from werkzeug import abort

from internetpoints import config, models
from internetpoints.storage import Session


# Setup Flask
app = Flask('internetpoints')
app.config.update(config.__dict__)


# CSRF protection

def random_string(size=20, characters=string.ascii_uppercase +
                                      string.ascii_lowercase +
                                      string.digits):
    return ''.join(random.choice(characters) for i in xrange(size))


@app.before_request
def csrf_protect():
    if request.method == "POST":
        token = session.pop('_csrf_token', None)
        if not token or token != request.form.get('_csrf_token'):
            abort(400)


def generate_csrf_token():
    if '_csrf_token' not in session:
        session['_csrf_token'] = random_string()
    return session['_csrf_token']


app.jinja_env.globals['csrf_token'] = generate_csrf_token


# Authentication
<commit_msg>Fixes issue with CSRF protection<commit_after>from flask import Flask, redirect, render_template, request, Response, url_for
from flask.globals import session
import functools
import random
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import joinedload
from sqlalchemy.orm.exc import NoResultFound
import string
from werkzeug import abort

from internetpoints import config, models
from internetpoints.storage import Session


# Setup Flask
app = Flask('internetpoints')
app.config.update(config.__dict__)


# CSRF protection

def random_string(size=20, characters=string.ascii_uppercase +
                                      string.ascii_lowercase +
                                      string.digits):
    return ''.join(random.choice(characters) for i in xrange(size))


@app.before_request
def csrf_protect():
    if request.method == "POST":
        token = session.get('_csrf_token', None)
        if not token or token != request.form.get('_csrf_token'):
            abort(400)


def generate_csrf_token():
    if '_csrf_token' not in session:
        session['_csrf_token'] = random_string()
    return session['_csrf_token']


app.jinja_env.globals['csrf_token'] = generate_csrf_token


# Authentication

{'fraction_non_alphanumeric': 0.08336738863914998, 'fraction_numerical': 0.004086636697997548, 'mean_word_length': 3.558659217877095, 'pattern_counts': {'":': 2, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4187824', 'n_tokens_mistral': 736, 'n_tokens_neox': 701, 'n_words': 188}