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

   # Which issue does this PR close?
   
   None.
   
   # Rationale for this change
   
   Four correctness and security defects exist in `opendal-service-mysql`:
   
   1. **Unescaped SQL identifiers**: `MysqlCore` interpolated `table`, 
`key_field`, and `value_field` directly inside backticks without escaping 
embedded `` ` `` characters, allowing malformed SQL or SQL injection when 
identifiers contain backticks.
   2. **Collation-dependent `LIKE` matching and unchecked path slicing in 
`list`**: Under MySQL's default case- and accent-insensitive collation 
(`utf8mb4_0900_ai_ci`, including `fixtures/mysql/init.sql`), `WHERE key LIKE ?` 
matches keys outside the requested prefix or root (for example 
`"app/secret.txt"` under `root = "/App/"`, or `"dir/b"` when recursively 
listing `"Dir/"`). `MysqlLister::next` passed every returned key directly to 
`build_relative_path`, causing debug assertion panics, cross-root leaks in 
release builds, and release-mode slice-index panics when a collation-equivalent 
key has a shorter UTF-8 byte length than the root prefix (for example `"é/"` 
matching `"/e\u{0301}/"`).
   3. **`NO_BACKSLASH_ESCAPES` incompatibility**: `MysqlCore::list` used 
backslash escaping for `LIKE` wildcards without an explicit `ESCAPE` clause, 
breaking wildcard escaping when `NO_BACKSLASH_ESCAPES` is enabled on the MySQL 
server.
   4. **URI and builder config validation gaps**: `MysqlConfig::from_uri` 
dropped URI username/password and overwrote an explicit `root` query/option 
with the URI path suffix. `MysqlBuilder::build` also accepted empty 
`connection_string`, `table`, `key_field`, or `value_field` (and NUL bytes in 
identifiers) when configured via `MysqlConfig`.
   
   # What changes are included in this PR?
   
   - Escape backticks (`\`` -> `\`\``) via `quote_identifier` when constructing 
`MysqlCore` and preserve the raw unquoted table name in `ServiceInfo::name`.
   - Update `MysqlCore::list` to use an explicit non-backslash `ESCAPE '!'` 
(escaping `!`, `%`, and `_`) and pair the index-friendly `LIKE ? ESCAPE '!'` 
predicate with `CONVERT(key USING utf8mb4) COLLATE utf8mb4_bin LIKE CONVERT(? 
USING utf8mb4) COLLATE utf8mb4_bin ESCAPE '!'` for exact UTF-8 codepoint prefix 
matching regardless of column collation or `NO_BACKSLASH_ESCAPES`.
   - Defensively verify exact prefix and root-prefix membership (`starts_with` 
/ `strip_prefix`) and path normalization in `MysqlLister::next` before 
constructing entries.
   - Preserve percent-encoded URI `username` and `password` when synthesizing 
`connection_string` in `MysqlConfig::from_uri`, and avoid overwriting an 
explicit `root` option.
   - Validate non-empty `connection_string` and non-empty, NUL-free `table`, 
`key_field`, and `value_field` in `MysqlBuilder::build`, returning 
`ErrorKind::ConfigInvalid`.
   - Add unit tests covering identifier quoting, `LIKE` escaping, config 
validation, `from_uri` credentials/precedence, and `MysqlLister` 
prefix/collation safety.
   
   # Are there any user-facing changes?
   
   - `Operator::from_uri("mysql://user:pass@host:port/table/root")` now 
preserves URI credentials and respects explicit `root` options.
   - `MysqlBuilder::build` now rejects empty 
`connection_string`/`table`/`key_field`/`value_field` or NUL bytes in SQL 
identifiers with `ErrorKind::ConfigInvalid`.
   - `list` operations now match prefixes strictly by UTF-8 codepoint instead 
of inheriting case-/accent-insensitive column collation behavior, and work 
consistently when MySQL runs with `NO_BACKSLASH_ESCAPES`.
   
   # Breaking changes
   
   # AI Usage Statement
   
   - **Harness:** pi
   - **Model:** Gemini 4 Neon
   - **Effort:** high
   - **Role:** Investigated the `opendal-service-mysql` defects, implemented 
the fixes and unit tests, and verified identifier escaping, 
`utf8mb4_0900_ai_ci` collation isolation, `NO_BACKSLASH_ESCAPES` behavior, 
`EXPLAIN` index range-scan preservation, and the `mysql` behavior test suite 
against a local MySQL 8.1.0 instance. Point operations (`get`, `get_length`, 
`set`, `delete`) still follow the table's configured column collation for 
equality lookups.
   


-- 
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