-
Notifications
You must be signed in to change notification settings - Fork 0
Sanitize MCP capture and board resource failures #2443
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
4 commits
Select commit
Hold shift + click to select a range
1facce8
Redact MCP resource failures
Chris0Jeky a4a5448
Merge remote-tracking branch 'origin/main' into issue-2351/mcp-captur…
Chris0Jeky 2106c96
Merge remote-tracking branch 'origin/main' into issue-2351/mcp-captur…
Chris0Jeky 8744a06
Merge branch 'main' into issue-2351/mcp-capture-board-error-safety
Chris0Jeky 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
132 changes: 132 additions & 0 deletions
132
backend/tests/Taskdeck.Api.Tests/BoardResourcesErrorSafetyTests.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,132 @@ | ||
| using FluentAssertions; | ||
| using Moq; | ||
| using Taskdeck.Api.Mcp; | ||
| using Taskdeck.Application.Interfaces; | ||
| using Taskdeck.Application.Services; | ||
| using Taskdeck.Domain.Common; | ||
| using Taskdeck.Domain.Enums; | ||
| using Taskdeck.Domain.Exceptions; | ||
| using Xunit; | ||
|
|
||
| namespace Taskdeck.Api.Tests; | ||
|
|
||
| public class BoardResourcesErrorSafetyTests | ||
| { | ||
| private const string HostileError = | ||
| "Bearer tdsk_test_secret C:\\Users\\alice\\taskdeck.db " + | ||
| "SQLite UNIQUE constraint failed: Boards.Name https://provider.example/v1/internal"; | ||
|
|
||
| [Fact] | ||
| public async Task ListBoards_UnexpectedAuthorizationFailure_UsesGenericMessage() | ||
| { | ||
| var userId = Guid.NewGuid(); | ||
| var boardId = Guid.NewGuid(); | ||
| var (resources, unitOfWork, authorization) = CreateResources(userId); | ||
| var boardRepository = new Mock<IBoardRepository>(MockBehavior.Strict); | ||
| unitOfWork | ||
| .SetupGet(value => value.Boards) | ||
| .Returns(boardRepository.Object); | ||
| boardRepository | ||
| .Setup(repository => repository.SearchIdsAsync(null, false, It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(new[] { boardId }); | ||
| authorization | ||
| .Setup(service => service.GetReadableBoardIdsAsync( | ||
| userId, | ||
| It.IsAny<IEnumerable<Guid>>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(Result.Failure<IReadOnlySet<Guid>>(ErrorCodes.UnexpectedError, HostileError)); | ||
|
|
||
| var action = () => resources.ListBoards(); | ||
|
|
||
| var exception = (await action.Should().ThrowAsync<InvalidOperationException>()).Which; | ||
| exception.Message.Should().Be( | ||
| $"MCP: failed to list boards: {SensitiveDataRedactor.GenericUnexpectedFailureMessage}"); | ||
| exception.Message.Should().NotContain(HostileError); | ||
| authorization.VerifyAll(); | ||
| boardRepository.VerifyAll(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("detail")] | ||
| [InlineData("column")] | ||
| [InlineData("card")] | ||
| [InlineData("labels")] | ||
| public async Task BoardAccess_UnexpectedFailure_UsesGenericMessage(string resource) | ||
| { | ||
| var userId = Guid.NewGuid(); | ||
| var boardId = Guid.NewGuid(); | ||
| var (resources, _, authorization) = CreateResources(userId); | ||
| authorization | ||
| .Setup(service => service.CanReadBoardAsync(userId, boardId)) | ||
| .ReturnsAsync(Result.Failure<bool>(ErrorCodes.UnexpectedError, HostileError)); | ||
|
|
||
| Func<Task<string>> action = resource switch | ||
| { | ||
| "detail" => () => resources.GetBoardDetail(boardId.ToString()), | ||
| "column" => () => resources.GetColumnCards(boardId.ToString(), Guid.NewGuid().ToString()), | ||
| "card" => () => resources.GetCardDetail(boardId.ToString(), Guid.NewGuid().ToString()), | ||
| "labels" => () => resources.GetBoardLabels(boardId.ToString()), | ||
| _ => throw new ArgumentOutOfRangeException(nameof(resource), resource, null) | ||
| }; | ||
|
|
||
| var exception = (await action.Should().ThrowAsync<InvalidOperationException>()).Which; | ||
| var prefix = resource switch | ||
| { | ||
| "detail" => "MCP: failed to get board detail: ", | ||
| "column" => "MCP: failed to access board: ", | ||
| "card" => "MCP: failed to access board: ", | ||
| "labels" => "MCP: failed to access board: ", | ||
| _ => throw new ArgumentOutOfRangeException(nameof(resource), resource, null) | ||
| }; | ||
| exception.Message.Should().Be(prefix + SensitiveDataRedactor.GenericUnexpectedFailureMessage); | ||
| authorization.VerifyAll(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetBoardDetail_KnownDomainFailure_PreservesStableMessage() | ||
| { | ||
| const string stableMessage = "You do not have access to this board."; | ||
| var userId = Guid.NewGuid(); | ||
| var boardId = Guid.NewGuid(); | ||
| var (resources, _, authorization) = CreateResources(userId); | ||
| authorization | ||
| .Setup(service => service.CanReadBoardAsync(userId, boardId)) | ||
| .ReturnsAsync(Result.Failure<bool>(ErrorCodes.Forbidden, stableMessage)); | ||
|
|
||
| var action = () => resources.GetBoardDetail(boardId.ToString()); | ||
|
|
||
| var exception = (await action.Should().ThrowAsync<InvalidOperationException>()).Which; | ||
| exception.Message.Should().Be($"MCP: failed to get board detail: {stableMessage}"); | ||
| authorization.VerifyAll(); | ||
| } | ||
|
|
||
| private static ( | ||
| BoardResources Resources, | ||
| Mock<IUnitOfWork> UnitOfWork, | ||
| Mock<IAuthorizationService> Authorization) CreateResources(Guid userId) | ||
| { | ||
| var unitOfWork = new Mock<IUnitOfWork>(MockBehavior.Strict); | ||
| var authorization = new Mock<IAuthorizationService>(MockBehavior.Strict); | ||
| var boardService = new BoardService(unitOfWork.Object, authorization.Object); | ||
| var resources = new BoardResources( | ||
| boardService, | ||
| new ColumnService(unitOfWork.Object), | ||
| new CardService(unitOfWork.Object), | ||
| new LabelService(unitOfWork.Object), | ||
| new FixedUserContextProvider(userId)); | ||
|
|
||
| return (resources, unitOfWork, authorization); | ||
| } | ||
|
|
||
| private sealed class FixedUserContextProvider(Guid userId) : IUserContextProvider | ||
| { | ||
| public Task<McpUserContext> GetCurrentContextAsync(CancellationToken cancellationToken = default) => | ||
| Task.FromResult(new McpUserContext(userId, ApiKeyScope.Full)); | ||
|
|
||
| public Task<Guid> GetCurrentUserIdAsync(CancellationToken cancellationToken = default) => | ||
| Task.FromResult(userId); | ||
|
|
||
| public Task<Guid?> GetUserIdAsync(CancellationToken cancellationToken = default) => | ||
| Task.FromResult<Guid?>(userId); | ||
| } | ||
| } |
98 changes: 98 additions & 0 deletions
98
backend/tests/Taskdeck.Api.Tests/CaptureResourcesErrorSafetyTests.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,98 @@ | ||
| using FluentAssertions; | ||
| using Moq; | ||
| using Taskdeck.Api.Mcp; | ||
| using Taskdeck.Application.DTOs; | ||
| using Taskdeck.Application.Interfaces; | ||
| using Taskdeck.Application.Services; | ||
| using Taskdeck.Domain.Common; | ||
| using Taskdeck.Domain.Enums; | ||
| using Taskdeck.Domain.Exceptions; | ||
| using Xunit; | ||
|
|
||
| namespace Taskdeck.Api.Tests; | ||
|
|
||
| public class CaptureResourcesErrorSafetyTests | ||
| { | ||
| private const string HostileError = | ||
| "Bearer tdsk_test_secret C:\\Users\\alice\\taskdeck.db " + | ||
| "SQLite UNIQUE constraint failed: Users.Email https://provider.example/v1/internal"; | ||
|
|
||
| [Fact] | ||
| public async Task ListCaptures_UnexpectedFailure_UsesGenericMessage() | ||
| { | ||
| var userId = Guid.NewGuid(); | ||
| var captureService = new Mock<ICaptureService>(MockBehavior.Strict); | ||
| captureService | ||
| .Setup(service => service.ListAsync( | ||
| userId, | ||
| It.IsAny<CaptureListFilterDto>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(Result.Failure<IReadOnlyList<CaptureItemSummaryDto>>( | ||
| ErrorCodes.UnexpectedError, | ||
| HostileError)); | ||
|
|
||
| var action = () => CreateResources(captureService.Object, userId).ListCaptures(); | ||
|
|
||
| var exception = (await action.Should().ThrowAsync<InvalidOperationException>()).Which; | ||
| exception.Message.Should().Be( | ||
| $"MCP: failed to list captures: {SensitiveDataRedactor.GenericUnexpectedFailureMessage}"); | ||
| exception.Message.Should().NotContain(HostileError); | ||
| captureService.VerifyAll(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetCaptureDetail_UnexpectedFailure_UsesGenericMessage() | ||
| { | ||
| var userId = Guid.NewGuid(); | ||
| var captureId = Guid.NewGuid(); | ||
| var captureService = new Mock<ICaptureService>(MockBehavior.Strict); | ||
| captureService | ||
| .Setup(service => service.GetByIdAsync(userId, captureId, It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(Result.Failure<CaptureItemDto>(ErrorCodes.UnexpectedError, HostileError)); | ||
|
|
||
| var action = () => CreateResources(captureService.Object, userId) | ||
| .GetCaptureDetail(captureId.ToString()); | ||
|
|
||
| var exception = (await action.Should().ThrowAsync<InvalidOperationException>()).Which; | ||
| exception.Message.Should().Be( | ||
| $"MCP: failed to get capture: {SensitiveDataRedactor.GenericUnexpectedFailureMessage}"); | ||
| exception.Message.Should().NotContain(HostileError); | ||
| captureService.VerifyAll(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetCaptureDetail_KnownDomainFailure_PreservesStableMessage() | ||
| { | ||
| const string stableMessage = "Capture item not found."; | ||
| var userId = Guid.NewGuid(); | ||
| var captureId = Guid.NewGuid(); | ||
| var captureService = new Mock<ICaptureService>(MockBehavior.Strict); | ||
| captureService | ||
| .Setup(service => service.GetByIdAsync(userId, captureId, It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(Result.Failure<CaptureItemDto>(ErrorCodes.NotFound, stableMessage)); | ||
|
|
||
| var action = () => CreateResources(captureService.Object, userId) | ||
| .GetCaptureDetail(captureId.ToString()); | ||
|
|
||
| var exception = (await action.Should().ThrowAsync<InvalidOperationException>()).Which; | ||
| exception.Message.Should().Be($"MCP: failed to get capture: {stableMessage}"); | ||
| captureService.VerifyAll(); | ||
| } | ||
|
|
||
| private static CaptureResources CreateResources( | ||
| ICaptureService captureService, | ||
| Guid userId) => | ||
| new(captureService, new FixedUserContextProvider(userId)); | ||
|
|
||
| private sealed class FixedUserContextProvider(Guid userId) : IUserContextProvider | ||
| { | ||
| public Task<McpUserContext> GetCurrentContextAsync(CancellationToken cancellationToken = default) => | ||
| Task.FromResult(new McpUserContext(userId, ApiKeyScope.Full)); | ||
|
|
||
| public Task<Guid> GetCurrentUserIdAsync(CancellationToken cancellationToken = default) => | ||
| Task.FromResult(userId); | ||
|
|
||
| public Task<Guid?> GetUserIdAsync(CancellationToken cancellationToken = default) => | ||
| Task.FromResult<Guid?>(userId); | ||
| } | ||
| } |
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.
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.