Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Strip non-ascii chars from headers
Browse files Browse the repository at this point in the history
Summary: Headers are specified to be ASCII: https://tools.ietf.org/html/rfc2616#section-4.2

Test Plan: None

Reviewed By: aiked

fbshipit-source-id: 8918d36
  • Loading branch information
illicitonion authored and facebook-github-bot committed Jan 19, 2017
1 parent e77264b commit b27a81b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/com/facebook/buck/artifact_cache/ArtifactCaches.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.facebook.buck.timing.DefaultClock;
import com.facebook.buck.util.AsyncCloseable;
import com.facebook.buck.util.HumanReadableException;
import com.google.common.base.CharMatcher;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -299,8 +300,9 @@ private static ArtifactCache createHttpArtifactCache(
storeClientBuilder.networkInterceptors().add(
chain -> chain.proceed(
chain.request().newBuilder()
.addHeader("X-BuckCache-User", System.getProperty("user.name", "<unknown>"))
.addHeader("X-BuckCache-Host", hostToReportToRemote)
.addHeader("X-BuckCache-User",
stripNonAscii(System.getProperty("user.name", "<unknown>")))
.addHeader("X-BuckCache-Host", stripNonAscii(hostToReportToRemote))
.build()));
int timeoutSeconds = cacheDescription.getTimeoutSeconds();
setTimeouts(storeClientBuilder, timeoutSeconds);
Expand Down Expand Up @@ -390,6 +392,17 @@ private static ArtifactCache createHttpArtifactCache(
.build());
}

private static String stripNonAscii(String str) {
if (CharMatcher.ASCII.matchesAllOf(str)) {
return str;
}
StringBuilder builder = new StringBuilder();
for (char c : str.toCharArray()) {
builder.append(CharMatcher.ASCII.matches(c) ? c : '?');
}
return builder.toString();
}

private static OkHttpClient.Builder setTimeouts(
OkHttpClient.Builder builder,
int timeoutSeconds) {
Expand Down

0 comments on commit b27a81b

Please sign in to comment.