Repository navigation
match redirect authority in http.redirect.allowed.uri check #3624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
rootvector2
wants to merge
1
commit into
apache:main
from
rootvector2:redirect-allowed-uri-authority
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
...s/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitAllowedRedirectUriTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /** | ||
| * 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.cxf.transport.http; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.net.InetAddress; | ||
| import java.net.InetSocketAddress; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.TreeMap; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import com.sun.net.httpserver.HttpServer; | ||
|
|
||
| import org.apache.cxf.Bus; | ||
| import org.apache.cxf.bus.extension.ExtensionManagerBus; | ||
| import org.apache.cxf.configuration.security.AuthorizationPolicy; | ||
| import org.apache.cxf.message.Exchange; | ||
| import org.apache.cxf.message.ExchangeImpl; | ||
| import org.apache.cxf.message.Message; | ||
| import org.apache.cxf.message.MessageImpl; | ||
| import org.apache.cxf.service.model.EndpointInfo; | ||
| import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| public class HTTPConduitAllowedRedirectUriTest { | ||
|
|
||
| private HttpServer redirectingServer; | ||
| private HttpServer otherServer; | ||
| private final AtomicReference<String> otherServerAuthorization = new AtomicReference<>(); | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| if (redirectingServer != null) { | ||
| redirectingServer.stop(0); | ||
| } | ||
| if (otherServer != null) { | ||
| otherServer.stop(0); | ||
| } | ||
| } | ||
|
|
||
| private HttpServer startServer(String host) throws IOException { | ||
| HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getByName(host), 0), 0); | ||
| server.start(); | ||
| return server; | ||
| } | ||
|
|
||
| private void startServers() throws IOException { | ||
| otherServer = startServer("127.0.0.1"); | ||
| otherServer.createContext("/", exchange -> { | ||
| otherServerAuthorization.set(exchange.getRequestHeaders().getFirst("Authorization")); | ||
| exchange.sendResponseHeaders(200, -1); | ||
| exchange.close(); | ||
| }); | ||
| redirectingServer = startServer("localhost"); | ||
| } | ||
|
|
||
| private void redirectTo(String location) { | ||
| redirectingServer.createContext("/", exchange -> { | ||
| exchange.getResponseHeaders().add("Location", location); | ||
| exchange.sendResponseHeaders(302, -1); | ||
| exchange.close(); | ||
| }); | ||
| } | ||
|
|
||
| private void invoke(String allowedRedirectUri) throws IOException { | ||
| Bus bus = new ExtensionManagerBus(); | ||
| EndpointInfo ei = new EndpointInfo(); | ||
| ei.setAddress(getRedirectingServerAddress() + "/service"); | ||
|
|
||
| HTTPConduit conduit = new URLConnectionHTTPConduit(bus, ei, null); | ||
| HTTPClientPolicy policy = new HTTPClientPolicy(); | ||
| policy.setAutoRedirect(true); | ||
| policy.setConnectionTimeout(5000); | ||
| policy.setReceiveTimeout(5000); | ||
| conduit.setClient(policy); | ||
| AuthorizationPolicy authPolicy = new AuthorizationPolicy(); | ||
| authPolicy.setUserName("user"); | ||
| authPolicy.setPassword("secret"); | ||
| conduit.setAuthorization(authPolicy); | ||
| conduit.finalizeConfig(); | ||
| conduit.setMessageObserver(m -> { }); | ||
|
|
||
| Message message = new MessageImpl(); | ||
| Map<String, List<String>> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); | ||
| List<String> contentTypes = new ArrayList<>(); | ||
| contentTypes.add("text/xml"); | ||
| headers.put("content-type", contentTypes); | ||
| message.put(Message.PROTOCOL_HEADERS, headers); | ||
| message.put(Message.HTTP_REQUEST_METHOD, "GET"); | ||
| message.put("http.redirect.allowed.uri", allowedRedirectUri); | ||
| Exchange exchange = new ExchangeImpl(); | ||
| exchange.setOutMessage(message); | ||
| message.setExchange(exchange); | ||
|
|
||
| conduit.prepare(message); | ||
| OutputStream os = message.getContent(OutputStream.class); | ||
| os.close(); | ||
| } | ||
|
|
||
| private String getRedirectingServerAddress() { | ||
| return "http://localhost:" + redirectingServer.getAddress().getPort(); | ||
| } | ||
|
|
||
| private String getOtherServerAddress() { | ||
| return "http://127.0.0.1:" + otherServer.getAddress().getPort(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRedirectToOtherHostViaUserInfoIsRejected() throws Exception { | ||
| startServers(); | ||
| // http://localhost:<port>@127.0.0.1:<otherPort>/ starts with the allowed URI, | ||
| // but its host is 127.0.0.1 | ||
| String allowed = getRedirectingServerAddress(); | ||
| redirectTo(allowed + "@" + getOtherServerAddress().substring("http://".length()) + "/"); | ||
|
|
||
| try { | ||
| invoke(allowed); | ||
| fail("Redirect to a different host must be rejected"); | ||
| } catch (IOException ex) { | ||
| // expected | ||
| } | ||
| assertNull("Credentials must not be sent to the redirect target", otherServerAuthorization.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRedirectToAllowedUriIsFollowed() throws Exception { | ||
| startServers(); | ||
| redirectTo(getOtherServerAddress() + "/service"); | ||
| invoke(getOtherServerAddress() + "/"); | ||
| assertEquals("Basic dXNlcjpzZWNyZXQ=", otherServerAuthorization.get()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are 2 properties in play here:
AUTO_REDIRECT_SAME_HOST_ONLY: this one only allow redirects to the same scheme/host and will address your concern I believeAUTO_REDIRECT_ALLOWED_URI: freestyle prefix match (the user could make the prefix as strict as possible)Hope it makes sense, thank you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. With
http.redirect.same.host.onlyset, the@evil.exampleand.evil.exampleredirects are already rejected, and a configured value with a trailing/(e.g.https://api.example.com/) closes it forhttp.redirect.allowed.uritoo. So this is a config concern rather than a bug in the check. Happy to close this, or turn it into a short doc note on the property that the value is a raw prefix match, whichever you prefer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will update docs shortly, seems like we never documented those, the pr could be closed, thanks @rootvector2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, closing this one. A note that
http.redirect.allowed.uriis a raw prefix match, so the value should end with/, would cover the case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://cxf.apache.org/docs/client-http-transport-including-ssl-support.html