Skip to content

Correctly implement fuzzers for new encryption streams - #131306

Open
alinpahontu2912 wants to merge 6 commits into
dotnet:mainfrom
alinpahontu2912:zipstream_fuzzers
Open

Correctly implement fuzzers for new encryption streams#131306
alinpahontu2912 wants to merge 6 commits into
dotnet:mainfrom
alinpahontu2912:zipstream_fuzzers

Conversation

@alinpahontu2912

@alinpahontu2912 alinpahontu2912 commented Jul 24, 2026

Copy link
Copy Markdown
Member

Correctly implement fuzzers fro the new encryption streams: zipcryptostream and winzipaesstream

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-meta
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the fuzzers for the new ZIP encryption streams to exercise the actual ZipArchive encryption/decryption paths (ZipCrypto and WinZip AES) rather than constructing the internal crypto streams via reflection.

Changes:

  • Replace reflection-based stream construction with ZipArchive.CreateEntry(..., password, ZipEncryptionMethod) + Open/OpenAsync(password) round-trip workflows.
  • Add validation that the entry is encrypted and the expected encryption method is recorded, and verify plaintext round-trips correctly (sync + async).
  • Add a “wrong password” scenario intended to ensure failures are handled as InvalidDataException (but it currently doesn’t assert failure if no exception is thrown).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/ZipCryptoStreamFuzzer.cs Moves from reflected ZipCryptoStream.Create to ZipArchive-based encrypt/decrypt round-trip for ZipCrypto (sync + async).
src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/WinZipAesStreamFuzzer.cs Moves from reflected WinZipAesStream.Create to ZipArchive-based encrypt/decrypt round-trip for AES128/192/256 (sync + async).
Comments suppressed due to low confidence (2)

src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/ZipCryptoStreamFuzzer.cs:101

  • The “wrong password must fail” check currently passes even if decryption succeeds without throwing. Use Assert.Throws so the fuzzer reliably flags regressions where a wrong password is accepted.
        // Decrypting with a wrong password must fail cleanly with InvalidDataException, never crash.
        try
        {
            using Stream stream = readEntry.Open("wrong-password".AsSpan());
            stream.CopyTo(Stream.Null);
        }
        catch (InvalidDataException)
        {
            // Expected: the header password verifier rejects the wrong key.
        }

src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/WinZipAesStreamFuzzer.cs:113

  • The “wrong password must fail” check currently passes even if decryption succeeds without throwing. Use Assert.Throws so the fuzzer reliably flags regressions where a wrong password is accepted.
        // Decrypting with a wrong password must fail cleanly with InvalidDataException, never crash.
        try
        {
            using Stream stream = readEntry.Open("wrong-password".AsSpan());
            stream.CopyTo(Stream.Null);
        }
        catch (InvalidDataException)
        {
            // Expected: the AES password verifier / HMAC rejects the wrong key.
        }

Comment thread src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/ZipCryptoStreamFuzzer.cs Outdated
Comment thread src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/WinZipAesStreamFuzzer.cs Outdated
Copilot AI review requested due to automatic review settings July 24, 2026 09:20
@alinpahontu2912 alinpahontu2912 changed the title Correctly implement fuzzers fro new encryption streams Correctly implement fuzzers for new encryption streams Jul 24, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/ZipCryptoStreamFuzzer.cs Outdated
Comment thread src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/WinZipAesStreamFuzzer.cs Outdated
Copilot AI review requested due to automatic review settings July 27, 2026 09:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/ZipCryptoStreamFuzzer.cs Outdated
Comment thread src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/WinZipAesStreamFuzzer.cs Outdated
@alinpahontu2912

Copy link
Copy Markdown
Member Author

@MihuBot fuzz WinZipAesStreamFuzzer

@MihuBot

MihuBot commented Jul 28, 2026

Copy link
Copy Markdown

Ran the fuzzer(s) successfully. Code coverage reports:

@alinpahontu2912

Copy link
Copy Markdown
Member Author

@MihuBot fuzz ZipCryptoStreamFuzzer

@MihuBot

MihuBot commented Jul 28, 2026

Copy link
Copy Markdown

Ran the fuzzer(s) successfully. Code coverage reports:

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 29, 2026 08:06
@alinpahontu2912

Copy link
Copy Markdown
Member Author

@MihuBot fuzz ZipCryptoStreamFuzzer

@alinpahontu2912

Copy link
Copy Markdown
Member Author

