Merge security review fixes: sandbox refuses async/stackalloc/MethodImpl, unwinding-overflow guard, deep-data types (#971); T-SQL ScriptDom single-SELECT guard (#974); Auth 0.5.0 directory re-query on revalidation (#975); fleet-diagnostics target check (#973)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Text;
|
||||
using Microsoft.SqlServer.TransactSql.ScriptDom;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Driver.Sql.Contracts;
|
||||
|
||||
@@ -8,10 +9,20 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.Sql.Contracts;
|
||||
/// and single-statement, where the provider can enforce that (see <c>ISqlDialect.BeginNamedQueryAsync</c>) —
|
||||
/// and the connecting principal should hold <c>SELECT</c> only. The guard exists so an operator learns at
|
||||
/// construction — not from a database audit — that a statement is not a plain read.
|
||||
/// <para>Rules, applied to the text <em>outside</em> string literals, quoted identifiers and comments:
|
||||
/// the statement begins with <c>SELECT</c> or <c>WITH</c>; it is a single statement (one trailing
|
||||
/// <c>;</c> is tolerated and removed); and none of the data- or schema-changing keywords appears anywhere
|
||||
/// — which also refuses <c>SELECT … INTO</c>, <c>FOR UPDATE</c> and a data-modifying CTE.</para>
|
||||
/// <para>Lexical rules, applied to the text <em>outside</em> string literals, quoted identifiers and comments:
|
||||
/// the statement begins with <c>SELECT</c> or <c>WITH</c>; it carries no second <c>;</c>-separated statement
|
||||
/// (one trailing <c>;</c> is tolerated and removed); and none of the data- or schema-changing keywords
|
||||
/// appears anywhere — which also refuses <c>SELECT … INTO</c>, <c>FOR UPDATE</c> and a data-modifying CTE.</para>
|
||||
/// <para><b><c>;</c> is NOT a statement boundary in T-SQL (lmxopcua#974, review 2026-12-02).</b> SQL Server
|
||||
/// runs <c>SELECT 1 COMMIT KILL 55</c> as three statements — no separator needed — so the lexical rules alone
|
||||
/// accepted, for SqlServer and Odbc, a <c>COMMIT</c> that ends the driver's rolled-back transaction followed by
|
||||
/// any non-listed statement (<c>DISABLE TRIGGER</c>, <c>KILL</c>, <c>SHUTDOWN</c>, <c>WRITETEXT</c> …). For
|
||||
/// SqlServer, and for Odbc and no provider (the backend may be SQL Server, so the strictest reading applies),
|
||||
/// the statement is also <b>parsed</b> 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 <c>OPENQUERY</c> /
|
||||
/// <c>OPENROWSET</c> / <c>OPENDATASOURCE</c>. An Odbc statement's positional <c>?</c> markers are read as
|
||||
/// variables for that parse; anything else SQL Server's grammar does not accept (PostgreSQL <c>::</c> casts,
|
||||
/// <c>LIMIT</c>) is refused for Odbc. The lexical rules stay as a second, independent layer.</para>
|
||||
/// <para><b>The lexer is dialect-aware, and refuses what it cannot read exactly as the server does
|
||||
/// (lmxopcua#974, review finding S-7).</b> Everything above depends on knowing where a literal or comment ends.
|
||||
/// One lexer for five dialects got that wrong in the direction that matters — it took live SQL for literal
|
||||
@@ -145,10 +156,102 @@ public static class SqlReadOnlyStatement
|
||||
"must read the same under every one";
|
||||
}
|
||||
|
||||
// T-SQL needs no ';' between statements (review 2026-12-02): the lexical rules above see one SELECT in
|
||||
// `SELECT 1 COMMIT KILL 55`, SQL Server runs three. SQL Server — and ODBC / no provider, whose backend may be
|
||||
// SQL Server, so the strictest reading applies — must also PARSE as exactly one SELECT.
|
||||
if (provider is null or SqlProvider.SqlServer or SqlProvider.Odbc
|
||||
&& TransactSqlProblem(agreed!, positional: provider != SqlProvider.SqlServer) is { } tsqlProblem)
|
||||
{
|
||||
return provider == SqlProvider.SqlServer
|
||||
? tsqlProblem
|
||||
: $"{tsqlProblem} ({DescribeUnknownBackend(provider)} must also be a single SELECT as SQL Server parses it)";
|
||||
}
|
||||
|
||||
normalised = agreed!;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses <paramref name="statement"/> with the SQL Server parser (under both <c>QUOTED_IDENTIFIER</c>
|
||||
/// settings) and returns the problem unless it is exactly one batch holding exactly one <c>SELECT</c> with no
|
||||
/// <c>INTO</c> and no pass-through rowset (<c>OPENQUERY</c> / <c>OPENROWSET</c> / <c>OPENDATASOURCE</c>, which
|
||||
/// run text on another server or read a file).
|
||||
/// </summary>
|
||||
/// <param name="statement">The lexically-accepted, normalised statement.</param>
|
||||
/// <param name="positional">The statement may carry positional <c>?</c> markers (ODBC), which T-SQL spells
|
||||
/// <c>@name</c>: each one outside a literal / comment is read as a variable for the parse.</param>
|
||||
private static string? TransactSqlProblem(string statement, bool positional)
|
||||
{
|
||||
var text = positional ? PositionalMarkersAsVariables(statement) : statement;
|
||||
foreach (var quotedIdentifiers in (ReadOnlySpan<bool>)[true, false])
|
||||
{
|
||||
var parser = new TSql170Parser(quotedIdentifiers);
|
||||
TSqlFragment fragment;
|
||||
IList<ParseError> errors;
|
||||
using (var reader = new StringReader(text)) fragment = parser.Parse(reader, out errors);
|
||||
|
||||
if (errors.Count > 0)
|
||||
{
|
||||
var first = errors[0];
|
||||
return $"the statement does not parse as T-SQL ({first.Message} at line {first.Line}, column {first.Column})";
|
||||
}
|
||||
|
||||
if (fragment is not TSqlScript script || script.Batches.Count != 1 || script.Batches[0].Statements.Count != 1)
|
||||
{
|
||||
var count = (fragment as TSqlScript)?.Batches.Sum(b => b.Statements.Count) ?? 0;
|
||||
return $"the statement carries more than one statement ({count} as SQL Server parses it — T-SQL runs " +
|
||||
"statements one after another without ';' between them, and GO separates batches); a named " +
|
||||
"query must be one SELECT";
|
||||
}
|
||||
|
||||
if (script.Batches[0].Statements[0] is not SelectStatement select)
|
||||
return "a named query must be a single SELECT (or WITH … SELECT) statement";
|
||||
if (select.Into is not null)
|
||||
return "a named query must be read-only, and SELECT … INTO creates a table";
|
||||
|
||||
var passThrough = new PassThroughRowsetFinder();
|
||||
select.Accept(passThrough);
|
||||
if (passThrough.Found is { } rowset)
|
||||
return $"a named query may not read through {rowset}: it runs text on another server (or reads a " +
|
||||
"file), outside the driver's read-only transaction";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>Replaces every positional <c>?</c> outside literals and comments (SQL Server lexicon) with <c>@pN</c>.</summary>
|
||||
private static string PositionalMarkersAsVariables(string statement)
|
||||
{
|
||||
if (Mask(statement, SqlServer, out var code) is not null) return statement;
|
||||
var builder = new StringBuilder(statement.Length + 8);
|
||||
var n = 0;
|
||||
for (var i = 0; i < statement.Length; i++)
|
||||
{
|
||||
if (code[i] == '?') builder.Append("@p").Append(n++.ToString(System.Globalization.CultureInfo.InvariantCulture)).Append(' ');
|
||||
else builder.Append(statement[i]);
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>Finds a rowset source that executes on another server or reads a file.</summary>
|
||||
private sealed class PassThroughRowsetFinder : TSqlFragmentVisitor
|
||||
{
|
||||
public string? Found { get; private set; }
|
||||
|
||||
public override void ExplicitVisit(OpenQueryTableReference node) { Found ??= "OPENQUERY"; base.ExplicitVisit(node); }
|
||||
|
||||
public override void ExplicitVisit(OpenRowsetTableReference node) { Found ??= "OPENROWSET"; base.ExplicitVisit(node); }
|
||||
|
||||
public override void ExplicitVisit(BulkOpenRowset node) { Found ??= "OPENROWSET(BULK …)"; base.ExplicitVisit(node); }
|
||||
|
||||
public override void ExplicitVisit(InternalOpenRowset node) { Found ??= "OPENROWSET"; base.ExplicitVisit(node); }
|
||||
|
||||
public override void ExplicitVisit(OpenRowsetCosmos node) { Found ??= "OPENROWSET"; base.ExplicitVisit(node); }
|
||||
|
||||
public override void ExplicitVisit(AdHocTableReference node) { Found ??= "OPENDATASOURCE"; base.ExplicitVisit(node); }
|
||||
}
|
||||
|
||||
private static string DescribeUnknownBackend(SqlProvider? provider) => provider == SqlProvider.Odbc
|
||||
? "an Odbc driver's backend is unknown, so its statement"
|
||||
: "a statement checked without a provider";
|
||||
|
||||
+3
@@ -7,6 +7,9 @@
|
||||
<NoWarn>$(NoWarn);CS1591</NoWarn>
|
||||
<RootNamespace>ZB.MOM.WW.OtOpcUa.Driver.Sql.Contracts</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.SqlServer.TransactSql.ScriptDom" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Core\ZB.MOM.WW.OtOpcUa.Core.Abstractions\ZB.MOM.WW.OtOpcUa.Core.Abstractions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user