diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-24 01:30:28 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-24 01:30:28 -0400 |
commit | 00957a23177f792d4e4962854697779831d51ca1 (patch) | |
tree | c603edd9f4a7e7a2ab8f35efbe1cbd523cb01c2f /src/StardewModdingAPI.Web | |
parent | d3f0c8e4d2d9ada099cba67c359c5df1d69a1257 (diff) | |
download | SMAPI-00957a23177f792d4e4962854697779831d51ca1.tar.gz SMAPI-00957a23177f792d4e4962854697779831d51ca1.tar.bz2 SMAPI-00957a23177f792d4e4962854697779831d51ca1.zip |
validate semantic versions in API (#336, #361)
Diffstat (limited to 'src/StardewModdingAPI.Web')
3 files changed, 14 insertions, 1 deletions
diff --git a/src/StardewModdingAPI.Web/Controllers/ModsController.cs b/src/StardewModdingAPI.Web/Controllers/ModsController.cs index c5c79600..4eaa66d2 100644 --- a/src/StardewModdingAPI.Web/Controllers/ModsController.cs +++ b/src/StardewModdingAPI.Web/Controllers/ModsController.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.RegularExpressions; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; @@ -27,6 +28,9 @@ namespace StardewModdingAPI.Web.Controllers /// <summary>The number of minutes update checks should be cached before refetching them.</summary> private readonly int CacheMinutes; + /// <summary>A regex which matches SMAPI-style semantic version.</summary> + private readonly string VersionRegex; + /********* ** Public methods @@ -40,6 +44,7 @@ namespace StardewModdingAPI.Web.Controllers this.Cache = cache; this.CacheMinutes = config.CacheMinutes; + this.VersionRegex = config.SemanticVersionRegex; string version = this.GetType().Assembly.GetName().Version.ToString(3); this.Repositories = @@ -103,7 +108,10 @@ namespace StardewModdingAPI.Web.Controllers result[modKey] = await this.Cache.GetOrCreateAsync($"{repository.VendorKey}:{modID}".ToLower(), async entry => { entry.AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(this.CacheMinutes); - return await repository.GetModInfoAsync(modID); + ModInfoModel info = await repository.GetModInfoAsync(modID); + if (info.Error == null && !Regex.IsMatch(info.Version, this.VersionRegex, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)) + info = new ModInfoModel(info.Name, info.Version, info.Url, $"Mod has invalid semantic version '{info.Version}'."); + return info; }); } diff --git a/src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs b/src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs index 8ca555a3..03de639e 100644 --- a/src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs +++ b/src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs @@ -12,6 +12,10 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels /// <summary>The number of minutes update checks should be cached before refetching them.</summary> public int CacheMinutes { get; set; } + /// <summary>A regex which matches SMAPI-style semantic version.</summary> + /// <remarks>Derived from SMAPI's SemanticVersion implementation.</remarks> + public string SemanticVersionRegex { get; set; } + /**** ** Chucklefish mod site ****/ diff --git a/src/StardewModdingAPI.Web/appsettings.json b/src/StardewModdingAPI.Web/appsettings.json index a0d4d078..852f6f71 100644 --- a/src/StardewModdingAPI.Web/appsettings.json +++ b/src/StardewModdingAPI.Web/appsettings.json @@ -7,6 +7,7 @@ }, "ModUpdateCheck": { "CacheMinutes": 60, + "SemanticVersionRegex": "^(?>(?<major>0|[1-9]\\d*))\\.(?>(?<minor>0|[1-9]\\d*))(?>(?:\\.(?<patch>0|[1-9]\\d*))?)(?:-(?<prerelease>(?>[a-z0-9]+[\\-\\.]?)+))?$", "ChucklefishKey": "Chucklefish", "ChucklefishUserAgent": "SMAPI/{0} (+https://github.com/Pathoschild/SMAPI)", |