-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: replace regex JSON parsing with DataContractJsonSerializer #40
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,9 @@ | |
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Reflection; | ||
| using System.Runtime.Serialization; | ||
| using System.Runtime.Serialization.Json; | ||
| using System.Security.Authentication; | ||
| using System.Text.RegularExpressions; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
|
|
@@ -20,6 +21,13 @@ internal sealed class UpdateInfo | |
| public bool IsUpdateAvailable; | ||
| } | ||
|
|
||
| [DataContract] | ||
| internal sealed class GitHubRelease | ||
| { | ||
| [DataMember] | ||
| public string tag_name { get; set; } | ||
| } | ||
|
|
||
| internal interface IUpdateClient | ||
| { | ||
| Task<string> DownloadStringAsync(string url, CancellationToken cancellationToken = default); | ||
|
|
@@ -110,7 +118,7 @@ public static async Task<UpdateInfo> CheckLatestAsync(IUpdateClient client, Plug | |
|
|
||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| string json = await client.DownloadStringAsync(ReleasesApiUrl, cancellationToken); | ||
| string tagName = GetNewestVersionTag(ExtractJsonStrings(json, "tag_name").ToArray()); | ||
| string tagName = GetNewestVersionTag(ExtractVersionTags(json)); | ||
|
|
||
| UpdateInfo info = new UpdateInfo(); | ||
| info.LatestVersion = tagName; | ||
|
|
@@ -188,18 +196,33 @@ private static string BuildChecksumUrl(string tagName) | |
| return "https://github.com/hieuck/KeePassAutoReload/releases/download/" + tagName + "/SHA256SUMS.txt"; | ||
| } | ||
|
|
||
| private static List<string> ExtractJsonStrings(string json, string name) | ||
| private static string[] ExtractVersionTags(string json) | ||
| { | ||
| List<string> values = new List<string>(); | ||
| if (string.IsNullOrEmpty(json)) return values; | ||
| if (string.IsNullOrWhiteSpace(json)) return new string[0]; | ||
|
|
||
| MatchCollection matches = Regex.Matches(json, "\"" + Regex.Escape(name) + "\"\\s*:\\s*\"(?<value>(?:\\\\.|[^\"])*)\""); | ||
| foreach (Match match in matches) | ||
| try | ||
| { | ||
| using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json))) | ||
| { | ||
| DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<GitHubRelease>)); | ||
| List<GitHubRelease> releases = (List<GitHubRelease>)serializer.ReadObject(stream); | ||
| if (releases == null) return new string[0]; | ||
|
|
||
| List<string> tags = new List<string>(); | ||
| foreach (GitHubRelease release in releases) | ||
| { | ||
| if (release != null && !string.IsNullOrWhiteSpace(release.tag_name)) | ||
| { | ||
| tags.Add(release.tag_name); | ||
| } | ||
| } | ||
| return tags.ToArray(); | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| if (match.Success) values.Add(Regex.Unescape(match.Groups["value"].Value)); | ||
| return new string[0]; | ||
| } | ||
|
Comment on lines
+222
to
225
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The catch block in Recommendation: At minimum, log the exception or provide a mechanism to surface the error, such as rethrowing or returning an error indicator. For example: catch (Exception ex)
{
// Log the exception or rethrow
// Logger.LogError(ex, "Failed to extract version tags from JSON");
return new string[0];
} |
||
|
|
||
| return values; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential Issue: Lack of Validation for
tagNamein UpdateInfo ConstructionThe code assigns
tagNamedirectly toUpdateInfo.LatestVersionand uses it to build URLs without validating that it is non-null and well-formed. IfExtractVersionTagsorGetNewestVersionTagreturns an empty or malformed string, this could result in broken URLs or incorrect update status.Recommendation: Add validation for
tagNamebefore constructing URLs and settingIsUpdateAvailable. For example:This will prevent propagating invalid data and improve robustness.