Andrea Cosentino created CAMEL-27928:
----------------------------------------

             Summary: camel-http-base - fileNameExtWhitelist should reject 
names with no extension
                 Key: CAMEL-27928
                 URL: https://issues.apache.org/jira/browse/CAMEL-27928
             Project: Camel
          Issue Type: Improvement
          Components: camel-http-common, camel-servlet, 
camel-platform-http-vertx, camel-jetty
            Reporter: Andrea Cosentino
            Assignee: Andrea Cosentino
             Fix For: 4.25.0


h3. Background

{{org.apache.camel.http.base.HttpHelper.isFileNameExtWhitelisted(String 
whitelist, String fileName)}} in {{camel-http-base}} is the single shared 
implementation behind the {{fileNameExtWhitelist}} upload option. It is called 
from:
* {{org.apache.camel.http.common.DefaultHttpBinding.isFileNameAccepted}} in 
{{camel-http-common}} (used by {{camel-servlet}} and {{camel-jetty}} when 
{{attachmentMultipartBinding=true}}); and
* {{VertxPlatformHttpConsumer}} in {{camel-platform-http-vertx}}.

The option is documented as the comma-separated list of file name extensions 
accepted for uploaded files (for example {{txt,pdf}}).

h3. Current behaviour

The method returns {{true}} as soon as the submitted file name has no parseable 
extension, before it consults the configured list:

{code:java}
if (whitelist == null) {
    return true;
}
String ext = FileUtil.onlyExt(fileName);
if (ext == null) {
    return true;   // accepted no matter what the configured list says
}
// ... case-insensitive, exact comma-token match against the list
{code}

{{FileUtil.onlyExt(name)}} returns {{null}} for a name with no dot, and for a 
name whose only dot is leading (a hidden file such as {{.config}} is 
deliberately treated as having no extension, not as a {{config}} extension). 
Consequently, when an operator configures {{fileNameExtWhitelist}} with a 
concrete list such as {{txt,pdf}}, an upload whose file name has no extension 
({{noextension}}) or is a hidden file ({{.config}}) is accepted even though it 
is not on the list. That is inconsistent with the documented purpose of the 
option.

The sibling binding {{camel-platform-http-starter}} (apache/camel-spring-boot) 
already treats a file name with no extension as not accepted while a concrete 
list is configured; this change brings the core HTTP path in line with it.

h3. Proposed behaviour

Decide the list-level states first, and only then look at the extension:
# {{whitelist == null}} -> accept (no filter configured). Unchanged.
# {{whitelist}} equals {{*}} (case-insensitive) -> accept everything, including 
names with no extension and hidden files. Unchanged.
# a concrete list is configured and {{FileUtil.onlyExt(fileName)}} returns 
{{null}} (no extension / hidden file) -> *reject*. This is the change.
# otherwise -> the existing case-insensitive, exact comma-token match. 
Unchanged.

Only names with no extension (and hidden files) under a concrete list flip from 
accepted to rejected. The {{null}} list, the {{*}} list, and every ordinary 
extension match keep their current result.

h3. Classes

* {{camel-http-base}}: 
{{org.apache.camel.http.base.HttpHelper#isFileNameExtWhitelisted}} - reorder as 
above (single point of change). Correct the method Javadoc, which currently 
states a file is accepted "when the file name has no extension".
* {{camel-http-common}}: 
{{org.apache.camel.http.common.DefaultHttpBinding#isFileNameAccepted}} - no 
logic change (it delegates to the shared method); update its mirrored Javadoc 
to match.

No change is needed in the servlet, jetty or platform-http-vertx callers 
themselves: they all route through the one shared method.

h3. Tests

In {{camel-http-base}} {{HttpHelperFileNameExtWhitelistTest}} (keep the 
module's existing JUnit {{assertTrue}}/{{assertFalse}} style; do not introduce 
AssertJ as an outlier here):
* Change {{aNameWithoutAnExtensionIsAccepted}} so a name with no extension is 
now rejected under a concrete list: {{isFileNameExtWhitelisted("txt", 
"noextension")}} is {{false}}.
* Add: a hidden file (leading-dot name such as {{.config}}) is rejected under a 
concrete list.
* Add: a name with no extension, and a hidden file, are still accepted when the 
list is {{*}} and when it is {{null}} (guards the preserved wildcard / unset 
behaviour).
* Keep the existing exact-token, multi-dot and case-insensitivity tests 
unchanged.

Integration coverage already exists for the servlet and jetty bindings 
({{MultipartUploadFileNameExtWhitelistTest}}, 
{{MultiPartFormFileNameExtWhitelistTest}}); add a no-extension case to each in 
the same style, and an equivalent for {{camel-platform-http-vertx}} if one is 
not already present.

h3. Documentation

This is a behaviour change for anyone who has configured 
{{fileNameExtWhitelist}}, so it needs an upgrade-guide note on {{main}}: a file 
name with no extension (and a hidden file) is now rejected while a concrete 
list is configured; the {{*}} list and the unset default are unchanged; a 
deployment that relied on the previous behaviour should widen the list 
explicitly.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to