Maven DF: match by known repository base instead of enumeration, when available

Summary

Follow-up to !4291 (closed), proposed by @arpitgogia in this review comment. Credit to him for the idea and the writeup this issue is based on.

!4291 fixes mirror/registry path-prefix matching by enumerating every possible group reading of the path (dropping one leading segment at a time) and checking each against policy. That's correct and necessary as a general fallback, but when we already know the repository's base URL — because the caller configured it in their own settings.xml — we don't need to guess at all: stripping the known base off the path gives the exact group, with one policy call instead of up to ten, and no risk of a shorter enumerated reading colliding with an unrelated real package.

The idea

<mirror>
  <id>corp</id>
  <mirrorOf>*</mirrorOf>
  <url>https://nexus.corp/repository/maven-public/</url>
</mirror>
https://nexus.corp/repository/maven-public/org/apache/logging/log4j/log4j-core/2.14.1/log4j-core-2.14.1.jar
└────────────── known base, strip it ─────┘└──────── group ────────┘

Stripping the known base gives org.apache.logging.log4j:log4j-core directly. Treating Maven Central's own base as "just another known base" gives the same one-call result for unmirrored builds too. Enumeration would then only run for bases we don't recognize.

Why this is worth doing, not just cheaper

  • One call per artifact instead of one per group segment. Directly reduces the cost !4291's own description flags as an accepted tradeoff (up to a 2.84x increase in policy calls).
  • Removes a real false-positive class, not just a theoretical one. Enumeration can generate a short reading that collides with an unrelated real package — e.g. a rule on the old relocated dom4j:dom4j also blocks the current org.dom4j:dom4j even from plain Central today, and a rule on foo:bar would also block com.foo:bar behind a mirror. Knowing the real base eliminates this category of mismatch entirely rather than reducing how often it occurs.
  • No dependence on group depth, so mavenMaxGroupDepth (the cap added to bound enumeration's worst case) becomes irrelevant for any request behind a known base.

What exists already

  • The proxy already has the request's host: serveTunnel sets req.URL.Host = authority (proxy.go). MavenMatcher currently reads only req.URL.Path.
  • !4289 already locates the settings file Maven will read (-s in MAVEN_ARGS, the command line, .mvn/maven.config, ~/.m2/settings.xml). Reading <mirrors> and profile <repositories>/<pluginRepositories> out of that file is a small step on top of locating it.
  • One wiring detail to solve: in !4289, proxy.New(manager.Matcher(), …) runs before SetArgs in pm.go's Run(). The matcher needs the resolved bases earlier than it currently gets the args, or needs to resolve them lazily.

What it can't replace

Enumeration has to remain as a permanent fallback — this is additive, not a replacement:

  • Repositories declared in the project POM, a parent POM, or a dependency's POM aren't in settings.xml at all.
  • mirrorOf patterns like external:* or !some-repo let specific repositories bypass a mirror; this issue doesn't take on evaluating those patterns, only stripping URLs we already have.
  • Global settings ($MAVEN_HOME/conf/settings.xml, -gs) aren't read by !4289 yet — see also the -gs gap already found empirically in that investigation, where the firewall didn't engage at all under -gs against released glab.
  • ${env.…} URLs and profile activation add their own resolution logic.
  • Gradle shares this matcher but has no settings file at all — its repositories live in build scripts.

The unknown-base path must never fall back to the old, unprefixed-only parse — only to !4291's enumeration.

Proposed scope

Known base → exact strip (one call). Unknown base → !4291's enumeration (unchanged). A rough PoC of the stripping mechanism itself (no <mirrorOf> evaluation, no POM awareness) exists on 637331-combined-fix-poc for reference.

Depends on

  • !4291 (closed) (Gap 1 — path-prefix enumeration, the fallback this builds on)
  • !4289 (closed) (Gap 2 — settings-file discovery, the source of known bases)

Not expected to block either of those landing first.

Relates to gitlab-org/gitlab#637331.