diff --git a/samples/Dressca/dressca-backend/src/Dressca.ApplicationCore/ApplicationService/AssetApplicationService.cs b/samples/Dressca/dressca-backend/src/Dressca.ApplicationCore/ApplicationService/AssetApplicationService.cs index 49a4eea6e..1d4ee27fc 100644 --- a/samples/Dressca/dressca-backend/src/Dressca.ApplicationCore/ApplicationService/AssetApplicationService.cs +++ b/samples/Dressca/dressca-backend/src/Dressca.ApplicationCore/ApplicationService/AssetApplicationService.cs @@ -46,7 +46,9 @@ public AssetApplicationService( /// public async Task GetAssetStreamInfoAsync(string assetCode) { - this.logger.LogDebug(Events.DebugEvent, LogMessages.AssetApplicationService_GetAssetStreamInfoStart, assetCode); + // ログインジェクションを防ぐために改行文字を取り除きます。 + var sanitizedAssetCode = assetCode.RemoveNewlineCharacters(); + this.logger.LogDebug(Events.DebugEvent, LogMessages.AssetApplicationService_GetAssetStreamInfoStart, sanitizedAssetCode); Asset? asset; Stream? stream; @@ -68,7 +70,7 @@ public async Task GetAssetStreamInfoAsync(string assetCode) scope.Complete(); } - this.logger.LogDebug(Events.DebugEvent, LogMessages.AssetApplicationService_GetAssetStreamInfoEnd, assetCode); + this.logger.LogDebug(Events.DebugEvent, LogMessages.AssetApplicationService_GetAssetStreamInfoEnd, sanitizedAssetCode); return new(asset, stream); } diff --git a/samples/Dressca/dressca-backend/src/Dressca.EfInfrastructure/Migrations/20241211091203_InitialCreate.cs b/samples/Dressca/dressca-backend/src/Dressca.EfInfrastructure/Migrations/20241211091203_InitialCreate.cs index b00b8bc67..627a745a5 100644 --- a/samples/Dressca/dressca-backend/src/Dressca.EfInfrastructure/Migrations/20241211091203_InitialCreate.cs +++ b/samples/Dressca/dressca-backend/src/Dressca.EfInfrastructure/Migrations/20241211091203_InitialCreate.cs @@ -1,5 +1,4 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Migrations; #nullable disable diff --git a/samples/Dressca/dressca-backend/src/Dressca.SystemCommon/StringExtentions.cs b/samples/Dressca/dressca-backend/src/Dressca.SystemCommon/StringExtentions.cs new file mode 100644 index 000000000..e370dc05a --- /dev/null +++ b/samples/Dressca/dressca-backend/src/Dressca.SystemCommon/StringExtentions.cs @@ -0,0 +1,25 @@ +using System.Diagnostics.CodeAnalysis; + +namespace System; + +/// +/// クラスの拡張メソッドを提供します。 +/// +public static class StringExtentions +{ + /// + /// 対象の文字列から改行文字(\r、\n)を取り除きます。 + /// + /// 対象の文字列。 + /// 元の文字列から改行文字を取り除いた文字列。 + [return: NotNullIfNotNull(nameof(target))] + public static string? RemoveNewlineCharacters(this string? target) + { + if (target == null) + { + return null; + } + + return target.Replace("\n", string.Empty).Replace("\r", string.Empty); + } +} diff --git a/samples/Dressca/dressca-backend/tests/Dressca.UnitTests.SystemCommon/StringExtentionsTest.cs b/samples/Dressca/dressca-backend/tests/Dressca.UnitTests.SystemCommon/StringExtentionsTest.cs new file mode 100644 index 000000000..9e1ffcaec --- /dev/null +++ b/samples/Dressca/dressca-backend/tests/Dressca.UnitTests.SystemCommon/StringExtentionsTest.cs @@ -0,0 +1,88 @@ +namespace Dressca.UnitTests.SystemCommon; + +public class StringExtentionsTest +{ + [Theory] + [InlineData("\r\nLine1Line2", "Line1Line2")] // CRとLFを含む、先頭 + [InlineData("Line1\rLine2", "Line1Line2")] // CRのみを含む、中間 + [InlineData("Line1Line2\n", "Line1Line2")] // LFのみを含む、末尾 + public void RemoveNewlineCharacters_改行文字があれば取り除かれる(string input, string expected) + { + // Arrange + + // Act + var actual = input.RemoveNewlineCharacters(); + + // Assert + Assert.Equal(expected, actual); + } + + [Fact] + public void RemoveNewlineCharacters_改行文字なし_変化なし() + { + // Arrange + var target = "Line1Line2"; + + // Act + var actual = target.RemoveNewlineCharacters(); + + // Assert + Assert.Equal(target, actual); + } + + [Fact] + public void RemoveNewlineCharacters_null_nullを返却() + { + // Arrange + string? target = null; + + // Act + var actual = target.RemoveNewlineCharacters(); + + // Assert + Assert.Null(actual); + } + + [Fact] + public void RemoveNewlineCharacters_空文字_変化なし() + { + // Arrange + var target = string.Empty; + + // Act + var actual = target.RemoveNewlineCharacters(); + + // Assert + Assert.Equal(target, actual); + } + + [Theory] + [InlineData(" ")] + [InlineData(" ")] + public void RemoveNewlineCharacters_空白文字_変化なし(string target) + { + // Arrange + + // Act + var actual = target.RemoveNewlineCharacters(); + + // Assert + Assert.Equal(target, actual); + } + + [Theory] + [InlineData("\r\n")] + [InlineData("\r")] + [InlineData("\n")] + public void RemoveNewlineCharacters_改行コードのみ_取り除かれて空文字になる(string target) + { + // Arrange + var expected = string.Empty; + + // Act + var actual = target.RemoveNewlineCharacters(); + + // Assert + Assert.Equal(expected, actual); + } +}