[GH-ISSUE #28] Multiple enumeration of AssertValidLicense results in incorrect validation #27

Closed
opened 2026-02-26 01:35:48 +03:00 by kerem · 1 comment
Owner

Originally created by @AntiGuideAkquinet on GitHub (Feb 24, 2023).
Original GitHub issue: https://github.com/junian/Standard.Licensing/issues/28

When given an invalid license, AssertValidLicense returns an IEnumerable with a ValidationFailure on the first enumeration but an empty IEnumerable on the second enumeration.

As a workaround, I suggest users of the library use ToArray or alike to enumerate the IEnumerable only once.

The reason for this behavior is the current implementation of the validators collection in ValidationChainBuilder as a queue. In AssertValidLicense, all validators will be dequeued every time, resulting in different results.

I suggest either the usage of a list or the change of AssertValidLicense behavior to throw instead of returning failures. The name indicates a throw on invalid licenses and could potentially be dangerous to people relying on the implied behavior.

using Standard.Licensing;
using Standard.Licensing.Validation;

var keyGenerator = Standard.Licensing.Security.Cryptography.KeyGenerator.Create();
var keyPair = keyGenerator.GenerateKeyPair();
var publicKey = keyPair.ToPublicKeyString();

var invalidLicense = @"<License>
  <Signature>WFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFg=</Signature>
</License>";

var licenseToVerify = License.Load(invalidLicense);

var validationFailures = licenseToVerify
    .Validate()
    .Signature(publicKey)
    .AssertValidLicense();

Console.WriteLine("validationFailures.Length: " + validationFailures.ToArray().Length);
Console.WriteLine("validationFailures.Length: " + validationFailures.ToArray().Length);

// validationFailures.Length: 1
// validationFailures.Length: 0
Originally created by @AntiGuideAkquinet on GitHub (Feb 24, 2023). Original GitHub issue: https://github.com/junian/Standard.Licensing/issues/28 When given an invalid license, AssertValidLicense returns an IEnumerable with a ValidationFailure on the first enumeration but an empty IEnumerable on the second enumeration. As a workaround, I suggest users of the library use ToArray or alike to enumerate the IEnumerable only once. The reason for this behavior is the current implementation of the validators collection in ValidationChainBuilder as a queue. In AssertValidLicense, all validators will be dequeued every time, resulting in different results. I suggest either the usage of a list or the change of `AssertValidLicense` behavior to throw instead of returning failures. The name indicates a throw on invalid licenses and could potentially be dangerous to people relying on the implied behavior. ```C# using Standard.Licensing; using Standard.Licensing.Validation; var keyGenerator = Standard.Licensing.Security.Cryptography.KeyGenerator.Create(); var keyPair = keyGenerator.GenerateKeyPair(); var publicKey = keyPair.ToPublicKeyString(); var invalidLicense = @"<License> <Signature>WFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFg=</Signature> </License>"; var licenseToVerify = License.Load(invalidLicense); var validationFailures = licenseToVerify .Validate() .Signature(publicKey) .AssertValidLicense(); Console.WriteLine("validationFailures.Length: " + validationFailures.ToArray().Length); Console.WriteLine("validationFailures.Length: " + validationFailures.ToArray().Length); // validationFailures.Length: 1 // validationFailures.Length: 0 ```
kerem closed this issue 2026-02-26 01:35:48 +03:00
Author
Owner

@skst commented on GitHub (Apr 27, 2024):

Closing this issue means that the following line in the README.md can be deleted. (I didn't think it was worth a pull request, but I'm happy to create one if that's preferred.) 🙂

Make sure to call validationFailures.ToList() or validationFailures.ToArray() before using the result multiple times.

<!-- gh-comment-id:2081087287 --> @skst commented on GitHub (Apr 27, 2024): Closing this issue means that the following line in the `README.md` can be deleted. (I didn't think it was worth a pull request, but I'm happy to create one if that's preferred.) 🙂 `Make sure to call validationFailures.ToList() or validationFailures.ToArray() before using the result multiple times.`
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/Standard.Licensing#27
No description provided.