Skip to content

filter: fix directory filters ignoring / and ** inside {} groups - #9778

Closed
vjymisal0 wants to merge 4 commits into
rclone:masterfrom
vjymisal0:fix/filter-dir-globs-braces
Closed

vjymisal0 wants to merge 4 commits into
rclone:masterfrom
vjymisal0:fix/filter-dir-globs-braces

Conversation

@vjymisal0

Copy link
Copy Markdown
Contributor

What is the purpose of this change?

Fixes #3250 — a filter whose brace group contains / or ** produced a directory rule matching every directory, so rclone walked the whole remote.

Using the reproduction from the issue:

rclone lsf -R --include '/a/{b,c/**}' . --dump filters

Before:

--- Directory filter rules ---
+ ^.*$      <--- matches every directory
+ ^a/(b|c/.*)$
- ^.*$

After:

--- Directory filter rules ---
+ ^a/$
+ ^a/c/.*/$
+ ^a/c/$
+ ^a/(b|c/.*)$
- ^.*$

Root cause

globToDirGlobs bails out via tooHardRe when a brace group contains / or **, returning /**. As noted in the issue, this case was punted on when the code was written.

The alternatives inside a brace group are a disjunction, so the directory globs for the whole glob are just the union of the directory globs of each alternative. expandBraces expands the groups (/a/{b,c/**} → /a/b, /a/c/**), and the existing splitting logic then runs over each brace-free result, with the results deduplicated in order.

Constructs that genuinely can't be expanded — nested braces and {{ regexp }} sections — still return /** exactly as before. Expansion is combinatorial across groups, so it is capped by maxBraceExpansions and falls back to the old behaviour if a glob would exceed it.

File filter rules are untouched: only the derived directory rules change, so this narrows which directories are traversed and never changes which files match.

Was the change discussed in an issue or in the forum before?

#3250, which is labelled good first issue / help wanted. Note the maintainer's comment there offers two options ("That should produce a warning I suppose, or I should fix it") — this PR implements the fix; happy to switch to just warning instead if that is preferred.

Checklist

  • I have read the contribution guidelines.
  • I have added tests for all changes in this PR if appropriate.
  • I have added a note to docs/content/changelog.md if appropriate. (n/a — bugfix, changelog is generated from commit messages)
  • I have checked that all the tests pass.

Testing

TestGlobToDirGlobs gains cases for the reported glob and for / and ** inside groups, including multiple groups in one glob. The two existing cases that asserted the /** fallback for a/{a,a*b,a**c}/d/ and /a/{a,a*b,a/c,d}/d/ now assert the expanded directory globs; the nested-brace and {{ regexp }} cases still assert /**.

./fs/filter/, ./fs/list/ and ./fs/march/ all pass, and gofmt/go vet are clean. Also verified end to end against a local tree that the directories which cannot contain a match are no longer descended into.

globToDirGlobs gave up on a glob whose brace group contained / or **
and returned /**, so every directory matched. A filter such as
--include '/a/{b,c/**}' then scanned the whole remote instead of just
the directories the glob can match.

Expand the brace groups into their alternatives and take the union of
their directory globs. Nested braces and {{ regexp }} sections are
still too hard and keep the previous behaviour - fixes rclone#3250
@vjymisal0
vjymisal0 requested a review from ncw as a code owner August 16, 2026 09:57
Copilot AI lite review requested due to automatic review settings August 16, 2026 09:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@CAOShurong

Copy link
Copy Markdown
Contributor

Verified on master (b382fe8)

I applied this patch locally onto a fresh master fetch and ran the suite:

  • git apply --check clean; go test ./fs/filter/ -run TestGlobToDirGlobs PASS
  • full package: go test ./fs/filter/, go test ./fs/, go vet ./fs/filter/ all green
  • expansion semantics spot-checked beyond the issue repro — nested braces
    ({a,{b,c}}/d/** → /** fallback), regexp sections ({{a,b,c}}/d/** →
    /**), adjacent groups ({a,b}{c,d}/e/** → split correctly),
    empty alternatives ({/,}a/**) — all behave as described in the PR body.

Latent unbounded recursion (not reachable from the CLI today, worth a guard)

There is an input class where tooHardRe matches but expandBraces finds no
brace group: any glob containing }} without an opening {, e.g.
a}}/c/**. In that case the first loop finds no { and returns
([]string{glob}, true) — the unchanged string — so
globToDirGlobs recurses into itself with the identical input forever. I
reproduced this with a package-level probe: globToDirGlobs("a}}/c/**")
ends in fatal error: stack overflow.

Why it isn't reachable right now: every public entry point
(Filter.Add / Filter.AddRule / NewFilter rule parsing) calls
GlobPathToRegexp before addDirGlobs, and its brace-balance check rejects
a}}/c/** with mismatched '{' and '}' in glob. I also brute-forced a corpus
of balanced-brace globs (nested, regexp sections, adjacent groups) and all
degrade gracefully.

Still, I'd suggest making the invariant local to where the recursion lives,
since globToDirGlobs is unexported and a future caller could bypass the CLI
gate — e.g. treat "no brace group found" as ok == false:

if start < 0 {
    // tooHardRe matched but there is no expandable group here
    // (e.g. a stray }}); let the caller fall back to /**.
    return nil, false
}

That turns the crash back into the pre-PR graceful /** behavior for that
class of input. Happy to verify a follow-up commit locally if you push one.

Nice work overall — the union-of-directory-globs decomposition is exactly the
right model for brace alternation, and the cap keeps the combinatorics bounded.

@vjymisal0

Copy link
Copy Markdown
Contributor Author

@CAOShurong Thanks for the thorough review and catch! I've pushed a commit (b86bf8f) adding a guard against {{\ and }}\ at the start of \expandBraces, along with unit tests covering unmatched brace globs in \globToDirGlobs. It now gracefully falls back to /**\ without recursion.

@vjymisal0

Copy link
Copy Markdown
Contributor Author

Thanks for verifying it against master and running the filter suites. Appreciate the detailed check.

@ncw

ncw commented Oct 14, 2026

Copy link
Copy Markdown
Member

Thanks for working on this, and sorry for the slow reply.

There's an earlier PR for the same issue, #9645, and we're going to go with that one. I ran both against the same set of filters, and this PR has a few problems that #9645 doesn't:

  • --include '\{a/**\}' causes a stack overflow
  • /a/{b[,/]c,d} skips a/b, so a/b/c is silently missed
  • valid filters with [...] inside a {} group, e.g. {x,a[/]b}, are rejected as mismatched brackets

So I'm going to close this one. Thanks again for the contribution. Reviews and testing on #9645 would be very welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

filter: fix directory filter globs for / and ** in {}

4 participants