Browse Source

Filter public project properties in VoloDocs mappings

pull/26353/head
maliming 4 weeks ago
parent
commit
ecdb0dde72
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 10
      modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationMappers.cs
  2. 8
      modules/docs/src/Volo.Docs.Common.Application/Volo/Docs/Common/DocsCommonApplicationMappers.cs
  3. 25
      modules/docs/src/Volo.Docs.Common.Application/Volo/Docs/Common/Projects/ProjectAppService.cs
  4. 21
      modules/docs/src/Volo.Docs.Common.Application/Volo/Docs/Common/Projects/ProjectDtoExtensions.cs
  5. 77
      modules/docs/test/Volo.Docs.Admin.Application.Tests/Volo/Docs/ProjectAdminAppServiceAuthorization_Tests.cs
  6. 5
      modules/docs/test/Volo.Docs.Admin.Application.Tests/Volo/Docs/ProjectAdminAppService_Tests.cs
  7. 45
      modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/ApplicationService_Tests.cs
  8. 134
      modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/DocumentAppService_Tests.cs
  9. 155
      modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/ProjectMapping_Tests.cs
  10. 264
      modules/docs/test/Volo.Docs.Application.Tests/Volo/Docs/PublicAppServiceDataLeakage_Tests.cs

10
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<VersionInfo,
[Mapper(RequiredMappingStrategy = RequiredMappingStrategy.Target)]
public partial class ProjectToProjectDtoMapper : MapperBase<Project, ProjectDto>
{
[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<Documen
[MapperIgnoreTarget(nameof(DocumentWithDetailsDto.Project))]
[MapperIgnoreTarget(nameof(DocumentWithDetailsDto.Contributors))]
public override partial void Map(Document source, DocumentWithDetailsDto destination);
}
}

8
modules/docs/src/Volo.Docs.Common.Application/Volo/Docs/Common/DocsCommonApplicationMappers.cs

@ -1,5 +1,6 @@
using Riok.Mapperly.Abstractions;
using Volo.Abp.Mapperly;
using Volo.Abp.Data;
using Volo.Docs.Common.Projects;
using Volo.Docs.Projects;
@ -8,9 +9,16 @@ namespace Volo.Docs.Common;
[Mapper(RequiredMappingStrategy = RequiredMappingStrategy.Target)]
public partial class ProjectToProjectDtoMapper : MapperBase<Project, ProjectDto>
{
[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)]

25
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<ProjectDto>(
ObjectMapper.Map<List<Project>, List<ProjectDto>>(projects)
);
return new ListResultDto<ProjectDto>(
projectDtos.Select(p => HidePrivateProperties(p)).ToList()
ObjectMapper.Map<List<Project>, List<ProjectDto>>(projects)
);
}
@ -52,9 +48,7 @@ namespace Volo.Docs.Common.Projects
{
var project = await _projectRepository.GetByShortNameAsync(shortName);
var projectDto = ObjectMapper.Map<Project, ProjectDto>(project);
return HidePrivateProperties(projectDto);
return ObjectMapper.Map<Project, ProjectDto>(project);
}
public virtual async Task<ListResultDto<VersionInfoDto>> GetVersionsAsync(string shortName)
@ -159,20 +153,5 @@ namespace Volo.Docs.Common.Projects
? project.GetProperty<GithubVersionProviderSource>("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;
}
}
}

21
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;
}
}

