From 9f6fbe16925f4c4c66da1da59309aa4f5dbc42a4 Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Sun, 29 Sep 2019 14:53:25 -0600 Subject: [PATCH] finish implementing permissions backend logic --- .../ContentRightsManagementTests.cs | 53 +++++++++++++++++++ Wabbajack/Validation/DTOs.cs | 7 +++ Wabbajack/Validation/ValidateModlist.cs | 24 +++++++++ 3 files changed, 84 insertions(+) diff --git a/Wabbajack.Test/ContentRightsManagementTests.cs b/Wabbajack.Test/ContentRightsManagementTests.cs index f95aeca5..c0e61225 100644 --- a/Wabbajack.Test/ContentRightsManagementTests.cs +++ b/Wabbajack.Test/ContentRightsManagementTests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.Policy; using Microsoft.VisualStudio.TestTools.UnitTesting; using Wabbajack.Common; using Wabbajack.Validation; @@ -32,6 +33,16 @@ namespace Wabbajack.Test CanUseInOtherGames: false "; + private static string server_whitelist = @" + + GoogleIDs: + - googleDEADBEEF + + AllowedPrefixes: + - https://somegoodplace.com/ + +"; + [TestInitialize] public void TestSetup() @@ -39,6 +50,7 @@ namespace Wabbajack.Test WorkQueue.Init((x, y, z) => { }, (min, max) => { }); validate = new ValidateModlist(); validate.LoadAuthorPermissionsFromString(permissions); + validate.LoadServerWhitelist(server_whitelist); } [TestMethod] @@ -174,6 +186,47 @@ namespace Wabbajack.Test errors = validate.Validate(modlist); Assert.AreEqual(errors.Count(), 1); + // Error due to file downloaded from 3rd party + modlist.GameType = Game.Skyrim; + modlist.Archives[0] = new DirectURLArchive() + { + URL = "https://somebadplace.com", + Hash = "DEADBEEF" + }; + errors = validate.Validate(modlist); + Assert.AreEqual(errors.Count(), 1); + + // Ok due to file downloaded from whitelisted 3rd party + modlist.GameType = Game.Skyrim; + modlist.Archives[0] = new DirectURLArchive() + { + URL = "https://somegoodplace.com/myfile", + Hash = "DEADBEEF" + }; + errors = validate.Validate(modlist); + Assert.AreEqual(errors.Count(), 0); + + + // Error due to file downloaded from bad 3rd party + modlist.GameType = Game.Skyrim; + modlist.Archives[0] = new GoogleDriveMod() + { + Id = "bleg", + Hash = "DEADBEEF" + }; + errors = validate.Validate(modlist); + Assert.AreEqual(errors.Count(), 1); + + // Error due to file downloaded from good 3rd party + modlist.GameType = Game.Skyrim; + modlist.Archives[0] = new GoogleDriveMod() + { + Id = "googleDEADBEEF", + Hash = "DEADBEEF" + }; + errors = validate.Validate(modlist); + Assert.AreEqual(errors.Count(), 0); + } } diff --git a/Wabbajack/Validation/DTOs.cs b/Wabbajack/Validation/DTOs.cs index 380e2caa..7c1d242c 100644 --- a/Wabbajack/Validation/DTOs.cs +++ b/Wabbajack/Validation/DTOs.cs @@ -37,4 +37,11 @@ namespace Wabbajack.Validation { public Permissions Permissions; } + + + public class ServerWhitelist + { + public List GoogleIDs; + public List AllowedPrefixes; + } } diff --git a/Wabbajack/Validation/ValidateModlist.cs b/Wabbajack/Validation/ValidateModlist.cs index b84865ba..77bec2a8 100644 --- a/Wabbajack/Validation/ValidateModlist.cs +++ b/Wabbajack/Validation/ValidateModlist.cs @@ -18,6 +18,7 @@ namespace Wabbajack.Validation public class ValidateModlist { public Dictionary AuthorPermissions { get; set; } + public ServerWhitelist ServerWhitelist { get; set; } public void LoadAuthorPermissionsFromString(string s) { @@ -27,6 +28,14 @@ namespace Wabbajack.Validation AuthorPermissions = d.Deserialize>(s); } + public void LoadServerWhitelist(string s) + { + var d = new DeserializerBuilder() + .WithNamingConvention(new PascalCaseNamingConvention()) + .Build(); + ServerWhitelist = d.Deserialize(s); + } + /// /// Takes all the permissions for a given Nexus mods and merges them down to a single permissions record /// the more specific record having precedence in each field. @@ -103,6 +112,21 @@ namespace Wabbajack.Validation .Where(m => m.GameName.ToLower() != nexus) .Do(m => ValidationErrors.Push($"The modlist is for {nexus} but {m.Name} is for game type {m.GameName} and is not allowed to be converted to other game types")); + modlist.Archives + .OfType() + .PMap(m => + { + if (!ServerWhitelist.GoogleIDs.Contains(m.Id)) + ValidationErrors.Push($"{m.Name} uses Google Drive id {m.Id} but that id is not in the file whitelist."); + }); + + modlist.Archives + .OfType() + .PMap(m => + { + if (!ServerWhitelist.AllowedPrefixes.Any(prefix => m.URL.StartsWith(prefix))) + ValidationErrors.Push($"{m.Name} will be downloaded from {m.URL} but that URL is not in the server whitelist"); + }); return ValidationErrors.ToList(); }