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
This commit is contained in:
Joseph Doherty
2026-11-09 15:26:03 -05:00
parent c4366539ca
commit b33f0f93c9
11 changed files with 417 additions and 30 deletions
+35 -2
View File
@@ -10,7 +10,7 @@
| Last reviewed | 2026-11-09 (re-review; baseline 2026-06-01 at `5f75cd4`) |
| Reviewer | Claude (2026-11-09 re-review, scadaproj#15; baseline: Claude automated) |
| Commit reviewed | `91fb2b4` |
| Open findings | 1 |
| Open findings | 0 |
## Summary
@@ -380,7 +380,7 @@ template that yields the entry's DN exactly as the search reports it.
| Severity | Low |
| Priority | P3 |
| Category | Correctness & logic bugs |
| Status | Open |
| Status | Resolved |
| Location | `ZB.MOM.WW.Auth/src/ZB.MOM.WW.Auth.ApiKeys.SqlServer/SqlServerApiKeyAdminStore.cs:45` (also `:46`, `:48`; `SqlServerApiKeyStore.cs:56,77`; `SqlServerApiKeyAuditStore.cs:30-32`; `SqliteApiKeyStoreImporter.cs:258-261,292-294`; columns in `SqlServerAuthSchema.cs:79-96`) |
**Description**
@@ -422,6 +422,39 @@ I verified this live against a SQL Server 2022 container:
`ApiKeyAdminCommands`.
- Make the importer report key-id collisions instead of counting them as plain skips.
**Resolution**
Resolved 2026-11-09 (scadaproj#46, Auth `0.4.4`, unpublished). Re-verified live against a throwaway SQL Server 2022
container (`RTM-CU22`): `FindByKeyIdAsync("LINE1")` found the row stored as `line1`, and the importer counted
`Line1` beside `line1` as a plain skip. The offline tests reproduced the truncation: an over-length create was sent
to the server. Fixed without a schema change, so the live shared store needs no migration. A new internal helper,
`SqlServerColumnLimits`, carries the rules:
- **Lengths.** `SqlServerApiKeyAdminStore.CreateAsync` throws `ArgumentException` before connecting when `KeyId` is
over 64, `KeyPrefix` over 16 or `DisplayName` over 256 characters. Every key-id lookup and mutation (`Find*`,
`MarkUsed`, `Revoke`, `Rotate`, `SetScopes`, `SetEnabled`, `Delete`) returns not-found or `false` for an id over
64 characters without querying. Before, the truncated parameter could match a different key that shares the
first 64 characters.
- **Case.** Every `WHERE key_id = @key_id` also requires `CAST(key_id AS VARBINARY(128)) = CAST(@key_id AS VARBINARY(128))`.
The collation-aware `=` still drives the primary-key seek, and the binary compare makes the match exact, like
SQLite's `BINARY`. The column collation is deliberately not changed: that would be a primary-key migration on a
live shared store. Consequence: on a case-insensitive database, `Line1` and `line1` still cannot both exist.
`CreateAsync` already fails loudly with a PK violation in that case, and the importer now reports it.
- **Importer.** The new `init` properties `ApiKeyImportResult.KeyIdCollisions` (case-only clashes with a key
already in SQL Server) and `KeysRejected` (over-length rows) list what was not imported and why. Before, both kinds
were folded into `KeysSkipped`, or imported truncated. `KeysSkipped` now counts only exact duplicates. The
insert-if-absent statement returns a three-way outcome from inside one statement, so it stays race-safe.
- **Audit text.** `key_id`, `event_type` and `remote_address` are still truncated to 64 characters. This is now
deliberate and documented on `SqlServerApiKeyAuditStore.AppendAsync`: an audit append runs after the action it
records, so refusing it would lose the record of a change that has already happened.
Tests:
- `SqlServerColumnLimitTests` (offline, refused loopback port). Over-length create is rejected for each of the three
fields, and all 8 key-id operations answer "not found" for an over-length id. These fail pre-fix with
`SqlException`. A positive control at exactly the limits still reaches the server.
- `Live/KeyIdFidelityLiveTests` (`AUTH_SQLSERVER_CONNSTR`). Case- and trailing-space-exact lookups, an importer
case-collision report, and an importer over-length rejection. All 3 fail pre-fix.
- The full SqlServer suite passes live: 101/101.
### Auth-009 — Canonical username is the first value of a possibly multi-valued attribute, not the value the user matched
| | |
+5 -8
View File
@@ -40,7 +40,7 @@ code-reviews/
## Summary
10 of 10 libraries reviewed. 16 pending findings across all libraries.
10 of 10 libraries reviewed. 13 pending findings across all libraries.
Coverage: Secrets, LocalDb, GalaxyRepository and Overview first reviewed, and Auth, Health,
Theme, Audit, Configuration and Telemetry re-reviewed, on 2026-11-09 (#15). Each finding also
@@ -51,15 +51,15 @@ carries a P1/P2/P3 priority (P1 = Critical/High, P2 = Medium, P3 = Low).
| Critical | 0 |
| High | 0 |
| Medium | 0 |
| Low | 16 |
| **Total** | **16** |
| Low | 13 |
| **Total** | **13** |
## Library Status
| Library | Last reviewed | Commit | Open (C/H/M/L) | Open | Total |
|---------|---------------|--------|----------------|------|-------|
| [Audit](Audit/findings.md) | 2026-11-09 | `91fb2b4` | 0/0/0/0 | 0 | 5 |
| [Auth](Auth/findings.md) | 2026-11-09 | `91fb2b4` | 0/0/0/3 | 3 | 10 |
| [Auth](Auth/findings.md) | 2026-11-09 | `91fb2b4` | 0/0/0/0 | 0 | 10 |
| [Configuration](Configuration/findings.md) | 2026-11-09 | `91fb2b4` | 0/0/0/0 | 0 | 4 |
| [GalaxyRepository](GalaxyRepository/findings.md) | 2026-11-09 | `91fb2b4` | 0/0/0/0 | 0 | 8 |
| [Health](Health/findings.md) | 2026-11-09 | `91fb2b4` | 0/0/0/0 | 0 | 7 |
@@ -88,13 +88,10 @@ _None open._
_None open._
### Low (16)
### Low (13)
| ID | Library | Title |
|----|---------|-------|
| Auth-008 | [Auth](Auth/findings.md) | SQL Server store silently truncates over-length values and matches key ids case-insensitively, diverging from SQLite |
| Auth-009 | [Auth](Auth/findings.md) | Canonical username is the first value of a possibly multi-valued attribute, not the value the user matched |
| Auth-010 | [Auth](Auth/findings.md) | `ApiKeyVerifier` lets store exceptions escape despite its "only exception path is cancellation" contract |
| Secrets-004 | [Secrets](Secrets/findings.md) | Corrupt or wrong-length crypto fields escape `Decrypt` as `ArgumentException`: not audited, and breaks the cipher contract |
| Secrets-005 | [Secrets](Secrets/findings.md) | Store-path rules are lexical: drive-relative paths pass "rooted", and `\\?\` / UNC / 8.3 / symlink spellings bypass "under content root" |
| Secrets-006 | [Secrets](Secrets/findings.md) | Resolver cache can re-cache a stale value after `Invalidate`, and expired plaintext is never evicted |