Xuanwo opened a new pull request, #8955:
URL: https://github.com/apache/opendal/pull/8955

   # Which issue does this PR close?
   
   None.
   
   # Rationale for this change
   
   `opendal-service-memcached` had four protocol framing, connection-pool, and 
configuration defects:
   
   1. **Pooled binary connection desynchronization and unchecked header 
arithmetic**: The binary client did not assign or verify request `opaque` 
tokens, did not validate response `magic`/`opcode`, and did not track whether a 
socket was interrupted mid-flight. If an async operation was dropped/cancelled 
while awaiting a response, the socket could be returned to the pool with unread 
response bytes still buffered, causing the subsequent `VERSION` recycle check 
to consume the stale reply and pass while leaving the `VERSION` response for 
the next caller (leading to cross-key data leaks or protocol corruption). In 
addition, non-`OK` responses (such as `0x0003 Value too large` or `0x0004 
Invalid arguments`) could leave error bodies unread on the socket, and 
`total_body_length - extras_len - key_len` used unchecked subtraction that 
could underflow or attempt a multi-gigabyte allocation on malformed headers.
   2. **Unchecked binary header length casts**: `key.len() as u16` and 
`total_body_length as u32` silently truncated oversized keys and packet bodies 
instead of rejecting them before writing to the wire.
   3. **Zero pool size panic**: `connection_pool_max_size = Some(0)` was 
forwarded directly to `asyncband::pool::bounded::Pool::new`, which panics on 
zero capacity instead of returning `ErrorKind::ConfigInvalid`.
   4. **URI credentials ignored**: `MemcachedConfig::from_uri` ignored 
`username` and `password` embedded in `memcached://user:pass@host:port/root` 
URIs, unlike sibling services (`redis`, `postgresql`, `ftp`, `sftp`, `webdav`).
   
   # What changes are included in this PR?
   
   - Track a `poisoned` state on `binary::Connection` that is set before 
writing any request bytes and cleared only after the matching response header 
and full body have been read and validated; 
`MemcachedConnectionManager::is_recyclable` rejects poisoned connections before 
issuing `VERSION`.
   - Assign a monotonically increasing `opaque` per binary request and validate 
`magic == 0x81`, `opcode == expected_opcode`, and `opaque == expected_opaque` 
on every response.
   - Use checked arithmetic for `total_body_length - (extras_length + 
key_length)`, enforce a bounded drain limit (`MAX_CONTROL_BODY_LENGTH`) on 
non-`GET`-`OK` control/error bodies so error payloads are cleanly drained 
without unbounded allocation, and map status codes consistently (e.g., 
`KEY_NOT_FOUND` only returns `Ok` for `GET`/`DELETE`, and retryable statuses 
mark temporary errors).
   - Validate key length (non-empty, `<= 250` bytes, fits `u16`) and total body 
length (checked addition fitting `u32`) before writing any bytes to the socket.
   - Reject `connection_pool_max_size == Some(0)` in `MemcachedBuilder::build` 
with `ErrorKind::ConfigInvalid`.
   - Populate `username` and `password` from URI userinfo in 
`MemcachedConfig::from_uri` while preserving explicit option overrides.
   - Add deterministic local TCP tests covering mid-flight cancellation, error 
body draining, malformed response headers, oversized key/body rejection, zero 
pool size rejection, and URI userinfo extraction, plus verify behavior tests 
against a local `memcached:1.6` instance.
   
   # Are there any user-facing changes?
   
   - `MemcachedConfig::from_uri` now extracts `username` and `password` from 
URI userinfo when those options are not explicitly overridden.
   - `MemcachedBuilder::build` now returns `ErrorKind::ConfigInvalid` when 
`connection_pool_max_size` is `Some(0)` instead of panicking.
   - Oversized memcached keys (`> 250` bytes) and packet bodies exceeding 
`u32::MAX` now return an `Unsupported` error before writing to the socket 
instead of silently truncating header fields.
   
   # Breaking changes
   
   # AI Usage Statement
   
   - Harness: pi
   - Model: Gemini 4 Neon
   - Effort: high
   - Role: Independently verified all four findings against `origin/main`, 
implemented the binary framing/pooling/validation/URI fixes in 
`core/services/memcached`, added deterministic local TCP unit tests, and 
verified formatting, clippy, unit/doc tests, rustdoc, and behavior tests 
against a local `memcached:1.6` container.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to