Prevent official plugins from updating to unmatched versions

This commit is contained in:
Łukasz Domeradzki 2024-04-21 21:31:37 +02:00
parent 1a732bbb93
commit efb7262113
No known key found for this signature in database
GPG key ID: 6B138B4C64555AEA
3 changed files with 60 additions and 4 deletions

View file

@ -45,7 +45,7 @@ namespace ArchiSteamFarm.OfficialPlugins.Monitoring;
[Export(typeof(IPlugin))]
[SuppressMessage("ReSharper", "MemberCanBeFileLocal")]
internal sealed class MonitoringPlugin : OfficialPlugin, IDisposable, IGitHubPluginUpdates, IWebInterface, IWebServiceProvider {
internal sealed class MonitoringPlugin : OfficialPlugin, IDisposable, IOfficialGitHubPluginUpdates, IWebInterface, IWebServiceProvider {
private const string MeterName = SharedInfo.AssemblyName;
private const string MetricNamePrefix = "asf";

View file

@ -114,11 +114,22 @@ public interface IGitHubPluginUpdates : IPluginUpdates {
throw new ArgumentNullException(nameof(releaseAssets));
}
return Task.FromResult(FindPossibleMatch(asfVersion, newPluginVersion, releaseAssets));
}
protected ReleaseAsset? FindPossibleMatch(Version asfVersion, Version newPluginVersion, IReadOnlyCollection<ReleaseAsset> releaseAssets) {
ArgumentNullException.ThrowIfNull(asfVersion);
ArgumentNullException.ThrowIfNull(newPluginVersion);
if ((releaseAssets == null) || (releaseAssets.Count == 0)) {
throw new ArgumentNullException(nameof(releaseAssets));
}
Dictionary<string, ReleaseAsset> assetsByName = releaseAssets.ToDictionary(static asset => asset.Name, StringComparer.OrdinalIgnoreCase);
foreach (string possibleMatch in GetPossibleMatches(asfVersion)) {
if (assetsByName.TryGetValue(possibleMatch, out ReleaseAsset? targetAsset)) {
return Task.FromResult<ReleaseAsset?>(targetAsset);
return targetAsset;
}
}
@ -126,12 +137,12 @@ public interface IGitHubPluginUpdates : IPluginUpdates {
HashSet<ReleaseAsset> zipAssets = releaseAssets.Where(static asset => asset.Name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)).ToHashSet();
if (zipAssets.Count == 1) {
return Task.FromResult<ReleaseAsset?>(zipAssets.First());
return zipAssets.First();
}
ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.PluginUpdateConflictingAssetsFound, Name, Version, newPluginVersion));
return Task.FromResult<ReleaseAsset?>(null);
return null;
}
protected async Task<Uri?> GetTargetReleaseURL(Version asfVersion, string asfVariant, bool asfUpdate, bool stable, bool forced) {

View file

@ -0,0 +1,45 @@
// ----------------------------------------------------------------------------------------------
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// ----------------------------------------------------------------------------------------------
// |
// Copyright 2015-2024 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
// |
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// |
// http://www.apache.org/licenses/LICENSE-2.0
// |
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ArchiSteamFarm.Web.GitHub.Data;
namespace ArchiSteamFarm.Plugins.Interfaces;
internal interface IOfficialGitHubPluginUpdates : IGitHubPluginUpdates {
Task<ReleaseAsset?> IGitHubPluginUpdates.GetTargetReleaseAsset(Version asfVersion, string asfVariant, Version newPluginVersion, IReadOnlyCollection<ReleaseAsset> releaseAssets) {
ArgumentNullException.ThrowIfNull(asfVersion);
ArgumentException.ThrowIfNullOrEmpty(asfVariant);
ArgumentNullException.ThrowIfNull(newPluginVersion);
if ((releaseAssets == null) || (releaseAssets.Count == 0)) {
throw new ArgumentNullException(nameof(releaseAssets));
}
// For official plugins, the ASF version must match the plugin version
// Refuse to find the match if that's not the case, otherwise fallback to default implementation
return Task.FromResult(asfVersion == newPluginVersion ? FindPossibleMatch(asfVersion, newPluginVersion, releaseAssets) : null);
}
}