Skip to content

Commit 9514a57

Browse files
authored
http: avoid logging user info from URLs (#452)
1 parent 39052f3 commit 9514a57

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/main/java/me/itzg/helpers/http/FetchBuilderBase.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.netty.handler.codec.http.HttpStatusClass;
99
import io.netty.handler.codec.http.HttpUtil;
1010
import java.net.URI;
11+
import java.net.URISyntaxException;
1112
import java.nio.charset.StandardCharsets;
1213
import java.nio.file.Path;
1314
import java.time.ZoneId;
@@ -47,14 +48,37 @@ public class FetchBuilderBase<SELF extends FetchBuilderBase<SELF>> {
4748
static protected class State {
4849
private final SharedFetch sharedFetch;
4950
private final URI uri;
51+
private final String userInfo;
5052
public String userAgentCommand;
5153
private Set<String> acceptContentTypes;
5254
private final Map<String, String> requestHeaders = new HashMap<>();
5355

5456
State(URI uri, SharedFetch sharedFetch) {
5557
// Netty seems to half-way URL encode paths that have unicode,
5658
// 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+
}
5882
this.sharedFetch = sharedFetch;
5983
}
6084
}
@@ -242,12 +266,13 @@ protected void applyHeaders(io.netty.handler.codec.http.HttpHeaders headers) {
242266
);
243267
}
244268

245-
final String rawUserInfo = state.uri.getRawUserInfo();
246-
if (rawUserInfo != null) {
269+
if (state.userInfo != null) {
247270
headers.set(
248271
AUTHORIZATION.toString(),
249272
"Basic " +
250-
Base64.getEncoder().encodeToString(rawUserInfo.getBytes(StandardCharsets.UTF_8))
273+
Base64.getEncoder().encodeToString(
274+
state.userInfo.getBytes(StandardCharsets.UTF_8)
275+
)
251276
);
252277
}
253278

0 commit comments

Comments
 (0)