View Javadoc
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.apache;
20  
21  import java.net.URI;
22  import java.util.Collection;
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  import org.apache.http.HttpHost;
27  import org.apache.http.HttpRequest;
28  import org.apache.http.HttpRequestInterceptor;
29  import org.apache.http.protocol.HttpContext;
30  import org.apache.http.protocol.HttpCoreContext;
31  import org.eclipse.aether.util.connector.transport.TransporterUtils;
32  
33  /**
34   * Scopes operator-configured request headers ({@code aether.transport.http.headers}) to the repository origin.
35   * <p>
36   * Configured headers are stamped on every request the transport creates and frequently carry credentials
37   * ({@code Authorization}, cookies, private token headers). Apache HttpClient's redirect execution re-sends the
38   * original request headers on each redirect hop, including hops that leave the repository origin, which would
39   * replay those credentials to the redirect target. This interceptor runs in the protocol layer - which HttpClient
40   * re-enters for every redirect hop - and removes the configured headers from any request whose target host is not
41   * the repository origin (same scheme, host and effective port). Challenge- and preemptive-authentication headers
42   * are attached below the protocol layer and are host-scoped by the credentials provider already; they are not
43   * affected by this interceptor.
44   *
45   * @since 2.0.23
46   */
47  final class OriginScopedHeadersInterceptor implements HttpRequestInterceptor {
48      private final HttpHost origin;
49  
50      private final Set<String> headerNames;
51  
52      OriginScopedHeadersInterceptor(HttpHost origin, Collection<?> headerNames) {
53          this.origin = origin;
54          this.headerNames = new HashSet<>();
55          for (Object headerName : headerNames) {
56              if (headerName != null) {
57                  this.headerNames.add(String.valueOf(headerName));
58              }
59          }
60      }
61  
62      @Override
63      public void process(HttpRequest request, HttpContext context) {
64          if (headerNames.isEmpty()) {
65              return;
66          }
67          Object attribute = context != null ? context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST) : null;
68          // fail closed: when the target host cannot be determined, do not attach configured headers
69          if (!(attribute instanceof HttpHost)
70                  || !TransporterUtils.isSameOrigin(
71                          URI.create(origin.toURI()), URI.create(((HttpHost) attribute).toURI()))) {
72              for (String headerName : headerNames) {
73                  request.removeHeaders(headerName);
74              }
75          }
76      }
77  }