Completed
Push — master ( 7b0c38...43a11b )
by -
01:39
created

explain_whitelist()   C

Complexity

Conditions 7

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 10.8579
Metric Value
dl 0
loc 24
ccs 12
cts 21
cp 0.5714
rs 5.5
cc 7
crap 10.8579
1 1
try:
2 1
    from urlparse import parse_qs
3
except ImportError:  # python3
4
    from urllib.parse import parse_qs
5
6 1
from flask import Blueprint, render_template, request, redirect, flash
7
8 1
from spike.model.naxsi_rules import NaxsiRules
9 1
from spike.model.naxsi_whitelist import NaxsiWhitelist
10
11 1
sandbox = Blueprint('sandbox', __name__)
12
13
14 1
@sandbox.route("/", methods=["GET"])
15
def index():
16 1
    return render_template("misc/sandbox.html")
17
18
19 1
@sandbox.route("/rule", methods=["POST"])
20
def rule():
21 1
    _textual_rule = request.form.get("rule", '')
22 1
    if not _textual_rule:
23 1
        return render_template("misc/sandbox.html")
24
25 1
    _rule = NaxsiRules()
26 1
    _rule.parse_rule(_textual_rule)
27
28 1
    if 'visualise_rule' in request.form:
29 1
        if _rule.detection.startswith('rx:'):
30 1
            return redirect('https://regexper.com/#' + _rule.detection[3:])
31 1
    elif 'explain_rule' in request.form:
32 1
        return render_template("misc/sandbox.html", rule_explaination=_rule.explain(), rule=_rule)
33
34
    if _rule.error:
35
        flash("ERROR: {0}".format(",".join(_rule.error)))
36
    if _rule.warnings:
37
        flash("WARNINGS: {0}".format(",".join(_rule.warnings)), 'warning')
38
    return render_template("misc/sandbox.html", rule=_rule)
39
40
41 1
@sandbox.route("/explain_rule/", methods=["GET", "POST"])
42
def explain_rule():
43 1
    rule_get = request.args.get('rule', '')
44 1
    rule_post = request.form.get("rule", '')
45 1
    if rule_get.isdigit():  # explain a rule by id
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...
46 1
        _rule = NaxsiRules.query.filter(NaxsiRules.sid == rule_get).first()
0 ignored issues
show
Bug introduced by
The Class NaxsiRules 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...
47 1
        if _rule is None:
48 1
            flash('Not rule with id %s' % rule_get)
49 1
            return redirect("/sandbox/")
50 1
    elif rule_get is not '':
51 1
        flash('Please provide a numeric id')
52 1
        return redirect("/sandbox/")
53 1
    elif not rule_post:
54 1
        flash('Please provide a rule')
55 1
        return redirect("/sandbox/")
56
    else:
57 1
        _rule = NaxsiRules()
58 1
        _rule.parse_rule(rule_post)
59
60 1
    return render_template("misc/sandbox.html", rule_explaination=_rule.explain(), rule=_rule)
61
62
63 1
@sandbox.route("/explain_whitelist/", methods=["GET", "POST"])
64
def explain_whitelist():
65 1
    whitelist_get = request.args.get('whitelist', '')
66 1
    whitelist_post = request.form.get('whitelist', '')
67 1
    if whitelist_get.isdigit():  # explain a whitelist by id
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...
68
        _wlist = NaxsiWhitelist.query.filter(NaxsiWhitelist.id == whitelist_get).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...
69
        if _wlist is None:
70
            flash('Not rule with id %s' % whitelist_get.id)
71
            return redirect("/sandbox/")
72 1
    elif whitelist_get is not '':
73
        flash('Please provide a numeric id')
74
        return redirect("/sandbox/")
75 1
    elif not whitelist_post:
76
        flash('Please provide a whitelist')
77
        return redirect("/sandbox/")
78
    else:
79 1
        _wlist = NaxsiWhitelist()
80 1
        _wlist.parse(whitelist_post)
81
82 1
    if _wlist.error:
83 1
        flash("ERROR: {0}".format(",".join(_wlist.error)))
84 1
    if _wlist.warnings:
85
        flash("WARNINGS: {0}".format(",".join(_wlist.warnings)), 'warning')
86 1
    return render_template("misc/sandbox.html", whitelist_explaination=_wlist.explain(), whitelist=_wlist)
87
88
89 1
@sandbox.route('/explain_nxlog/', methods=["POST"])
90
def explain_nxlog():
91 1
    nxlog = request.form.get("nxlog", '')
92 1
    if not nxlog:
93 1
        return redirect("/sandbox/")
94
95 1
    start = nxlog.find("ip=")
96 1
    if start < 0:
97 1
        flash('{} is an invalid extlog, string "ip=" not found.'.format(nxlog))
98 1
        return redirect("/sandbox/")
99
100 1
    end = nxlog.find(", ")
101 1
    if end < 0:
102 1
        flash('{} is an invalid extlog, string "," not found.'.format(nxlog))
103 1
        return redirect("/sandbox/")
104
105
    # Flatten the dict, since parse_qs is a bit annoying
106 1
    nxdic = parse_qs(nxlog[start:end])
107 1
    for key, value in nxdic.items():
108 1
        nxdic[key] = value[0]
109
110 1
    explain = "Peer <strong>{}</strong> performed a request to <strong>{}</strong> on URI <strong>{}</strong> ".format(
111
        nxdic['ip'], nxdic['server'], nxdic['uri'])
112
113 1
    scores = list()
114 1
    cpt = 0
115 1
    while "cscore{}".format(cpt) in nxdic:
116 1
        cscore = "cscore{}".format(cpt)
117 1
        score = "score{}".format(cpt)
118 1
        scores.append("that reached a <strong>{}</strong> score of <strong>{}</strong> ".format(
119
            nxdic[cscore], nxdic[score]))
120 1
        cpt += 1
121 1
    explain += ' and '.join(scores)
122
123 1
    cpt = 0
124 1
    named = list()
125 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...
126 1
        _id = "id{}".format(cpt)
127 1
        _var_name = "var_name{}".format(cpt)
128 1
        _zone = "zone{}".format(cpt)
129 1
        if "var_name{}".format(cpt) in nxdic:
130 1
            named.append("id <strong>{}</strong> in var named <strong>{}</strong> of zone <strong>{}</strong>".format(
131
                nxdic[_id], nxdic[_var_name], nxdic[_zone]))
132
        else:
133
            named.append("id <strong>{}</strong> in zone <strong>{}</strong>".format(nxdic[_id], nxdic[_zone]))
134 1
        cpt += 1
135 1
    explain += ' and '.join(named)
136
137
    return render_template("misc/sandbox.html", nxlog_explaination=explain, nxlog=nxlog)
138