Search in sources :

Example 1 with HttpMethod

use of com.endava.cats.http.HttpMethod in project cats by Endava.

the class FuzzingDataFactory method getFuzzDataForNonBodyMethods.

/**
 * A similar FuzzingData object will be created for GET or DELETE requests. The "payload" will be a JSON with all the query or path params.
 * In order to achieve this a synthetic object is created that will act as a root object holding all the query or path params as child schemas.
 * The method returns a list of FuzzingData as you might have oneOf operations which will create multiple payloads.
 *
 * @param path      the current path
 * @param item      the current path item
 * @param openAPI   the full OpenAPI object
 * @param operation the OpenApi operation
 * @return a list of FuzzingData objects
 */
private List<FuzzingData> getFuzzDataForNonBodyMethods(String path, PathItem item, Operation operation, OpenAPI openAPI, HttpMethod method) {
    ObjectSchema syntheticSchema = this.createSyntheticSchemaForGet(operation.getParameters());
    globalContext.getSchemaMap().put(SYNTH_SCHEMA_NAME + operation.getOperationId(), syntheticSchema);
    Set<String> queryParams = this.extractQueryParams(syntheticSchema);
    List<String> payloadSamples = this.getRequestPayloadsSamples(null, SYNTH_SCHEMA_NAME + operation.getOperationId());
    Map<String, List<String>> responses = this.getResponsePayloads(operation, operation.getResponses().keySet());
    Map<String, List<String>> responsesContentTypes = this.getResponseContentTypes(operation, operation.getResponses().keySet());
    List<String> requestContentTypes = this.getRequestContentTypes(operation, openAPI);
    return payloadSamples.stream().map(payload -> FuzzingData.builder().method(method).path(path).headers(this.extractHeaders(operation)).payload(payload).responseCodes(operation.getResponses().keySet()).reqSchema(syntheticSchema).pathItem(item).schemaMap(globalContext.getSchemaMap()).responses(responses).responseContentTypes(responsesContentTypes).requestPropertyTypes(globalContext.getRequestDataTypes()).requestContentTypes(requestContentTypes).queryParams(queryParams).openApi(openAPI).tags(operation.getTags()).reqSchemaName(SYNTH_SCHEMA_NAME).build()).collect(Collectors.toList());
}
Also used : CatsGlobalContext(com.endava.cats.model.CatsGlobalContext) OpenApiUtils(com.endava.cats.util.OpenApiUtils) Parameter(io.swagger.v3.oas.models.parameters.Parameter) HashMap(java.util.HashMap) Operation(io.swagger.v3.oas.models.Operation) JsonParser(com.google.gson.JsonParser) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) StringUtils(org.apache.commons.lang3.StringUtils) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) Inject(javax.inject.Inject) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) FilesArguments(com.endava.cats.args.FilesArguments) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) FuzzingData(com.endava.cats.model.FuzzingData) Schema(io.swagger.v3.oas.models.media.Schema) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) ProcessingArguments(com.endava.cats.args.ProcessingArguments) Content(io.swagger.v3.oas.models.media.Content) PathItem(io.swagger.v3.oas.models.PathItem) MediaType(io.swagger.v3.oas.models.media.MediaType) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) TreeMap(java.util.TreeMap) HttpMethod(com.endava.cats.http.HttpMethod) Optional(java.util.Optional) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Collections(java.util.Collections) PayloadGenerator(com.endava.cats.model.generator.PayloadGenerator) CatsHeader(com.endava.cats.model.CatsHeader) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with HttpMethod

use of com.endava.cats.http.HttpMethod in project cats by Endava.

the class CatsCommand method fuzzPath.

