PR: Sub Issue 1 - Security hardening (SSL fix, HTTP auth, HttpClient pooling)#313
Merged
Conversation
…ling - Remove CheckValidationResult() returning always true - Add StrictSslValidationPolicy (default: only accept valid certs) - Add ISslValidationPolicy interface for pluggable cert validation - Add IHttpAuthProvider with 4 implementations: BearerTokenAuthProvider, ApiKeyAuthProvider, HmacAuthProvider, NoOpAuthProvider - Add HttpAuthProviderFactory for auto-selection - Replace per-request new HttpClient() with static singleton - Add exponential-backoff retry (3 attempts, 1s/2s/4s) - Preserve backward-compatible static API surface Closes #308
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to harden GeneralUpdate.Core network security by removing the SSL certificate validation bypass, introducing pluggable SSL validation and HTTP auth abstractions, and reworking VersionService to reuse a shared HttpClient with retry logic.
Changes:
- Added
ISslValidationPolicyand a default strict implementation (StrictSslValidationPolicy) to avoid accepting invalid TLS certificates. - Added
IHttpAuthProviderplus built-in implementations (NoOp/Bearer/API-Key/HMAC) and a factory to apply request authentication. - Refactored
Network/VersionServiceto use a sharedHttpClient, pluggable TLS validation, pluggable auth, and exponential-backoff retries.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| src/c#/GeneralUpdate.Core/Security/ISslValidationPolicy.cs | Introduces a pluggable certificate validation interface and a strict default policy. |
| src/c#/GeneralUpdate.Core/Security/IHttpAuthProvider.cs | Adds a request-auth abstraction with multiple provider implementations and a factory. |
| src/c#/GeneralUpdate.Core/Network/VersionService.cs | Reworks version/report POSTs to use shared HttpClient + SSL policy + auth provider + retries. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+71
to
+75
| private async Task<VersionRespDTO> ValidateAsync(string url, string v, int at, int pf, string pid, | ||
| CancellationToken t = default) | ||
| { | ||
| var p = new Dictionary<string, object> { ["Version"] = v, ["AppType"] = at, ["Platform"] = pf, ["ProductId"] = pid }; | ||
| return await PostAsync<VersionRespDTO>(url, p, VersionRespJsonContext.Default.VersionRespDTO, t); |
Comment on lines
+57
to
63
| public static Task<VersionRespDTO> Validate(string url, string version, | ||
| int appType, string appKey, int platform, string productId, | ||
| string scheme = null, string token = null) | ||
| { | ||
| var parameters = new Dictionary<string, object> | ||
| { | ||
| { "Version", version }, | ||
| { "AppType", appType }, | ||
| { "AppKey", appKey }, | ||
| { "Platform", platform }, | ||
| { "ProductId", productId } | ||
| }; | ||
| return await PostTaskAsync<VersionRespDTO>(httpUrl, parameters, VersionRespJsonContext.Default.VersionRespDTO, scheme, token); | ||
| var a = HttpAuthProviderFactory.Create(scheme, token, appKey); | ||
| return new VersionService(a).ValidateAsync(url, version, appType, platform, productId); | ||
| } |
Comment on lines
+109
to
+114
| private static bool IsRetryable(Exception ex) | ||
| { | ||
| if (ex is OperationCanceledException) return false; | ||
| if (ex is TaskCanceledException or TimeoutException or System.IO.IOException) return true; | ||
| if (ex is HttpRequestException h && (h.Message ?? "").Contains("timeout", StringComparison.OrdinalIgnoreCase)) return true; | ||
| return false; |
Comment on lines
+103
to
+105
| var r = await _sharedClient.SendAsync(req, cts.Token).ConfigureAwait(false); | ||
| r.EnsureSuccessStatusCode(); | ||
| var rj = await r.Content.ReadAsStringAsync().ConfigureAwait(false); |
| { | ||
| var handler = new HttpClientHandler(); | ||
| handler.ServerCertificateCustomValidationCallback = SharedCertValidation; | ||
| _sharedClient = new HttpClient(handler, disposeHandler: false); |
Comment on lines
+66
to
+67
| var h = new HMACSHA256(Encoding.UTF8.GetBytes(key)) | ||
| .ComputeHash(Encoding.UTF8.GetBytes(data)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #308
Changes (on unified GeneralUpdate.Core)
Files
Build
✅ 0 errors