Skip to content

fix: prevent index out of bounds panic in SwaggerUIHandler#3291

Closed
mango766 wants to merge 1 commit intogofr-dev:developmentfrom
mango766:fix/swagger-handler-panic
Closed

fix: prevent index out of bounds panic in SwaggerUIHandler#3291
mango766 wants to merge 1 commit intogofr-dev:developmentfrom
mango766:fix/swagger-handler-panic

Conversation

@mango766
Copy link
Copy Markdown

@mango766 mango766 commented Apr 7, 2026

Summary

Fixes #3214.

SwaggerUIHandler panics with index out of bounds when the requested filename has no . in it, because strings.Split returns a single-element slice and the code accesses split[1] directly.

Changes

  • Check len(split) > 1 before accessing the extension
  • Use split[len(split)-1] to correctly handle filenames with multiple dots (e.g. file.min.js)
  • Fall back to application/octet-stream when no extension is found or mime.TypeByExtension returns empty

1 file changed: pkg/gofr/swagger.go

Test plan

  • Request a file without extension → should return application/octet-stream instead of panicking
  • Request index.html → should still return text/html as before
  • Request app.min.js → should return application/javascript

When the requested filename has no dot (e.g. 'Dockerfile'),
strings.Split returns a single-element slice and accessing
split[1] panics with index out of range.

Check the slice length before accessing the extension, and use
the last element to handle filenames with multiple dots correctly.
Fall back to application/octet-stream if no extension is found.

Fixes gofr-dev#3214
@Umang01-hash
Copy link
Copy Markdown
Member

Hey @mango766! This issue was already taken and we have a similar PR #3217. Closing here to avoid duplicacy!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants