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. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
