s9: harden the codec/client surface against malformed server input (+ perf, docs, CI) - #449
Open
MattJackson wants to merge 7 commits into
Open
s9: harden the codec/client surface against malformed server input (+ perf, docs, CI)#449MattJackson wants to merge 7 commits into
MattJackson wants to merge 7 commits into
Conversation
MattJackson
force-pushed
the
stack/s9
branch
from
September 8, 2026 03:10
b011283 to
960ee69
Compare
…per poll The PLP / TEXT / NTEXT / IMAGE value decoders read their payload one byte (or one u16) per `poll_read`, turning an N-byte column value into N async state-machine polls on every row. Add a packet-aware `read_bytes_into` primitive to `SqlReadBytes` that fills a buffer by copying the largest available contiguous slice each iteration — O(packets) instead of O(bytes) — while preserving the existing `MAX_PREALLOC` windowed-reservation cap so a lying server length can't force a large up-front allocation. Route every LOB value decoder through it; decode output is byte-for-byte identical, locked by the existing round-trip tests plus new packet-boundary tests. Also drop a duplicate packet-header decode in `PacketCodec::decode` (the length is now peeked inline) and move the `length < HEADER_BYTES` guard ahead of consuming the header so a malformed short packet is rejected without draining bytes.
…rrupting Server-controlled value bytes could previously panic the connection task or silently produce wrong data. Harden every value decode/encode path: - numeric: reconstruct the 12/16-byte magnitude with checked arithmetic and reject magnitudes past i128::MAX; use `unsigned_abs` on encode (no i128::MIN panic); validate scale/precision (0..=38) at decode before constructing. - time (chrono + time backends): a SmallDateTime minute field >= 1440 no longer panics (chrono) or silently wraps to the wrong wall-clock time (time) — both return a protocol error; reject DATETIME2 scale > 9 and out-of-range day/offset values; surface out-of-range chrono dates as errors, not asserts. - sql_variant: checked 16-byte magnitude arithmetic; cross-checked prop/data lengths. - add the length-validation guard the sibling decoders already have to the `time`/`guid` column paths; bound-check the interpolated numeric scale; guard the XML blob-length multiply against overflow. `numeric`/`guid` magnitudes also switch to the bulk `read_bytes_into` reader. Each fix ships with a red-before-green unit test.
Make token decode desync-proof and token encode overflow-proof, and align a few spots with MS-TDS: - decode: ERROR/INFO/LOGINACK now read exactly their declared Length into a bounded buffer (a Length/content mismatch is a clean error, not a stream desync); reject odd-length ORDER, out-of-range ENVCHANGE packet sizes, and COLINFO entries that overrun their token; strict (non-lossy) UTF-16. - encode: replace four hand-rolled B_VARCHAR length counters (which wrapped a u8 past 255 and corrupted the wire) and the truncating XML/UDT length prefixes with shared, bound-checked `encode_b_varchar`/`encode_us_varchar` helpers; de-duplicate the ALL_HEADERS block. - negotiation: apply the LOGINACK-negotiated TDS version to the connection context so the version-dependent DONE/ERROR field widths are actually reached; fail (don't downgrade) when Required encryption is declined; exhaustive TokenType dispatch; preserve a trailing empty result set in the command stream. Adds server-free coverage for the TokenStream state machine (via a test-only mock connection) and the token length-mismatch paths.
- SQL identifiers: one shared strict validator for the bulk table/column and TVP `db_type` paths (which are interpolated into a batch because T-SQL can't parameterize identifiers). It rejects statement-breakers (`;`, quotes, `--`, `=`), top-level spaces, and delimiter-adjacent token splicing (`[t]UNION(..)`, `foo(1)UNION(..)`) while keeping multi-part names and parameterized types working. Documented as defense-in-depth, not a substitute for trust. - credentials: store the AAD token in `Zeroizing`; surface a GSSAPI error instead of unwrapping. - connection integrity: guard the bulk-load write path and `send_sensitive_login` with the poisoned-connection check so a cancelled/dropped write can't be followed by a silently-desynced reuse; roll back a partial bulk row on encode failure (now tested). - TLS: role-specific cert/key file-extension checks; named SSRP constants.
`FieldAttr::parse` panicked inside the proc-macro on a malformed or duplicate `#[colname]` attribute, giving users a raw macro panic instead of a normal compiler diagnostic. Return `syn::Error`s pointed at the offending span, like the rest of the `TableValueRow` derive already does.
- add a 0.13.0 CHANGELOG section and refresh the README feature table (drop the removed sql-browser-async-std, add winauth/sspi-rs/serde). - add `# Errors`/`# Panics` sections to the public fallible/panicking APIs and correct the chrono `DateTime` type-mapping doc tables.
… lane - declare `rust-version = 1.88` so the MSRV is enforced by cargo, not just CI. - add a `[licenses]` allow-list and run `cargo deny check ... licenses`. - add one Linux smoke lane exercising chrono + time + the decimal crates together, which per-feature-isolated lanes couldn't catch.
MattJackson
force-pushed
the
stack/s9
branch
from
September 8, 2026 18:07
960ee69 to
9870b07
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on top of s8 (#447). This is a defensive-hardening + foundation slice, not a feature — it makes the driver robust against a malicious or buggy server, tightens the security surface, and speeds up the per-row read path, without changing any happy-path behavior.
Verification
SmallDateTimeminute field ≥ 1440 now returns a protocol error instead of panicking, an over-long B_VARCHAR name errors instead of wrapping its length prefix, ERROR/INFO tokens with a length/content mismatch fail cleanly instead of desyncing the stream. The decode-hardening tests assert against crafted byte buffers, so they run without a server (cargo test --lib).ntextvalue round-trips.clippy --all-targets, andfmt.Happy-path behavior is unchanged — the changes only reject malformed input and batch the reads.
The 7 commits (the stack reads from the wire inward)
poll_read, i.e. N async polls per N-byte value per row. A new packet-awareread_bytes_intomakes it O(packets), keeping theMAX_PREALLOCcap so a lying length can't over-allocate.SmallDateTimepanic (and a silent wrong-time on thetimebackend); checked numeric/sql_variant magnitude arithmetic; scale/precision and DATETIME2/time length validation.Length(a mismatch is a clean error, not a stream desync); replaces four hand-rolled B_VARCHARu8counters that wrapped past 255 and corrupted the wire with bound-checked shared helpers; applies the LOGINACK-negotiated TDS version; fails (doesn't downgrade) whenRequiredencryption is declined.db_typeidentifiers (interpolated into a batch because T-SQL can't parameterize them); rejects statement-breakers and token-splicing while keeping multi-part/parameterized names working (documented defense-in-depth). AAD token inZeroizing; bulk-load + sensitive-login guarded by the poisoned-connection check with partial-row rollback.#[colname]— the derive returnssyn::Errorinstead of panicking inside the proc-macro.#[non_exhaustive] Error.Worth explicit attention
Erroris now#[non_exhaustive]— a deliberate semver call so future variants aren't breaking. Happy to revert if you'd rather not commit to that yet.#[colname]macro now emits a compile error where it previously panicked — user-visible, strictly better diagnostic.Requiredis carried consistent withmain(0.13.1) and noted in the changelog; no new decision here.Reviewable commit-by-commit; happy to split further or drop any commit (e.g. the
#[non_exhaustive]one) if you'd prefer to take it separately.