Fix crash with terraform modules using host:port sources#14541
Merged
Conversation
When a Terraform module source includes a port in the hostname (e.g., registry.terraform.io:443/terraform-aws-modules/vpc/aws), the RegistryClient#url_for method crashed with URI::InvalidComponentError because URI#host= doesn't accept host:port format. Fix by splitting the hostname into host and port components before assigning them to the URI separately. Fixes #12782 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a Terraform registry client crash when module sources include host:port by correctly separating host and port when constructing service URLs, and adds regression coverage.
Changes:
- Update
RegistryClient#url_forto splithostnameintohostandportbefore assigning toURI. - Add a registry client spec covering
hostnamevalues that include a port. - Add a Terraform fixture + parser spec asserting
registry_hostnamepreserves the port from registry sources.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| terraform/lib/dependabot/terraform/registry_client.rb | Fix URI construction for registries configured as host:port. |
| terraform/spec/dependabot/terraform/registry_client_spec.rb | Regression spec ensuring service discovery + module version fetch uses the configured port. |
| terraform/spec/dependabot/terraform/file_parser_spec.rb | Spec ensuring registry module sources with ports are parsed into registry_hostname correctly. |
| terraform/spec/fixtures/projects/registry_with_port/main.tf | Fixture project containing a module source with registry.terraform.io:443/.... |
Instead of naive string splitting, use URI.parse("https://#{hostname}")
to properly handle IPv6 literals and reject invalid port values.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes #12782
When a Terraform module source includes a port in the hostname (e.g.,
registry.terraform.io:443/terraform-aws-modules/vpc/aws), theRegistryClient#url_formethod crashes withURI::InvalidComponentError.Why
Ruby's
URI#host=only accepts a hostname, nothost:port. Theurl_formethod was assigning the fullhostnamestring (which may include a port) directly touri.host:How
Split the hostname into host and port components before assigning them to the URI:
This is the minimal fix — it keeps
hostnameas-is throughout the class (preserving token lookup inheaders_for) and only splits when constructing URIs.Local verification
Reproduced the issue end-to-end with the Dependabot CLI using a local repo containing the exact manifest from the issue:
Before (published image):
After (rebuilt image with fix):