Skip to content

Commit 8fb5cab

Browse files
committed
Add UnitTest for Sql Injection
1 parent 101dd69 commit 8fb5cab

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

MssqlMcp/MssqlMcp.Tests/UnitTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,28 @@ public async Task UpdateData_ReturnsError_WhenSqlIsInvalid()
187187
Assert.False(result.Success);
188188
Assert.Contains("syntax", result.Error ?? string.Empty, StringComparison.OrdinalIgnoreCase);
189189
}
190+
191+
[Fact]
192+
public async Task SqlInjection_NotExecuted_When_QueryFails()
193+
{
194+
// Ensure table exists
195+
var createResult = await _tools.CreateTable($"CREATE TABLE {_tableName} (Id INT PRIMARY KEY, Name NVARCHAR(100))") as DbOperationResult;
196+
Assert.NotNull(createResult);
197+
Assert.True(createResult.Success);
198+
199+
// Attempt SQL Injection
200+
var maliciousInput = "1; DROP TABLE " + _tableName + "; --";
201+
var sql = $"INSERT INTO {_tableName} (Id, Name) VALUES ({maliciousInput}, 'Malicious')";
202+
var result = await _tools.InsertData(sql) as DbOperationResult;
203+
204+
Assert.NotNull(result);
205+
Assert.False(result.Success);
206+
Assert.Contains("syntax", result.Error ?? string.Empty, StringComparison.OrdinalIgnoreCase);
207+
208+
// Verify table still exists
209+
var describeResult = await _tools.DescribeTable(_tableName) as DbOperationResult;
210+
Assert.NotNull(describeResult);
211+
Assert.True(describeResult.Success);
212+
}
190213
}
191214
}

0 commit comments

Comments
 (0)