diff --git a/PlotLine.Tests/HelpVisibilityTests.cs b/PlotLine.Tests/HelpVisibilityTests.cs index 307403c0..a70ba01f 100644 --- a/PlotLine.Tests/HelpVisibilityTests.cs +++ b/PlotLine.Tests/HelpVisibilityTests.cs @@ -29,8 +29,28 @@ internal static class HelpVisibilityTests } context.HttpContext.User=new ClaimsPrincipal(new ClaimsIdentity(new[]{new Claim(ClaimTypes.Email,"approved-admin@example.invalid")},"Fixture")); Check(AdministratorAccess.IsAllowed(context.HttpContext.User,config) && help.GetAllArticles().Count==409,"Existing AdminOnly rule retains full administrator documentation"); - foreach(var a in restricted)Check(help.GetByContextKey(a.ContextKey)?.Visibility=="administrator","Administrators retain every restricted context identifier"); + var controller=new PlotLine.Controllers.HelpController(help,new HelpMarkdownRenderer(help),config) { ControllerContext=new(){HttpContext=context.HttpContext} }; + foreach(var a in restricted) + { + Check(help.GetByContextKey(a.ContextKey)?.Visibility=="administrator","Administrators retain every restricted context identifier"); + context.HttpContext.Request.Path=a.Url; + Check(controller.Article(a.CategorySlug,a.Slug) is Microsoft.AspNetCore.Mvc.ViewResult {Model:PlotLine.ViewModels.HelpCentreArticleViewModel model} + && model.Article.ContextKey==a.ContextKey && model.RenderedHtml.Length>0,"Authorized administrator controller retains rendered help body"); + } context.HttpContext.User=new ClaimsPrincipal();Check(help.GetAllArticles().Count==367,"Singleton cache does not leak prior administrator view"); + var temp=Path.Combine(Path.GetTempPath(),"pd-private-help-images-"+Guid.NewGuid().ToString("N"));Directory.CreateDirectory(Path.Combine(temp,"assets")); + try + { + File.WriteAllText(Path.Combine(temp,"manifest.json"),"{\"format\":2,\"audience\":\"application\",\"version\":\"images\"}"); + var records=new[]{new PlotLine.Models.HelpArticle{Visibility="public",ContextKey="public.image",Category="Guides",CategorySlug="guides",Slug="public",MarkdownContent="![Public](public.png)"},new PlotLine.Models.HelpArticle{Visibility="administrator",ContextKey="admin.image",Category="Guides",CategorySlug="guides",Slug="private",MarkdownContent="![Private](private.png)"}}; + File.WriteAllText(Path.Combine(temp,"articles.json"),System.Text.Json.JsonSerializer.Serialize(records));File.WriteAllBytes(Path.Combine(temp,"assets/public.png"),[1]);File.WriteAllBytes(Path.Combine(temp,"assets/private.png"),[2]); + var mediaConfig=new ConfigurationBuilder().AddInMemoryCollection(new Dictionary{["HelpContent:Path"]=temp,["Admin:AllowedEmails:0"]="approved-admin@example.invalid"}).Build(); + var media=new HelpService(environment,mediaConfig,context);var drawer=new PlotLine.Controllers.HelpDrawerController(media,new HelpMarkdownRenderer(media),environment); + Check(drawer.Image("public.png") is Microsoft.AspNetCore.Mvc.PhysicalFileResult && drawer.Image("private.png") is Microsoft.AspNetCore.Mvc.NotFoundResult,"Anonymous media inherits source article visibility"); + context.HttpContext.User=new ClaimsPrincipal(new ClaimsIdentity(new[]{new Claim(ClaimTypes.Email,"approved-admin@example.invalid")},"Fixture")); + Check(drawer.Image("private.png") is Microsoft.AspNetCore.Mvc.PhysicalFileResult,"Administrators retain private publication assets"); + } + finally{Directory.Delete(temp,true);} Console.WriteLine("PASS help visibility: 409 classified articles, 42 restricted; anonymous/user/admin lookups, search, navigation, related links and cache isolation"); } } diff --git a/PlotLine/Controllers/HelpDrawerController.cs b/PlotLine/Controllers/HelpDrawerController.cs index 376cfd04..ff5ab402 100644 --- a/PlotLine/Controllers/HelpDrawerController.cs +++ b/PlotLine/Controllers/HelpDrawerController.cs @@ -36,7 +36,13 @@ public sealed class HelpDrawerController( return NotFound(); } - var imageRoot = Path.Combine(environment.ContentRootPath, "Help", "Images"); + // Media inherits visibility from its referencing article. Use that article's + // immutable publication root so an atomic publication switch cannot mix versions. + var article = help.GetAllArticles().FirstOrDefault(a => string.Equals(a.Screenshot, file, StringComparison.Ordinal) + || System.Text.RegularExpressions.Regex.Matches(a.MarkdownContent, @"!\[[^\]]*\]\(([^)]+)\)") + .Any(m => string.Equals(m.Groups[1].Value, file, StringComparison.Ordinal))); + if (article is null) return NotFound(); + var imageRoot = article.AssetRoot ?? Path.Combine(environment.ContentRootPath, "Help", "Images"); var fullPath = Path.GetFullPath(Path.Combine(imageRoot, file)); var safeRoot = Path.GetFullPath(imageRoot).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) + Path.DirectorySeparatorChar; if (!fullPath.StartsWith(safeRoot, StringComparison.OrdinalIgnoreCase) || !System.IO.File.Exists(fullPath)) diff --git a/PlotLine/Models/HelpModels.cs b/PlotLine/Models/HelpModels.cs index f4ebb122..06593125 100644 --- a/PlotLine/Models/HelpModels.cs +++ b/PlotLine/Models/HelpModels.cs @@ -3,6 +3,7 @@ namespace PlotLine.Models; public sealed class HelpArticle { public string Visibility { get; set; } = "internal"; + internal string? AssetRoot { get; set; } public string Title { get; set; } = string.Empty; public string Category { get; set; } = string.Empty; public string CategorySlug { get; set; } = string.Empty; diff --git a/PlotLine/Services/HelpService.cs b/PlotLine/Services/HelpService.cs index 5463d5e8..08f3e6ef 100644 --- a/PlotLine/Services/HelpService.cs +++ b/PlotLine/Services/HelpService.cs @@ -49,6 +49,7 @@ public sealed class HelpService(IWebHostEnvironment environment, IConfiguration? cachedArticles = System.Text.Json.JsonSerializer.Deserialize>( File.ReadAllText(Path.Combine(root, "articles.json"))) ?? throw new InvalidDataException("Empty approved help publication."); + foreach (var article in cachedArticles) article.AssetRoot = Path.Combine(root, "assets"); publishedIdentity = identity; } return cachedArticles; @@ -256,6 +257,7 @@ public sealed class HelpService(IWebHostEnvironment environment, IConfiguration? var article = new HelpArticle { Visibility = GetValue(metadata, "visibility").ToLowerInvariant(), + AssetRoot = Path.Combine(Path.GetDirectoryName(articleRoot)!, "Images"), Title = GetValue(metadata, "title"), Category = GetValue(metadata, "category"), CategorySlug = Slugify(GetValue(metadata, "category")),