Exemplo n.º 1
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())
Exemplo n.º 2
0
def test_push_get_purge_context():
    root = Root()
    push_context(root)
    assert_equals(len(CONTEXTS), 1)
    assert_equals(get_context().block, root)
    root = Root()
    push_context(root)
    assert_equals(len(CONTEXTS), 2)
    assert_equals(get_context().block, root)

    purge_context()
    assert_equals(len(CONTEXTS), 0)
Exemplo n.º 3
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'))
Exemplo n.º 4
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'))
Exemplo n.º 5
0
    def _update_variables(self, directive):
        # TODO(buglloc): finish him!
        if not directive.provide_variables:
            return

        context = get_context()
        for var in directive.variables:
            if var.name == 0:
                # All regexps must clean indexed variables
                context.clear_index_vars()
            context.add_var(var.name, var)
Exemplo n.º 6
0
    def audit(self, directive):
        value = directive.args[0]
        if not value:
            return

        context = get_context()
        if context.block.name == 'location' and context.block.is_internal:
            # Exclude internal locations
            return

        parsed = self.parse_uri_re.match(value)
        if not parsed:
            return

        res = self._check_script(parsed.group('scheme'), directive)
        if not res:
            self._check_script(parsed.group('host'), directive)
Exemplo n.º 7
0
def compile_script(script):
    """
    Compile Nginx script to list of variables.
    Example:
        compile_script('http://$foo:$bar') ->
            [Variable('http://'), Variable($foo), Variable(':', Variable($bar).

    :param str script: Nginx scrip.
    :return Variable[]: list of variable.
    """
    depends = []
    context = get_context()
    for i, var in enumerate(EXTRACT_RE.split(str(script))):
        if i % 2:
            # Variable
            var = var.strip('{}\x20')
            var = context.get_var(var)
            if var:
                depends.append(var)
        elif var:
            # Literal
            depends.append(Variable(name=None, value=var, have_script=False))
    return depends
Exemplo n.º 8
0
def test_context_depend_variables():
    push_context(Root())
    assert_equals(len(get_context().variables['index']), 0)
    assert_equals(len(get_context().variables['name']), 0)

    get_context().add_var(1, Variable(1, value='one'))
    get_context().add_var('some', Variable('some', value='some'))

    assert_equals(get_context().get_var(1).value, 'one')
    assert_equals(get_context().get_var('some').value, 'some')

    # Checks top context variables are still exists
    push_context(Root())
    assert_equals(get_context().get_var(1).value, 'one')
    assert_equals(get_context().get_var('some').value, 'some')

    # Checks variable overriding
    get_context().add_var('some', Variable('some', value='some_new'))
    get_context().add_var('foo', Variable('foo', value='foo'))
    assert_not_equals(get_context().get_var('some').value, 'some')
    assert_equals(get_context().get_var('some').value, 'some_new')
    assert_equals(get_context().get_var('foo').value, 'foo')
    assert_equals(get_context().get_var(1).value, 'one')

    # Checks variables after restore previous context
    pop_context()
    assert_not_equals(get_context().get_var('some').value, 'some_new')
    assert_equals(get_context().get_var('some').value, 'some')
    assert_equals(get_context().get_var('foo'), None)
    assert_equals(get_context().get_var(1).value, 'one')