public void fuzzPath(Map.Entry<String, PathItem> pathItemEntry, OpenAPI openAPI) {
    /* WE NEED TO ITERATE THROUGH EACH HTTP OPERATION CORRESPONDING TO THE CURRENT PATH ENTRY*/
    LOGGER.info(" ");
    LOGGER.start("Start fuzzing path {}", pathItemEntry.getKey());
    List<FuzzingData> fuzzingDataList = fuzzingDataFactory.fromPathItem(pathItemEntry.getKey(), pathItemEntry.getValue(), openAPI);
    if (fuzzingDataList.isEmpty()) {
        LOGGER.warning("Skipping path {}. HTTP method not supported yet!", pathItemEntry.getKey());
        return;
    }
    List<FuzzingData> fuzzingDataListWithHttpMethodsFiltered = fuzzingDataList.stream().filter(fuzzingData -> filterArguments.getHttpMethods().contains(fuzzingData.getMethod())).collect(Collectors.toList());
    List<HttpMethod> excludedHttpMethods = fuzzingDataList.stream().map(FuzzingData::getMethod).filter(method -> !filterArguments.getHttpMethods().contains(method)).collect(Collectors.toList());
    List<Fuzzer> allFuzzersSorted = filterArguments.getAllRegisteredFuzzers();
    List<String> configuredFuzzers = filterArguments.getFuzzersForPath();
    LOGGER.info("The following HTTP methods won't be executed for path {}: {}", pathItemEntry.getKey(), excludedHttpMethods);
    LOGGER.info("{} configured fuzzers out of {} total fuzzers: {}", configuredFuzzers.size(), (long) allFuzzersSorted.size(), configuredFuzzers);
    /*We only run the fuzzers supplied and exclude those that do not apply for certain HTTP methods*/
    for (Fuzzer fuzzer : allFuzzersSorted) {
        if (configuredFuzzers.contains(fuzzer.toString())) {
            CatsUtil.filterAndPrintNotMatching(fuzzingDataListWithHttpMethodsFiltered, data -> !fuzzer.skipForHttpMethods().contains(data.getMethod()), LOGGER, "HTTP method {} is not supported by {}", t -> t.getMethod().toString(), fuzzer.toString()).forEach(data -> {
                LOGGER.info("Fuzzer {} and payload: {}", ansi().fgGreen().a(fuzzer.toString()).reset(), data.getPayload());
                testCaseListener.beforeFuzz(fuzzer.getClass());
                fuzzer.fuzz(data);
                testCaseListener.afterFuzz();
            });
        } else {
            LOGGER.debug("Skipping fuzzer {} for path {} as configured!", fuzzer, pathItemEntry.getKey());
        }
    }
}
Also used : ExecutionStatisticsListener(com.endava.cats.report.ExecutionStatisticsListener) CatsGlobalContext(com.endava.cats.model.CatsGlobalContext) PrettyLoggerFactory(io.github.ludovicianul.prettylogger.PrettyLoggerFactory) OpenApiUtils(com.endava.cats.util.OpenApiUtils) VersionProvider(com.endava.cats.util.VersionProvider) PrettyLogger(io.github.ludovicianul.prettylogger.PrettyLogger) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Fuzzer(com.endava.cats.Fuzzer) CheckArguments(com.endava.cats.args.CheckArguments) FilesArguments(com.endava.cats.args.FilesArguments) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) FuzzingData(com.endava.cats.model.FuzzingData) Schema(io.swagger.v3.oas.models.media.Schema) CommandLine(picocli.CommandLine) LinkedHashSet(java.util.LinkedHashSet) Ansi.ansi(org.fusesource.jansi.Ansi.ansi) CatsUtil(com.endava.cats.util.CatsUtil) ProcessingArguments(com.endava.cats.args.ProcessingArguments) ApiArguments(com.endava.cats.args.ApiArguments) TestCaseListener(com.endava.cats.report.TestCaseListener) PathItem(io.swagger.v3.oas.models.PathItem) AutoComplete(picocli.AutoComplete) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) AuthArguments(com.endava.cats.args.AuthArguments) IgnoreArguments(com.endava.cats.args.IgnoreArguments) List(java.util.List) FilterArguments(com.endava.cats.args.FilterArguments) FuzzingDataFactory(com.endava.cats.factory.FuzzingDataFactory) HttpMethod(com.endava.cats.http.HttpMethod) Dependent(javax.enterprise.context.Dependent) FunctionalFuzzer(com.endava.cats.fuzzer.fields.FunctionalFuzzer) ReportingArguments(com.endava.cats.args.ReportingArguments) Fuzzer(com.endava.cats.Fuzzer) FunctionalFuzzer(com.endava.cats.fuzzer.fields.FunctionalFuzzer) FuzzingData(com.endava.cats.model.FuzzingData) HttpMethod(com.endava.cats.http.HttpMethod)

Aggregations

FilesArguments (com.endava.cats.args.FilesArguments)2 ProcessingArguments (com.endava.cats.args.ProcessingArguments)2 HttpMethod (com.endava.cats.http.HttpMethod)2 CatsGlobalContext (com.endava.cats.model.CatsGlobalContext)2 FuzzingData (com.endava.cats.model.FuzzingData)2 OpenApiUtils (com.endava.cats.util.OpenApiUtils)2 OpenAPI (io.swagger.v3.oas.models.OpenAPI)2 PathItem (io.swagger.v3.oas.models.PathItem)2 Schema (io.swagger.v3.oas.models.media.Schema)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 Fuzzer (com.endava.cats.Fuzzer)1 ApiArguments (com.endava.cats.args.ApiArguments)1 AuthArguments (com.endava.cats.args.AuthArguments)1 CheckArguments (com.endava.cats.args.CheckArguments)1 FilterArguments (com.endava.cats.args.FilterArguments)1 IgnoreArguments (com.endava.cats.args.IgnoreArguments)1