diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationMappers.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationMappers.cs index bd8474b7b9..e9cef7a6e9 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationMappers.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationMappers.cs @@ -1,6 +1,7 @@ using Riok.Mapperly.Abstractions; using Volo.Docs.Documents; using Volo.Abp.Mapperly; +using Volo.Abp.Data; using Volo.Docs.Common.Projects; using Volo.Docs.Projects; @@ -33,9 +34,16 @@ public partial class VersionInfoToVersionInfoDtoMapper : MapperBase { + [MapProperty(nameof(Project.ExtraProperties), nameof(ProjectDto.ExtraProperties), Use = nameof(MapPublicExtraProperties))] public override partial ProjectDto Map(Project source); + [MapProperty(nameof(Project.ExtraProperties), nameof(ProjectDto.ExtraProperties), Use = nameof(MapPublicExtraProperties))] public override partial void Map(Project source, ProjectDto destination); + + private static ExtraPropertyDictionary MapPublicExtraProperties(ExtraPropertyDictionary source) + { + return source.ToPublicProjectExtraProperties(); + } } [Mapper(RequiredMappingStrategy = RequiredMappingStrategy.Target)] @@ -48,4 +56,4 @@ public partial class DocumentToDocumentWithDetailsDtoMapper : MapperBase { + [MapProperty(nameof(Project.ExtraProperties), nameof(ProjectDto.ExtraProperties), Use = nameof(MapPublicExtraProperties))] public override partial ProjectDto Map(Project source); + [MapProperty(nameof(Project.ExtraProperties), nameof(ProjectDto.ExtraProperties), Use = nameof(MapPublicExtraProperties))] public override partial void Map(Project source, ProjectDto destination); + + private static ExtraPropertyDictionary MapPublicExtraProperties(ExtraPropertyDictionary source) + { + return source.ToPublicProjectExtraProperties(); + } } [Mapper(RequiredMappingStrategy = RequiredMappingStrategy.Target)] diff --git a/modules/docs/src/Volo.Docs.Common.Application/Volo/Docs/Common/Projects/ProjectAppService.cs b/modules/docs/src/Volo.Docs.Common.Application/Volo/Docs/Common/Projects/ProjectAppService.cs index eb5c9bc7db..0ee6a269b4 100644 --- a/modules/docs/src/Volo.Docs.Common.Application/Volo/Docs/Common/Projects/ProjectAppService.cs +++ b/modules/docs/src/Volo.Docs.Common.Application/Volo/Docs/Common/Projects/ProjectAppService.cs @@ -39,12 +39,8 @@ namespace Volo.Docs.Common.Projects { var projects = await _projectRepository.GetListAsync(); - var projectDtos = new List( - ObjectMapper.Map, List>(projects) - ); - return new ListResultDto( - projectDtos.Select(p => HidePrivateProperties(p)).ToList() + ObjectMapper.Map, List>(projects) ); } @@ -52,9 +48,7 @@ namespace Volo.Docs.Common.Projects { var project = await _projectRepository.GetByShortNameAsync(shortName); - var projectDto = ObjectMapper.Map(project); - - return HidePrivateProperties(projectDto); + return ObjectMapper.Map(project); } public virtual async Task> GetVersionsAsync(string shortName) @@ -159,20 +153,5 @@ namespace Volo.Docs.Common.Projects ? project.GetProperty("GithubVersionProviderSource") : GithubVersionProviderSource.Releases; } - - private ProjectDto HidePrivateProperties(ProjectDto project) - { - if (project.HasProperty("GitHubAccessToken")) - { - project.SetProperty("GitHubAccessToken", null); - } - - if (project.HasProperty("GitHubUserAgent")) - { - project.SetProperty("GitHubUserAgent", null); - } - - return project; - } } } diff --git a/modules/docs/src/Volo.Docs.Common.Application/Volo/Docs/Common/Projects/ProjectDtoExtensions.cs b/modules/docs/src/Volo.Docs.Common.Application/Volo/Docs/Common/Projects/ProjectDtoExtensions.cs new file mode 100644 index 0000000000..ab08bbf19c --- /dev/null +++ b/modules/docs/src/Volo.Docs.Common.Application/Volo/Docs/Common/Projects/ProjectDtoExtensions.cs @@ -0,0 +1,21 @@ +using Volo.Abp.Data; + +namespace Volo.Docs.Common.Projects; + +public static class ProjectDtoExtensions +{ + public static ExtraPropertyDictionary ToPublicProjectExtraProperties(this ExtraPropertyDictionary source) + { + var extraProperties = new ExtraPropertyDictionary(); + + foreach (var propertyName in new[] { "GithubVersionProviderSource", "VersionBranchPrefix" }) + { + if (source != null && source.TryGetValue(propertyName, out var value)) + { + extraProperties[propertyName] = value; + } + } + + return extraProperties; + } +} diff --git a/modules/docs/test/Volo.Docs.Admin.Application.Tests/Volo/Docs/ProjectAdminAppServiceAuthorization_Tests.cs b/modules/docs/test/Volo.Docs.Admin.Application.Tests/Volo/Docs/ProjectAdminAppServiceAuthorization_Tests.cs new file mode 100644 index 0000000000..59f84f123c --- /dev/null +++ b/modules/docs/test/Volo.Docs.Admin.Application.Tests/Volo/Docs/ProjectAdminAppServiceAuthorization_Tests.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Security.Claims; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using NSubstitute; +using Shouldly; +using Volo.Abp.Application.Dtos; +using Volo.Abp.Authorization; +using Volo.Abp.Authorization.Permissions; +using Volo.Abp.Data; +using Volo.Abp.Users; +using Volo.Docs.Admin.Projects; +using Volo.Docs.Projects; +using Xunit; + +namespace Volo.Docs +{ + public class ProjectAdminAppServiceAuthorization_Tests : DocsAdminApplicationTestBase + { + protected override void AfterAddApplication(IServiceCollection services) + { + base.AfterAddApplication(services); + services.Replace(ServiceDescriptor.Transient()); + services.Replace(ServiceDescriptor.Transient()); + services.Replace(ServiceDescriptor.Transient()); + var permissionChecker = Substitute.For(); + permissionChecker.IsGrantedAsync(Arg.Any(), Arg.Any()).Returns(false); + services.Replace(ServiceDescriptor.Singleton(permissionChecker)); + } + + [Theory] + [InlineData("GetAsync")] + [InlineData("GetListAsync")] + [InlineData("CreateAsync")] + [InlineData("UpdateAsync")] + public async Task Should_Deny_Project_Responses_Without_Permission(string method) + { + GetRequiredService().ShouldBeOfType(); + GetRequiredService().IsAuthenticated.ShouldBeFalse(); + var service = GetRequiredService(); + var id = GetRequiredService().ProjectId; + + Task InvokeAsync() + { + return method switch + { + "GetAsync" => service.GetAsync(id), + "GetListAsync" => service.GetListAsync(new PagedAndSortedResultRequestDto()), + "CreateAsync" => service.CreateAsync(new CreateProjectDto + { + Name = "Denied project", ShortName = "denied-project", Format = "md", + DefaultDocumentName = "CLI", NavigationDocumentName = "docs-nav.json", + ParametersDocumentName = "docs-params.json", DocumentStoreType = "GitHub", + ExtraProperties = new Dictionary() + }), + "UpdateAsync" => service.UpdateAsync(id, new UpdateProjectDto + { + Name = "Denied update", Format = "md", DefaultDocumentName = "CLI", + NavigationDocumentName = "docs-nav.json", ParametersDocumentName = "docs-params.json", + ExtraProperties = new Dictionary() + }), + _ => throw new ArgumentOutOfRangeException(nameof(method)) + }; + } + + await Should.ThrowAsync(InvokeAsync); + var repository = GetRequiredService(); + var project = await repository.GetAsync(id); + project.Name.ShouldBe("ABP vNext"); + project.GetProperty("GitHubAccessToken").ShouldBe("123456"); + (await repository.GetListAsync()).Count.ShouldBe(1); + } + } +} diff --git a/modules/docs/test/Volo.Docs.Admin.Application.Tests/Volo/Docs/ProjectAdminAppService_Tests.cs b/modules/docs/test/Volo.Docs.Admin.Application.Tests/Volo/Docs/ProjectAdminAppService_Tests.cs index a64102a723..04b2a12794 100644 --- a/modules/docs/test/Volo.Docs.Admin.Application.Tests/Volo/Docs/ProjectAdminAppService_Tests.cs +++ b/modules/docs/test/Volo.Docs.Admin.Application.Tests/Volo/Docs/ProjectAdminAppService_Tests.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using Shouldly; using Volo.Abp.Application.Dtos; +using Volo.Abp.Data; using Volo.Docs.Admin.Projects; using Volo.Docs.Projects; using Xunit; @@ -29,6 +30,7 @@ namespace Volo.Docs projects.ShouldNotBeNull(); projects.TotalCount.ShouldBe(1); projects.Items.ShouldContain(x => x.Name == "ABP vNext"); + projects.Items.Single().GetProperty("GitHubAccessToken").ShouldBe("123456"); } [Fact] @@ -37,6 +39,7 @@ namespace Volo.Docs var project = await _projectAdminAppService.GetAsync(_testData.ProjectId); project.ShouldNotBeNull(); project.Id.ShouldBe(_testData.ProjectId); + project.GetProperty("GitHubAccessToken").ShouldBe("123456"); } [Fact] @@ -77,6 +80,7 @@ namespace Volo.Docs projectDto.DocumentStoreType.ShouldBe(createProjectDto.DocumentStoreType); projectDto.ExtraProperties.Except(createProjectDto.ExtraProperties).Any().ShouldBe(false); + projectDto.GetProperty("GitHubAccessToken").ShouldBe("123456"); } [Fact] @@ -109,6 +113,7 @@ namespace Volo.Docs projectDto.MainWebsiteUrl.ShouldBe(updateProjectDto.MainWebsiteUrl); projectDto.LatestVersionBranchName.ShouldBe(updateProjectDto.LatestVersionBranchName); projectDto.ExtraProperties.ShouldContainKey("test"); + projectDto.GetProperty("GitHubAccessToken").ShouldBe("123456"); } [Fact] diff --git a/modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/ApplicationService_Tests.cs b/modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/ApplicationService_Tests.cs index 662db4c6aa..79b66ca79d 100644 --- a/modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/ApplicationService_Tests.cs +++ b/modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/ApplicationService_Tests.cs @@ -1,6 +1,10 @@ -using System.Threading.Tasks; +using System.Text.Json; +using System.Threading.Tasks; using Shouldly; +using Volo.Abp.Data; +using Volo.Abp.Uow; using Volo.Docs.Common.Projects; +using Volo.Docs.Projects; using Xunit; namespace Volo.Docs @@ -23,6 +27,7 @@ namespace Volo.Docs projects.ShouldNotBeNull(); projects.Items.Count.ShouldBe(1); projects.Items.ShouldContain(x => x.Id == _testData.ProjectId); + projects.Items[0].ExtraProperties.ShouldBeEmpty(); } [Fact] @@ -31,6 +36,44 @@ namespace Volo.Docs var project = await _projectAppService.GetAsync("abp"); project.ShouldNotBeNull(); project.ShortName.ShouldBe("abp"); + project.ExtraProperties.ShouldBeEmpty(); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task Should_Only_Return_Public_Project_Properties(bool list) + { + const string secret = "LOCAL_TEST_UNKNOWN_SECRET_MARKER"; + var repository = GetRequiredService(); + + using (var uow = GetRequiredService().Begin()) + { + var project = await repository.GetAsync(_testData.ProjectId); + project.SetProperty("CustomSecret", secret); + project.SetProperty("GithubVersionProviderSource", GithubVersionProviderSource.Branches); + project.SetProperty("VersionBranchPrefix", "rel-"); + await repository.UpdateAsync(project, autoSave: true); + + var result = list + ? (await _projectAppService.GetListAsync()).Items[0] + : await _projectAppService.GetAsync("abp"); + + result.ExtraProperties.Keys.ShouldBe(new[] { "GithubVersionProviderSource", "VersionBranchPrefix" }, ignoreOrder: true); + result.GetProperty("GithubVersionProviderSource") + .ShouldBe(GithubVersionProviderSource.Branches); + result.GetProperty("VersionBranchPrefix").ShouldBe("rel-"); + JsonSerializer.Serialize(result).ShouldNotContain(secret); + project.GetProperty("CustomSecret").ShouldBe(secret); + project.GetProperty("GitHubAccessToken").ShouldBe("123456"); + project.GetProperty("GitHubRootUrl").ShouldNotBeNullOrEmpty(); + + await uow.CompleteAsync(); + } + + var savedProject = await repository.GetAsync(_testData.ProjectId); + savedProject.GetProperty("CustomSecret").ShouldBe(secret); + savedProject.GetProperty("GitHubAccessToken").ShouldBe("123456"); } [Fact] diff --git a/modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/DocumentAppService_Tests.cs b/modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/DocumentAppService_Tests.cs new file mode 100644 index 0000000000..8dac88e11d --- /dev/null +++ b/modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/DocumentAppService_Tests.cs @@ -0,0 +1,134 @@ +using System; +using System.Net.Http; +using System.Text.Json; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using NSubstitute; +using Shouldly; +using Volo.Abp.Data; +using Volo.Abp.Uow; +using Volo.Docs.Documents; +using Volo.Docs.GitHub.Documents; +using Volo.Docs.Projects; +using Xunit; + +namespace Volo.Docs +{ + public class DocumentAppService_Tests : DocsApplicationTestBase + { + protected override void AfterAddApplication(IServiceCollection services) + { + base.AfterAddApplication(services); + + var environment = Substitute.For(); + environment.EnvironmentName.Returns(Environments.Production); + services.AddSingleton(environment); + } + + [Theory] + [InlineData(false, "Cached")] + [InlineData(true, "Cached")] + [InlineData(false, "Fresh")] + [InlineData(true, "Fresh")] + [InlineData(false, "ExpiredRefresh")] + [InlineData(true, "ExpiredRefresh")] + [InlineData(false, "ExpiredFallback")] + [InlineData(true, "ExpiredFallback")] + [InlineData(false, "Development")] + [InlineData(true, "Development")] + public async Task Should_Not_Expose_Private_Project_Properties(bool defaultDocument, string retrievalMode) + { + const string token = "LOCAL_TEST_TOKEN_MARKER"; + const string userAgent = "LOCAL_TEST_USER_AGENT_MARKER"; + const string customSecret = "LOCAL_TEST_CUSTOM_SECRET_MARKER"; + var testData = GetRequiredService(); + var projectRepository = GetRequiredService(); + var documentAppService = GetRequiredService(); + var repositoryManager = GetRequiredService(); + var documentName = retrievalMode == "Fresh" ? "Uncached.md" : "CLI.md"; + + if (retrievalMode == "Development") + { + GetRequiredService().EnvironmentName = Environments.Development; + } + + using (var uow = GetRequiredService().Begin()) + { + var project = await projectRepository.GetAsync(testData.ProjectId); + project.SetProperty("GitHubAccessToken", token); + project.SetProperty("GitHubUserAgent", userAgent); + project.SetProperty("CustomSecret", customSecret); + project.SetProperty("GithubVersionProviderSource", GithubVersionProviderSource.Branches); + project.SetProperty("VersionBranchPrefix", "2."); + project.SetDefaultDocumentName(retrievalMode == "Fresh" ? "Uncached" : "CLI"); + await projectRepository.UpdateAsync(project, autoSave: true); + + if (retrievalMode is "ExpiredFallback" or "ExpiredRefresh") + { + var documentRepository = GetRequiredService(); + var cachedDocument = await documentRepository.FindAsync(project.Id, documentName, "en", "2.0.0"); + cachedDocument.LastCachedTime = DateTime.Now.AddDays(-1); + await documentRepository.UpdateAsync(cachedDocument, autoSave: true); + if (retrievalMode == "ExpiredFallback") + { + repositoryManager.GetFileRawStringContentAsync(Arg.Any(), Arg.Any(), Arg.Any()) + .Returns(Task.FromException(new HttpRequestException("Document source unavailable."))); + } + } + + var document = defaultDocument + ? await documentAppService.GetDefaultAsync(new GetDefaultDocumentInput + { + ProjectId = project.Id, + LanguageCode = "en", + Version = "0.0" + }) + : await documentAppService.GetAsync(new GetDocumentInput + { + ProjectId = project.Id, + Name = documentName, + LanguageCode = "en", + Version = "0.0" + }); + + document.Name.ShouldBe(documentName); + document.Version.ShouldBe("2.0.0"); + document.Content.ShouldBe(retrievalMode is "Fresh" or "Development" or "ExpiredRefresh" ? "stringContent" : "this is abp cli"); + document.Project.Id.ShouldBe(project.Id); + document.Project.GetProperty("GitHubAccessToken").ShouldBeNull(); + document.Project.GetProperty("GitHubUserAgent").ShouldBeNull(); + document.Project.ExtraProperties.Keys.ShouldBe(new[] { "GithubVersionProviderSource", "VersionBranchPrefix" }, ignoreOrder: true); + document.Project.GetProperty("GithubVersionProviderSource") + .ShouldBe(GithubVersionProviderSource.Branches); + document.Project.GetProperty("VersionBranchPrefix").ShouldBe("2."); + + var json = JsonSerializer.Serialize(document, new JsonSerializerOptions(JsonSerializerDefaults.Web)); + json.ShouldNotContain(token); + json.ShouldNotContain(userAgent); + json.ShouldNotContain(customSecret); + project.GetProperty("GitHubAccessToken").ShouldBe(token); + project.GetProperty("GitHubUserAgent").ShouldBe(userAgent); + project.GetProperty("CustomSecret").ShouldBe(customSecret); + project.GetProperty("GitHubRootUrl").ShouldNotBeNullOrEmpty(); + + if (retrievalMode == "Cached") + { + await repositoryManager.DidNotReceive().GetFileRawStringContentAsync(Arg.Any(), Arg.Any(), Arg.Any()); + } + else + { + await repositoryManager.Received().GetFileRawStringContentAsync(Arg.Any(), token, userAgent); + } + + await uow.CompleteAsync(); + } + + var savedProject = await projectRepository.GetAsync(testData.ProjectId); + savedProject.GetProperty("GitHubAccessToken").ShouldBe(token); + savedProject.GetProperty("GitHubUserAgent").ShouldBe(userAgent); + savedProject.GetProperty("CustomSecret").ShouldBe(customSecret); + savedProject.GetProperty("GitHubRootUrl").ShouldNotBeNullOrEmpty(); + } + } +} diff --git a/modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/ProjectMapping_Tests.cs b/modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/ProjectMapping_Tests.cs new file mode 100644 index 0000000000..226009ffa3 --- /dev/null +++ b/modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/ProjectMapping_Tests.cs @@ -0,0 +1,155 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.Json; +using Shouldly; +using Volo.Abp.Data; +using Volo.Abp.Mapperly; +using Volo.Abp.ObjectExtending; +using Volo.Abp.ObjectMapping; +using Volo.Docs.Common; +using Volo.Docs.Common.Projects; +using Volo.Docs.Projects; +using Xunit; + +namespace Volo.Docs +{ + public class ProjectMapping_Tests : DocsApplicationTestBase + { + static ProjectMapping_Tests() + { + ObjectExtensionManager.Instance.AddOrUpdateProperty("RegisteredSecret"); + ObjectExtensionManager.Instance.AddOrUpdateProperty("RegisteredSecret"); + } + + public static IEnumerable MappingCases() + { + foreach (var documentMapper in new[] { false, true }) + { + foreach (var mode in new[] + { + "DirectNew", "DirectExisting", "DirectAliased", "ObjectNew", "ObjectExisting", + "ObjectAliased", "ListNew", "ListExisting", "ArrayNew", "ArrayExisting" + }) + { + foreach (var properties in new[] { "Public", "PrivateOnly", "Null" }) + { + yield return new object[] { documentMapper, mode, properties }; + } + } + } + } + + [Theory] + [MemberData(nameof(MappingCases))] + public void Should_Only_Map_Public_Extra_Properties(bool documentMapper, string mode, string properties) + { + Project source = new MappingProject(); + source.SetProperty("GitHubAccessToken", "SOURCE_TOKEN_MARKER"); + source.SetProperty("GitHubUserAgent", "SOURCE_USER_AGENT_MARKER"); + source.SetProperty("CustomSecret", "SOURCE_CUSTOM_SECRET_MARKER"); + source.SetProperty("RegisteredSecret", "SOURCE_REGISTERED_SECRET_MARKER"); + source.SetProperty("Path", "SOURCE_PATH_MARKER"); + source.SetProperty("NestedSecret", new Dictionary { ["value"] = "SOURCE_NESTED_SECRET_MARKER" }); + if (properties == "Public") + { + source.SetProperty("GithubVersionProviderSource", GithubVersionProviderSource.Branches); + source.SetProperty("VersionBranchPrefix", "rel-"); + } + if (properties == "Null") + { + ((MappingProject)source).ClearExtraProperties(); + } + + var sourceProperties = source.ExtraProperties; + var sourceJson = JsonSerializer.Serialize(sourceProperties); + var destination = new ProjectDto + { + ExtraProperties = new ExtraPropertyDictionary + { + ["TargetOnlySecret"] = "TARGET_SECRET_MARKER", + ["VersionBranchPrefix"] = "STALE_PREFIX_MARKER" + } + }; + if (mode.EndsWith("Aliased", StringComparison.Ordinal) && sourceProperties != null) + { + destination.ExtraProperties = sourceProperties; + } + + MapperBase directMapper = documentMapper + ? new ProjectToProjectDtoMapper() + : new Common.ProjectToProjectDtoMapper(); + IObjectMapper objectMapper = documentMapper + ? GetRequiredService>() + : GetRequiredService>(); + ProjectDto result; + switch (mode) + { + case "DirectNew": + result = directMapper.Map(source); + break; + case "DirectExisting": + case "DirectAliased": + directMapper.Map(source, destination); + result = destination; + break; + case "ObjectNew": + result = objectMapper.Map(source); + break; + case "ObjectExisting": + case "ObjectAliased": + result = objectMapper.Map(source, destination); + break; + case "ListNew": + result = objectMapper.Map, List>(new List { source }).Single(); + break; + case "ListExisting": + result = objectMapper.Map(new List { source }, new List { destination, new ProjectDto() }).Single(); + break; + case "ArrayNew": + result = objectMapper.Map(new[] { source }).Single(); + break; + case "ArrayExisting": + result = objectMapper.Map(new[] { source }, new[] { destination, new ProjectDto() }).Single(); + break; + default: + throw new ArgumentOutOfRangeException(nameof(mode)); + } + + result.Id.ShouldBe(source.Id); + result.Name.ShouldBe(source.Name); + result.ExtraProperties.ShouldNotBeSameAs(sourceProperties); + if (properties == "Public") + { + result.ExtraProperties.Keys.ShouldBe(new[] { "GithubVersionProviderSource", "VersionBranchPrefix" }, ignoreOrder: true); + result.GetProperty("GithubVersionProviderSource").ShouldBe(GithubVersionProviderSource.Branches); + result.GetProperty("VersionBranchPrefix").ShouldBe("rel-"); + } + else + { + result.ExtraProperties.ShouldBeEmpty(); + } + + var json = JsonSerializer.Serialize(result); + json.ShouldNotContain("SOURCE_"); + json.ShouldNotContain("TARGET_SECRET_MARKER"); + json.ShouldNotContain("STALE_PREFIX_MARKER"); + source.ExtraProperties.ShouldBeSameAs(sourceProperties); + JsonSerializer.Serialize(sourceProperties).ShouldBe(sourceJson); + result.SetProperty("VersionBranchPrefix", "changed-"); + JsonSerializer.Serialize(sourceProperties).ShouldBe(sourceJson); + } + + private sealed class MappingProject : Project + { + public MappingProject() : base(Guid.NewGuid(), "Mapping test", "mapping-test", "GitHub", "md") + { + } + + public void ClearExtraProperties() + { + ExtraProperties = null; + } + } + } +} diff --git a/modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/PublicAppServiceDataLeakage_Tests.cs b/modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/PublicAppServiceDataLeakage_Tests.cs new file mode 100644 index 0000000000..e2396d7184 --- /dev/null +++ b/modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/PublicAppServiceDataLeakage_Tests.cs @@ -0,0 +1,264 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; +using NSubstitute; +using Shouldly; +using Volo.Abp.Content; +using Volo.Abp.Application.Services; +using Volo.Abp.Data; +using Volo.Abp.Uow; +using Volo.Docs.Common.Documents; +using Volo.Docs.Common.Projects; +using Volo.Docs.Documents; +using Volo.Docs.Documents.FullSearch.Elastic; +using Volo.Docs.GitHub.Documents; +using Volo.Docs.Projects; +using Volo.Docs.Projects.Pdf; +using Xunit; + +namespace Volo.Docs +{ + public class PublicAppServiceDataLeakage_Tests : DocsApplicationTestBase + { + private static readonly string[] SecretNames = + { + "GitHubAccessToken", "GitHubUserAgent", "CustomSecret", "RegisteredSecret", "Path", "NestedSecret" + }; + + protected override void AfterAddApplication(IServiceCollection services) + { + base.AfterAddApplication(services); + var environment = Substitute.For(); + environment.EnvironmentName.Returns(Environments.Production); + services.AddSingleton(environment); + + var search = Substitute.For(); + search.SearchAsync(default, default, default, default).ReturnsForAnyArgs(new EsDocumentResult + { + TotalCount = 1, + EsDocuments = new List + { + new EsDocument { Name = "CLI.md", FileName = "CLI.md", Version = "2.0.0", LanguageCode = "en", Highlight = new List { "Public snippet" } } + } + }); + services.AddSingleton(search); + + var fileStore = Substitute.For(); + fileStore.GetOrNullAsync(default, default, default) + .ReturnsForAnyArgs(_ => new MemoryStream(Encoding.UTF8.GetBytes("Public document"))); + services.AddSingleton(fileStore); + services.Configure(options => options.Enable = false); + } + + public static IEnumerable PublicMethods() + { + foreach (var service in PublicContracts()) + { + foreach (var method in service.GetMethods()) + { + yield return new object[] { service.Name + "." + method.Name }; + } + } + } + + private static IEnumerable PublicContracts() + { + return new[] { typeof(IProjectAppService).Assembly, typeof(IDocumentAppService).Assembly } + .SelectMany(assembly => assembly.GetExportedTypes()) + .Where(type => type.IsInterface && typeof(IApplicationService).IsAssignableFrom(type)); + } + + [Fact] + public void Should_Cover_All_Public_Application_Service_Methods() + { + var contracts = PublicContracts().ToArray(); + foreach (var contract in contracts) + { + contract.GetMethods().Select(method => method.Name).Distinct().Count() + .ShouldBe(contract.GetMethods().Length, "Overloaded methods need signature-specific disclosure tests."); + } + + foreach (var implementation in new[] { typeof(DocumentAppService).Assembly, typeof(ProjectAppService).Assembly } + .SelectMany(assembly => assembly.GetExportedTypes()) + .Where(type => type.IsClass && !type.IsAbstract && typeof(IApplicationService).IsAssignableFrom(type))) + { + var methods = implementation.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly) + .Where(method => !method.IsSpecialName).Select(MethodSignature); + var covered = contracts.Where(contract => contract.IsAssignableFrom(implementation)) + .SelectMany(contract => contract.GetMethods()).Select(MethodSignature).Distinct(); + methods.ShouldBe(covered, ignoreOrder: true); + } + } + + private static string MethodSignature(MethodInfo method) + { + return method.Name + "(" + string.Join(",", method.GetParameters().Select(parameter => parameter.ParameterType.FullName)) + ")"; + } + + [Theory] + [MemberData(nameof(PublicMethods))] + public async Task Should_Not_Expose_Project_Secrets_From_Any_Public_Method(string method) + { + var testData = GetRequiredService(); + var repository = GetRequiredService(); + var github = GetRequiredService(); + github.GetFileRawStringContentAsync(Arg.Any(), Arg.Any(), Arg.Any()) + .Returns(call => + { + var url = call.ArgAt(0); + if (url.Contains("docs-nav.json", StringComparison.Ordinal)) + { + return "{\"items\":[{\"text\":\"CLI\",\"path\":\"CLI.md\",\"items\":[]}]}"; + } + if (url.Contains("docs-params.json", StringComparison.Ordinal)) + { + return "{\"parameters\":[{\"name\":\"platform\",\"displayName\":\"Platform\",\"values\":{\"dotnet\":\"Dotnet\"}}]}"; + } + if (url.Contains(DocsDomainConsts.LanguageConfigFileName, StringComparison.Ordinal)) + { + return "{\"languages\":[{\"code\":\"en\",\"displayName\":\"English\",\"isDefault\":true}]}"; + } + return "Public document"; + }); + + using (var uow = GetRequiredService().Begin()) + { + var project = await repository.GetAsync(testData.ProjectId); + foreach (var name in SecretNames) + { + project.SetProperty(name, "PRIVATE_MARKER_" + name); + } + project.SetProperty("NestedSecret", new Dictionary { ["value"] = "PRIVATE_MARKER_NestedSecret" }); + project.SetProperty("GithubVersionProviderSource", GithubVersionProviderSource.Releases); + project.SetProperty("VersionBranchPrefix", "rel-"); + project.SetDefaultDocumentName("CLI"); + project.AddPdfFile(project.Id, "abp-2.0.0-en.zip", "2.0.0", "en"); + await repository.UpdateAsync(project, autoSave: true); + var original = JsonSerializer.Serialize(project.ExtraProperties); + + var result = await InvokeAsync(method, project.Id); + result.ShouldNotBeNull(); + if (result is IRemoteStreamContent remote) + { + using (remote) + { + remote.FileName.ShouldBe("abp-2.0.0-en.zip"); + using var reader = new StreamReader(remote.GetStream()); + var content = await reader.ReadToEndAsync(); + content.ShouldBe("Public document"); + result = new { remote.FileName, remote.ContentType, Content = content }; + } + } + var json = JsonSerializer.Serialize(result, result.GetType(), new JsonSerializerOptions(JsonSerializerDefaults.Web)); + json.ShouldNotContain("PRIVATE_MARKER_"); + using var document = JsonDocument.Parse(json); + AssertNoPrivateProperties(document.RootElement); + JsonSerializer.Serialize(project.ExtraProperties).ShouldBe(original); + await uow.CompleteAsync(); + } + + var saved = await repository.GetAsync(testData.ProjectId); + foreach (var name in SecretNames.Where(name => name != "NestedSecret")) + { + saved.GetProperty(name).ShouldBe("PRIVATE_MARKER_" + name); + } + } + + private async Task InvokeAsync(string method, Guid projectId) + { + var projects = GetRequiredService(); + var documents = GetRequiredService(); + switch (method) + { + case "IProjectAppService.GetListAsync": + var list = await projects.GetListAsync(); + list.Items.ShouldHaveSingleItem(); + return list; + case "IProjectAppService.GetAsync": + return await projects.GetAsync("abp"); + case "IProjectAppService.GetVersionsAsync": + var versions = await projects.GetVersionsAsync("abp"); + versions.Items.ShouldHaveSingleItem(); + return versions; + case "IProjectAppService.GetDefaultLanguageCodeAsync": + var language = await projects.GetDefaultLanguageCodeAsync("abp", "2.0.0"); + language.ShouldBe("en"); + return language; + case "IProjectAppService.GetLanguageListAsync": + var languages = await projects.GetLanguageListAsync("abp", "2.0.0"); + languages.Languages.ShouldHaveSingleItem().Code.ShouldBe("en"); + return languages; + case "IDocumentAppService.GetAsync": + return await documents.GetAsync(new GetDocumentInput { ProjectId = projectId, Name = "CLI.md", LanguageCode = "en", Version = "2.0.0" }); + case "IDocumentAppService.GetDefaultAsync": + return await documents.GetDefaultAsync(new GetDefaultDocumentInput { ProjectId = projectId, LanguageCode = "en", Version = "2.0.0" }); + case "IDocumentAppService.GetNavigationAsync": + var navigation = await documents.GetNavigationAsync(new GetNavigationDocumentInput { ProjectId = projectId, LanguageCode = "en", Version = "2.0.0" }); + navigation.Items.ShouldHaveSingleItem().Path.ShouldBe("CLI.md"); + return navigation; + case "IDocumentAppService.GetParametersAsync": + var parameters = await documents.GetParametersAsync(new GetParametersDocumentInput { ProjectId = projectId, LanguageCode = "en", Version = "2.0.0" }); + parameters.Parameters.ShouldHaveSingleItem().Name.ShouldBe("platform"); + return parameters; + case "IDocumentAppService.GetResourceAsync": + var resource = await documents.GetResourceAsync(new GetDocumentResourceInput { ProjectId = projectId, Name = "image.png", LanguageCode = "en", Version = "2.0.0" }); + resource.Content.ShouldBe(new byte[] { 1, 2, 3 }); + return resource; + case "IDocumentAppService.SearchAsync": + var search = await documents.SearchAsync(new DocumentSearchInput { ProjectId = projectId, Context = "CLI", LanguageCode = "en", Version = "2.0.0" }); + search.TotalCount.ShouldBe(1); + search.Items.ShouldHaveSingleItem().Name.ShouldBe("CLI.md"); + return search; + case "IDocumentAppService.FullSearchEnabledAsync": + var enabled = await documents.FullSearchEnabledAsync(); + enabled.ShouldBeFalse(); + return enabled; + case "IDocumentAppService.GetUrlsAsync": + var urls = await documents.GetUrlsAsync("/docs/"); + urls.ShouldNotBeEmpty(); + urls.ShouldContain("/docs/en/abp/2.0.0/CLI"); + return urls; + case "IDocumentPdfAppService.DownloadPdfAsync": + return await GetRequiredService().DownloadPdfAsync(new DocumentPdfGeneratorInput { ProjectId = projectId, LanguageCode = "en", Version = "2.0.0" }); + case "IDocumentPdfAppService.ExistsAsync": + var exists = await GetRequiredService().ExistsAsync(new DocumentPdfGeneratorInput { ProjectId = projectId, LanguageCode = "en", Version = "2.0.0" }); + exists.ShouldBeTrue(); + return exists; + default: + throw new InvalidOperationException("Add a disclosure regression case for " + method); + } + } + + private static void AssertNoPrivateProperties(JsonElement element) + { + if (element.ValueKind == JsonValueKind.Object) + { + foreach (var property in element.EnumerateObject()) + { + SecretNames.ShouldNotContain(property.Name); + if (property.Name == "extraProperties") + { + property.Value.EnumerateObject().Select(item => item.Name) + .ShouldBe(new[] { "GithubVersionProviderSource", "VersionBranchPrefix" }, ignoreOrder: true); + } + AssertNoPrivateProperties(property.Value); + } + } + else if (element.ValueKind == JsonValueKind.Array) + { + foreach (var item in element.EnumerateArray()) + { + AssertNoPrivateProperties(item); + } + } + } + } +} diff --git a/npm/ng-packs/scripts/publish.ts b/npm/ng-packs/scripts/publish.ts index 102973d8dd..8ba3f5b681 100644 --- a/npm/ng-packs/scripts/publish.ts +++ b/npm/ng-packs/scripts/publish.ts @@ -1,6 +1,7 @@ import program from 'commander'; import execa from 'execa'; import fse from 'fs-extra'; +import { join } from 'path'; import replaceWithPreview from './replace-with-preview'; const semverParse = require('semver/functions/parse'); @@ -74,9 +75,10 @@ program.parse(process.argv); try { await fse.rename('../lerna.publish.json', '../lerna.json'); - let tag: string; + let tag: string | undefined; if (program.preview) tag = 'preview'; else if (semverParse(program.nextVersion).prerelease?.length) tag = 'next'; + else tag = await resolveMaintenanceDistTag(program.nextVersion, registry); await execa( 'yarn', @@ -117,3 +119,25 @@ async function updateVersion(version: string) { await execa('yarn', ['replace-with-tilde']); } + +// npm refuses to move the "latest" dist-tag to an older version. A patch of a +// previous release line (10.6.2 while 10.7.0 is latest) must use another tag. +async function resolveMaintenanceDistTag(version: string, registry: string) { + try { + const { stdout } = await execa( + 'node', + [ + join(__dirname, '../../publish-utils.js'), + '--distTag', + registry, + '--customVersion', + version, + ], + { cwd: join(__dirname, '../..') }, + ); + return stdout.trim() || undefined; + } catch (error) { + console.error(error.stderr || error); + return undefined; + } +} diff --git a/npm/publish-mvc.ps1 b/npm/publish-mvc.ps1 index c9737baeda..5832364536 100644 --- a/npm/publish-mvc.ps1 +++ b/npm/publish-mvc.ps1 @@ -22,6 +22,11 @@ $IsPrerelease = $(node publish-utils.js --prerelease --customVersion $Version) - if ($IsPrerelease) { $PacksPublishCommand = $PacksPublishCommand.Substring(0, $PacksPublishCommand.Length - 1) + " --tag next'" +} else { + $MaintenanceTag = "$(node publish-utils.js --distTag $Registry --customVersion $Version)".Trim() + if ($MaintenanceTag) { + $PacksPublishCommand = $PacksPublishCommand.Substring(0, $PacksPublishCommand.Length - 1) + " --tag $MaintenanceTag'" + } } $commands = ( diff --git a/npm/publish-ng.ps1 b/npm/publish-ng.ps1 index dac52b4427..080a1cde5a 100644 --- a/npm/publish-ng.ps1 +++ b/npm/publish-ng.ps1 @@ -19,7 +19,7 @@ if (-Not $Registry) { $UpdateNgPacksCommand = "yarn update-version $Version" $NgPacksPublishCommand = "npm run publish-packages -- --nextVersion $Version --skipGit --registry $Registry --skipVersionValidation" -$UpdateGulpCommand = "yarn update-gulp --registry $Registry" +$UpdateGulpCommand = "yarn update-gulp --registry $Registry --targetVersion $Version" $UpdateLeptonXCommand = "yarn update-lepton-x-versions -v $LeptonXVersion"; $IsPrerelease = $(node publish-utils.js --prerelease --customVersion $Version) -eq "true"; diff --git a/npm/publish-utils.js b/npm/publish-utils.js index d59f10a661..29a607e57a 100644 --- a/npm/publish-utils.js +++ b/npm/publish-utils.js @@ -1,11 +1,18 @@ +const { execFileSync } = require('child_process'); const { program } = require('commander'); const fse = require('fs-extra'); +const semverLt = require('semver/functions/lt'); const semverParse = require('semver/functions/parse'); +const semverValid = require('semver/functions/valid'); program.version('0.0.1'); program.option('-n, --nextVersion', 'version in common.props'); program.option('-pr, --prerelease', 'whether version is prerelease'); program.option('-cv, --customVersion ', 'set exact version'); +program.option( + '-dt, --distTag ', + 'print a dist-tag when this version is older than the registry latest tag', +); program.parse(process.argv); @@ -14,6 +21,8 @@ if (program.nextVersion) console.log(getVersion()); if (program.prerelease) console.log(!!semverParse(getVersion()).prerelease?.length); +if (program.distTag) console.log(resolvePublishDistTag(program.distTag)); + function getVersion() { if (program.customVersion) return program.customVersion; const commonProps = fse.readFileSync('../common.props').toString(); @@ -23,3 +32,34 @@ function getVersion() { const last = commonProps.indexOf(versionEndTag); return commonProps.substring(first, last); } + +// npm refuses to move the "latest" dist-tag to an older version. A patch of a +// previous release line (10.6.2 while 10.7.0 is latest) must use another tag. +function resolvePublishDistTag(registry) { + const version = getVersion(); + const parsed = semverParse(version); + if (!parsed || parsed.prerelease.length) { + return ''; + } + + let latest = ''; + try { + latest = execFileSync( + 'npm', + ['view', '@abp/core', 'version', '--registry', registry], + { + encoding: 'utf8', + shell: true, + stdio: ['ignore', 'pipe', 'ignore'], + }, + ).trim(); + } catch (e) { + return ''; + } + + if (!semverValid(latest) || !semverLt(version, latest)) { + return ''; + } + + return `latest-${parsed.major}.${parsed.minor}`; +} diff --git a/npm/update-gulp.js b/npm/update-gulp.js index 0788e5bb13..25481ffde2 100644 --- a/npm/update-gulp.js +++ b/npm/update-gulp.js @@ -4,6 +4,8 @@ const childProcess = require('child_process'); const execa = require('execa'); const fse = require('fs-extra'); const { program } = require('commander'); +const semverLt = require('semver/functions/lt'); +const semverValid = require('semver/functions/valid'); program.version('0.0.1'); program.option('-pr, --prerelease', 'whether version is prerelease'); @@ -12,6 +14,10 @@ program.option( 'NPM server registry', 'https://registry.npmjs.org' ); +program.option( + '-tv, --targetVersion ', + 'version being published; an older patch line is not moved up to the latest tag' +); program.parse(process.argv); const gulp = (folderPath) => { @@ -31,13 +37,17 @@ const gulp = (folderPath) => { } }; +// ncu's default target is the "latest" dist-tag. Publishing 10.6.2 while +// 10.7.0 is already latest would rewrite ~10.6.2 to ~10.7.0. Stay on the +// same minor for that maintenance release. A release that is itself latest +// still follows the latest tag, so a minor bump can move 10.7.x to 10.8.0. +const ncuTargetFlag = resolveNcuTargetFlag(); + const updatePackages = (pkgJsonPath) => { try { const result = childProcess .execSync( - `ncu "/^@abp.*$/" --packageFile ${pkgJsonPath} -u${ - program.prerelease ? ' --target newest' : '' - } --registry ${program.registry}` + `ncu "/^@abp.*$/" --packageFile ${pkgJsonPath} -u${ncuTargetFlag} --registry ${program.registry}` ) .toString(); console.log('\x1b[0m', result); @@ -46,6 +56,53 @@ const updatePackages = (pkgJsonPath) => { } }; +function resolveNcuTargetFlag() { + if (program.prerelease) { + return ' --target newest'; + } + + const version = getReleaseVersion(); + if (!semverValid(version)) { + return ''; + } + + let latest = ''; + try { + latest = childProcess + .execFileSync( + 'npm', + ['view', '@abp/core', 'version', '--registry', program.registry], + { encoding: 'utf8', shell: true, stdio: ['ignore', 'pipe', 'ignore'] } + ) + .trim(); + } catch (e) { + return ''; + } + + if (semverValid(latest) && semverLt(version, latest)) { + return ' --target patch'; + } + + return ''; +} + +function getReleaseVersion() { + if (program.targetVersion) { + return program.targetVersion; + } + + try { + const commonProps = fse.readFileSync(path.join(__dirname, '../common.props')).toString(); + const versionTag = ''; + const versionEndTag = ''; + const first = commonProps.indexOf(versionTag) + versionTag.length; + const last = commonProps.indexOf(versionEndTag); + return commonProps.substring(first, last).trim(); + } catch (e) { + return ''; + } +} + (async () => { console.time(); let files = await glob('../**/package.json');