Exemplo n.º 1
0
def _make_match_string_from_pattern(parsetree, makebad=False, groups=None):
    collect = []
    if groups is None:
        groups = {}
    for op, val in parsetree:
        if op is sre_constants.LITERAL:
            if makebad:
                collect.append(chr((val ^ 4) & 0xFF))  # flip bit 4
                if random.randint(0, 9) == 0:
                    makebad = False  # don't error everything
            else:
                collect.append(chr(val))
        elif op is sre_constants.CATEGORY:
            collect.append(get_substitute(val, makebad))
        elif op is sre_constants.IN:
            if val[0][0] is sre_constants.CATEGORY:
                collect.append(
                    _make_match_string_from_pattern(val, False, groups))
            else:
                collect.append(chr(random.choice(val)[1]))
        elif op is sre_constants.BRANCH:
            collect.append(
                _make_match_string_from_pattern(val[1][random.randint(0, 1)],
                                                False, groups))
        elif op is sre_constants.SUBPATTERN:
            string = _make_match_string_from_pattern(val[1], False, groups)
            groups[val[0]] = string
            collect.append(string)
        elif op is sre_constants.MAX_REPEAT or op is sre_constants.MIN_REPEAT:
            for i in xrange(random.randint(val[0], min(val[1], 10))):
                collect.append(
                    _make_match_string_from_pattern(val[2], False, groups))
        elif op is sre_constants.ANY:
            collect.append(random.choice(ANYCHAR))
        elif op is sre_constants.GROUPREF:
            collect.append(groups[val])
        elif op is sre_constants.AT:
            pass  # ignore anchors
        else:
            raise UnhandledOpError("Unhandled RE op: %r" % (op, ))
    if makebad:  # in case it didn't get done yet.
        collect.insert(random.randrange(0, len(collect)),
                       random.choice(ascii.printable))
    return "".join(collect)
Exemplo n.º 2
0
 def get(self, request):
     # This key has to fit into a 32 bit int for the javascript side.
     key = quote(struct.pack(b"I", random.randint(0, 4294967295)))
     parms = {
         b"redirect": request.GET.get("redir", request.environ.get("HTTP_REFERER", "/")),
         b"key": key,
     }
     msg = request.GET.get("message")
     if msg:
         parms[b"message"] = '<p class="error">%s</p>' % (msg,)
     else:
         parms[b"message"] = ''
     return framework.HttpResponse(LOGIN_PAGE % parms, b"application/xhtml+xml")
Exemplo n.º 3
0
def _make_match_string_from_pattern(parsetree, makebad=False, groups=None):
    collect = []
    if groups is None:
        groups = {}
    for op, val in parsetree:
        if op is sre_constants.LITERAL:
            if makebad:
                collect.append(chr((val ^ 4) & 0xFF)) # flip bit 4
                if random.randint(0,9) == 0:
                    makebad = False # don't error everything
            else:
                collect.append(chr(val))
        elif op is sre_constants.CATEGORY:
            collect.append(get_substitute(val, makebad))
        elif op is sre_constants.IN:
            if val[0][0] is sre_constants.CATEGORY:
                collect.append(_make_match_string_from_pattern(val, False, groups))
            else:
                collect.append(chr(random.choice(val)[1]))
        elif op is sre_constants.BRANCH:
            collect.append(_make_match_string_from_pattern(val[1][random.randint(0,1)], False, groups))
        elif op is sre_constants.SUBPATTERN:
            string = _make_match_string_from_pattern(val[1], False, groups)
            groups[val[0]] = string
            collect.append(string)
        elif op is sre_constants.MAX_REPEAT or op is sre_constants.MIN_REPEAT:
            for i in xrange(random.randint(val[0], min(val[1], 10))):
                collect.append(_make_match_string_from_pattern(val[2], False, groups))
        elif op is sre_constants.ANY:
            collect.append(random.choice(ANYCHAR))
        elif op is sre_constants.GROUPREF:
            collect.append(groups[val])
        elif op is sre_constants.AT:
            pass # ignore anchors
        else:
            raise UnhandledOpError("Unhandled RE op: %r" % (op,))
    if makebad: # in case it didn't get done yet.
        collect.insert(random.randrange(0, len(collect)), random.choice(ascii.printable))
    return "".join(collect)