Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QCAlgorithm.Download incompatible with S3 Authentication Headers #8030

Closed
4 tasks done
Grosner300 opened this issue May 15, 2024 · 1 comment · Fixed by #8055
Closed
4 tasks done

QCAlgorithm.Download incompatible with S3 Authentication Headers #8030

Grosner300 opened this issue May 15, 2024 · 1 comment · Fixed by #8055
Assignees
Labels

Comments

@Grosner300
Copy link

Expected Behavior

QCAlgorithm.Download permits AWS S3 Authentication Headers. The code for Download appears to have changed in #7976 from WebClient to HttpClient which adds strict header validation when using .Add to add a header

Actual Behavior

Exception:
Unhandled exception. System.FormatException: The format of value 'AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20130524/us-east-1/s3/aws4_request, SignedHeaders=host;range;x-amz-date,Signature=EXAMPLE_SIGNATURE' is invalid.

Potential Solution

In Api.cs DownloadBytes, use
client.Value.TryAddWithoutValidation(header.Key, header.Value); instead of client.Value.DefaultRequestHeaders.Add(header.Key, header.Value);

Reproducing the Problem

In QuantConnect
var headers = new Dictionary<string, string>
{
	{"Authorization", "AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20130524/us-east-1/s3/aws4_request, SignedHeaders=host;range;x-amz-date,Signature=EXAMPLE_SIGNATURE"}
};
Download("https://www.example.com", headers.ToList());
Unit Test
[Test, Parallelizable(ParallelScope.Self)]
public void Download_With_S3_Authentication_Header_Successfully()
{
    var algo = new QCAlgorithm();
    algo.SetApi(new Api.Api());

    var headers = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("Authorization","AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20130524/us-east-1/s3/aws4_request, SignedHeaders=host;range;x-amz-date,Signature=EXAMPLE_SIGNATURE")
    };

    var content = string.Empty;
    Assert.DoesNotThrow(() => content = algo.Download("https://www.quantconnect.com/", headers));
    Assert.IsNotEmpty(content);
}
Outside QuantConnect
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Clear();
httpClient.DefaultRequestHeaders.Add("Authorization", "AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20130524/us-east-1/s3/aws4_request, SignedHeaders=host;range;x-amz-date,Signature=EXAMPLE_SIGNATURE");
httpClient.DefaultRequestHeaders.Dump();

System Information

Windows 11

Checklist

  • I have completely filled out this template
  • I have confirmed that this issue exists on the current master branch
  • I have confirmed that this is not a duplicate issue by searching issues
  • I have provided detailed steps to reproduce the issue
@Martin-Molinero
Copy link
Member

Thank you for the report @Grosner300!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants