Skip to content

Correct extension handling for dotfiles in paths - #37463

Open
sandeep780049 wants to merge 1 commit into
spring-projects:mainfrom
sandeep780049:dotfile-extension-handling
Open

sandeep780049 wants to merge 1 commit into
spring-projects:mainfrom
sandeep780049:dotfile-extension-handling

Conversation

@sandeep780049

@sandeep780049 sandeep780049 commented Oct 12, 2026 •

Copy link
Copy Markdown

Motivation

StringUtils.getFilenameExtension(String) and StringUtils.stripFilenameExtension(String) search for the last '.' in the entire path rather than in the filename. As a result, a dotfile (a filename that starts with a dot and has no extension) is misclassified:

StringUtils.getFilenameExtension("my/path/.hidden");   // "hidden"  (should be null)
StringUtils.stripFilenameExtension("my/path/.hidden"); // "my/path/" (should be unchanged)

This affects consumers such as MediaTypeFactory, StandardScriptEvaluator, resource version strategies, and ResourceHttpRequestHandler, which can treat .gitignore-style resources as if they had a file extension.

Closes gh-37462

Solution

Both methods now resolve the filename first and look up the last dot within the filename only. A leading dot in the filename (extIndex <= 0) is never treated as an extension separator:

StringUtils.getFilenameExtension("my/path/.hidden");   // null
StringUtils.stripFilenameExtension("my/path/.hidden"); // "my/path/.hidden"

Behavior for all existing cases, including "myfile." (empty extension), "my/dir/myfile.txt", and paths whose folder separator appears after the last dot, is unchanged.

Testing

  • Added getFilenameExtension cases: ".hidden" and "myPath/.hidden".
  • Added stripFilenameExtension cases: ".hidden" and "mypath/.hidden".
  • ./gradlew :spring-core:test --tests org.springframework.util.StringUtilsTests — 94 tests, 0 failures.
  • ./gradlew :spring-core:checkstyleMain passes.

Signed-off-by: Sandeep Kumar sandeep780049@users.noreply.github.com

getFilenameExtension() and stripFilenameExtension() measured the last
dot against the entire path, which misclassified dotfiles: a leading
dot in "my/path/.hidden" was treated as an extension separator and
".hidden" was returned as the extension. The methods now evaluate the
dot position against the filename alone, and a leading dot in the
filename is never treated as an extension separator.

Closes spring-projectsgh-37462
Signed-off-by: Sandeep Kumar <sandeep780049@users.noreply.github.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 12, 2026
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Oct 13, 2026

This branch has not been deployed

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

Labels

in: core Issues in core modules (aop, beans, core, context, expression) status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getFilenameExtension and stripFilenameExtension misreport dotfiles

3 participants