Skip to content

WW-5840 Provide a path-stripped original file name for action-based file uploads - #2116

Open
g0w6y wants to merge 2 commits into
apache:mainfrom
g0w6y:WW-5840-sanitized-upload-filename
Open

g0w6y wants to merge 2 commits into
apache:mainfrom
g0w6y:WW-5840-sanitized-upload-filename

Conversation

@g0w6y

@g0w6y g0w6y commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Fixes WW-5840

UploadedFile.getOriginalName() returns the file name exactly as the client sent it, including any directory part. The framework itself never uses that value as a path (the stored temp file is always UUID-named). The legacy pre-7.0 getFileNames()/getCanonicalName() pair on MultiPartRequest stripped the directory part, but the replacement ActionFileUploadInterceptor/UploadedFilesAware API passes the raw value through unchanged and has no basename accessor.

  • Adds UploadedFile#getOriginalBaseName() (@since 7.6.0), a default method returning the last path segment via FilenameUtils.getName, alongside the raw accessor. It is for display only: the result may still be empty or .., so it must be validated before use in a filesystem path.
  • Switches the showcase upload actions, which only use the name for display, to the new accessor.

Verified: AbstractMultiPartRequestTest gains a test covering ../evil.csv and the Windows-style C:\x\y.txt (raw accessor unchanged, basename accessor returns evil.csv / y.txt). Full core suite: 3742 run, 0 failures, 0 errors. apps/showcase changes are one-token accessor swaps; its module could not be built locally because of an unrelated offline plugin-resolution gap.

…ile uploads

UploadedFile.getOriginalName() returns the file name exactly as the client
sent it in the multipart Content-Disposition header, including any
directory part. The framework itself never uses that value as a path (the
stored temp file is always UUID-named), but the legacy FileUploadInterceptor
path stripped it via FilenameUtils.getName() before 7.0, and the replacement
ActionFileUploadInterceptor/UploadedFilesAware API passes the raw value
through unchanged.

Adds UploadedFile#getSanitizedOriginalName() (basename only, via
FilenameUtils.getName), alongside the existing raw accessor, with javadoc on
both making the raw/sanitised distinction explicit. The showcase upload
actions, which only ever use the name for display, switch to the sanitised
accessor.

Tests: AbstractMultiPartRequestTest gains a benign-marker test proving the
raw accessor is unsanitised and the new sanitised one is not. Full core
suite: 3742/0 failures (includes JakartaMultiPartRequestTest 42/42,
JakartaStreamMultiPartRequestTest 43/43). apps/showcase changes are
mechanical 1-line accessor swaps with no test asserting the exact filename
string; could not run its own suite in this environment due to an unrelated,
pre-existing local dependency gap in plugins/tiles (struts2-velocity-tools-
view-jakarta never built on this machine), confirmed unrelated to this
change by inspection.

@lukaszlenart lukaszlenart left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for splitting this out of #2115. The approach (a basename accessor next to the raw one, no rejection of names) is the one we want. A few things before merge:

  1. @since 7.5.0 should be @since 7.6.0. 7.5.0 and 7.5.1 are already released; WW-5840 targets 7.6.0.

  2. The Javadoc of getSanitizedOriginalName() promises more than a basename strip delivers. It says the result is suitable "for building a path under an application-controlled directory", but FilenameUtils.getName returns .. unchanged and turns a/b/ into an empty string, so new File(dir, getSanitizedOriginalName()) can still resolve to dir itself or its parent. That contradicts the "must not be used to build a filesystem path" note on getOriginalName() just above. Suggested wording: basename only, intended for display; validate it before using it in a filesystem path. The name getSanitized... also suggests more validation than the method performs. Something like getOriginalBaseName() would describe it more precisely. Worth settling now, since this becomes public API.

  3. Please trim the comments.

    • The new method is one line, but its Javadoc runs to ~20 lines, most of it history (the pre-7.0 getFileNames()/getCanonicalName() pair, "has no caller in the current flow"). That context belongs in the PR description and commit message. In the Javadoc, two or three lines saying what it returns and what it doesn't guarantee are enough.
    • The four-line comment in the ActionFileUploadInterceptor example can be a single line.
    • In the test, the Javadoc block and the // Fix: / // Control: narration can go. The assertions read fine on their own.
  4. Minor: please add a Windows-style case to the test. WW-5840 mentions backslash-separated paths. One more assertion, e.g. an original name of C:\x\y.txt giving y.txt, would cover that.

I ran JakartaMultiPartRequestTest and JakartaStreamMultiPartRequestTest locally against this branch, and the new test passes with both parsers.

@g0w6y

g0w6y commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Pushed b6961fd addressing all four points:

  1. @since is now 7.6.0.
  2. Renamed to getOriginalBaseName(); the Javadoc now says basename only, for display, validate before using in a filesystem path (and notes it can be empty or ..).
  3. Trimmed the Javadoc, the interceptor example comment (now one line) and the test narration; history moved to the PR description.
  4. Added a C:\x\y.txt -> y.txt assertion.

Full core suite passes locally (3742 run, 0 failures).

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.

2 participants