diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs index 0d5ec998497f9..ff91a8b2ed1e5 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs @@ -725,12 +725,6 @@ private static bool TryGetNextExtendedAttribute( } line = line.Slice(spacePos + 1).TrimStart((byte)' '); - // If there are any more spaces, it's malformed. - if (line.IndexOf((byte)' ') >= 0) - { - return false; - } - // Find the equal separator. int equalPos = line.IndexOf((byte)'='); if (equalPos < 0) diff --git a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.GlobalExtendedAttributes.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.GlobalExtendedAttributes.Tests.cs index 2661c542ae4c0..9874835fdda23 100644 --- a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.GlobalExtendedAttributes.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.GlobalExtendedAttributes.Tests.cs @@ -82,5 +82,27 @@ public void ExtractGlobalExtendedAttributesEntry_Throws() Assert.Throws(() => entry.ExtractToFile(Path.Join(root.Path, "file"), overwrite: true)); } } + + [Theory] + [InlineData("key", "value")] + [InlineData("key ", " value ")] + [InlineData(" key ", " value ")] + [InlineData("many sla/s\\hes", "/////////////\\\\\\///////////")] + public void GlobalExtendedAttribute_Roundtrips(string key, string value) + { + var stream = new MemoryStream(); + using (var writer = new TarWriter(stream, leaveOpen: true)) + { + writer.WriteEntry(new PaxGlobalExtendedAttributesTarEntry(new Dictionary() { { key, value } })); + } + + stream.Position = 0; + using (var reader = new TarReader(stream)) + { + PaxGlobalExtendedAttributesTarEntry entry = Assert.IsType(reader.GetNextEntry()); + Assert.Equal(1, entry.GlobalExtendedAttributes.Count); + Assert.Equal(KeyValuePair.Create(key.TrimStart(), value), entry.GlobalExtendedAttributes.First()); + } + } } } diff --git a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs index 17c67423c390b..d46bf6c829c29 100644 --- a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs @@ -325,6 +325,28 @@ public void PaxSizeLargerThanMaxAllowedByStream() Assert.Throws(() => reader.GetNextEntry()); } + [Theory] + [InlineData("key", "value")] + [InlineData("key ", " value ")] + [InlineData(" key ", " value ")] + [InlineData("many sla/s\\hes", "/////////////\\\\\\///////////")] + public void PaxExtendedAttribute_Roundtrips(string key, string value) + { + var stream = new MemoryStream(); + using (var writer = new TarWriter(stream, leaveOpen: true)) + { + writer.WriteEntry(new PaxTarEntry(TarEntryType.Directory, "entryName", new Dictionary() { { key, value } })); + } + + stream.Position = 0; + using (var reader = new TarReader(stream)) + { + PaxTarEntry entry = Assert.IsType(reader.GetNextEntry()); + Assert.Equal(5, entry.ExtendedAttributes.Count); + Assert.Contains(KeyValuePair.Create(key.TrimStart(), value), entry.ExtendedAttributes); + } + } + private static void VerifyDataStreamOfTarUncompressedInternal(string testFolderName, string testCaseName, bool copyData) { using MemoryStream archiveStream = GetTarMemoryStream(CompressionMethod.Uncompressed, testFolderName, testCaseName);