-
Notifications
You must be signed in to change notification settings - Fork 8
fix: OWASP AI Agent Security hardening for chat and MCP endpoints #1073
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
Show all changes
12 commits
Select commit
Hold shift + click to select a range
51881d0
fix: implement OWASP AI Agent security findings (#1064-#1071)
BenjaminMichaelis f4bcfab
fix: address multi-agent review findings (security hardening iteratio…
BenjaminMichaelis b1119cc
fix: cleanup from second agent review pass
BenjaminMichaelis a3dd028
fix: address PR #1073 review feedback (security hardening iteration 3)
BenjaminMichaelis 6400a67
fix: narrow token-limit exception type and clean up minor review find…
BenjaminMichaelis e5c3dad
fix: per-call MemoryCacheEntryOptions and add TokenLimitExceededExcep…
BenjaminMichaelis 068ff8f
test: DRY up McpApiTokenServiceTests and ResponseIdValidationServiceT…
BenjaminMichaelis f40cce1
fix: address PR #1073 second-round feedback (security hardening itera…
BenjaminMichaelis 9a50e7b
fix: resolve streaming responseId duplication and ownership gaps
BenjaminMichaelis f5ad578
fix: ResponseIdValidationService owns its IMemoryCache; remove shared…
BenjaminMichaelis ecea989
fix: force parameterless ResponseIdValidationService ctor via factory…
BenjaminMichaelis 518f50d
Merge remote-tracking branch 'origin/main' into agents/ai-agent-secur…
Copilot 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
130 changes: 130 additions & 0 deletions
130
EssentialCSharp.Web.Tests/ResponseIdValidationServiceTests.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,130 @@ | ||
| using EssentialCSharp.Web.Services; | ||
| using Microsoft.Extensions.Caching.Memory; | ||
|
|
||
| namespace EssentialCSharp.Web.Tests; | ||
|
|
||
| public class ResponseIdValidationServiceTests | ||
| { | ||
| // Match production SizeLimit so SetSize(1) is exercised in tests, not silently ignored. | ||
| private static MemoryCache CreateCache() => new(new MemoryCacheOptions { SizeLimit = 10_000 }); | ||
|
|
||
| private static ResponseIdValidationService CreateService(MemoryCache cache) => new(cache); | ||
|
|
||
| [Test] | ||
| [Arguments(null)] | ||
| [Arguments("")] | ||
| public async Task ValidateResponseId_BlankResponseId_AllowsNewConversation(string? responseId) | ||
| { | ||
| using var cache = CreateCache(); | ||
| var service = CreateService(cache); | ||
|
|
||
| bool result = service.ValidateResponseId("user1", responseId); | ||
|
|
||
| await Assert.That(result).IsTrue(); | ||
| } | ||
|
|
||
| [Test] | ||
| [Arguments(null)] | ||
| [Arguments("")] | ||
| public async Task ValidateResponseId_BlankUserId_Rejects(string? userId) | ||
| { | ||
| using var cache = CreateCache(); | ||
| var service = CreateService(cache); | ||
|
|
||
| bool result = service.ValidateResponseId(userId, "resp_123"); | ||
|
|
||
| await Assert.That(result).IsFalse(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task ValidateResponseId_CacheMiss_AllowsGracefulDegradation() | ||
| { | ||
| using var cache = CreateCache(); | ||
| var service = CreateService(cache); | ||
| // No RecordResponseId call — simulate server restart / different instance | ||
|
|
||
| bool result = service.ValidateResponseId("user1", "resp_unknown"); | ||
|
|
||
| await Assert.That(result).IsTrue(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task ValidateResponseId_RecordedByOwner_Validates() | ||
| { | ||
| using var cache = CreateCache(); | ||
| var service = CreateService(cache); | ||
| service.RecordResponseId("user1", "resp_abc"); | ||
|
|
||
| bool result = service.ValidateResponseId("user1", "resp_abc"); | ||
|
|
||
| await Assert.That(result).IsTrue(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task ValidateResponseId_RecordedByDifferentUser_Rejects() | ||
| { | ||
| using var cache = CreateCache(); | ||
| var service = CreateService(cache); | ||
| service.RecordResponseId("user1", "resp_abc"); | ||
|
|
||
| bool result = service.ValidateResponseId("user2", "resp_abc"); | ||
|
|
||
| await Assert.That(result).IsFalse(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task RecordResponseId_NullInputs_DoesNotThrow() | ||
| { | ||
| using var cache = CreateCache(); | ||
| var service = CreateService(cache); | ||
|
|
||
| service.RecordResponseId(null, "resp_abc"); | ||
| service.RecordResponseId("user1", null); | ||
| service.RecordResponseId(null, null); | ||
|
|
||
| // Verify the service is still functional after no-op calls | ||
| service.RecordResponseId("user1", "resp_abc"); | ||
| await Assert.That(service.ValidateResponseId("user1", "resp_abc")).IsTrue(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task ValidateResponseId_MultipleResponseIds_EachValidatedIndependently() | ||
| { | ||
| using var cache = CreateCache(); | ||
| var service = CreateService(cache); | ||
| service.RecordResponseId("user1", "resp_001"); | ||
| service.RecordResponseId("user1", "resp_002"); | ||
|
|
||
| await Assert.That(service.ValidateResponseId("user1", "resp_001")).IsTrue(); | ||
| await Assert.That(service.ValidateResponseId("user1", "resp_002")).IsTrue(); | ||
| // Unrecorded ID for same user → cache miss → allow | ||
| await Assert.That(service.ValidateResponseId("user1", "resp_003")).IsTrue(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task ValidateResponseId_TwoUsers_IsolatedFromEachOther() | ||
| { | ||
| using var cache = CreateCache(); | ||
| var service = CreateService(cache); | ||
| service.RecordResponseId("user1", "resp_A"); | ||
| service.RecordResponseId("user2", "resp_B"); | ||
|
|
||
| await Assert.That(service.ValidateResponseId("user1", "resp_A")).IsTrue(); | ||
| await Assert.That(service.ValidateResponseId("user2", "resp_B")).IsTrue(); | ||
| await Assert.That(service.ValidateResponseId("user2", "resp_A")).IsFalse(); | ||
| await Assert.That(service.ValidateResponseId("user1", "resp_B")).IsFalse(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task RecordResponseId_SizeLimitEnforced_EntryCountedInCache() | ||
| { | ||
| using var cache = CreateCache(); | ||
| var service = CreateService(cache); | ||
|
|
||
| // Record an entry — with SizeLimit set, SetSize(1) should count toward the cache size. | ||
| service.RecordResponseId("user1", "resp_size_test"); | ||
|
|
||
| // Verify it was recorded (i.e., not silently evicted due to misconfiguration). | ||
| await Assert.That(service.ValidateResponseId("user1", "resp_size_test")).IsTrue(); | ||
| } | ||
| } |
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.