fix: prevent index out of bounds panic in SwaggerUIHandler#3291
Closed
mango766 wants to merge 1 commit intogofr-dev:developmentfrom
Closed
fix: prevent index out of bounds panic in SwaggerUIHandler#3291mango766 wants to merge 1 commit intogofr-dev:developmentfrom
mango766 wants to merge 1 commit intogofr-dev:developmentfrom
Conversation
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
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3214.
SwaggerUIHandlerpanics with index out of bounds when the requested filename has no.in it, becausestrings.Splitreturns a single-element slice and the code accessessplit[1]directly.Changes
len(split) > 1before accessing the extensionsplit[len(split)-1]to correctly handle filenames with multiple dots (e.g.file.min.js)application/octet-streamwhen no extension is found ormime.TypeByExtensionreturns empty1 file changed:
pkg/gofr/swagger.goTest plan
application/octet-streaminstead of panickingindex.html→ should still returntext/htmlas beforeapp.min.js→ should returnapplication/javascript