예제 #1
0
파일: decoder.py 프로젝트: AtomLaw/Ally-Py
    def decoderFor(self, argumentKey, ofType):
        '''
        Create a decoder exploit for the provided type.
        
        @param argumentKey: string
            The argument name to be used for storing the decoded value.
        @param ofType: Type
            The type to create the decoding exploit for.
        @return: callable(**data)
            The exploit that provides the decoding.
        '''
        assert isinstance(argumentKey, str), 'Invalid argument key %s' % argumentKey
        assert isinstance(ofType, Type), 'Invalid type %s' % ofType

        decoder = self._cache.get(ofType)
        if decoder is None:
            assert log.debug('Creating decoder for type \'%s\'', ofType) or True
            if isinstance(ofType, TypeModel):
                assert isinstance(ofType, TypeModel)
                decoder = self.decoderModel(ofType, obtainOnDict(argumentKey, ofType.clazz))
            else:
                assert log.debug('Cannot decode object type \'%s\'', ofType) or True
                return None

            self._cache[ofType] = decoder

        return decoder
예제 #2
0
    def decoderFor(self, argumentKey, ofType):
        '''
        Create a decoder exploit for the provided type.
        
        @param argumentKey: string
            The argument name to be used for storing the decoded value.
        @param ofType: Type
            The type to create the decoding exploit for.
        @return: callable(**data)
            The exploit that provides the decoding.
        '''
        assert isinstance(argumentKey,
                          str), 'Invalid argument key %s' % argumentKey
        assert isinstance(ofType, Type), 'Invalid type %s' % ofType

        decoder = self._cache.get(ofType)
        if decoder is None:
            assert log.debug('Creating decoder for type \'%s\'',
                             ofType) or True
            if isinstance(ofType, TypeModel):
                assert isinstance(ofType, TypeModel)
                decoder = self.decoderModel(
                    ofType, obtainOnDict(argumentKey, ofType.clazz))
            else:
                assert log.debug('Cannot decode object type \'%s\'',
                                 ofType) or True
                return None

            self._cache[ofType] = decoder

        return decoder
예제 #3
0
    def decodeInvoker(self, invoker):
        """
        Create a decode exploit for the invoker.
        
        @param invoker: Invoker
            The invoker to create a parameters decoder for.
        @return: callable(**data) -> boolean
            The exploit that provides the invoker decoding.
        """
        assert isinstance(invoker, Invoker), "Invalid invoker %s" % invoker

        children, ordered = {}, {}
        for inp in invoker.inputs:
            assert isinstance(inp, Input)
            typeInp = inp.type
            assert isinstance(typeInp, Type)

            if typeInp.isPrimitive:
                if isinstance(typeInp, Iter):
                    assert isinstance(typeInp, Iter)

                    setter = setterWithGetter(obtainOnDict(inp.name, list), list.append)
                    inpDecode = self.decodePrimitiveList(setter, typeInp.itemType)
                else:
                    inpDecode = self.decodePrimitive(setterOnDict(inp.name), typeInp)
                children[inp.name] = inpDecode

            elif isinstance(typeInp, TypeQuery):
                assert isinstance(typeInp, TypeQuery)
                assert isinstance(typeInp.query, Query)

                childrenQuery, orderedQuery, getterQuery = {}, {}, obtainOnDict(inp.name, inp.type.clazz)
                for nameEntry, classCriteria in typeInp.query.criterias.items():

                    getter = getterChain(getterQuery, getterOnObj(nameEntry))
                    childrenQuery[nameEntry] = self.decodeCriteria(typeFor(classCriteria), getter)

                    if issubclass(classCriteria, AsOrdered):
                        orderedQuery[nameEntry] = self.decodeSetOrder(
                            typeInp.criteriaEntryTypeFor(nameEntry), getterQuery
                        )

                isUpdated = False
                if invoker.output.isOf(typeInp.owner):
                    # If the query is a main query and also there is no name conflict then add the query children to
                    # the main children
                    if set(childrenQuery.keys()).isdisjoint(children.keys()) and set(orderedQuery).isdisjoint(ordered):
                        isUpdated = True
                        children.update(childrenQuery)
                        ordered.update(orderedQuery)

                if not isUpdated:
                    children[inp.name] = self.decodePath(childrenQuery)
                    ordered[inp.name] = self.decodePath(orderedQuery)

        if self.nameOrderAsc in children:
            log.error("Name conflict for '%s' in %s", self.nameOrderAsc, invoker)
        elif self.nameOrderDesc in children:
            log.error("Name conflict for '%s' in %s", self.nameOrderDesc, invoker)
        else:
            exploitOrder = self.decodePath(ordered)
            children[self.nameOrderAsc] = self.decodeOrder(True, exploitOrder)
            children[self.nameOrderDesc] = self.decodeOrder(False, exploitOrder)

        exploitPath = self.decodePath(children)

        def exploit(path, **data):
            assert isinstance(path, str), "Invalid path %s" % path

            path = deque(path.split(self.separatorName))
            return exploitPath(path=path, **data)

        return exploit
예제 #4
0
    def decodeInvoker(self, invoker):
        '''
        Create a decode exploit for the invoker.
        
        @param invoker: Invoker
            The invoker to create a parameters decoder for.
        @return: callable(**data) -> boolean
            The exploit that provides the invoker decoding.
        '''
        assert isinstance(invoker, Invoker), 'Invalid invoker %s' % invoker

        children, ordered = {}, {}
        for inp in invoker.inputs:
            assert isinstance(inp, Input)
            typeInp = inp.type
            assert isinstance(typeInp, Type)

            if typeInp.isPrimitive:
                if isinstance(typeInp, Iter):
                    assert isinstance(typeInp, Iter)

                    setter = setterWithGetter(obtainOnDict(inp.name, list),
                                              list.append)
                    inpDecode = self.decodePrimitiveList(
                        setter, typeInp.itemType)
                else:
                    inpDecode = self.decodePrimitive(setterOnDict(inp.name),
                                                     typeInp)
                children[inp.name] = inpDecode

            elif isinstance(typeInp, TypeQuery):
                assert isinstance(typeInp, TypeQuery)
                assert isinstance(typeInp.query, Query)

                childrenQuery, orderedQuery, getterQuery = {}, {}, obtainOnDict(
                    inp.name, inp.type.clazz)
                for nameEntry, classCriteria in typeInp.query.criterias.items(
                ):

                    getter = getterChain(getterQuery, getterOnObj(nameEntry))
                    childrenQuery[nameEntry] = self.decodeCriteria(
                        typeFor(classCriteria), getter)

                    if issubclass(classCriteria, AsOrdered):
                        orderedQuery[nameEntry] = self.decodeSetOrder(
                            typeInp.criteriaEntryTypeFor(nameEntry),
                            getterQuery)

                isUpdated = False
                if invoker.output.isOf(typeInp.owner):
                    # If the query is a main query and also there is no name conflict then add the query children to
                    # the main children
                    if set(childrenQuery.keys()).isdisjoint(children.keys(
                    )) and set(orderedQuery).isdisjoint(ordered):
                        isUpdated = True
                        children.update(childrenQuery)
                        ordered.update(orderedQuery)

                if not isUpdated:
                    children[inp.name] = self.decodePath(childrenQuery)
                    ordered[inp.name] = self.decodePath(orderedQuery)

        if self.nameOrderAsc in children:
            log.error('Name conflict for \'%s\' in %s', self.nameOrderAsc,
                      invoker)
        elif self.nameOrderDesc in children:
            log.error('Name conflict for \'%s\' in %s', self.nameOrderDesc,
                      invoker)
        else:
            exploitOrder = self.decodePath(ordered)
            children[self.nameOrderAsc] = self.decodeOrder(True, exploitOrder)
            children[self.nameOrderDesc] = self.decodeOrder(
                False, exploitOrder)

        exploitPath = self.decodePath(children)

        def exploit(path, **data):
            assert isinstance(path, str), 'Invalid path %s' % path

            path = deque(path.split(self.separatorName))
            return exploitPath(path=path, **data)

        return exploit