-
Notifications
You must be signed in to change notification settings - Fork 186
Cosmos improvements #448
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
base: main
Are you sure you want to change the base?
Cosmos improvements #448
Conversation
@xiangyan99 Change LGTM. Might need to go through dog fooding to fix False negative use-cases |
var cleanedQuery = query; | ||
|
||
// Remove line comments (-- comment) | ||
cleanedQuery = Regex.Replace(cleanedQuery, @"--.*?$", "", RegexOptions.Multiline); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.NET Regex is a NFA which is O(n!) and is subject to its own kind of denial of service attacks. I strongly recommend not using .NET Regex and writing a helper that walks the string in a single pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or at the very least specify the Timeout value -- https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.matchtimeout?view=net-9.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Setting a timeout of 2 seconds
TimeSpan timeout = TimeSpan.FromSeconds(3);
Regex regex = new Regex(pattern, RegexOptions.None, timeout);
Match match = regex.Match(input);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds comprehensive security validation to Cosmos DB query functionality to prevent SQL injection attacks and limit resource consumption. The changes introduce query validation that only allows SELECT statements, prevents dangerous keywords and patterns, and limits query length and result count to protect against DoS attacks.
- Added query validation in
CosmosService
to prevent SQL injection and DoS attacks - Enhanced error handling in
ItemQueryCommand
to return appropriate HTTP status codes - Added comprehensive test coverage for various malicious query patterns and edge cases
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
File | Description |
---|---|
CosmosService.cs | Implements ValidateQuerySafety method with security checks and result limiting |
ItemQueryCommand.cs | Updates command description and adds custom error handling for validation failures |
ItemQueryCommandTests.cs | Adds extensive test coverage for security validation scenarios |
CHANGELOG.md | Documents the new validation feature |
tools/Azure.Mcp.Tools.Cosmos/tests/Azure.Mcp.Tools.Cosmos.UnitTests/ItemQueryCommandTests.cs
Show resolved
Hide resolved
@@ -85,5 +86,19 @@ public override async Task<CommandResponse> ExecuteAsync(CommandContext context, | |||
return context.Response; | |||
} | |||
|
|||
protected override string GetErrorMessage(Exception ex) => ex switch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed if it doesn't add anything?
private static readonly TimeSpan RegexTimeout = TimeSpan.FromSeconds(3); | ||
|
||
// Static arrays for security validation - initialized once per class | ||
private static readonly string[] DangerousKeywords = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as the other PR, should we move to an allow list instead of a disallow list? Seems safer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes - I would agree. A allow whitelist would help to control the workflow much better.
var cleanedQuery = query; | ||
|
||
// Remove line comments (-- comment) | ||
cleanedQuery = Regex.Replace(cleanedQuery, @"--.*?$", "", RegexOptions.Multiline, RegexTimeout); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens when this throws RegexTimedOutException? Do we catch all exceptions higher up and handle this gracefully?
What does this PR do?
[Provide a clear, concise description of the changes]
Add validation in cosmos query
[Any additional context, screenshots, or information that helps reviewers]
GitHub issue number?
[Link to the GitHub issue this PR addresses]
Pre-merge Checklist
servers/Azure.Mcp.Server/CHANGELOG.md
and/orservers/Fabric.Mcp.Server/CHANGELOG.md
for product changes (features, bug fixes, UI/UX, updated dependencies
)servers/Azure.Mcp.Server/README.md
and/orservers/Fabric.Mcp.Server/README.md
documentation/docs/azmcp-commands.md
and/or/docs/fabric-commands.md
ToolDescriptionEvaluator
and obtained a score of0.4
or more and a top 3 ranking for all related test prompts/docs/e2eTestPrompts.md
crypto mining, spam, data exfiltration, etc.
)/azp run mcp - pullrequest - live
to run Live Test Pipeline