Skip to content

Fix: Index out of bounds panic in SwaggerUIHandler for files without extension #3214

@NitinKumar004

Description

@NitinKumar004

Bug Description

SwaggerUIHandler in pkg/gofr/swagger.go:53 panics with an index out of bounds error when the requested filename has no . (dot) in it.

Root Cause

The code splits the filename on . and directly accesses split[1] without checking the length:

func SwaggerUIHandler(c *Context) (any, error) {
    fileName := c.PathParam("name")
    if fileName == "" {
        fileName = "index.html"
    }

    data, err := fs.ReadFile("static/" + fileName)
    if err != nil {
        c.Errorf("Failed to read Swagger UI file %s from embedded file system: %v", fileName, err)
        return nil, err
    }

    split := strings.Split(fileName, ".")
    ct := mime.TypeByExtension("." + split[1])  // PANIC: index out of range if no "." in fileName
    // ...
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions