Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PAX extended attribute reading logic to treat '=' character as valid in the value strings. #82810

Merged
merged 5 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -733,12 +733,6 @@ private static bool TryGetNextExtendedAttribute(
ReadOnlySpan<byte> keySlice = line.Slice(0, equalPos);
ReadOnlySpan<byte> valueSlice = line.Slice(equalPos + 1);

// If the value contains an =, it's malformed.
if (valueSlice.IndexOf((byte)'=') >= 0)
{
return false;
}

// Return the parsed key and value.
key = Encoding.UTF8.GetString(keySlice);
value = Encoding.UTF8.GetString(valueSlice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,7 @@ public void ExtractGlobalExtendedAttributesEntry_Throws()
}

[Theory]
[InlineData("key", "value")]
[InlineData("key ", "value ")]
[InlineData(" key", " value")]
[InlineData(" key ", " value ")]
[InlineData(" key spaced ", " value spaced ")]
[InlineData("many sla/s\\hes", "/////////////\\\\\\///////////")]
[MemberData(nameof(GetPaxExtendedAttributesRoundtripTestData))]
public void GlobalExtendedAttribute_Roundtrips(string key, string value)
{
var stream = new MemoryStream();
Expand All @@ -104,6 +99,7 @@ public void GlobalExtendedAttribute_Roundtrips(string key, string value)
PaxGlobalExtendedAttributesTarEntry entry = Assert.IsType<PaxGlobalExtendedAttributesTarEntry>(reader.GetNextEntry());
Assert.Equal(1, entry.GlobalExtendedAttributes.Count);
Assert.Equal(KeyValuePair.Create(key, value), entry.GlobalExtendedAttributes.First());
Assert.Null(reader.GetNextEntry());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,30 +333,6 @@ public void PaxSizeLargerThanMaxAllowedByStream()
Assert.Throws<ArgumentOutOfRangeException>(() => reader.GetNextEntry());
}

[Theory]
[InlineData("key", "value")]
[InlineData("key ", "value ")]
[InlineData(" key", " value")]
[InlineData(" key ", " value ")]
[InlineData(" key spaced ", " value spaced ")]
[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<string, string>() { { key, value } }));
}

stream.Position = 0;
using (var reader = new TarReader(stream))
{
PaxTarEntry entry = Assert.IsType<PaxTarEntry>(reader.GetNextEntry());
Assert.Equal(5, entry.ExtendedAttributes.Count);
Assert.Contains(KeyValuePair.Create(key, value), entry.ExtendedAttributes);
}
}

private static void VerifyDataStreamOfTarUncompressedInternal(string testFolderName, string testCaseName, bool copyData)
{
using MemoryStream archiveStream = GetTarMemoryStream(CompressionMethod.Uncompressed, testFolderName, testCaseName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,25 @@ public void TarReader_LeaveOpen_False_CopiedDataNotDisposed()
ds.Dispose();
}
}

[Theory]
[MemberData(nameof(GetPaxExtendedAttributesRoundtripTestData))]
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<string, string>() { { key, value } }));
}

stream.Position = 0;
using (var reader = new TarReader(stream))
{
PaxTarEntry entry = Assert.IsType<PaxTarEntry>(reader.GetNextEntry());
Assert.Equal(5, entry.ExtendedAttributes.Count);
Assert.Contains(KeyValuePair.Create(key, value), entry.ExtendedAttributes);
Assert.Null(reader.GetNextEntry());
}
}
}
}
16 changes: 16 additions & 0 deletions src/libraries/System.Formats.Tar/tests/TarTestsBase.Pax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,21 @@ protected void VerifyExtendedAttributeTimestamps(PaxTarEntry pax)
VerifyExtendedAttributeTimestamp(pax, PaxEaATime, MinimumTime);
VerifyExtendedAttributeTimestamp(pax, PaxEaCTime, MinimumTime);
}

public static IEnumerable<object[]> GetPaxExtendedAttributesRoundtripTestData()
{
yield return new object[] { "key", "value" };
yield return new object[] { "key ", "value " };
yield return new object[] { " key", " value" };
yield return new object[] { " key ", " value " };
yield return new object[] { " key spaced ", " value spaced " };
yield return new object[] { "many sla/s\\hes", "/////////////\\\\\\///////////" };
yield return new object[] { "key", "=" };
yield return new object[] { "key", "=value" };
yield return new object[] { "key", "va=lue" };
yield return new object[] { "key", "value=" };
// real world scenario
yield return new object[] { "MSWINDOWS.rawsd", "AQAAgBQAAAAkAAAAAAAAAAAAAAABAgAAAAAABSAAAAAhAgAAAQIAAAAAAAUgAAAAIQIAAA==" };
}
}
}