[
https://issues.apache.org/jira/browse/KNOX-3610?focusedWorklogId=1057729&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-1057729
]
ASF GitHub Bot logged work on KNOX-3610:
----------------------------------------
Author: ASF GitHub Bot
Created on: 30/Nov/26 21:43
Start Date: 30/Nov/26 21:43
Worklog Time Spent: 10m
Work Description: smolnar82 opened a new pull request, #1555:
URL: https://github.com/apache/knox/pull/1555
[KNOX-3610](https://issues.apache.org/jira/browse/KNOX-3610) - Replace the
javax-based cors-filter with a jakarta CORS filter
## What changes were proposed in this pull request?
When `cors.enabled=true`, every request to the topology failed with HTTP 500
(`ClassCastException: com.thetransactioncompany.cors.CORSFilter cannot be cast
to jakarta.servlet.Filter`). `cors-filter` 2.9.1 is javax-based, and Knox has
been on jakarta since KNOX-3238. This PR replaces it with a filter owned by
Knox.
- **New `org.apache.knox.gateway.webappsec.filter.CORSFilter`** (jakarta),
wired in by `WebAppSecContributor`. It reproduces the semantics of
`cors-filter` 2.9.1 for every documented `cors.*` parameter:
- the same defaults;
- preflight handling: Knox answers the preflight itself and doesn't
continue the chain;
- the same error statuses and bodies: 403 for a denied origin or an
unsupported header, 405 for an unsupported method, 400 for an invalid header,
403 for a non-CORS request when `allowGenericHttpRequests=false`;
- credentials and `Vary: Origin` handling;
- subdomain matching and request tagging;
- CORS headers kept across `response.reset()`.
I didn't use Jetty's ee10 `CrossOriginFilter`. It is deprecated and
behaves differently: no 403/405 responses and no strict mode.
- **Parameter names are now case-insensitive.** `WebAppSecContributor`
lowercases `cors.*` parameter names, but `cors-filter` looked them up in
camelCase. So in every release since 2.0.0, all `cors.*` settings except
`cors.enabled` were silently ignored, and the filter always allowed **any
origin, with credentials**. The new filter honors the configured values.
- **Behavior change:** deployments with a restrictive `cors.allowOrigin`
(or methods/headers) will now have it enforced. The docs include an upgrade
note.
- **Invalid values** (a malformed origin, a non-numeric `maxAge`, a
non-boolean flag) now fail filter init with a message naming the parameter.
- **Dependencies:** `com.thetransactioncompany:cors-filter` (and with it
`java-property-utils`) is removed from the root and webappsec POMs.
- **Docs** (`config_webappsec_provider.md`):
- parameter names are case-insensitive, and lists may be separated by
commas and/or whitespace;
- how preflights are handled, and that same-origin requests aren't treated
as CORS;
- with credentials on, the origin is echoed even when `allowOrigin=*`;
- what happens on invalid values;
- the upgrade note.
## How was this patch tested?
- `mvn -pl gateway-provider-security-webappsec verify` passes, including
checkstyle, PMD and spotbugs.
- New `CORSFilterTest` (19 tests):
- requests without an Origin, defaults, wildcard with and without
credentials, exposed headers;
- denied origins, unsupported methods and headers, invalid headers,
preflight responses and echoed request headers;
- subdomains (including a scheme mismatch), strict mode, same-origin
requests, request tagging;
- case-insensitive parameter names, headers kept across reset, invalid
config.
- `WebAppSecContributorTest.testContributedCORSFilterServesRequests`
contributes the filter with camelCase params, then loads the contributed class
with the context class loader and casts it to `jakarta.servlet.Filter`, as
`GatewayFilter` does. It initializes the filter with the descriptor params and
runs a plain request, a preflight and a denied-origin request through it.
- Reproduced the bug first: on the old jar both new CI topologies returned
500 with the `ClassCastException` in `gateway.log`.
- Docker Compose E2E, run locally on this branch: 205 passed, pylint
10.00/10.
## Integration Tests
- New topologies:
- `cors.xml`: an origin allow list, subdomains, restricted methods and
headers, exposed headers, credentials, `maxAge=600`.
- `cors-strict.xml`: `allowGenericHttpRequests=false`, no credentials.
Both are added to the CI Dockerfile.
- New `test_cors.py` (9 tests, plain HTTP requests, no browser):
- `cors`:
- a request without an Origin passes through with no CORS headers;
- a preflight from an allowed origin returns all the `Access-Control-*`
headers and `Vary`;
- a simple GET returns `Allow-Origin`, `Allow-Credentials` and
`Expose-Headers`;
- an allowed subdomain is accepted;
- a denied origin gets 403 on both the actual request and the preflight;
- an unsupported method gets 405 (preflight and actual);
- an unsupported header gets 403.
- `cors-strict`:
- a request without an Origin gets 403;
- any origin gets `*` without credentials;
- a preflight returns the default methods.
## Performance implications
Negligible. The filter only runs on topologies with `cors.enabled=true`.
Configuration is parsed once in `init()`. Each request does a few header
lookups and, for CORS requests, a set lookup for the origin and the request
headers. The filter makes no remote calls and keeps no shared state. Preflights
are answered before authentication and dispatch, as before.
Issue Time Tracking
-------------------
Worklog Id: (was: 1057729)
Remaining Estimate: 0h
Time Spent: 10m
> 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
> Priority: Blocker
> Fix For: 3.1.0
>
> Time Spent: 10m
> Remaining Estimate: 0h
>
> 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)