Files
scadaproj/ZB.MOM.WW.Auth/src/ZB.MOM.WW.Auth.ApiKeys.SqlServer/SqlServerColumnLimits.cs
T
Joseph Doherty b33f0f93c9 fix(auth): SQL Server key store refuses truncation and matches key ids exactly (Auth-008)
- Over-length key records (key_id > 64, prefix > 16, display name > 256) are
  refused with ArgumentException before connecting; an over-64 key-id lookup
  or mutation answers not-found without querying (SqlClient silently
  truncated the sized parameter, so a different id sharing the first 64
  characters matched).
- Every key_id predicate adds an exact VARBINARY comparison, so lookups are
  case- and trailing-space-exact like SQLite; the PK seek is unchanged and
  there is no schema change.
- SqliteApiKeyStoreImporter reports case-only key-id collisions and
  over-length rows in new init lists (KeyIdCollisions, KeysRejected) instead
  of counting them as skips or importing them truncated.
- Audit text truncation kept deliberately and documented.

Offline refused-port tests and live SQL Server 2022 tests fail pre-fix;
SqlServer suite 101/101 live. Auth findings now 0 open; CLAUDE.md records
the #46/#50 work; code-reviews/README.md regenerated.

(scadaproj#46)

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
2026-11-09 15:26:03 -05:00

62 lines
3.2 KiB
C#

using ZB.MOM.WW.Auth.Abstractions.ApiKeys;
namespace ZB.MOM.WW.Auth.ApiKeys.SqlServer;
/// <summary>
/// Keeps the SQL Server stores' key-id handling identical to the SQLite store they port (Auth-008):
/// no silent truncation and exact key-id matching.
/// </summary>
/// <remarks>
/// <para>
/// <b>Lengths.</b> Every parameter carries an explicit size, and SqlClient truncates an over-length
/// value to that size without an error. So an over-length key record is refused up front, and a
/// lookup of an over-length key id answers "not found" without querying — such a key cannot exist,
/// and the truncated parameter would otherwise match a different key that shares its first 64
/// characters.
/// </para>
/// <para>
/// <b>Case.</b> The <c>key_id</c> column uses the database's collation, case-insensitive on a stock
/// SQL Server, whereas SQLite compares key ids byte-for-byte. Changing the column's collation would
/// need a schema migration of a primary key on a live shared store, so every <c>key_id</c> predicate
/// instead adds <see cref="ExactKeyIdMatch"/>: the collation-aware <c>=</c> still drives the primary-key
/// seek, and the binary comparison then rejects a row that differs in case or trailing spaces.
/// </para>
/// </remarks>
internal static class SqlServerColumnLimits
{
/// <summary>Width of <c>api_keys.key_id</c> (and <c>api_key_audit.key_id</c>).</summary>
public const int KeyId = 64;
/// <summary>Width of <c>api_keys.key_prefix</c>.</summary>
public const int KeyPrefix = 16;
/// <summary>Width of <c>api_keys.display_name</c>.</summary>
public const int DisplayName = 256;
/// <summary>
/// Appended to every <c>key_id = @key_id</c>. <c>NVARCHAR</c> → <c>VARBINARY</c> yields the UTF-16
/// bytes, so the comparison is exact (case, accents and trailing spaces all significant).
/// </summary>
public const string ExactKeyIdMatch = "CAST(key_id AS VARBINARY(128)) = CAST(@key_id AS VARBINARY(128))";
/// <summary>Whether a key with this id could be stored at all.</summary>
public static bool CanExist(string keyId) => keyId.Length <= KeyId;
/// <summary>The first over-length field of <paramref name="record"/>, described, or <see langword="null"/> when every field fits.</summary>
public static string? OverLengthField(ApiKeyRecord record) =>
record.KeyId.Length > KeyId ? Describe(nameof(ApiKeyRecord.KeyId), record.KeyId.Length, KeyId)
: record.KeyPrefix.Length > KeyPrefix ? Describe(nameof(ApiKeyRecord.KeyPrefix), record.KeyPrefix.Length, KeyPrefix)
: record.DisplayName.Length > DisplayName ? Describe(nameof(ApiKeyRecord.DisplayName), record.DisplayName.Length, DisplayName)
: null;
/// <summary>Throws <see cref="ArgumentException"/> when a field of <paramref name="record"/> exceeds its column.</summary>
public static void EnsureFits(ApiKeyRecord record)
{
if (OverLengthField(record) is { } problem)
throw new ArgumentException($"API key record cannot be stored in SQL Server: {problem}.", nameof(record));
}
private static string Describe(string field, int length, int max) =>
$"{field} is {length} characters, more than the column's {max}";
}