fix: fail closed when the key ring would be stored unencrypted#63
Merged
Conversation
The DataProtection key ring is persisted in the same Postgres as the data it protects, but at-rest encryption was optional — so a non-Development deployment without a configured certificate stored the keys in cleartext, and a DB dump could decrypt the SMTP password, login tokens, and auth cookies. UseAppFoundation now refuses to start outside Development when the resolved KeyManagementOptions.XmlEncryptor is null (i.e. no cert / Key Vault / KMS configured). Checking the resolved encryptor reflects the actual end state regardless of how protection was wired. A host that accepts an unencrypted key ring (e.g. storage encrypted by other means) opts in via AppFoundationOptions.AllowUnprotectedKeyRing. Closes #54
Add a Production configuration section covering the settings a non-Development host must provide now that the foundation fails fast on unsafe defaults: data-protection key encryption (cert / Key Vault / AllowUnprotectedKeyRing), reverse-proxy trust (KnownProxyNetworks via env, with an nginx + Docker example), and the schema-creation mode. Expand the AppFoundationOptions table with the newer options.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #54 (F5 — DataProtection key-ring encryption optional and co-located with protected data).
What
UseAppFoundationnow refuses to start outside Development when the DataProtection key ring would be persisted without at-rest encryption. The check inspects the resolvedKeyManagementOptions.XmlEncryptor: it is non-null whenever encryption is configured — certificate (DataProtection:CertificatePath), Azure Key Vault, KMS, etc. — so it reflects the actual end state regardless of how protection was wired, not just whether the foundation's own cert path was set.InvalidOperationExceptionwith an actionable message.AppFoundationOptions.AllowUnprotectedKeyRingopt-out for hosts whose database storage is encrypted at rest by other means.Logic extracted to an internal
EnsureKeyRingProtected(isDevelopment, allowUnprotectedKeyRing, xmlEncryptor)helper and unit-tested.Why
The key ring lives in the same Postgres as the data it protects. With encryption optional, a production deployment that forgot the certificate stored the keys in cleartext — so a single DB dump/backup would decrypt the SMTP password, the
/loginhandoff tokens, and any auth cookies issued with this key ring.A host running in a non-Development environment must now configure key encryption or it will fail to start:
DataProtection:CertificatePath(+DataProtection:CertificatePassword), orAppFoundationOptions.ConfigureDataProtection, orAppFoundationOptions.AllowUnprotectedKeyRing = trueto keep the old behavior.The exception message states these options.
Tests
EnsureKeyRingProtectedTests: throws when non-Dev + no encryptor + not allowed; does not throw when an encryptor is present, when explicitly allowed, or in Development. ExistingAddAppFoundationDataProtectionTestsremain green (they exercise registration, notUseAppFoundation).Verification
dotnet build -c Release— 0 errors.dotnet test -c Release— 70/70 passing (4 new).main.