Skip to content

Commit

Permalink
small api refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
pyneda committed Nov 28, 2024
1 parent f79e4c3 commit 30fcc43
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 43 deletions.
38 changes: 20 additions & 18 deletions api/fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,31 @@ type PlaygroundFuzzResponse struct {
// @Param input body PlaygroundFuzzInput true "Set the fuzzing request configuration"
// @Success 200 {string} PlaygroundFuzzResponse
// @Failure 400 {object} ErrorResponse
// @Failure 500 {object} ErrorResponse
// @Security ApiKeyAuth
// @Router /api/v1/playground/fuzz [post]
func FuzzRequest(c *fiber.Ctx) error {
input := new(PlaygroundFuzzInput)

if err := c.BodyParser(input); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Cannot parse JSON",
return c.Status(fiber.StatusBadRequest).JSON(ErrorResponse{
Error: "Bad Request",
Message: "Cannot parse JSON body",
})
}

if err := validate.Struct(input); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Validation failed",
"message": err.Error(),
return c.Status(fiber.StatusBadRequest).JSON(ErrorResponse{
Error: "Validation Failed",
Message: err.Error(),
})
}

session, err := db.Connection.GetPlaygroundSession(input.SessionID)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Invalid session",
"message": "The provided session ID does not seem valid",
return c.Status(fiber.StatusBadRequest).JSON(ErrorResponse{
Error: "Invalid Session",
Message: "The provided session ID does not seem valid",
})
}

Expand All @@ -67,24 +69,24 @@ func FuzzRequest(c *fiber.Ctx) error {
task, err := db.Connection.NewTask(session.WorkspaceID, &session.ID, title, db.TaskStatusPending, db.TaskTypePlaygroundFuzzer)
if err != nil {
log.Error().Err(err).Interface("task", task).Msg("Task creation failed")
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "There was an error initiating fuzzing",
"message": "Cannot create a new task",
return c.Status(fiber.StatusBadRequest).JSON(ErrorResponse{
Error: "Fuzzing Initialization Failed",
Message: "Cannot create a new task",
})
}
requestsCount, err := manual.Fuzz(fuzzOptions, task.ID)
if err != nil {
log.Error().Err(err).Interface("options", fuzzOptions).Msg("Failed to initiate playground fuzzing")
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "There was an error initiating fuzzing",
"message": err.Error(),
return c.Status(fiber.StatusInternalServerError).JSON(ErrorResponse{
Error: "Fuzzing Initialization Failed",
Message: err.Error(),
})
}

return c.JSON(fiber.Map{
"message": "Fuzzing initiated successfully",
"task_id": task.ID,
"requests_count": requestsCount,
return c.JSON(PlaygroundFuzzResponse{
Message: "Fuzzing initiated successfully",
TaskID: task.ID,
RequestsCount: requestsCount,
})

}
11 changes: 6 additions & 5 deletions api/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ func ReplayRequest(c *fiber.Ctx) error {
input := new(PlaygroundReplayInput)

if err := c.BodyParser(input); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Cannot parse JSON",
return c.Status(fiber.StatusBadRequest).JSON(ErrorResponse{
Error: "Bad Request",
Message: "Cannot parse JSON body",
})
}

Expand Down Expand Up @@ -93,9 +94,9 @@ func ReplayRequest(c *fiber.Ctx) error {
result, err := manual.Replay(options)
if err != nil {
log.Error().Err(err).Msg("Error replaying request")
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "There was an error replaying the request",
"message": err.Error(),
return c.Status(fiber.StatusBadRequest).JSON(ErrorResponse{
Error: "Request Replay Failed",
Message: err.Error(),
})
}

Expand Down
12 changes: 12 additions & 0 deletions docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,12 @@
"schema": {
"$ref": "#/definitions/api.ErrorResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/api.ErrorResponse"
}
}
}
}
Expand Down Expand Up @@ -2073,6 +2079,12 @@
"in": "query",
"required": true
},
{
"type": "string",
"description": "Search query",
"name": "query",
"in": "query"
},
{
"type": "integer",
"default": 50,
Expand Down
8 changes: 8 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2185,6 +2185,10 @@ paths:
description: Bad Request
schema:
$ref: '#/definitions/api.ErrorResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/api.ErrorResponse'
security:
- ApiKeyAuth: []
summary: Schedules a new task to fuzz the provided request
Expand Down Expand Up @@ -2624,6 +2628,10 @@ paths:
name: task
required: true
type: integer
- description: Search query
in: query
name: query
type: string
- default: 50
description: Number of items per page
in: query
Expand Down
57 changes: 37 additions & 20 deletions pkg/discovery/openapi_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,57 @@ var OpenAPIPaths = []string{
"docs/swagger.json",
"docs/swagger.yaml",
"docs/swagger.yml",
"swagger/properties.json",
"swagger/properties.yaml",
"swagger/docs.json",
"swagger/docs.yaml",
"openapi.json",
"openapi.yaml",
"openapi.yml",
"swagger.json",
"swagger.yaml",
"swagger.yml",
"api-spec.json",
"api-spec.yaml",
"api-spec.yml",
"v1/openapi.json",
"v1/swagger.json",
"v2/openapi.json",
"v2/swagger.json",
"v3/openapi.json",
"v3/swagger.json",
"v1/api-docs.json",
"v2/api-docs.json",
"v3/api-docs.json",
"api/v1/swagger.json",
"api/v2/swagger.json",
"api/v3/swagger.json",
"documentation/openapi.json",
"documentation/swagger.json",
"api/documentation/openapi.json",
"api/documentation/swagger.json",
"api-documentation/openapi.json",
"api-documentation/swagger.json",
"spec/openapi.json",
"spec/swagger.json",
"api/spec/openapi.json",
"api/spec/swagger.json",
"schema/openapi.json",
"schema/swagger.json",
"api/schema/openapi.json",
"api/schema/swagger.json",
"reference/openapi.json",
"reference/swagger.json",
"api/reference/openapi.json",
"api/reference/swagger.json",
"swagger-ui/swagger.json",
"swagger-resources/swagger.json",
"api/swagger-resources/swagger.json",
"swagger-config.json",
"api-definition.json",
"api/definition/swagger.json",
}

// var swaggerUIMarkers = []string{
// "<title>api doc",
// "<title>openapi",
// "<title>swagger",
// "api documentation",
// "api-doc",
// "api-docs",
// "api-docs-ui",
// "api-documentation",
// "api-doc-ui",
// "api-docs-page",
// "openapi",
// "openapi-ui",
// "redoc",
// "swagger-editor",
// "swagger ui",
// "swagger-ui",
// "swaggerui",
// }

func IsOpenAPIValidationFunc(history *db.History) (bool, string, int) {
confidence := 50
details := make([]string, 0)
Expand Down

0 comments on commit 30fcc43

Please sign in to comment.