fix: persist DataProtection keys to filesystem (fixes #174)#179
Merged
Conversation
5408b31 to
f030172
Compare
Configure ASP.NET Core DataProtection to store keys in DATA_DIR/dataprotection-keys instead of using ephemeral in-memory storage. Keys now survive container restarts. Uses the existing DATA_DIR env var (Dockerfile/docker-compose) with a fallback to ./data/ for standalone dev.
…ne' may silently drop its earlier arguments' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
18 tests covering: service registration, key file persistence, protect/unprotect round-trips (various payloads, purpose isolation), key survival across provider re-creation (simulated container restarts), DATA_DIR configuration, fallback path, auto-directory creation, and application name isolation.
f030172 to
9151968
Compare
- Add changelog entry under [Unreleased] > Fixed - Add DataProtection config to CLAUDE.md Configuration section - Add test file to File Locations table and Testing section
…ne' may silently drop its earlier arguments' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
…ne' may silently drop its earlier arguments' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
…ne' may silently drop its earlier arguments' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
…ne' may silently drop its earlier arguments' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
…ne' may silently drop its earlier arguments' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
| [Fact] | ||
| public void KeyFiles_AreWrittenToConfiguredDirectory() | ||
| { | ||
| var keysDir = Path.Combine(this._tempDir, "dataprotection-keys"); |
| [Fact] | ||
| public void KeyFiles_AreValidXml() | ||
| { | ||
| var keysDir = Path.Combine(this._tempDir, "dataprotection-keys"); |
| [Fact] | ||
| public void DataDir_UsesConfigurationValue_WhenSet() | ||
| { | ||
| var customDir = Path.Combine(this._tempDir, "custom-data"); |
| { | ||
| var customDir = Path.Combine(this._tempDir, "custom-data"); | ||
| Directory.CreateDirectory(customDir); | ||
| var keysDir = Path.Combine(customDir, "dataprotection-keys"); |
| public void DataDir_CreatesKeySubdirectory_Automatically() | ||
| { | ||
| // DATA_DIR exists but dataprotection-keys subdirectory does not yet | ||
| var freshDir = Path.Combine(this._tempDir, "fresh"); |
| // DATA_DIR exists but dataprotection-keys subdirectory does not yet | ||
| var freshDir = Path.Combine(this._tempDir, "fresh"); | ||
| Directory.CreateDirectory(freshDir); | ||
| var keysDir = Path.Combine(freshDir, "dataprotection-keys"); |
| [Fact] | ||
| public void ApplicationName_IsolatesKeyRings() | ||
| { | ||
| var dirA = Path.Combine(this._tempDir, "app-a"); |
| public void ApplicationName_IsolatesKeyRings() | ||
| { | ||
| var dirA = Path.Combine(this._tempDir, "app-a"); | ||
| var dirB = Path.Combine(this._tempDir, "app-b"); |
| Directory.CreateDirectory(dirB); | ||
|
|
||
| // Use the same key directory but different application names | ||
| var sharedKeysDir = Path.Combine(this._tempDir, "shared-keys"); |
| { | ||
| var services = new ServiceCollection(); | ||
| services.AddDataProtection() | ||
| .PersistKeysToFileSystem(new DirectoryInfo(Path.Combine(dataDir, "dataprotection-keys"))) |
Remove unused EnsureRelativePath helper and restore consistent Path.Combine usage in test file (hardcoded literals, no path traversal risk). Keep Path.Join in production code where DATA_DIR comes from user input.
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.
Summary
DATA_DIR/dataprotection-keysinstead of ephemeral in-memory storageDATA_DIRenv var (/app/datain Docker) with a./data/fallback for standalone devDetails
AddDataProtection()+PersistKeysToFileSystem()are included in the ASP.NET Core shared framework/app/dataalready exists and is volume-mountedSetApplicationNameensures key ring isolation across environmentsJwt:Secretvia HMAC-SHA256, independent of DataProtection1a09108)Test plan
dotnet build— 0 errorsdotnet test— 983/983 passing (18 new DataProtection tests)dotnet format --verify-no-changes— no new issuesnpx prettier --check— all files passnpx jest --ci— 589/589 passing./data/dataprotection-keys/key-*.xmlfiles appear after startupdocker compose logs | grep "No XML encryptor"docker compose restartCloses #174