819f1b4665
Each finding is a focused validation guard or upper bound at a trust boundary.
Highlights:
- Commons-015: EncryptionMetadata ctor now validates Algorithm (AES-256-GCM
only), Kdf (PBKDF2-SHA256 only), Iterations ([100k, 10M]), non-null Salt/IV.
- Transport-004: new BundleUnlockRateLimiter (sliding-window, per-key,
singleton) wired into BundleImporter.LoadAsync; over-budget callers see
BundleUnlockRateLimitedException. Per-bundle 3-strike + per-window cap.
- ESG-022: ExternalSystemClient.InvokeHttpAsync allow-lists the documented
GET/POST/PUT/PATCH/DELETE set (case-insensitive); unknown verbs throw.
- SEL-015: SiteEventLogger queue now bounded (10k cap, DropOldest); dropped
events fault their Task and increment FailedWriteCount so the drop is
observable instead of an unbounded memory growth.
- SEL-017: EventLogQueryService clamps caller-supplied PageSize to a new
MaxQueryPageSize cap (default 500) so int.MaxValue can't OOM the host.
- SEL-020: LogEventAsync rejects severities outside {Info, Warning, Error}
(matches SQLite BINARY-collation query filter).
- InboundAPI-020: ContentType "json" check now case-insensitive
(application/JSON no longer slips through as not-json).
- InboundAPI-024: _knownBadMethods capped at 1000 entries (drops new entries
once full); per-request DB lookup remains the correctness path.
- SR-025: HandleSetStaticAttribute validates the attribute name against the
deployed config; unknown names now return Success=false instead of
leaking orphan override rows into the SQLite store.
- TE-021: MoveTemplateAsync runs the sibling-name-collision check at the
destination, mirroring TemplateFolderService.MoveFolderAsync.
- TE-022: LockEnforcer's once-locked-stays-locked rule now also covers
LockedInDerived (was previously only IsLocked).
New regression tests across 8 test projects (EncryptionMetadata, rate
limiter, ESG client allow-list, SEL bounded channel / PageSize clamp /
severity validation, InboundAPI ContentType + bad-methods cap, SiteRT
unknown-attribute, TemplateEngine MoveTemplate + LockedInDerived).
Build clean; affected suites all green. README regenerated: 93 open (was 104).
Note: a separate manual re-run was needed for the SiteEventLogging hunk
because its initial subagent's source edits never landed on disk despite
reporting success (file-collision-style failure mode).
114 lines
4.5 KiB
C#
114 lines
4.5 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using ScadaLink.Commons.Types.Transport;
|
|
using ScadaLink.Transport.Encryption;
|
|
|
|
namespace ScadaLink.Transport.Tests.Encryption;
|
|
|
|
public sealed class BundleSecretEncryptorTests
|
|
{
|
|
// Commons-015 sets the documented PBKDF2 floor to 100_000 (OWASP minimum);
|
|
// the production value is 600_000. Using the floor keeps the test fast while
|
|
// remaining a valid EncryptionMetadata.Iterations.
|
|
private const int TestIterations = EncryptionMetadata.MinPbkdf2Iterations;
|
|
|
|
[Fact]
|
|
public void Encrypt_then_Decrypt_roundtrips_arbitrary_bytes()
|
|
{
|
|
var sut = new BundleSecretEncryptor();
|
|
var plaintext = Encoding.UTF8.GetBytes("the quick brown fox jumps over the lazy dog");
|
|
|
|
var (ciphertext, metadata) = sut.Encrypt(plaintext, "correct-horse-battery-staple", TestIterations);
|
|
var recovered = sut.Decrypt(ciphertext, metadata, "correct-horse-battery-staple");
|
|
|
|
Assert.Equal(plaintext, recovered);
|
|
}
|
|
|
|
[Fact]
|
|
public void Decrypt_with_wrong_passphrase_throws_CryptographicException()
|
|
{
|
|
var sut = new BundleSecretEncryptor();
|
|
var plaintext = Encoding.UTF8.GetBytes("secret payload");
|
|
|
|
var (ciphertext, metadata) = sut.Encrypt(plaintext, "right-pass", TestIterations);
|
|
|
|
Assert.ThrowsAny<CryptographicException>(() => sut.Decrypt(ciphertext, metadata, "wrong-pass"));
|
|
}
|
|
|
|
[Fact]
|
|
public void Decrypt_with_tampered_ciphertext_throws_CryptographicException()
|
|
{
|
|
var sut = new BundleSecretEncryptor();
|
|
var plaintext = Encoding.UTF8.GetBytes("secret payload");
|
|
|
|
var (ciphertext, metadata) = sut.Encrypt(plaintext, "pass", TestIterations);
|
|
ciphertext[0] ^= 0xFF; // Flip every bit in the first ciphertext byte.
|
|
|
|
Assert.ThrowsAny<CryptographicException>(() => sut.Decrypt(ciphertext, metadata, "pass"));
|
|
}
|
|
|
|
[Fact]
|
|
public void Encrypt_produces_distinct_ciphertext_for_same_input_due_to_random_iv()
|
|
{
|
|
var sut = new BundleSecretEncryptor();
|
|
var plaintext = Encoding.UTF8.GetBytes("same input");
|
|
|
|
var (ct1, meta1) = sut.Encrypt(plaintext, "pass", TestIterations);
|
|
var (ct2, meta2) = sut.Encrypt(plaintext, "pass", TestIterations);
|
|
|
|
Assert.NotEqual(ct1, ct2);
|
|
Assert.NotEqual(meta1.IvB64, meta2.IvB64);
|
|
Assert.NotEqual(meta1.SaltB64, meta2.SaltB64);
|
|
}
|
|
|
|
[Fact]
|
|
public void Decrypt_with_mismatched_AAD_throws_CryptographicException()
|
|
{
|
|
// T-005: AES-GCM AAD binds extra context (e.g. manifest non-derivative fields)
|
|
// into the auth tag. Decrypting with different AAD yields a tag mismatch even
|
|
// when the passphrase and ciphertext are correct.
|
|
var sut = new BundleSecretEncryptor();
|
|
var plaintext = Encoding.UTF8.GetBytes("secret payload");
|
|
var aadOnEncrypt = Encoding.UTF8.GetBytes("manifest-canonical-hash-A");
|
|
var aadOnDecrypt = Encoding.UTF8.GetBytes("manifest-canonical-hash-B");
|
|
|
|
var (ciphertext, metadata) = sut.Encrypt(plaintext, "pass", TestIterations, aadOnEncrypt);
|
|
|
|
Assert.ThrowsAny<CryptographicException>(
|
|
() => sut.Decrypt(ciphertext, metadata, "pass", aadOnDecrypt));
|
|
}
|
|
|
|
[Fact]
|
|
public void Decrypt_with_matching_AAD_recovers_plaintext()
|
|
{
|
|
// T-005: round-trip with non-empty AAD must succeed when both sides match.
|
|
var sut = new BundleSecretEncryptor();
|
|
var plaintext = Encoding.UTF8.GetBytes("secret payload");
|
|
var aad = Encoding.UTF8.GetBytes("manifest-canonical-hash");
|
|
|
|
var (ciphertext, metadata) = sut.Encrypt(plaintext, "pass", TestIterations, aad);
|
|
var recovered = sut.Decrypt(ciphertext, metadata, "pass", aad);
|
|
|
|
Assert.Equal(plaintext, recovered);
|
|
}
|
|
|
|
[Fact]
|
|
public void Encrypt_emits_metadata_matching_decryption_inputs()
|
|
{
|
|
var sut = new BundleSecretEncryptor();
|
|
var plaintext = Encoding.UTF8.GetBytes("payload");
|
|
|
|
var (ciphertext, metadata) = sut.Encrypt(plaintext, "pass", TestIterations);
|
|
|
|
Assert.Equal("AES-256-GCM", metadata.Algorithm);
|
|
Assert.Equal("PBKDF2-SHA256", metadata.Kdf);
|
|
Assert.Equal(TestIterations, metadata.Iterations);
|
|
// Salt is 16 bytes (24 chars b64 incl padding), Iv is 12 bytes (16 chars b64 incl padding).
|
|
Assert.Equal(16, Convert.FromBase64String(metadata.SaltB64).Length);
|
|
Assert.Equal(12, Convert.FromBase64String(metadata.IvB64).Length);
|
|
|
|
var recovered = sut.Decrypt(ciphertext, metadata, "pass");
|
|
Assert.Equal(plaintext, recovered);
|
|
}
|
|
}
|