|
16 | 16 |
|
17 | 17 | package dev.onvoid.webrtc.client; |
18 | 18 |
|
| 19 | +import java.nio.ByteBuffer; |
| 20 | +import java.nio.CharBuffer; |
| 21 | +import java.nio.charset.CodingErrorAction; |
19 | 22 | import java.nio.charset.StandardCharsets; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.function.UnaryOperator; |
20 | 25 |
|
21 | 26 | /** |
22 | 27 | * A message received on a {@link DataChannel}. It holds a copy of the data and |
@@ -62,8 +67,93 @@ public int size() { |
62 | 67 | return data.length; |
63 | 68 | } |
64 | 69 |
|
| 70 | + /** |
| 71 | + * Returns message type and byte length, without exposing payload data. |
| 72 | + * Use {@link #text()}, {@link #bytes()} or {@link #preview(int)} explicitly |
| 73 | + * when content is needed. |
| 74 | + */ |
65 | 75 | @Override |
66 | 76 | public String toString() { |
67 | | - return binary ? "Message[" + data.length + " bytes]" : "Message[" + text() + "]"; |
| 77 | + return "Message[type=" + (binary ? "binary" : "text") + ", length=" + data.length + " bytes]"; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns metadata with an escaped, bounded payload preview. Text uses |
| 82 | + * UTF-8; binary data uses hexadecimal. An incomplete trailing UTF-8 |
| 83 | + * character is omitted. Control characters are escaped for single-line logs. |
| 84 | + * |
| 85 | + * @param maxBytes maximum input bytes to inspect; must be nonnegative. |
| 86 | + * @return the diagnostic representation. |
| 87 | + */ |
| 88 | + public String preview(int maxBytes) { |
| 89 | + return preview(maxBytes, UnaryOperator.identity()); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns a bounded preview with application-defined masking, applied before |
| 94 | + * escaping. The mask receives only the bounded prefix, so masking rules must |
| 95 | + * also handle secrets cut off at its end. Its result is limited to twice |
| 96 | + * {@code maxBytes} UTF-16 units (enough for hexadecimal), then escaped. |
| 97 | + * At most six output characters are used per UTF-16 unit. |
| 98 | + * |
| 99 | + * @param maxBytes maximum input bytes to inspect; must be nonnegative. |
| 100 | + * @param mask transforms the prefix to remove sensitive values; must return |
| 101 | + * a non-null string. |
| 102 | + * @return metadata and the masked preview, with a truncation indicator. |
| 103 | + * @throws IllegalArgumentException if maxBytes is negative. |
| 104 | + * @throws NullPointerException if mask or its result is null. |
| 105 | + */ |
| 106 | + public String preview(int maxBytes, UnaryOperator<String> mask) { |
| 107 | + if (maxBytes < 0) { |
| 108 | + throw new IllegalArgumentException("Negative preview limit"); |
| 109 | + } |
| 110 | + Objects.requireNonNull(mask, "mask"); |
| 111 | + int count = Math.min(data.length, maxBytes); |
| 112 | + String prefix; |
| 113 | + boolean truncated = count < data.length; |
| 114 | + if (binary) { |
| 115 | + StringBuilder hex = new StringBuilder(); |
| 116 | + for (int i = 0; i < count; i++) { |
| 117 | + int value = data[i] & 0xff; |
| 118 | + hex.append(Character.forDigit(value >>> 4, 16)); |
| 119 | + hex.append(Character.forDigit(value & 15, 16)); |
| 120 | + } |
| 121 | + prefix = hex.toString(); |
| 122 | + } |
| 123 | + else { |
| 124 | + ByteBuffer input = ByteBuffer.wrap(data, 0, count); |
| 125 | + CharBuffer output = CharBuffer.allocate(count); |
| 126 | + StandardCharsets.UTF_8.newDecoder() |
| 127 | + .onMalformedInput(CodingErrorAction.REPLACE) |
| 128 | + .onUnmappableCharacter(CodingErrorAction.REPLACE) |
| 129 | + .decode(input, output, count == data.length); |
| 130 | + truncated |= input.hasRemaining(); |
| 131 | + output.flip(); |
| 132 | + prefix = output.toString(); |
| 133 | + } |
| 134 | + String masked = Objects.requireNonNull(mask.apply(prefix), "mask result"); |
| 135 | + int end = (int) Math.min(masked.length(), 2L * maxBytes); |
| 136 | + if (end > 0 && end < masked.length() && Character.isHighSurrogate(masked.charAt(end - 1))) { |
| 137 | + end--; |
| 138 | + } |
| 139 | + truncated |= end < masked.length(); |
| 140 | + StringBuilder escaped = new StringBuilder(); |
| 141 | + for (int i = 0; i < end; i++) { |
| 142 | + char ch = masked.charAt(i); |
| 143 | + if (ch == '\\' || ch == '"') { |
| 144 | + escaped.append('\\').append(ch); |
| 145 | + } |
| 146 | + else if (Character.isISOControl(ch) || Character.getType(ch) == Character.FORMAT |
| 147 | + || ch == '\u2028' || ch == '\u2029') { |
| 148 | + escaped.append("\\u"); |
| 149 | + for (int shift = 12; shift >= 0; shift -= 4) { |
| 150 | + escaped.append(Character.forDigit((ch >>> shift) & 15, 16)); |
| 151 | + } |
| 152 | + } |
| 153 | + else { |
| 154 | + escaped.append(ch); |
| 155 | + } |
| 156 | + } |
| 157 | + return toString() + " preview=\"" + escaped + "\"" + (truncated ? " (truncated)" : ""); |
68 | 158 | } |
69 | 159 | } |
0 commit comments