Repository navigation
fix(infra): AccountId must be a non-nullable Guid or both tenant write layers refuse (#673) #695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
40 changes: 40 additions & 0 deletions
40
src/Cluckwork.Infrastructure/Persistence/TenantAccountIdShapeException.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| namespace Cluckwork.Infrastructure.Persistence; | ||
|
|
||
| // #673 — both write-side tenant layers key on AccountId being a non-nullable | ||
| // Guid, and both used to fall THROUGH for any other shape: the model walk in | ||
| // AppDbContext.OnModelCreating gave a Guid? or a converted AccountId no | ||
| // concurrency token, and TenantStampInterceptor skipped any value that did not | ||
| // box to a Guid. A tenant-owned entity mapped with `Guid? AccountId` therefore | ||
| // reopened the #562 hole — a detached Update/Remove naming another farm's row | ||
| // wrote through, with every test green. | ||
| // | ||
| // Both layers now throw this instead of continuing: at model build for the | ||
| // wrong CLR type (so a mis-typed AccountId fails every boot and every test), | ||
| // and at SaveChanges for a value that is not a Guid. Distinct from | ||
| // TenantWriteMismatchException, which means a real cross-tenant VALUE; this one | ||
| // means the mapping itself cannot be checked. Caught nowhere, deliberately. | ||
| public sealed class TenantAccountIdShapeException : InvalidOperationException | ||
| { | ||
| public string EntityType { get; } | ||
|
|
||
| private TenantAccountIdShapeException(string message, string entityType) | ||
| : base(message) => EntityType = entityType; | ||
|
|
||
| public static TenantAccountIdShapeException ForModel(string entityType, Type clrType) => | ||
| new($"{entityType}.AccountId is mapped as {clrType.Name}, but tenant isolation requires a " + | ||
| "non-nullable Guid: the concurrency-token walk and the write guard can only check that " + | ||
| "shape, so any other one is a silent cross-tenant write (#673).", | ||
| entityType); | ||
|
|
||
| public static TenantAccountIdShapeException ForMapping(string entityType, string state, Type clrType) => | ||
| new($"Refusing to save {state} {entityType}: its AccountId is mapped as {clrType.Name} rather than " + | ||
| "a non-nullable Guid, so the value this guard sees can be right while the row carries no " + | ||
| "concurrency token to compare it against (#673).", | ||
| entityType); | ||
|
|
||
| public static TenantAccountIdShapeException ForWrite(string entityType, string state, object? value) => | ||
| new($"Refusing to save {state} {entityType}: its AccountId is " + | ||
| $"{(value is null ? "null" : $"a {value.GetType().Name}")} rather than a Guid, so the write " + | ||
| "guard cannot check it against the resolved tenant (#673).", | ||
| entityType); | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.