|
8 | 8 | import io.netty.handler.codec.http.HttpStatusClass; |
9 | 9 | import io.netty.handler.codec.http.HttpUtil; |
10 | 10 | import java.net.URI; |
| 11 | +import java.net.URISyntaxException; |
11 | 12 | import java.nio.charset.StandardCharsets; |
12 | 13 | import java.nio.file.Path; |
13 | 14 | import java.time.ZoneId; |
@@ -47,14 +48,37 @@ public class FetchBuilderBase<SELF extends FetchBuilderBase<SELF>> { |
47 | 48 | static protected class State { |
48 | 49 | private final SharedFetch sharedFetch; |
49 | 50 | private final URI uri; |
| 51 | + private final String userInfo; |
50 | 52 | public String userAgentCommand; |
51 | 53 | private Set<String> acceptContentTypes; |
52 | 54 | private final Map<String, String> requestHeaders = new HashMap<>(); |
53 | 55 |
|
54 | 56 | State(URI uri, SharedFetch sharedFetch) { |
55 | 57 | // Netty seems to half-way URL encode paths that have unicode, |
56 | 58 | // so instead we'll pre-"encode" the URI |
57 | | - this.uri = URI.create(uri.toASCIIString()); |
| 59 | + final URI encoded = URI.create(uri.toASCIIString()); |
| 60 | + |
| 61 | + if (uri.getRawUserInfo() != null) { |
| 62 | + this.userInfo = uri.getRawUserInfo(); |
| 63 | + try { |
| 64 | + this.uri = new URI( |
| 65 | + encoded.getScheme(), |
| 66 | + // just show first letter of username for sanity confirmation |
| 67 | + encoded.getRawUserInfo().charAt(0) + "***:***", |
| 68 | + encoded.getHost(), |
| 69 | + encoded.getPort(), |
| 70 | + encoded.getPath(), |
| 71 | + encoded.getQuery(), |
| 72 | + encoded.getFragment() |
| 73 | + ); |
| 74 | + } catch (URISyntaxException e) { |
| 75 | + throw new GenericException("Failed to redact user info", e); |
| 76 | + } |
| 77 | + } |
| 78 | + else { |
| 79 | + this.userInfo = null; |
| 80 | + this.uri = encoded; |
| 81 | + } |
58 | 82 | this.sharedFetch = sharedFetch; |
59 | 83 | } |
60 | 84 | } |
@@ -242,12 +266,13 @@ protected void applyHeaders(io.netty.handler.codec.http.HttpHeaders headers) { |
242 | 266 | ); |
243 | 267 | } |
244 | 268 |
|
245 | | - final String rawUserInfo = state.uri.getRawUserInfo(); |
246 | | - if (rawUserInfo != null) { |
| 269 | + if (state.userInfo != null) { |
247 | 270 | headers.set( |
248 | 271 | AUTHORIZATION.toString(), |
249 | 272 | "Basic " + |
250 | | - Base64.getEncoder().encodeToString(rawUserInfo.getBytes(StandardCharsets.UTF_8)) |
| 273 | + Base64.getEncoder().encodeToString( |
| 274 | + state.userInfo.getBytes(StandardCharsets.UTF_8) |
| 275 | + ) |
251 | 276 | ); |
252 | 277 | } |
253 | 278 |
|
|
0 commit comments