Skip to content

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

Open
jonathanpeppers wants to merge 3 commits into
mainfrom
jonathanpeppers-validate-maven-artifact
Open

[Java.Interop.Tools.Maven] Validate Artifact coordinates#1479
jonathanpeppers wants to merge 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 <223556219+Copilot@users.noreply.github.com>
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
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jonathanpeppers jonathanpeppers added the ready-to-review This PR is ready to review/merge, thanks! label Jun 22, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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