Beispiel #1
0
    def audit(self, directive):
        if directive.operand not in ['~', '~*', '!~', '!~*']:
            # Not regexp
            return

        if directive.variable not in ['$http_referer', '$http_origin']:
            # Not interesting
            return

        invalid_referers = set()
        regexp = Regexp(directive.value, case_sensitive=(directive.operand in ['~', '!~']))
        for value in regexp.generate('/', anchored=True):
            if value.startswith('^'):
                value = value[1:]
            else:
                value = 'http://evil.com/' + value

            if value.endswith('$'):
                value = value[:-1]
            elif not value.endswith('/'):
                value += '.evil.com'

            valid = self.valid_re.match(value)
            if not valid or valid.group('domain') == 'evil.com':
                invalid_referers.add(value)

        if invalid_referers:
            invalid_referers = '", "'.join(invalid_referers)
            name = 'origin' if directive.variable == '$http_origin' else 'referrer'
            severity = gixy.severity.HIGH if directive.variable == '$http_origin' else gixy.severity.MEDIUM
            reason = 'Regex matches "{value}" as a valid {name}.'.format(value=invalid_referers, name=name)
            self.add_issue(directive=directive, reason=reason, severity=severity)
Beispiel #2
0
def test_regexp_boundary():
    var = Variable(name='simple', value=Regexp('.*'), boundary=Regexp('/[a-z]', strict=True))
    assert_false(var.depends)
    assert_true(var.regexp)

    assert_true(var.can_startswith('/'))
    assert_false(var.can_startswith('a'))
    assert_false(var.can_contain('/'))
    assert_true(var.can_contain('a'))
    assert_false(var.can_contain('0'))
    assert_false(var.can_contain('\n'))
    assert_true(var.must_contain('/'))
    assert_false(var.must_contain('a'))
    assert_true(var.must_startswith('/'))
    assert_false(var.must_startswith('a'))
Beispiel #3
0
def test_script_boundary():
    get_context().add_var('foo', Variable(name='foo', value=Regexp('.*'), boundary=Regexp('[a-z]', strict=True)))
    var = Variable(name='simple', value='/$foo', boundary=Regexp('[/a-z0-9]', strict=True))
    assert_true(var.depends)
    assert_false(var.regexp)

    assert_false(var.can_startswith('/'))
    assert_false(var.can_startswith('a'))
    assert_false(var.can_contain('/'))
    assert_true(var.can_contain('a'))
    assert_false(var.can_contain('\n'))
    assert_false(var.can_contain('0'))
    assert_true(var.must_contain('/'))
    assert_false(var.must_contain('a'))
    assert_true(var.must_startswith('/'))
    assert_false(var.must_startswith('a'))
Beispiel #4
0
def test_group_can_contains():
    source = '/some/(?P<action>[^/:.]+)/'
    reg = Regexp(source)
    assert_true(
        reg.can_contain('\n'),
        'Whole regex "{src}" can contains {sym!r}'.format(src=source,
                                                          sym='\\n'))

    assert_true(
        reg.group(0).can_contain('\n'),
        'Group 0 from regex "{src}" can contains {sym!r}'.format(src=source,
                                                                 sym='\\n'))

    assert_true(
        reg.group('action').can_contain('\n'),
        'Group "action" from regex "{src}" can contains {sym!r}'.format(
            src=source, sym='\\n'))

    assert_true(
        reg.group(1).can_contain('\n'),
        'Group 1 from regex "{src}" can contains {sym!r}'.format(src=source,
                                                                 sym='\\n'))

    assert_false(
        reg.group('action').can_contain('/'),
        'Group "action" from regex "{src}" CAN\'T (!) contain {sym!r}'.format(
            src=source, sym='/'))
Beispiel #5
0
    def variables(self):
        if not self.modifier or self.modifier not in ('~', '~*'):
            return []

        regexp = Regexp(self.path, case_sensitive=self.modifier == '~')
        result = []
        for name, group in regexp.groups.items():
            result.append(Variable(name=name, value=group, boundary=None, provider=self))
        return result
Beispiel #6
0
    def variables(self):
        if self.operand != '~':
            return []

        regexp = Regexp(self.value, case_sensitive=self.operand == '~')
        result = []
        for name, group in regexp.groups.items():
            result.append(Variable(name=name, value=group, boundary=None, provider=self))
        return result
Beispiel #7
0
def validate():
    body = request.get_json()
    regex = body.get('regex', '')
    danger = body.get('danger', 'a')[:1]

    try:
        regex = Regexp(regex)
        values = regex.generate(char=danger, anchored=True)
        result = {'status': 'ok', 'result': [x for x in values]}
    except Exception as e:
        result = {
            'status': 'failed',
            'error': 'Error occurred: {}'.format(str(e))
        }

    return Response(response=json.dumps(result),
                    status=200,
                    mimetype='application/json')
Beispiel #8
0
def test_push_failed_with_regexp_py35_gixy_10():
    push_context(Root())
    assert_equals(len(get_context().variables['index']), 0)
    assert_equals(len(get_context().variables['name']), 0)

    regexp = Regexp('^/some/(.*?)')
    for name, group in regexp.groups.items():
        get_context().add_var(name, Variable(name=name, value=group))

    push_context(Root())
Beispiel #9
0
 def variables(self):
     regexp = Regexp(self.pattern, case_sensitive=True)
     result = []
     for name, group in regexp.groups.items():
         result.append(
             Variable(name=name,
                      value=group,
                      boundary=self.boundary,
                      provider=self))
     return result
Beispiel #10
0
def builtin_var(name):
    for builtin, regexp in BUILTIN_VARIABLES.items():
        if builtin.endswith('_'):
            if not name.startswith(builtin):
                continue
        elif name != builtin:
            continue

        if regexp:
            return Variable(name=name, value=Regexp(regexp, strict=True, case_sensitive=False))
        return Variable(name=name, value='builtin', have_script=False)
    return None
Beispiel #11
0
def test_regexp():
    var = Variable(name='simple', value=Regexp('^/.*'))
    assert_false(var.depends)
    assert_true(var.regexp)

    assert_true(var.can_startswith('/'))
    assert_false(var.can_startswith('a'))
    assert_true(var.can_contain('a'))
    assert_false(var.can_contain('\n'))
    assert_true(var.must_contain('/'))
    assert_false(var.must_contain('a'))
    assert_true(var.must_startswith('/'))
    assert_false(var.must_startswith('a'))
Beispiel #12
0
def test_script():
    get_context().add_var('foo', Variable(name='foo', value=Regexp('.*')))
    var = Variable(name='simple', value='/$foo')
    assert_true(var.depends)
    assert_false(var.regexp)

    assert_false(var.can_startswith('/'))
    assert_false(var.can_startswith('a'))
    assert_true(var.can_contain('/'))
    assert_true(var.can_contain('a'))
    assert_false(var.can_contain('\n'))
    assert_true(var.must_contain('/'))
    assert_false(var.must_contain('a'))
    assert_true(var.must_startswith('/'))
    assert_false(var.must_startswith('a'))
Beispiel #13
0
def check_positive_must_startswith(regexp, char, strict):
    reg = Regexp(regexp, case_sensitive=True, strict=strict)
    assert_true(reg.must_startswith(char),
                '"{}" MUST start\'s with "{}"'.format(regexp, char))

    reg = Regexp(regexp, case_sensitive=False, strict=strict)
    char = char.upper()
    assert_true(
        reg.must_startswith(char),
        '"{}" (case insensitive) MUST start\'s with "{}"'.format(regexp, char))
Beispiel #14
0
def check_positive_must_contain(regexp, char):
    reg = Regexp(regexp, case_sensitive=True)
    assert_true(reg.must_contain(char),
                '"{}" must contain with "{}"'.format(regexp, char))

    reg = Regexp(regexp, case_sensitive=False)
    char = char.upper()
    assert_true(
        reg.must_contain(char),
        '"{}" (case insensitive) must contain with "{}"'.format(regexp, char))
Beispiel #15
0
def check_negative_contain(regexp, char):
    reg = Regexp(regexp, case_sensitive=True)
    assert_false(reg.can_contain(char),
                 '"{}" should not contain "{}"'.format(regexp, char))

    reg = Regexp(regexp, case_sensitive=False)
    char = char.upper()
    assert_false(
        reg.can_contain(char),
        '"{}" (case insensitive) should not contain "{}"'.format(regexp, char))
