35d14936ac
The named-query guard's only statement boundary was ';', but T-SQL runs ';'-less batches: 'SELECT 1 COMMIT KILL 55', '... COMMIT DISABLE TRIGGER ALL ON DATABASE', '... COMMIT SHUTDOWN WITH NOWAIT' and a TEXTPTR/WRITETEXT write were all accepted for SqlServer AND Odbc — a COMMIT ending the rolled-back transaction, then anything not on the keyword list. For SqlServer, and for Odbc / no provider (backend unknown: strictest reading), SqlReadOnlyStatement now also parses the statement with Microsoft.SqlServer.TransactSql.ScriptDom 180.154.0 (central package pin; both QUOTED_IDENTIFIER settings) and requires exactly one batch holding exactly one SelectStatement, no INTO, and no OPENQUERY/OPENROWSET/OPENDATASOURCE. Odbc '?' markers are read as variables for the parse. The lexical guard stays as a second layer. Tests: SqlReadOnlyStatementTransactSqlTests (11 batch strings x SqlServer/Odbc/ none, pass-through rowsets — all failed first; CTE/subquery/window/param/UNION/ FOR XML/OPTION and ODBC '?' positive controls). SqlNamedQueryTests' SQLite-backed Odbc stand-in used the T-SQL reserved word 'top' as an alias (renamed). Live (SQL Server 10.100.0.35,14330): SqlServerNamedQueryBatchLiveTests proves 'SELECT 1 AS v COMMIT UPDATE ...' commits the old way, the guard refuses it, and a CTE+window+subquery+@param query reads Good; SqlServerReadTests / write-mode / trigger suites unchanged (30/30). Class comment and docs/drivers/Sql.md corrected. Co-Authored-By: Claude <noreply@anthropic.com>
82 lines
4.5 KiB
C#
82 lines
4.5 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Sql.Contracts;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Sql.Tests;
|
|
|
|
/// <summary>
|
|
/// lmxopcua#974 (security review 2026-12-02): T-SQL does not need <c>;</c> between statements. The lexical guard's
|
|
/// only statement boundary was <c>;</c>, so <c>SELECT 1 COMMIT KILL 55</c> was "one SELECT" to it and three
|
|
/// statements to SQL Server — the <c>COMMIT</c> ending the driver's always-rolled-back transaction, then anything.
|
|
/// For SQL Server (and for ODBC / no provider, whose backend is unknown and so must pass the strictest reading) the
|
|
/// statement is now parsed with <c>Microsoft.SqlServer.TransactSql.ScriptDom</c> and must be exactly one batch
|
|
/// holding exactly one <c>SELECT</c> with no <c>INTO</c> and no pass-through rowset.
|
|
/// </summary>
|
|
public sealed class SqlReadOnlyStatementTransactSqlTests
|
|
{
|
|
/// <summary>Accepted by the lexical guard for SqlServer AND Odbc before the fix.</summary>
|
|
public static TheoryData<string> SemicolonLessBatches => new()
|
|
{
|
|
"SELECT 1 COMMIT DISABLE TRIGGER ALL ON DATABASE",
|
|
"SELECT 1 COMMIT KILL 55",
|
|
"SELECT 1 COMMIT SHUTDOWN WITH NOWAIT",
|
|
"SELECT 1 COMMIT DECLARE @p varbinary(16) SELECT @p = TEXTPTR(Col) FROM dbo.T WRITETEXT dbo.T.Col @p 'pwned'",
|
|
"SELECT 1 COMMIT BEGIN TRAN SELECT 2",
|
|
"SELECT 1 WAITFOR DELAY '00:10:00'",
|
|
"SELECT 1 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED",
|
|
"SELECT a FROM t\nGO\nSELECT 2",
|
|
"WITH c AS (SELECT 1 AS a) SELECT a FROM c COMMIT RECONFIGURE",
|
|
"SELECT 1 COMMIT DBCC SHRINKDATABASE (0)",
|
|
"SELECT 1 COMMIT BACKUP LOG x TO DISK = 'nul'",
|
|
};
|
|
|
|
[Theory]
|
|
[MemberData(nameof(SemicolonLessBatches))]
|
|
public void A_semicolon_less_batch_is_refused_for_SqlServer(string sql)
|
|
=> SqlReadOnlyStatement.Check(sql, SqlProvider.SqlServer, out _).ShouldNotBeNull();
|
|
|
|
[Theory]
|
|
[MemberData(nameof(SemicolonLessBatches))]
|
|
public void A_semicolon_less_batch_is_refused_for_Odbc_and_without_a_provider(string sql)
|
|
{
|
|
SqlReadOnlyStatement.Check(sql, SqlProvider.Odbc, out _).ShouldNotBeNull();
|
|
SqlReadOnlyStatement.Check(sql, out _).ShouldNotBeNull();
|
|
}
|
|
|
|
/// <summary>One statement, but not a plain read: pass-through rowsets run text on another server.</summary>
|
|
[Theory]
|
|
[InlineData("SELECT * FROM OPENQUERY(lnk, 'SELECT 1')")]
|
|
[InlineData("SELECT * FROM OPENROWSET('SQLNCLI', 'Server=x;Trusted_Connection=yes;', 'SELECT 1')")]
|
|
[InlineData("SELECT * FROM OPENDATASOURCE('SQLNCLI', 'Data Source=x;Integrated Security=SSPI').db.dbo.t")]
|
|
[InlineData("SELECT * FROM OPENROWSET(BULK 'c:\\x.txt', SINGLE_CLOB) AS b")]
|
|
public void A_pass_through_rowset_is_refused(string sql)
|
|
=> SqlReadOnlyStatement.Check(sql, SqlProvider.SqlServer, out _).ShouldNotBeNull();
|
|
|
|
[Fact]
|
|
public void The_refusal_names_the_statement_count()
|
|
=> SqlReadOnlyStatement.Check("SELECT 1 COMMIT KILL 55", SqlProvider.SqlServer, out _)
|
|
.ShouldNotBeNull().ShouldContain("statement");
|
|
|
|
/// <summary>Positive controls: ordinary T-SQL reads — CTEs, subqueries, window functions, parameters — still pass.</summary>
|
|
[Theory]
|
|
[InlineData("SELECT a FROM t WHERE x = @x")]
|
|
[InlineData("SELECT a FROM t WHERE x = @x;")]
|
|
[InlineData("WITH latest AS (SELECT tag, val, ROW_NUMBER() OVER (PARTITION BY tag ORDER BY ts DESC) AS rn FROM dbo.hist WHERE ts > @since) SELECT tag, val FROM latest WHERE rn = 1")]
|
|
[InlineData("SELECT TOP (@n) a, (SELECT MAX(b) FROM u WHERE u.k = t.k) AS m, SUM(c) OVER () AS total FROM t WHERE k IN (SELECT k FROM v WHERE w = @w) ORDER BY a")]
|
|
[InlineData("SELECT [it's], [a]]b], [update], \"quoted\" FROM t -- trailing comment")]
|
|
[InlineData("SELECT a FROM #staging /* note */ WHERE b LIKE 'x%' OPTION (RECOMPILE)")]
|
|
[InlineData("SELECT CAST(a AS varchar(10)) + N'x' AS s, CASE WHEN b > 0 THEN 1 ELSE 0 END AS f FROM t FOR XML PATH('r')")]
|
|
[InlineData("SELECT a FROM t UNION ALL SELECT b FROM u EXCEPT SELECT c FROM v")]
|
|
public void An_ordinary_T_SQL_read_passes(string sql)
|
|
=> SqlReadOnlyStatement.Check(sql, SqlProvider.SqlServer, out _).ShouldBeNull();
|
|
|
|
[Theory]
|
|
[InlineData("SELECT a FROM t WHERE x = ? AND y = ?")]
|
|
[InlineData("SELECT a, '?' AS q FROM t WHERE x = ? ORDER BY a")]
|
|
public void An_ODBC_read_with_positional_markers_passes(string sql)
|
|
{
|
|
SqlReadOnlyStatement.Check(sql, SqlProvider.Odbc, out var normalised).ShouldBeNull();
|
|
normalised.ShouldBe(sql);
|
|
}
|
|
}
|