Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/LaunchDarkly.EventSource/EventSourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,20 @@ private HttpRequestMessage CreateHttpRequestMessage(Uri uri, string lastEventId)
{
foreach (var item in _configuration.RequestHeaders)
{
request.Headers.Add(item.Key, item.Value);
try
{
request.Headers.Add(item.Key, item.Value);
}
catch (FormatException)
{
// Avoid showing the Authorization header value in the exception message
if (item.Key.Equals("Authorization", StringComparison.OrdinalIgnoreCase))
{
throw new FormatException("The Authorization header is invalid.");
}

throw;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,39 @@
}
}

[Fact]
public async Task MalformedAuthorizationHeaderDoesNotExposeKeyInException()
{
using (var server = HttpServer.Start(EmptyStreamThatStaysOpen))
{
var taskComplete = new TaskCompletionSource<bool>();
var errorMessage = string.Empty;

var invalidSdkKey = "\nsecret-api-key-with";
var headers = new Dictionary<string, string> { { "Authorization", invalidSdkKey } };

using (var es = MakeEventSource(server.Uri, builder => builder.RequestHeaders(headers)))
{
es.Error += (sender, args) =>
{
errorMessage = args.Exception.Message;
taskComplete.SetResult(true);
};

_ = Task.Run(es.StartAsync);

// Wait for the error event with a timeout
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(5));
var completedTask = await Task.WhenAny(taskComplete.Task, timeoutTask);

Assert.True(completedTask == taskComplete.Task, "Test timed out waiting for error event");
Assert.DoesNotContain(invalidSdkKey, errorMessage);
}
}
}

[Fact]
public async void OpenedEventIncludesHeaders()

Check warning on line 200 in test/LaunchDarkly.EventSource.Tests/EventSourceHttpBehaviorTest.cs

View workflow job for this annotation

GitHub Actions / build-and-run (ubuntu-latest)

Support for 'async void' unit tests is being removed from xUnit.net v3. To simplify upgrading, convert the test to 'async Task' instead. (https://xunit.net/xunit.analyzers/rules/xUnit1048)

Check warning on line 200 in test/LaunchDarkly.EventSource.Tests/EventSourceHttpBehaviorTest.cs

View workflow job for this annotation

GitHub Actions / build-and-run (windows-latest)

Support for 'async void' unit tests is being removed from xUnit.net v3. To simplify upgrading, convert the test to 'async Task' instead. (https://xunit.net/xunit.analyzers/rules/xUnit1048)
{
var taskComplete = new TaskCompletionSource<bool>();

Expand All @@ -192,7 +223,7 @@
}
}

Assert.True(taskComplete.Task.Result);

Check warning on line 226 in test/LaunchDarkly.EventSource.Tests/EventSourceHttpBehaviorTest.cs

View workflow job for this annotation

GitHub Actions / build-and-run (ubuntu-latest)

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 226 in test/LaunchDarkly.EventSource.Tests/EventSourceHttpBehaviorTest.cs

View workflow job for this annotation

GitHub Actions / build-and-run (windows-latest)

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)
}

[Fact]
Expand Down
Loading