77
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<IAuthorizationService, AbpAuthorizationService>());
services.Replace(ServiceDescriptor.Transient<IAbpAuthorizationService, AbpAuthorizationService>());
services.Replace(ServiceDescriptor.Transient<IMethodInvocationAuthorizationService, MethodInvocationAuthorizationService>());
var permissionChecker = Substitute.For<IPermissionChecker>();
permissionChecker.IsGrantedAsync(Arg.Any<ClaimsPrincipal>(), Arg.Any<string>()).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<IMethodInvocationAuthorizationService>().ShouldBeOfType<MethodInvocationAuthorizationService>();
GetRequiredService<ICurrentUser>().IsAuthenticated.ShouldBeFalse();
var service = GetRequiredService<IProjectAdminAppService>();
var id = GetRequiredService<DocsTestData>().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<string, object>()
}),
"UpdateAsync" => service.UpdateAsync(id, new UpdateProjectDto
{
Name = "Denied update", Format = "md", DefaultDocumentName = "CLI",
NavigationDocumentName = "docs-nav.json", ParametersDocumentName = "docs-params.json",
ExtraProperties = new Dictionary<string, object>()
}),
_ => throw new ArgumentOutOfRangeException(nameof(method))
};
}
await Should.ThrowAsync<AbpAuthorizationException>(InvokeAsync);
var repository = GetRequiredService<IProjectRepository>();
var project = await repository.GetAsync(id);
project.Name.ShouldBe("ABP vNext");
project.GetProperty<string>("GitHubAccessToken").ShouldBe("123456");
(await repository.GetListAsync()).Count.ShouldBe(1);
}
}
}

5
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<string>("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<string>("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<string>("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<string>("GitHubAccessToken").ShouldBe("123456");
}
[Fact]

45
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<IProjectRepository>();
using (var uow = GetRequiredService<IUnitOfWorkManager>().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>("GithubVersionProviderSource")
.ShouldBe(GithubVersionProviderSource.Branches);
result.GetProperty<string>("VersionBranchPrefix").ShouldBe("rel-");
JsonSerializer.Serialize(result).ShouldNotContain(secret);
project.GetProperty<string>("CustomSecret").ShouldBe(secret);
project.GetProperty<string>("GitHubAccessToken").ShouldBe("123456");
project.GetProperty<string>("GitHubRootUrl").ShouldNotBeNullOrEmpty();
await uow.CompleteAsync();
}
var savedProject = await repository.GetAsync(_testData.ProjectId);
savedProject.GetProperty<string>("CustomSecret").ShouldBe(secret);
savedProject.GetProperty<string>("GitHubAccessToken").ShouldBe("123456");
}
[Fact]

134
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<IHostEnvironment>();
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<DocsTestData>();
var projectRepository = GetRequiredService<IProjectRepository>();
var documentAppService = GetRequiredService<IDocumentAppService>();
var repositoryManager = GetRequiredService<IGithubRepositoryManager>();
var documentName = retrievalMode == "Fresh" ? "Uncached.md" : "CLI.md";
if (retrievalMode == "Development")
{
GetRequiredService<IHostEnvironment>().EnvironmentName = Environments.Development;
}
using (var uow = GetRequiredService<IUnitOfWorkManager>().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<IDocumentRepository>();
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<string>(), Arg.Any<string>(), Arg.Any<string>())
.Returns(Task.FromException<string>(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<string>("GitHubAccessToken").ShouldBeNull();
document.Project.GetProperty<string>("GitHubUserAgent").ShouldBeNull();
document.Project.ExtraProperties.Keys.ShouldBe(new[] { "GithubVersionProviderSource", "VersionBranchPrefix" }, ignoreOrder: true);
document.Project.GetProperty<GithubVersionProviderSource>("GithubVersionProviderSource")
.ShouldBe(GithubVersionProviderSource.Branches);
document.Project.GetProperty<string>("VersionBranchPrefix").ShouldBe("2.");
var json = JsonSerializer.Serialize(document, new JsonSerializerOptions(JsonSerializerDefaults.Web));
json.ShouldNotContain(token);
json.ShouldNotContain(userAgent);
json.ShouldNotContain(customSecret);
project.GetProperty<string>("GitHubAccessToken").ShouldBe(token);
project.GetProperty<string>("GitHubUserAgent").ShouldBe(userAgent);
project.GetProperty<string>("CustomSecret").ShouldBe(customSecret);
project.GetProperty<string>("GitHubRootUrl").ShouldNotBeNullOrEmpty();
if (retrievalMode == "Cached")
{
await repositoryManager.DidNotReceive().GetFileRawStringContentAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>());
}
else
{
await repositoryManager.Received().GetFileRawStringContentAsync(Arg.Any<string>(), token, userAgent);
}
await uow.CompleteAsync();
}
var savedProject = await projectRepository.GetAsync(testData.ProjectId);
savedProject.GetProperty<string>("GitHubAccessToken").ShouldBe(token);
savedProject.GetProperty<string>("GitHubUserAgent").ShouldBe(userAgent);
savedProject.GetProperty<string>("CustomSecret").ShouldBe(customSecret);
savedProject.GetProperty<string>("GitHubRootUrl").ShouldNotBeNullOrEmpty();
}
}
}

