Completed
Push — master ( 86dfcb...7c8092 )
by -
01:41
created

del_sid()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125
Metric Value
dl 0
loc 3
ccs 1
cts 2
cp 0.5
rs 10
cc 1
crap 1.125
1 1
try:
2 1
    from urlparse import parse_qs
3
except ImportError:  # python3
4
    from urllib.parse import parse_qs
5 1
import logging
6 1
from time import time
7
8 1
from flask import Blueprint, render_template, request, redirect, flash, Response, url_for
9 1
from sqlalchemy.exc import SQLAlchemyError
0 ignored issues
show
Configuration introduced by
The import sqlalchemy.exc could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
10
11 1
from spike.model import db
12 1
from spike.model.naxsi_whitelist import NaxsiWhitelist
13 1
from spike.model.naxsi_whitelistsets import NaxsiWhitelistSets
14 1
from spike.model.naxsi_rules import NaxsiRules
15 1
from spike.model import naxsi_mz
16
17 1
whitelists = Blueprint('whitelists', __name__)
18
19
20 1
@whitelists.route("/")
21
def index():
22 1
    _wlist = NaxsiWhitelist.query.order_by(NaxsiWhitelist.wid.desc()).all()
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelist does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
23 1
    if not _wlist:
24 1
        flash("No whitelist found, please create one", "success")
25 1
        return redirect(url_for('whitelists.new'))
26
    return render_template("whitelists/index.html", whitelists=_wlist)
27
28
29 1
@whitelists.route("/plain/<string:wid>", methods=["GET"])
30
def plain(wid):
31
    _wlist = NaxsiWhitelist.query.filter(NaxsiRules.sid == wid).first()
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelist does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
32
    if not _wlist:
33
        flash("no rules found, please create one", "error")
34
        return redirect(url_for('whitelists.index'))
35
    return Response(_wlist.fullstr(), mimetype='text/plain')
36
37
38 1
@whitelists.route("/view/<string:wid>", methods=["GET"])
39
def view(wid):
40
    _wlist = NaxsiWhitelist.query.filter(NaxsiRules.sid == wid).first()
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelist does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
41
    if _wlist is None:
42
        flash("no rules found, please create one", "error")
43
        return redirect(url_for('whitelists.index'))
44
    return render_template("rules/view.html", rule=_wlist, rtext=_wlist)
45
46
47 1
@whitelists.route("/edit/<string:wid>", methods=["GET"])
48
def edit(wid):
49
    return redirect(url_for('whitelists.new'))
50
51
52 1
@whitelists.route("/explain/", methods=["GET", "POST"])
53
def explain():
54
    return redirect(url_for('whitelists.new'))
55
56
57 1
@whitelists.route("/del/<string:wid>", methods=["GET"])
58
def del_sid(wid):
59
    return redirect(url_for('whitelists.new'))
60
61
62 1
@whitelists.route("/generate", methods=["GET", "POST"])
63
def generate():
64 1
    if request.method == "GET":
65 1
        return render_template("misc/whitelist_generator.html")
66 1
    nxlogs = request.form.get('nxlogs', '')
67
68 1
    if not nxlogs:
69 1
        flash('Please input nxlogs')
70 1
        return render_template("misc/whitelist_generator.html")
71
72 1
    whitelist = list()
73 1
    for nxlog in nxlogs.split('\n'):
74 1
        if not nxlog:
75
            continue
76 1
        start = nxlog.find("ip=")
77 1
        if start < 0:
78 1
            flash('{} is an invalid extlog, string "ip=" not found.'.format(nxlog))
79 1
            return render_template("misc/whitelist_generator.html", nxlogs=nxlogs)
80
81 1
        end = nxlog.find(", ")
82 1
        if end < 0:
83 1
            flash('{} is an invalid extlog, string "," not found.'.format(nxlog))
84 1
            return render_template("misc/whitelist_generator.html", nxlogs=nxlogs)
85
86
        # Flatten the dict, since parse_qs is a bit annoying
87 1
        nxdic = parse_qs(nxlog[start:end])
88 1
        for key, value in nxdic.items():
89 1
            nxdic[key] = value[0]
90
91 1
        cpt = 0
92 1
        while "id{}".format(cpt) in nxdic:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93 1
            _id = "id{}".format(cpt)
94 1
            _var_name = "var_name{}".format(cpt)
95 1
            _zone = "zone{}".format(cpt)
96 1
            if "var_name{}".format(cpt) in nxdic:
97 1
                whitelist.append('BasicRule wl:{} "mz:{}:{}"'.format(nxdic[_id], nxdic[_var_name], nxdic[_zone]))
98
            else:
99
                whitelist.append('BasicRule wl:{} "mz:{}"'.format(nxdic[_id], nxdic[_var_name]))
100 1
            cpt += 1
101 1
    return render_template("misc/whitelist_generator.html", whitelist='\n'.join(whitelist) + ';', nxlogs=nxlogs)
102
103
104 1
@whitelists.route('/new', methods=["GET", "POST"])
105
def new():
106 1
    if request.method == "GET":
107 1
        _whitelistesets = NaxsiWhitelistSets.query.all()
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelistSets does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
108 1
        return render_template('whitelists/new.html', matchzones=naxsi_mz, whitelistsets=_whitelistesets)
109
110
    logging.debug('Posted new request: %s', request.form)
111
    mz = "|".join(filter(len, request.form.getlist("mz") + request.form.getlist("custom_mz_val")))
112
113
    score = request.form.get("score", "")
0 ignored issues
show
Unused Code introduced by
The variable score seems to be unused.
Loading history...
114
    score += ':'
115
    score += request.form.get("score_%s" % request.form.get("score", ""), "")
116
117
    wlist = NaxsiWhitelist(wid=request.form.get("id", ""), timestamp=int(time()),
118
                            whitelistset=request.form.get("whitelistset", ""), mz=mz, active=1,
119
                            negative=request.form.get("negative", "") == 'checked')
120
121
    wlist.validate()
0 ignored issues
show
Bug introduced by
The Instance of NaxsiWhitelist does not seem to have a member named validate.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
122
123
    if wlist.error:
124
        flash("ERROR: {0}".format(",".join(wlist.error)))
125
        return redirect("/rules/new")
126
    elif wlist.warnings:
127
        flash("WARNINGS: {0}".format(",".join(wlist.warnings)))
128
    db.session.add(wlist)
129
130
    try:
131
        db.session.commit()
132
        flash('Created!')
133
    except SQLAlchemyError as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ([a-z_][a-z0-9_]{1,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
134
        flash("Error : %s" % e, "error")
135
136
    return redirect(url_for('whitelists.index'))
137