davsclaus commented on code in PR #27945: URL: https://github.com/apache/camel/pull/27945#discussion_r4295630250
########## docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc: ########## @@ -523,6 +523,21 @@ registered. To restore the previous behaviour, set `webhookSecretTokenEnabled=false`: the webhook is then registered without a secret token, and webhook requests are not verified. +=== camel-netty-http - the security constraint resolves dot-segments in the request path + +A `netty-http` consumer configured with a `securityConstraint` now resolves `.` and `..` segments in the request path +(including their percent-encoded `%2e` form) before the path is matched against the configured inclusions and exclusions, +following the `remove_dot_segments` algorithm of RFC 3986. The constraint is therefore evaluated against the effective +resolved path, which is the same resource the request is routed to. Excess `..` segments cannot climb above the root. Review Comment: "which is the same resource the request is routed to" is not quite right: netty-http routes on the raw path and passes the unresolved path to the route in `CamelHttpPath`. Worth rewording once the approach is settled. ########## components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpBasicAuthConstraintDotSegmentTest.java: ########## @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.netty.http; + +import org.apache.camel.BindToRegistry; +import org.apache.camel.CamelExecutionException; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +import static org.apache.camel.test.junit6.TestSupport.assertIsInstanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * A request containing . or .. segments is routed to the consumer without resolving those segments, so the security + * constraint has to be evaluated against the effective resolved path. Otherwise a request such as /foo/../admin/x (or + * its percent-encoded /foo/%2e%2e/admin/x form) reaches the route while skipping the constraint that guards /admin/*. + */ +class NettyHttpBasicAuthConstraintDotSegmentTest extends BaseNettyTestSupport { + + @Override + public void doPreSetup() { + System.setProperty("java.security.auth.login.config", "src/test/resources/myjaas.config"); + } + + @Override + public void doPostTearDown() { + System.clearProperty("java.security.auth.login.config"); + } + + @BindToRegistry("mySecurityConfig") + public NettyHttpSecurityConfiguration loadSecConf() { + NettyHttpSecurityConfiguration security = new NettyHttpSecurityConfiguration(); + security.setRealm("karaf"); + SecurityAuthenticator auth = new JAASSecurityAuthenticator(); + auth.setName("karaf"); + security.setSecurityAuthenticator(auth); + + // a specific inclusion, not a catch-all: only /admin/* below the endpoint path is restricted + SecurityConstraintMapping matcher = new SecurityConstraintMapping(); + matcher.addInclusion("/admin/*"); + security.setSecurityConstraint(matcher); + + return security; + } + + @Test + void plainPathIsChallenged() { + assertChallenged("netty-http:http://localhost:{{port}}/foo/admin/x"); + } + + @Test + void literalDotSegmentIsChallenged() { + // without resolving the .. the constraint target is /../admin/x, which matches no inclusion + assertChallenged("netty-http:http://localhost:{{port}}/foo/../admin/x"); Review Comment: `/foo/../admin/x` resolves to `/admin/x`, which is outside the `/foo` context, so the context-path strip does not apply. It only matches because the absolute `/admin/x` happens to look like the endpoint-relative `/admin/*`. Could the test use an in-context traversal such as `/foo/x/../admin/y` (and `/foo/x/%2e%2e/admin/y` for the encoded case), plus the reverse case `/foo/admin/../public/x` with a `/public/*` exclusion? ########## components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java: ########## @@ -218,7 +218,11 @@ protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Except } private String extractTarget(URI uri) { - String target = uri.getPath(); + // resolve any . and .. segments first, so the security constraint is evaluated against the + // effective resolved resource path and not the raw request path. Without this a request such + // as /foo/../admin/x (or its percent-encoded /foo/%2e%2e/admin/x form) is matched against the + // inclusions in its unresolved form and can evade a constraint that guards /admin/* (CAMEL-25705) + String target = removeDotSegments(uri.getPath()); Review Comment: Dispatch (`HttpServerMultiplexChannelHandler.getHandler` uses the raw `request.uri()`) and the route (`CamelHttpPath` comes from `uri.getRawPath()` in `DefaultNettyHttpBinding.stripPath`) do not resolve dot-segments, so after this change the constraint and the route look at different paths. For example, `/foo/admin/../public/x` with a `/public/*` exclusion is now unauthenticated, while the route sees `CamelHttpPath=/admin/../public/x`. One option is to refuse such requests when a constraint is configured, for example: ```java String requestPath = uri.getPath(); if (!requestPath.equals(removeDotSegments(requestPath))) { // the constraint and the route would see different paths: refuse it // ... reply 400 Bad Request and close, like the other early returns } ``` The other is to call `restricted(...)` on both the raw and the resolved target, and challenge if either is restricted. -- 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]