@MihuBot fuzz WinZipAesStreamFuzzer

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/ZipCryptoStreamFuzzer.cs Outdated
Comment thread src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/WinZipAesStreamFuzzer.cs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 29, 2026 08:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@alinpahontu2912

Copy link
Copy Markdown
Member Author

@MihuBot fuzz ZipCryptoStreamFuzzer

@MihuBot

MihuBot commented Jul 29, 2026

Copy link
Copy Markdown

Ran the fuzzer(s) successfully. Code coverage reports:

@MihaZupan MihaZupan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks a lot better!

Comment thread src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/WinZipAesStreamFuzzer.cs Outdated
Comment thread src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/WinZipAesStreamFuzzer.cs Outdated
Comment thread src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/WinZipAesStreamFuzzer.cs Outdated
Comment thread src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/WinZipAesStreamFuzzer.cs Outdated
Comment thread src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/WinZipAesStreamFuzzer.cs Outdated
Comment thread src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/ZipCryptoStreamFuzzer.cs Outdated
Copilot AI review requested due to automatic review settings July 30, 2026 12:06
@alinpahontu2912

Copy link
Copy Markdown
Member Author

@MihuBot fuzz ZipEncryptionStreamFuzzer

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers;
@MihuBot

MihuBot commented Jul 30, 2026

Copy link
Copy Markdown

Ran the fuzzer(s) successfully. Code coverage reports:

Comment on lines +151 to +175
Stream wrongStream = async
? await readEntry.OpenAsync(wrongPassword.AsSpan())
: readEntry.Open(wrongPassword.AsSpan());
try
{
if (async)
{
await wrongStream.CopyToAsync(Stream.Null);
}
else
{
wrongStream.CopyTo(Stream.Null);
}
}
finally
{
if (async)
{
await wrongStream.DisposeAsync();
}
else
{
wrongStream.Dispose();
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Stream wrongStream = async
? await readEntry.OpenAsync(wrongPassword.AsSpan())
: readEntry.Open(wrongPassword.AsSpan());
try
{
if (async)
{
await wrongStream.CopyToAsync(Stream.Null);
}
else
{
wrongStream.CopyTo(Stream.Null);
}
}
finally
{
if (async)
{
await wrongStream.DisposeAsync();
}
else
{
wrongStream.Dispose();
}
}
if (async)
{
await using Stream wrongStream = await readEntry.OpenAsync(wrongPassword.AsSpan());
await wrongStream.CopyToAsync(Stream.Null);
}
else
{
using Stream wrongStream = readEntry.Open(wrongPassword.AsSpan());
wrongStream.CopyTo(Stream.Null);
}

Comment on lines +139 to +140
// AES is authenticated: the password verifier and HMAC make accepting a wrong key
// cryptographically infeasible, so a wrong password must fail with InvalidDataException.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// AES is authenticated: the password verifier and HMAC make accepting a wrong key
// cryptographically infeasible, so a wrong password must fail with InvalidDataException.
// The AES variant of ZIP encryption uses a form of HMAC that makes accidentally accepting the wrong key
// statistically unlikely, so a wrong password must fail with InvalidDataException.

"AES is authenticated" and "cryptographically infeasible" are too strong statements here :)

RoundTrip(buffer.Memory, password, method, async: true).GetAwaiter().GetResult();
}

private static async Task RoundTrip(ReadOnlyMemory<byte> content, string password, ZipEncryptionMethod method, bool async)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just making the note that you may see less edge-case coverage because your decryption logic is only exercising well-formed inputs produced by our own library.

Comment on lines +42 to +49
if (bytes.IsEmpty)
{
return;
}

// Use the first byte to select the encryption method so all variants get exercised.
ZipEncryptionMethod method = s_methods[bytes[0] % s_methods.Length];
ReadOnlySpan<byte> payload = bytes.Slice(1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (bytes.IsEmpty)
{
return;
}
// Use the first byte to select the encryption method so all variants get exercised.
ZipEncryptionMethod method = s_methods[bytes[0] % s_methods.Length];
ReadOnlySpan<byte> payload = bytes.Slice(1);
if (bytes.Length < 2)
{
return;
}
// Use the first byte to select the encryption method so all variants get exercised.
ZipEncryptionMethod method = s_methods[bytes[0] % s_methods.Length];
ReadOnlySpan<byte> payload = bytes.Slice(2); // Slice 2 to keep MemoryMarshal.Cast<byte, char> below aligned

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants