Skip to content
This repository was archived by the owner on Aug 27, 2026. It is now read-only.

[Java.Interop.Tools.Maven] Validate Artifact coordinates - #1479

Merged
jonathanpeppers merged 3 commits into
mainfrom
jonathanpeppers-validate-maven-artifact
Jun 23, 2026
Merged

jonathanpeppers merged 3 commits into
mainfrom
jonathanpeppers-validate-maven-artifact

Conversation

@jonathanpeppers

Copy link
Copy Markdown
Member

Context: dotnet/android's <AndroidMavenLibrary> MSBuild item relies on Artifact/Artifact.TryParse to validate Maven coordinates supplied by users, but today the constructor blindly assigns its fields and TryParse only checks for three colon-separated parts. Empty strings, whitespace, path traversal sequences (../), and characters that are illegal per the Maven coordinate spec all slip through.

This PR adds structural validation to Artifact:

  • groupId and artifactId must match [A-Za-z0-9_\-.]+ per the Maven Coordinates spec.
  • version rejects whitespace, :, and path separators (/, \). Maven versions are otherwise permissive, so no further parsing is attempted.
  • The constructor throws ArgumentNullException / ArgumentException with the offending parameter and value.
  • TryParse returns false for any invalid input (and requires all three parts to be non-empty) without throwing.
  • Parse continues to throw ArgumentException with the existing message format.

The constructor still allows an empty version so that existing internal callers — Dependency.ToArtifact() and Project.TryGetParentPomArtifact() — can keep producing partial coordinates when a POM omits <version> and inherits it from a parent POM. The user-input paths (TryParse/Parse) still require a non-empty version.

Tests

Added tests/Java.Interop.Tools.Maven-Tests/ArtifactTests.cs covering:

  • Valid coordinates round-trip through ctor / TryParse / Parse, including GroupId / Id / Version / ArtifactString / VersionedArtifactString.
  • Invalid inputs rejected by both ctor (throws) and TryParse (returns false, out param null): null, empty, whitespace-only, wrong number of :-separated parts, illegal chars (space, :, /, @, !, etc.), and path-traversal sequences like ../ / ..\ in any of the three positions including the version.
  • The constructor's empty-version allowance is covered by a dedicated test.

All 92 tests in Java.Interop.Tools.Maven-Tests pass.

Benefit to downstream

<AndroidMavenLibrary> (whose MavenExtensions.TryParseArtifactWithVersion currently only splits on :) gets stricter validation for free as soon as it picks up this version of Java.Interop.Tools.Maven.

Context: https://github.com/dotnet/android (the `<AndroidMavenLibrary>`
MSBuild item relies on `Artifact`/`Artifact.TryParse` to validate Maven
coordinates supplied by users, but today the constructor blindly assigns
its fields and `TryParse` only checks for three colon-separated parts.
Empty strings, whitespace, and characters that are illegal per the Maven
coordinate spec all slip through.

Add structural validation to `Artifact`:

  * `groupId` and `artifactId` must match `[A-Za-z0-9_\-.]+` per
    https://maven.apache.org/pom.html#Maven_Coordinates.
  * `version` rejects whitespace, `:`, and path separators (`/`, `\`).
    Maven versions are otherwise permissive, so no further parsing.
  * The constructor throws `ArgumentNullException` / `ArgumentException`
    with the offending parameter and value.
  * `TryParse` returns `false` for any invalid input (and requires all
    three parts to be non-empty) without throwing.
  * `Parse` continues to throw `ArgumentException` with the existing
    message format.

The constructor still allows an empty `version` so that existing internal
callers - `Dependency.ToArtifact ()` and `Project.TryGetParentPomArtifact ()` -
can keep producing partial coordinates when a POM omits `<version>` and
inherits it from a parent. `TryParse`/`Parse` (the user-input path) still
require a non-empty version.

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings June 22, 2026 16:14

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.

Pull request overview

This PR strengthens Java.Interop.Tools.Maven.Models.Artifact so it properly validates Maven coordinates (groupId/artifactId/version) when constructed or parsed, preventing malformed or potentially unsafe user-supplied coordinates from slipping through (notably for downstream consumers like dotnet/android’s <AndroidMavenLibrary>).

Changes:

  • Added validation to Artifact constructor for groupId/artifactId character set and for version (rejecting whitespace, :, and path separators), while still allowing an empty constructor version for internal “inherited version” scenarios.
  • Updated Artifact.TryParse to reject invalid inputs (including null and empty parts) without throwing.
  • Added NUnit tests covering valid/invalid coordinates and the constructor’s empty-version allowance.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/Java.Interop.Tools.Maven-Tests/ArtifactTests.cs Adds comprehensive tests for Artifact ctor/TryParse/Parse validation behavior.
src/Java.Interop.Tools.Maven/Models/Artifact.cs Implements stricter structural validation for Maven coordinates and updates parsing behavior.

Comment thread src/Java.Interop.Tools.Maven/Models/Artifact.cs
@jonathanpeppers jonathanpeppers added the ready-to-review This PR is ready to review/merge, thanks! label Jun 22, 2026
@jonathanpeppers
jonathanpeppers merged commit 7049364 into main Jun 23, 2026
2 checks passed
@jonathanpeppers
jonathanpeppers deleted the jonathanpeppers-validate-maven-artifact branch June 23, 2026 14:14
jonathanpeppers added a commit that referenced this pull request Aug 21, 2026
…1498)

Supersedes #1495 with the complete release-applicable Maven backport as exactly one commit on top of the Java.Interop commit pinned by dotnet/android `release/10.0.1xx` (`33992194b9373e9322244612c94ed9941b9bc2fd`).

Backports:

- #1480: assert resolved Maven cache paths remain under `CacheDirectory`
- #1479: validate Maven artifact coordinates
- Route Maven integration-test resolvers through `dotnet-public-maven`
- Remove java-source-utils' project-local `mavenCentral()` and use Android's shared approved repository settings when embedded, with an equivalent standalone Java.Interop fallback
- Coupled Maven hardening tests

The Kotlin Gradle fixture from Android main is intentionally excluded because it does not exist at this release pin.

Validation:

- Java.Interop.Tools.Maven tests: 106 passed, 0 failed
- java-source-utils assembly: succeeded
- java-source-utils standalone CI-mode settings evaluation: succeeded
- Fresh-cache java-source-utils `compileJava`: succeeded entirely through `dotnet-public-maven`
- java-source-utils legacy suite: 16 passed before two existing missing-resource NPEs (`JavaType.java`) unrelated to repository routing

Feed prerequisite: the release JavaParser `3.18.0` dependency graph must be mirrored into `dotnet-public-maven` for anonymous CI resolution. The complete graph has been seeded and fresh-cache resolution is confirmed.

Co-authored-by: Copilot App <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

ready-to-review This PR is ready to review/merge, thanks!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants