Skip to content

Commit e5f7933

Browse files
authored
fix: Avoid showing authorization value in exception (#111)
1 parent 07477d2 commit e5f7933

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/LaunchDarkly.EventSource/EventSourceService.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,20 @@ private HttpRequestMessage CreateHttpRequestMessage(Uri uri, string lastEventId)
199199
{
200200
foreach (var item in _configuration.RequestHeaders)
201201
{
202-
request.Headers.Add(item.Key, item.Value);
202+
try
203+
{
204+
request.Headers.Add(item.Key, item.Value);
205+
}
206+
catch (FormatException)
207+
{
208+
// Avoid showing the Authorization header value in the exception message
209+
if (item.Key.Equals("Authorization", StringComparison.OrdinalIgnoreCase))
210+
{
211+
throw new FormatException("The Authorization header is invalid.");
212+
}
213+
214+
throw;
215+
}
203216
}
204217
}
205218

test/LaunchDarkly.EventSource.Tests/EventSourceHttpBehaviorTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,37 @@ public void HttpRequestModifier()
165165
}
166166
}
167167

168+
[Fact]
169+
public async Task MalformedAuthorizationHeaderDoesNotExposeKeyInException()
170+
{
171+
using (var server = HttpServer.Start(EmptyStreamThatStaysOpen))
172+
{
173+
var taskComplete = new TaskCompletionSource<bool>();
174+
var errorMessage = string.Empty;
175+
176+
var invalidSdkKey = "\nsecret-api-key-with";
177+
var headers = new Dictionary<string, string> { { "Authorization", invalidSdkKey } };
178+
179+
using (var es = MakeEventSource(server.Uri, builder => builder.RequestHeaders(headers)))
180+
{
181+
es.Error += (sender, args) =>
182+
{
183+
errorMessage = args.Exception.Message;
184+
taskComplete.SetResult(true);
185+
};
186+
187+
_ = Task.Run(es.StartAsync);
188+
189+
// Wait for the error event with a timeout
190+
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(5));
191+
var completedTask = await Task.WhenAny(taskComplete.Task, timeoutTask);
192+
193+
Assert.True(completedTask == taskComplete.Task, "Test timed out waiting for error event");
194+
Assert.DoesNotContain(invalidSdkKey, errorMessage);
195+
}
196+
}
197+
}
198+
168199
[Fact]
169200
public async void OpenedEventIncludesHeaders()
170201
{

0 commit comments

Comments
 (0)