Sandor Molnar created KNOX-3610:
-----------------------------------
Summary: WebAppSec CORS filter fails with ClassCastException after
the Jakarta Servlet migration
Key: KNOX-3610
URL: https://issues.apache.org/jira/browse/KNOX-3610
Project: Apache Knox
Issue Type: Bug
Reporter: Sandor Molnar
Assignee: Sandor Molnar
Fix For: 3.1.0
h2. Problem
When CORS is enabled in the WebAppSec provider ({{cors.enabled=true}}), every
request to the topology fails with HTTP 500:
{noformat}
jakarta.servlet.ServletException: java.lang.ClassCastException: class
com.thetransactioncompany.cors.CORSFilter cannot be cast to class
jakarta.servlet.Filter
at
org.apache.knox.gateway.GatewayFilter$Holder.getInstance(GatewayFilter.java:424)
at
org.apache.knox.gateway.GatewayFilter$Holder.doFilter(GatewayFilter.java:390)
...
{noformat}
{{WebAppSecContributor}} adds {{com.thetransactioncompany.cors.CORSFilter}} to
the filter chain. That class comes from
{{com.thetransactioncompany:cors-filter:2.9.1}}, which is built against
{{javax.servlet}}: its {{init}} takes a {{javax.servlet.FilterConfig}}.
KNOX-3238 moved Knox to {{jakarta.servlet}} but kept this dependency.
{{GatewayFilter}} therefore can't cast the filter to {{jakarta.servlet.Filter}}.
Because the filter is instantiated on the first request, the topology still
deploys. Every request then returns 500, including requests that don't use CORS
at all, such as curl calls.
h3. Impact
* Any topology with {{cors.enabled=true}} is unusable. One example is the
documented KnoxIDF Polaris Console integration
({{knoxidf/integrations/polaris_console.md}}), which needs CORS on three
surfaces.
* The bug is a regression in the unreleased 3.1.0: no release tag contains
KNOX-3238.
* Tests didn't catch it. {{WebAppSecContributorTest}} checks only which filter
class is contributed and never instantiates it. No Docker Compose topology
enables CORS.
h3. Reproduce
# Add {{<param><name>cors.enabled</name><value>true</value></param>}} to a
topology's WebAppSec provider.
# Send any request to that topology (e.g. {{curl -k
https://localhost:8443/gateway/<topology>/...}}).
# The response is HTTP 500 with the {{ClassCastException}} above.
h3. Workaround
Set {{cors.enabled=false}}. Browser clients from another origin then don't work.
h2. Proposed solution
Replace the javax-only filter with a Jakarta-compatible CORS implementation,
and keep the existing {{cors.*}} configuration working:
* Candidates:
** {{org.eclipse.jetty.ee10.servlets.CrossOriginFilter}}.
{{jetty-ee10-servlets}} is already a dependency of
{{gateway-provider-security-webappsec}}, which uses it for {{DoSFilter}}.
** A small Knox-owned CORS filter.
** A Jakarta release of cors-filter, if one exists.
* Map the documented parameters ({{config_webappsec_provider.md}}) to the new
implementation:
** {{cors.allowOrigin}}, {{cors.allowSubdomains}}, {{cors.supportedMethods}},
{{cors.supportedHeaders}}, {{cors.exposedHeaders}},
{{cors.supportsCredentials}}, {{cors.maxAge}},
{{cors.allowGenericHttpRequests}}, {{cors.tagRequests}}.
** The Jetty filter uses different names ({{allowedOrigins}},
{{allowedMethods}}, {{allowedHeaders}}, {{exposedHeaders}},
{{allowCredentials}}, {{preflightMaxAge}}).
** Behavior also differs. cors-filter rejects a disallowed origin with 403, or
a disallowed method with 405, and {{allowGenericHttpRequests=false}} makes it
strict. The Jetty filter only omits the CORS response headers. Either keep the
old behavior or document the change.
* Remove {{cors-filter}} from the root and webappsec POMs.
h2. Acceptance criteria
* With {{cors.enabled=true}}, the topology serves requests, and a preflight
from an allowed origin gets the expected {{Access-Control-*}} headers.
* Every documented {{cors.*}} parameter keeps its documented meaning, or
{{config_webappsec_provider.md}} is updated where behavior changes.
* A test instantiates and runs the contributed CORS filter, so a
{{javax}}/{{jakarta}} mismatch fails the build.
* An integration test (Docker Compose) runs a preflight and a simple CORS
request against a topology with CORS enabled.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)