Skip to content

armgit / ECC

Public Imported from GitHub
0

The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.

fix(gateguard): match glob bracket classes exactly

Jeffrey Montoyae5b95ce921f4
diff --git a/scripts/hooks/gateguard-fact-force.js b/scripts/hooks/gateguard-fact-force.js
index 85633d774..912694f4a 100644
--- a/scripts/hooks/gateguard-fact-force.js
+++ b/scripts/hooks/gateguard-fact-force.js
@@ -2600,9 +2600,11 @@ function globTokens(glob) {
tokens.push({ type: 'star' });
} else if (ch === '?') {
tokens.push({ type: 'one' });
- } else if (ch === '[' && glob.indexOf(']', i + 2) !== -1) {
- tokens.push({ type: 'one' }); // any character class: over-approximated as one character
- i = glob.indexOf(']', i + 2);
+ } else if (ch === '[') {
+ const parsed = bracketClass(glob, i);
+ if (!parsed) return null;
+ tokens.push(parsed.token);
+ i = parsed.end;
} else {
tokens.push({ type: 'lit', ch });
}
@@ -2610,9 +2612,36 @@ function globTokens(glob) {
return tokens;
}
-/** Glob match by dynamic programming, so a hostile glob cannot cause regex backtracking. */
+/** `[...]` with members, ranges, `!`/`^` negation and a leading literal `]`; null when unclosed. */
+function bracketClass(glob, open) {
+ let i = open + 1;
+ const negated = glob[i] === '!' || glob[i] === '^';
+ if (negated) i += 1;
+ const ranges = [];
+ for (let first = true; i < glob.length && (first || glob[i] !== ']'); first = false) {
+ if (glob[i + 1] === '-' && i + 2 < glob.length && glob[i + 2] !== ']') {
+ ranges.push([glob[i], glob[i + 2]]);
+ i += 3;
+ } else {
+ ranges.push([glob[i], glob[i]]);
+ i += 1;
+ }
+ }
+ if (i >= glob.length) return null;
+ return { token: { type: 'class', negated, ranges }, end: i };
+}
+
+function classMatches(token, ch) {
+ if (ch === '/') return false;
+ const forms = [ch, ch.toLowerCase(), ch.toUpperCase()];
+ const member = token.ranges.some(([lo, hi]) => forms.some(form => form >= lo && form <= hi));
+ return member !== token.negated;
+}
+
+/** Glob match by dynamic programming, so a hostile glob cannot cause regex backtracking; null when malformed. */
function wildcardMatch(glob, text) {
const tokens = globTokens(glob);
+ if (!tokens) return null;
const n = text.length;
let next = new Array(n + 1).fill(false);
next[n] = true;
@@ -2623,6 +2652,7 @@ function wildcardMatch(glob, text) {
for (let j = n; j >= 0; j--) {
if (token.type === 'lit') row[j] = j < n && text[j] === token.ch && next[j + 1];
else if (token.type === 'one') row[j] = j < n && text[j] !== '/' && next[j + 1];
+ else if (token.type === 'class') row[j] = j < n && classMatches(token, text[j]) && next[j + 1];
else if (token.type === 'star') row[j] = next[j] || (j < n && text[j] !== '/' && row[j + 1]);
else if (token.type === 'deep') row[j] = next[j] || (j < n && row[j + 1]);
else {
@@ -2639,12 +2669,15 @@ function normalizeFilterGlob(glob) {
return String(glob).toLowerCase().replace(/\\/g, '/').replace(/^(?:\.\/)+/, '').replace(/^\/+/, '').replace(/\/+$/, '');
}
-/** Null when the glob is past the length or brace bounds. */
-function globMatches(glob, text) {
+/** Null when the glob is past the length or brace bounds; a malformed alternative counts as `malformed`. */
+function globMatches(glob, text, malformed) {
if (glob.length > MAX_FILTER_GLOB_CHARS) return null;
const alternatives = expandBraces(glob);
if (!alternatives) return null;
- return alternatives.some(alternative => wildcardMatch(alternative, text));
+ return alternatives.some(alternative => {
+ const matched = wildcardMatch(alternative, text);
+ return matched === null ? malformed : matched;
+ });
}
function targetSegments(ctx) {
@@ -2658,7 +2691,7 @@ function exclusionCoversTarget(exclusion, segments, stem) {
if (stem !== null && stem.test(glob)) return true;
for (let start = 0; start < segments.length; start++) {
for (let end = start + 1; end <= segments.length; end++) {
- const matched = globMatches(glob, segments.slice(start, end).join('/'));
+ const matched = globMatches(glob, segments.slice(start, end).join('/'), true);
if (matched !== false) return true;
}
}
@@ -2674,13 +2707,13 @@ function filtersAdmitTarget(item, ctx, stem) {
return !exclusions.some(exclusion => exclusionCoversTarget(exclusion, segments, stem));
}
-/** Basename include globs that all miss the target stop its stem from crediting; oversized globs admit. */
+/** Basename include globs that all miss the target stop its stem from crediting; oversized globs admit, malformed ones do not. */
function includesAdmitTarget(item, ctx) {
const includes = Array.isArray(item.includes) ? item.includes : [];
if (includes.length === 0) return true;
const segments = targetSegments(ctx);
const base = segments[segments.length - 1] || '';
- return includes.some(include => globMatches(normalizeFilterGlob(include).replace(/^(?:\*\*\/)+/, ''), base) !== false);
+ return includes.some(include => globMatches(normalizeFilterGlob(include).replace(/^(?:\*\*\/)+/, ''), base, false) !== false);
}
function isBasenameGlob(glob) {
diff --git a/tests/hooks/gateguard-fact-force.test.js b/tests/hooks/gateguard-fact-force.test.js
index 68c19fe1d..e11e64aa5 100644
--- a/tests/hooks/gateguard-fact-force.test.js
+++ b/tests/hooks/gateguard-fact-force.test.js
@@ -5019,6 +5019,36 @@ function runTests() {
});
}
+ // --- Bracket classes in filter globs match exactly ---
+ const bracketCases = [
+ ['include class of a dot does not admit another character', 'src/fooXjs', "rg -g 'foo[.]js' fooXjs .", false],
+ ['include class of a dot admits the dot', 'src/widget.py', "rg -g 'widget[.]py' widget .", true],
+ ['include negated class admits another character', 'src/widget.py', "rg -g 'widge[!x].py' widget .", true],
+ ['include negated class rejects its member', 'src/widget.py', "rg -g 'widge[!t].py' widget .", false],
+ ['include caret-negated class rejects its member', 'src/widget.py', "rg -g 'widge[^t].py' widget .", false],
+ ['include range admits a member', 'src/widget.py', "rg -g 'widge[r-u].py' widget .", true],
+ ['include range rejects a non-member', 'src/widget.py', "rg -g 'widge[a-f].py' widget .", false],
+ ['include range is case-insensitive', 'src/widget.py', "rg -g 'widge[R-U].py' widget .", true],
+ ['include class with a literal ] first', 'src/widget.py', "rg -g 'widge[]t].py' widget .", true],
+ ['Grep include class of a dot does not admit another character', 'src/fooXjs', { pattern: 'fooXjs', glob: 'foo[.]js' }, false],
+ ['include with an unclosed class admits nothing', 'src/widget.py', "rg -g 'widget[.py' widget .", false],
+ ['include with an empty class admits nothing', 'src/widget.py', "rg -g 'widget[].py' widget .", false],
+ ['exclusion class covering the target blocks it', 'src/widget.py', "rg -g '!widge[t].py' widget .", false],
+ ['exclusion range covering the target blocks it', 'src/widget.py', "rg -g '!widge[s-u].py' widget .", false],
+ ['exclusion class of a dot does not cover another character', 'src/widgetXjs', "rg -g '!widget[.]js' widgetXjs .", true],
+ ['exclusion negated class does not cover its member', 'src/widget.py', "rg -g '!widge[!t].py' widget .", true],
+ ['exclusion with an unclosed class covers the target', 'src/widget.py', "rg -g '!zzz[' widget .", false],
+ ['exclusion with an empty negated class covers the target', 'src/widget.py', "rg -g '!zzz[!]' widget .", false]
+ ];
+ for (const [label, rel, search, credited] of bracketCases) {
+ c3Case(`bracket class: ${label}`, () => {
+ const input = typeof search === 'string' ? { command: search } : { ...search, path: c3Root };
+ const out = c3Edit(`${c3Root}/${rel}`, r2Turn(typeof search === 'string' ? 'Bash' : 'Grep', input));
+ if (credited) c3AssertCredited(out, label);
+ else c3AssertDenied(out, label);
+ });
+ }
+
// --- Same-turn sibling creations and counters ---
const c4Root = fs.realpathSync(fs.mkdtempSync(path.join(tmpRoot, 'gateguard-c4-proj-')));
const c4Env = { CLAUDE_PROJECT_DIR: c4Root, CLAUDE_TRANSCRIPT_PATH: '' };