Skip to content

Conversation

@pepicrft
Copy link

@pepicrft pepicrft commented Dec 4, 2025

Summary

Add validation for source control URLs at multiple levels to handle malformed URLs gracefully.

Problem

When macOS git-credential-osxkeychain fails to find credentials, it outputs error messages that can get concatenated with the original URL:

https://github.com/owner/repo.git': failed looking up identity for https://github.com/owner/repo.git

These malformed URLs were being sent to the registry server, causing errors. This was observed in the Tuist registry where we added server-side validation to return HTTP 400 for invalid URLs (see tuist/tuist#8829).

Solution

1. Add isValid property to SourceControlURL

public var isValid: Bool {
    // URLs with whitespace are invalid
    guard !self.urlString.contains(where: \.isWhitespace) else {
        return false
    }
    // Check for standard URL format or SSH-style git URLs
    ...
}

Valid source control URLs:

  • Don't contain whitespace (error messages contain spaces)
  • Are parseable as standard URLs with a host, OR
  • Match SSH-style git format (user@host:path)

2. Early validation in mapRegistryIdentity

guard url.isValid else {
    throw RegistryError.invalidSourceControlURL(url)
}

This validates URLs before making any HTTP requests to the registry.

3. Handle HTTP 400 from registry servers

case 400:
    throw RegistryError.invalidSourceControlURL(scmURL)

If a URL passes client validation but the server still rejects it with 400 Bad Request, we throw the same specific error.

Changes

  • Sources/Basics/SourceControlURL.swift: Added isValid computed property
  • Sources/Workspace/Workspace+Registry.swift: Added early URL validation
  • Sources/PackageRegistry/RegistryClient.swift:
    • Added invalidSourceControlURL error case
    • Handle 400 response in lookupIdentities
  • Tests/BasicsTests/SourceControlURLTests.swift: Added tests for URL validation
  • Tests/PackageRegistryTests/RegistryClientTests.swift: Added test for 400 handling

Test Plan

  • SourceControlURLTests - Tests for isValid property (4 tests)
  • LookupIdentities - Tests for registry client including 400 handling (6 tests)
  • All tests pass (10 tests total)
  • Build succeeds

Related

🤖 Generated with Claude Code

// Git credential errors often produce messages like:
// "'https://github.com/owner/repo.git': failed looking up identity for ..."
// which can get concatenated with the URL.
guard let url = scmURL.url,
Copy link
Contributor

Choose a reason for hiding this comment

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

wouldn't a better fix be to catch the errors when forming the scmURL over handling it here?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I'm adjusting that. I also added some code to handle 400 coming from the server.

@pepicrft pepicrft force-pushed the fix/validate-scm-url-before-registry-lookup branch from 943bf5f to 732de76 Compare December 4, 2025 16:32
@pepicrft pepicrft changed the title Validate source control URL before registry identity lookup Handle 400 Bad Request for invalid source control URLs in registry lookup Dec 4, 2025
@pepicrft pepicrft force-pushed the fix/validate-scm-url-before-registry-lookup branch from 732de76 to 641e363 Compare December 4, 2025 16:34
@pepicrft pepicrft changed the title Handle 400 Bad Request for invalid source control URLs in registry lookup Handle invalid source control URLs in registry identity lookup Dec 4, 2025
Add validation for source control URLs at multiple levels:

1. Add `isValid` property to `SourceControlURL` that checks:
   - URL doesn't contain whitespace (indicates malformed URL)
   - URL is parseable as a standard URL with a host, OR
   - URL matches SSH-style git format (user@host:path)

2. Validate URLs early in `mapRegistryIdentity` before making
   registry requests

3. Handle HTTP 400 responses from the registry server by throwing
   `RegistryError.invalidSourceControlURL`

This handles cases where malformed URLs (e.g., containing git credential
error messages like "'URL': failed looking up identity...") are passed
to the registry.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@pepicrft pepicrft force-pushed the fix/validate-scm-url-before-registry-lookup branch from 641e363 to a1a3f98 Compare December 4, 2025 16:41
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.

2 participants