Skip to content

Add UnitTest for Sql Injection #43

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions MssqlMcp/MssqlMcp.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,28 @@ public async Task UpdateData_ReturnsError_WhenSqlIsInvalid()
Assert.False(result.Success);
Assert.Contains("syntax", result.Error ?? string.Empty, StringComparison.OrdinalIgnoreCase);
}

[Fact]
public async Task SqlInjection_NotExecuted_When_QueryFails()
{
// Ensure table exists
var createResult = await _tools.CreateTable($"CREATE TABLE {_tableName} (Id INT PRIMARY KEY, Name NVARCHAR(100))") as DbOperationResult;
Assert.NotNull(createResult);
Assert.True(createResult.Success);

// Attempt SQL Injection
var maliciousInput = "1; DROP TABLE " + _tableName + "; --";
var sql = $"INSERT INTO {_tableName} (Id, Name) VALUES ({maliciousInput}, 'Malicious')";
var result = await _tools.InsertData(sql) as DbOperationResult;

Assert.NotNull(result);
Assert.False(result.Success);
Assert.Contains("syntax", result.Error ?? string.Empty, StringComparison.OrdinalIgnoreCase);

// Verify table still exists
var describeResult = await _tools.DescribeTable(_tableName) as DbOperationResult;
Assert.NotNull(describeResult);
Assert.True(describeResult.Success);
}
}
}