| | | 1 | | namespace AsyncResponse; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Applies the wire policy for a remote exception stack trace shared across the durable channels: |
| | | 5 | | /// whether it travels on the wire at all, and a hard length cap so a buggy or hostile remote cannot |
| | | 6 | | /// push a multi-megabyte trace into the envelope (and from there into generic exception logs). |
| | | 7 | | /// </summary> |
| | | 8 | | internal static class RemoteStackTrace |
| | | 9 | | { |
| | | 10 | | private const string TruncationMarker = "… [stack trace truncated by AsyncResponse]"; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// The publish-side policy: returns <c>null</c> when <paramref name="include"/> is <c>false</c>, |
| | | 14 | | /// otherwise the trace capped to <paramref name="maxLength"/>. |
| | | 15 | | /// </summary> |
| | | 16 | | public static string? ForWire(string? stackTrace, bool include, int maxLength) |
| | 3 | 17 | | => include ? Cap(stackTrace, maxLength) : null; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Truncates <paramref name="stackTrace"/> to <paramref name="maxLength"/> characters, appending a |
| | | 21 | | /// marker when truncated. Used on both publish (bound what we emit) and receive (bound what we |
| | | 22 | | /// accept from a remote we do not control). A non-positive cap leaves the input unchanged so a |
| | | 23 | | /// misconfiguration cannot silently erase diagnostics. |
| | | 24 | | /// </summary> |
| | | 25 | | public static string? Cap(string? stackTrace, int maxLength) |
| | | 26 | | { |
| | 3 | 27 | | if (string.IsNullOrEmpty(stackTrace) || maxLength <= 0 || stackTrace.Length <= maxLength) |
| | 3 | 28 | | return stackTrace; |
| | | 29 | | |
| | 3 | 30 | | return string.Concat(stackTrace.AsSpan(0, maxLength), TruncationMarker); |
| | | 31 | | } |
| | | 32 | | } |