explain_whitelist()   F
last analyzed

Complexity

Conditions 9

Size

Total Lines 32

Duplication

Lines 18
Ratio 56.25 %

Code Coverage

Tests 26
CRAP Score 9.0294

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
c 1
b 0
f 0
dl 18
loc 32
ccs 26
cts 28
cp 0.9286
crap 9.0294
rs 3
1 1
try:
2 1
    from urlparse import parse_qs
0 ignored issues
show
Unused Code introduced by
Unused parse_qs imported from urlparse
Loading history...
3
except ImportError:  # python3
4
    from urllib.parse import parse_qs
5
6 1
from flask import Blueprint, render_template, request, redirect, flash, url_for
7
8 1
from nxapi import nxlog
0 ignored issues
show
Configuration introduced by
The import nxapi 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...
9
10 1
from spike.model.naxsi_rules import NaxsiRules
11 1
from spike.model.naxsi_whitelist import NaxsiWhitelist
12
13 1
sandbox = Blueprint('sandbox', __name__)
14
15
16 1
@sandbox.route("/", methods=["GET"])
17
def index():
18 1
    return render_template("misc/sandbox.html")
19
20
21 1
@sandbox.route("/explain_rule/", methods=["GET", "POST"])
22
def explain_rule():
23 1
    errors = warnings = list()
24 1
    rule_get = request.args.get('rule', '')
25 1
    rule_post = request.form.get("rule", '')
26 1 View Code Duplication
    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.
Loading history...
27 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...
28 1
        if _rule is None:
29 1
            flash('Not rule with id %s' % rule_get)
30 1
            return redirect(url_for("sandbox.index"))
31 1
    elif rule_get is not '':
32 1
        flash('Please provide a numeric id')
33 1
        return redirect(url_for("sandbox.index"))
34 1
    elif not rule_post:
35 1
        flash('Please provide a rule')
36 1
        return redirect(url_for("sandbox.index"))
37
    else:
38 1
        _rule = NaxsiRules()
39 1
        errors, warnings, rdict = _rule.parse_rule(rule_post)
40 1
        _rule = NaxsiRules()
41 1
        _rule.from_dict(rdict)
42 1
        _rule.errors = errors
43 1
        _rule.warnings = warnings
44
45 1
        if _rule.errors:
46 1
            flash('You rule is wrong', 'error')
47 1
            return render_template("misc/sandbox.html")
48
49 1
    if 'visualise_rule' in request.form:
50 1
        if _rule.detection.startswith('rx:'):
51
            return redirect('https://regexper.com/#' + _rule.detection[3:])
52
        else:
53 1
            flash('The rule is not a regexp, so you can not visualize it.', category='error')
54
55 1
    if errors:
56
        for error in errors:
57
            flash(error, category='error')
58 1
    if warnings:
59 1
        for warnings in warnings:
60 1
            flash(warnings, category='warning')
61
62 1
    return render_template("misc/sandbox.html", rule_explaination=_rule.explain(), rule=_rule)
63
64
65 1
@sandbox.route("/explain_whitelist/", methods=["GET", "POST"])
66
def explain_whitelist():
67 1
    whitelist_get = request.args.get('whitelist', '')
68 1
    whitelist_post = request.form.get('whitelist', '')
69 1 View Code Duplication
    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.
Loading history...
70 1
        _wl = 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...
71 1
        if _wl is None:
72 1
            flash('Not rule with id %s' % whitelist_get)
73 1
            return redirect(url_for("sandbox.index"))
74 1
    elif whitelist_get is not '':
75 1
        flash('Please provide a numeric id')
76 1
        return redirect(url_for("sandbox.index"))
77 1
    elif not whitelist_post:
78 1
        flash('Please provide a whitelist')
79 1
        return redirect(url_for("sandbox.index"))
80
    else:
81 1
        _wl = NaxsiWhitelist()
82 1
        errors, warnings, rdict = _wl.parse(whitelist_post)
83 1
        _wl = NaxsiWhitelist()
84 1
        _wl.from_dict(rdict)
85 1
        _wl.errors = errors
86 1
        _wl.warnings = warnings
87
88 1
    if _wl.errors:
89 1
        for error in _wl.errors:
90 1
            flash(error, category='error')
91 1
            return render_template("misc/sandbox.html", whitelist=_wl)
92 1
    if _wl.warnings:
93
        for warnings in _wl.warnings:
94
            flash(warnings, category='warning')
95
96 1
    return render_template("misc/sandbox.html", whitelist_explaination=_wl.explain(), whitelist=_wl)
97
98
99 1
@sandbox.route('/explain_nxlog/', methods=["POST"])
100
def explain_nxlog():
101 1
    _nxlog = request.form.get("nxlog", '')
102 1
    if not _nxlog:
103 1
        return redirect(url_for("sandbox.index"))
104
105 1
    errors, nxdic = nxlog.parse_nxlog(_nxlog)
106 1
    if errors:
107 1
        flash(''.join(errors))
108 1
        return redirect(url_for("sandbox.index"))
109
110
    return render_template("misc/sandbox.html", nxlog_explaination=nxlog.explain_nxlog(nxdic), nxlog=_nxlog)
111