Skip to content

Commit 661c171

Browse files
committed
Better truncation of sensitive values
1 parent 751b79e commit 661c171

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

common/src/main/java/xyz/gianlu/librespot/common/Utils.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ public static String randomString(@NotNull Random random, int length) {
4949
return new String(chars);
5050
}
5151

52+
@NotNull
53+
public static String truncateMiddle(@NotNull String str, int length) {
54+
if (length <= 1) throw new IllegalStateException();
55+
56+
int first = length / 2;
57+
String result = str.substring(0, first);
58+
result += "...";
59+
result += str.substring(str.length() - (length - first));
60+
return result;
61+
}
62+
5263
@NotNull
5364
public static String readLine(@NotNull InputStream in) throws IOException {
5465
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

core/src/main/java/xyz/gianlu/librespot/connectstate/DeviceStateHandler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import xyz.gianlu.librespot.Version;
1717
import xyz.gianlu.librespot.common.AsyncWorker;
1818
import xyz.gianlu.librespot.common.ProtoUtils;
19+
import xyz.gianlu.librespot.common.Utils;
1920
import xyz.gianlu.librespot.core.Session;
2021
import xyz.gianlu.librespot.core.TimeProvider;
2122
import xyz.gianlu.librespot.dealer.DealerClient;
@@ -245,11 +246,11 @@ private void putConnectState(@NotNull Connect.PutStateRequest req) {
245246
try {
246247
session.api().putConnectState(connectionId, req);
247248
if (LOGGER.getLevel().isLessSpecificThan(Level.TRACE)) {
248-
LOGGER.info("Put state. {ts: {}, connId: {}[truncated], reason: {}, request: {}}", req.getClientSideTimestamp(),
249-
connectionId.substring(0, 6), req.getPutStateReason(), TextFormat.shortDebugString(putState));
249+
LOGGER.info("Put state. {ts: {}, connId: {}, reason: {}, request: {}}", req.getClientSideTimestamp(),
250+
Utils.truncateMiddle(connectionId, 10), req.getPutStateReason(), TextFormat.shortDebugString(putState));
250251
} else {
251-
LOGGER.info("Put state. {ts: {}, connId: {}[truncated], reason: {}}", req.getClientSideTimestamp(),
252-
connectionId.substring(0, 6), req.getPutStateReason());
252+
LOGGER.info("Put state. {ts: {}, connId: {}, reason: {}}", req.getClientSideTimestamp(),
253+
Utils.truncateMiddle(connectionId, 10), req.getPutStateReason());
253254
}
254255
} catch (IOException | MercuryClient.MercuryException ex) {
255256
LOGGER.error("Failed updating state.", ex);

core/src/main/java/xyz/gianlu/librespot/core/TokenProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.apache.logging.log4j.Logger;
66
import org.jetbrains.annotations.NotNull;
77
import org.jetbrains.annotations.Nullable;
8+
import xyz.gianlu.librespot.common.Utils;
89
import xyz.gianlu.librespot.mercury.MercuryClient;
910
import xyz.gianlu.librespot.mercury.MercuryRequests;
1011

@@ -86,8 +87,8 @@ public boolean expired() {
8687
public String toString() {
8788
return "StoredToken{" +
8889
"expiresIn=" + expiresIn +
89-
", accessToken='" + accessToken.substring(0, 10) + "[truncated]'" +
90-
", scopes=" + Arrays.toString(scopes) +
90+
", accessToken='" + Utils.truncateMiddle(accessToken, 12) +
91+
"', scopes=" + Arrays.toString(scopes) +
9192
", timestamp=" + timestamp +
9293
'}';
9394
}

0 commit comments

Comments
 (0)