Beispiel #16
0
def check_positive_contain(regexp, char):
    reg = Regexp(regexp, case_sensitive=True)
    assert_true(reg.can_contain(char),
                '{reg!r} should contain {chr!r}'.format(reg=regexp, chr=char))

    reg = Regexp(regexp, case_sensitive=False)
    char = char.upper()
    assert_true(
        reg.can_contain(char),
        '{reg!r} (case insensitive) should contain {chr!r}'.format(reg=regexp,
                                                                   chr=char))
Beispiel #17
0
def check_negative_startswith(regexp, char, strict):
    reg = Regexp(regexp, case_sensitive=True, strict=strict)
    assert_false(reg.can_startswith(char),
                 '"{}" can\'t start\'s with "{}"'.format(regexp, char))

    reg = Regexp(regexp, case_sensitive=False, strict=strict)
    char = char.upper()
    assert_false(
        reg.can_startswith(char),
        '"{}" (case insensitive) can\'t start\'s with "{}"'.format(
            regexp, char))
Beispiel #18
0
def check_positive_startswith(regexp, char, strict):
    reg = Regexp(regexp, case_sensitive=True, strict=strict)
    assert_true(
        reg.can_startswith(char),
        '{reg!r} can start\'s with {chr!r}'.format(reg=regexp, chr=char))

    reg = Regexp(regexp, case_sensitive=False, strict=strict)
    char = char.upper()
    assert_true(
        reg.can_startswith(char),
        '{reg!r} (case insensitive) can start\'s with {chr!r}'.format(
            reg=regexp, chr=char))
Beispiel #19
0
def check_negative_must_contain(regexp, char):
    reg = Regexp(regexp, case_sensitive=True)
    assert_false(
        reg.must_contain(char),
        '{reg!r} must NOT contain with {chr!r}'.format(reg=regexp, chr=char))

    reg = Regexp(regexp, case_sensitive=False)
    char = char.upper()
    assert_false(
        reg.must_contain(char),
        '{reg!r} (case insensitive) must NOT contain with {chr!r}'.format(
            reg=regexp, chr=char))
Beispiel #20
0
def check_negative_must_startswith(regexp, char, strict):
    reg = Regexp(regexp, case_sensitive=True, strict=strict)
    assert_false(
        reg.must_startswith(char),
        '{reg!r} MUST NOT start\'s with {chr!r}'.format(reg=regexp, chr=char))

    reg = Regexp(regexp, case_sensitive=False, strict=strict)
    char = char.upper()
    assert_false(
        reg.must_startswith(char),
        '{reg!r} (case insensitive) MUST NOT start\'s with {chr!r}'.format(
            reg=regexp, chr=char))
Beispiel #21
0
class RewriteDirective(Directive):
    nginx_name = 'rewrite'
    provide_variables = True
    boundary = Regexp('[^\s\r\n]')

    def __init__(self, name, args):
        super(RewriteDirective, self).__init__(name, args)
        self.pattern = args[0]
        self.replace = args[1]
        self.flag = None
        if len(args) > 2:
            self.flag = args[2]

    @property
    def variables(self):
        regexp = Regexp(self.pattern, case_sensitive=True)
        result = []
        for name, group in regexp.groups.items():
            result.append(Variable(name=name, value=group, boundary=self.boundary, provider=self))
        return result
Beispiel #22
0
def test_gen_anchor():

    reg = Regexp('^some$')
    val = next(reg.generate('', anchored=False))
    assert_equals(val, 'some')

    reg = Regexp('^some$')
    val = next(reg.generate('', anchored=True))
    assert_equals(val, '^some$')

    reg = Regexp('^some$', strict=True)
    val = next(reg.generate('', anchored=False))
    assert_equals(val, 'some')

    reg = Regexp('^some$', strict=True)
    val = next(reg.generate('', anchored=True))
    assert_equals(val, '^some$')
Beispiel #23
0
def check_generate(regexp, values):
    reg = Regexp(regexp)
    assert_equals(sorted(reg.generate('|', anchored=True)), sorted(values))
Beispiel #24
0
def check_to_string(regexp, string):
    reg = Regexp(regexp)
    assert_equals(str(reg), string)
Beispiel #25
0
def check_groups_names(regexp, groups):
    reg = Regexp(regexp)
    assert_equals(set(reg.groups.keys()), set(groups))
Beispiel #26
0
def test_strict_generate():
    reg = Regexp('^foo|bar', strict=True)
    assert_equals(sorted(reg.generate('|', anchored=True)),
                  sorted({'^foo', '^bar'}))