1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.eclipse.aether.transport.jetty;
20
21 import java.net.URI;
22 import java.util.Collection;
23 import java.util.Set;
24 import java.util.TreeSet;
25
26 import org.eclipse.jetty.client.Request;
27
28 /**
29 * A client-level request headers listener that scopes operator-configured request headers
30 * ({@code aether.transport.http.headers}) and preemptively applied {@code Authorization} to the repository origin.
31 * <p>
32 * Configured headers are stamped on every request the transporter creates and frequently carry credentials
33 * ({@code Authorization}, cookies, private token headers). Jetty's redirector copies the original request headers
34 * onto every redirect hop it follows - including hops that leave the repository origin - which would replay those
35 * credentials to the redirect target host. Since each Jetty transporter instance is bound to exactly one
36 * repository, and copied redirect requests pass through the client-level request listeners again (the same
37 * mechanism {@link InsecureRedirectGuard} relies on), removing the scoped headers from any request whose target is
38 * not the repository origin (same scheme, case-insensitive host and effective port) confines them to the
39 * repository itself. Challenge-based authentication from the authentication store is URI-scoped by Jetty already
40 * and is unaffected. Can be disabled via
41 * {@link JettyTransporterConfigurationKeys#CONFIG_PROP_ORIGIN_SCOPED_HEADERS}.
42 * <p>
43 * Registered as a {@link Request.HeadersListener} rather than a {@link Request.QueuedListener} so that the
44 * callback fires <em>after</em> Jetty's content decoder factories have populated the {@code Accept-Encoding}
45 * header.
46 *
47 * @since 2.0.23
48 */
49 final class OriginScopedHeadersListener implements Request.HeadersListener {
50 private final URI origin;
51
52 private final Set<String> scopedHeaderNames;
53
54 OriginScopedHeadersListener(URI origin, Collection<String> scopedHeaderNames) {
55 this.origin = origin;
56 this.scopedHeaderNames = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
57 this.scopedHeaderNames.addAll(scopedHeaderNames);
58 }
59
60 @Override
61 public void onHeaders(Request request) {
62 if (scopedHeaderNames.isEmpty()) {
63 return;
64 }
65 URI target = request.getURI();
66 // fail closed: when the target cannot be determined, do not send the scoped headers
67 if (target == null || !isSameOrigin(origin, target)) {
68 request.headers(headers -> {
69 for (String scopedHeaderName : scopedHeaderNames) {
70 headers.remove(scopedHeaderName);
71 }
72 });
73 }
74 }
75
76 static boolean isSameOrigin(URI origin, URI target) {
77 if (origin.getScheme() == null
78 || origin.getHost() == null
79 || target.getScheme() == null
80 || target.getHost() == null) {
81 return false;
82 }
83 return origin.getScheme().equalsIgnoreCase(target.getScheme())
84 && origin.getHost().equalsIgnoreCase(target.getHost())
85 && effectivePort(origin.getScheme(), origin.getPort())
86 == effectivePort(target.getScheme(), target.getPort());
87 }
88
89 /**
90 * Determines the effective port: the explicit port if present, otherwise the default port implied by the
91 * scheme.
92 */
93 static int effectivePort(String scheme, int port) {
94 if (port >= 0) {
95 return port;
96 }
97 return "https".equalsIgnoreCase(scheme) ? 443 : 80;
98 }
99 }