155
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<Project, string>("RegisteredSecret");
ObjectExtensionManager.Instance.AddOrUpdateProperty<ProjectDto, string>("RegisteredSecret");
}
public static IEnumerable<object[]> 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<string, object> { ["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<Project, ProjectDto> directMapper = documentMapper
? new ProjectToProjectDtoMapper()
: new Common.ProjectToProjectDtoMapper();
IObjectMapper objectMapper = documentMapper
? GetRequiredService<IObjectMapper<DocsApplicationModule>>()
: GetRequiredService<IObjectMapper<DocsCommonApplicationModule>>();
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<Project, ProjectDto>(source);
break;
case "ObjectExisting":
case "ObjectAliased":
result = objectMapper.Map(source, destination);
break;
case "ListNew":
result = objectMapper.Map<List<Project>, List<ProjectDto>>(new List<Project> { source }).Single();
break;
case "ListExisting":
result = objectMapper.Map(new List<Project> { source }, new List<ProjectDto> { destination, new ProjectDto() }).Single();
break;
case "ArrayNew":
result = objectMapper.Map<Project[], ProjectDto[]>(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>("GithubVersionProviderSource").ShouldBe(GithubVersionProviderSource.Branches);
result.GetProperty<string>("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;
}
}
}
}

264
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<IHostEnvironment>();
environment.EnvironmentName.Returns(Environments.Production);
services.AddSingleton(environment);
var search = Substitute.For<IDocumentFullSearch>();
search.SearchAsync(default, default, default, default).ReturnsForAnyArgs(new EsDocumentResult
{
TotalCount = 1,
EsDocuments = new List<EsDocument>
{
new EsDocument { Name = "CLI.md", FileName = "CLI.md", Version = "2.0.0", LanguageCode = "en", Highlight = new List<string> { "Public snippet" } }
}
});
services.AddSingleton(search);
var fileStore = Substitute.For<IProjectPdfFileStore>();
fileStore.GetOrNullAsync(default, default, default)
.ReturnsForAnyArgs(_ => new MemoryStream(Encoding.UTF8.GetBytes("Public document")));
services.AddSingleton(fileStore);
services.Configure<DocsElasticSearchOptions>(options => options.Enable = false);
}
public static IEnumerable<object[]> PublicMethods()
{
foreach (var service in PublicContracts())
{
foreach (var method in service.GetMethods())
{
yield return new object[] { service.Name + "." + method.Name };
}
}
}
private static IEnumerable<Type> 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<DocsTestData>();
var repository = GetRequiredService<IProjectRepository>();
var github = GetRequiredService<IGithubRepositoryManager>();
github.GetFileRawStringContentAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>())
.Returns(call =>
{
var url = call.ArgAt<string>(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<IUnitOfWorkManager>().Begin())
{
var project = await repository.GetAsync(testData.ProjectId);
foreach (var name in SecretNames)
{
project.SetProperty(name, "PRIVATE_MARKER_" + name);
}
project.SetProperty("NestedSecret", new Dictionary<string, object> { ["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<string>(name).ShouldBe("PRIVATE_MARKER_" + name);
}
}
private async Task<object> InvokeAsync(string method, Guid projectId)
{
var projects = GetRequiredService<IProjectAppService>();
var documents = GetRequiredService<IDocumentAppService>();
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<IDocumentPdfAppService>().DownloadPdfAsync(new DocumentPdfGeneratorInput { ProjectId = projectId, LanguageCode = "en", Version = "2.0.0" });
case "IDocumentPdfAppService.ExistsAsync":
var exists = await GetRequiredService<IDocumentPdfAppService>().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);
}
}
}
}
}
Loading…
Cancel
Save