Skip to content

fix: Ensure trailing slash is added to source URIs added via gem sources#9055

Open
zirni wants to merge 6 commits intoruby:masterfrom
zirni:fix-gem-sources-trailing-slash
Open

fix: Ensure trailing slash is added to source URIs added via gem sources#9055
zirni wants to merge 6 commits intoruby:masterfrom
zirni:fix-gem-sources-trailing-slash

Conversation

@zirni
Copy link

@zirni zirni commented Nov 5, 2025

GitHub's private gem registry expects the first path segment after the host
to represent the namespace, typically the organization or user name. [1]

When adding a source with

  gem sources --add https://user:password@rubygems.pkg.github.com/my-org

without a trailing slash, the last path segment ("my-org") is interpreted as a
file and removed during relative path resolution. This causes the resulting
URI to become

  https://user:password@rubygems.pkg.github.com/gems/foo.gem

instead of the correct

  https://user:password@rubygems.pkg.github.com/my-org/gems/foo.gem. [2]

Example error:

  gem source -a https://user:password@rubygems.pkg.github.com/my-org
  gem install -rf foo.gem
  rubygems/remote_fetcher.rb:238:in `fetch_http': bad response Not Found 404 (https://user:REDACTED@rubygems.pkg.github.com/gems/foo-0.7.1.gem) (Gem::RemoteFetcher::FetchError)

Although this behavior complies with RFC 2396, it's incompatible with GitHub's
gem registry requirements.

The remote fetcher is just append a relative path without using ./ [3]

To address this, we automatically append a trailing slash when adding new gem
sources.

As illustrated in [4] and [5], given the base URI

  http://a/b/c/d;p?q

and a relative path

  g/f

the resolution process replaces "d;p?q" and yields

  http://a/b/c/g/f

[1] https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-rubygems-registry#authenticating-with-a-personal-access-token
[2] https://github.com/ruby/rubygems/blob/master/lib/rubygems/vendor/uri/lib/uri/generic.rb#L1053
[3] https://github.com/ruby/rubygems/blob/master/lib/rubygems/remote_fetcher.rb#L148
[4] https://www.rfc-editor.org/rfc/rfc2396#section-5.2
[5] https://www.rfc-editor.org/rfc/rfc2396#appendix-C

@hsbt hsbt force-pushed the fix-gem-sources-trailing-slash branch from a9fbf4c to 7a78385 Compare February 13, 2026 03:12
zirni and others added 4 commits March 17, 2026 17:00
GitHub's private gem registry expects the first path segment after the host
to represent the namespace, typically the organization or user name. [1]

When adding a source with
```
  gem sources --add https://user:password@rubygems.pkg.github.com/my-org
```

without a trailing slash, the last path segment ("my-org") is interpreted as a
file and removed during relative path resolution. This causes the resulting
URI to become
```
  https://user:password@rubygems.pkg.github.com/gems/foo.gem
```

instead of the correct
```
  https://user:password@rubygems.pkg.github.com/my-org/gems/foo.gem. [2]
```

Example error:
```
  gem source -a https://user:password@rubygems.pkg.github.com/my-org
  gem install -rf foo.gem
  rubygems/remote_fetcher.rb:238:in `fetch_http': bad response Not Found 404 (https://user:REDACTED@rubygems.pkg.github.com/gems/foo-0.7.1.gem) (Gem::RemoteFetcher::FetchError)
```

Although this behavior complies with RFC 2396, it's incompatible with GitHub's
gem registry requirements.

The remote fetcher is just append a relative path without using ./ [3]

To address this, we automatically append a trailing slash when adding new gem
sources.

As illustrated in [4] and [5], given the base URI
```
  http://a/b/c/d;p?q
```
and a relative path
```
  g/f
```
the resolution process replaces "d;p?q" and yields
```
  http://a/b/c/g/f
```

[1] https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-rubygems-registry#authenticating-with-a-personal-access-token
[2] https://github.com/ruby/rubygems/blob/master/lib/rubygems/vendor/uri/lib/uri/generic.rb#L1053
[3] https://github.com/ruby/rubygems/blob/master/lib/rubygems/remote_fetcher.rb#L148
[4] https://www.rfc-editor.org/rfc/rfc2396#section-5.2
[5] https://www.rfc-editor.org/rfc/rfc2396#appendix-C
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
When sources are added with `add_trailing_slash`, the stored URI gets a
trailing slash. Without the same normalization on remove, users cannot
delete a source using the original URI they typed (without trailing slash).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@hsbt hsbt force-pushed the fix-gem-sources-trailing-slash branch from 7a78385 to fd55c7e Compare March 17, 2026 08:04
hsbt and others added 2 commits March 17, 2026 17:26
The setup_fake_source helper registered spec data at URLs like
"https://host/path//specs.4.8.gz" (double slash) when the URI had
a trailing slash. This caused load_specs to fail silently in some
tests, which were unintentionally relying on that failure.

Fixes:
- Normalize trailing slash in setup_fake_source to prevent double-slash
- Fix test_execute_add/append_https_rubygems_org: these now correctly
  succeed since load_specs can find the spec data
- Fix test_execute_prepend_without_trailing_slash: prepend adds to
  the front of the source list, not the end
- Fix test_execute_remove_redundant_source_trailing_slash: path-less
  URIs like "http://sample.repo" are not modified by add_trailing_slash

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@hsbt
Copy link
Member

hsbt commented Mar 17, 2026

@kou I fixed some issues of this PR. Could you review this again?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants