Skip to content

Commit 2330844

Browse files
committed
fix(log-guard): make descendant matching case-sensitive via substr, not LIKE
Re-review found the LIKE form over-broad: SQLite LIKE is ASCII case-insensitive by default, so 'hyper_util::%' also suppressed HYPER_UTIL::child. Rust target paths are case-sensitive, and silently discarding a differently-cased target is a wrong answer rather than a safe default. substr(NEW.target, 1, N) = 'X::' is a plain case-sensitive comparison with no wildcard metacharacters, which also removes the _/% hazard that LIKE needed an ESCAPE clause to contain. Documented the deliberate narrowing: upstream's raw prefix rule would also match a sibling crate named hyper_utilities. Suppressing an unrelated crate's logs is worse for a guard that silently discards rows, so this matches the module-descendant relation instead; the existing regression pins hyper_utilities as preserved, and HYPER_UTIL::child is now pinned alongside it.
1 parent 017b9bc commit 2330844

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

‎src/codex/log-guard/protection.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,27 @@ const CURRENT_LOG_COLUMNS = [
5252
* high-volume child targets — the ones that actually fill the database — kept
5353
* writing while compat mode reported itself active.
5454
*
55-
* The comparison is `target = X OR target LIKE X || '::%' ESCAPE '\\'`. The
56-
* ESCAPE clause is load-bearing: `_` is a single-character LIKE wildcard, so an
57-
* unescaped `hyper_util` would also match `hyperXutil`, and `hyper_utilities`
58-
* would slip past the boundary. Every `_`, `%` and `\\` in the literal is
59-
* escaped before interpolation. The trailing `'::'` keeps a sibling crate whose
60-
* name merely starts with the same letters from matching.
55+
* The comparison uses `substr`, not `LIKE`. SQLite's `LIKE` is ASCII
56+
* case-insensitive by default, so a `LIKE` form would also suppress
57+
* `HYPER_UTIL::child` — Rust target paths are case-sensitive, and silently
58+
* dropping a differently-cased target is a wrong answer, not a safe default.
59+
* `substr(NEW.target, 1, N) = 'X::'` is a plain case-sensitive comparison with
60+
* no wildcard metacharacters to escape, which also removes the `_`/`%` hazard
61+
* that `LIKE` would have required an ESCAPE clause to contain.
62+
*
63+
* The `'::'` boundary is deliberate and NARROWER than upstream's raw prefix
64+
* rule: `Targets::with_target("hyper_util")` would also match a sibling crate
65+
* named `hyper_utilities`. Suppressing an unrelated crate's logs is worse for
66+
* a guard that silently discards rows, so this matches the module-descendant
67+
* relation instead. The existing regression pins `hyper_utilities` as
68+
* preserved.
6169
*
6270
* `opentelemetry_sdk` stays exact because upstream registers it with exact
6371
* equality rather than a prefix filter.
6472
*/
6573
function targetOrDescendant(target: string): string {
66-
const escaped = target.replace(/[\\%_]/g, ch => `\\${ch}`);
67-
return `(NEW.target = '${target}' OR NEW.target LIKE '${escaped}::%' ESCAPE '\\')`;
74+
const prefix = `${target}::`;
75+
return `(NEW.target = '${target}' OR substr(NEW.target, 1, ${prefix.length}) = '${prefix}')`;
6876
}
6977

7078
function anyTargetOrDescendant(targets: readonly string[]): string {

‎tests/codex-log-guard-protection.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,16 @@ describe("Codex Log Guard protection", () => {
146146
// A near-prefix must NOT match: the '::' boundary is what separates a child
147147
// module from an unrelated crate that merely starts with the same letters.
148148
insert(databasePath, "TRACE", "hyper_utilities");
149+
// Rust target paths are case-sensitive. SQLite LIKE is ASCII
150+
// case-insensitive, so a LIKE-based prefix would have suppressed this
151+
// unrelated target too; substr() keeps the comparison exact.
152+
insert(databasePath, "TRACE", "HYPER_UTIL::child");
149153
// opentelemetry_sdk stays exact upstream, so its children are preserved.
150154
insert(databasePath, "TRACE", "opentelemetry_sdk::trace");
151155

152156
expect(rows(databasePath)).toEqual([
153157
{ level: "TRACE", target: "hyper_utilities" },
158+
{ level: "TRACE", target: "HYPER_UTIL::child" },
154159
{ level: "TRACE", target: "opentelemetry_sdk::trace" },
155160
]);
156161
});

0 commit comments

Comments
 (0)