Fix URI#host= to wrap IPv6 address in brackets#16164
Fix URI#host= to wrap IPv6 address in brackets#16164straight-shoota merged 8 commits intocrystal-lang:masterfrom
URI#host= to wrap IPv6 address in brackets#16164Conversation
as the square brackets are only required for URIs
|
let me know if there is interest in this patch and I'll add some specs |
|
This is related to #12798: URI currently allows invalid path and invalid host values, and we should validate or fix them. For consistency, we should always wrap IPv6 addresses, including the |
|
@straight-shoota updated with a new host setter and specs implemented |
URI#host= to wrap IPv6 address in brackets
|
I looked at how URI in Ruby behaves, and only uri.host = "::1" # => URI::InvalidComponentError
uri.host = "[::1]" # => hostname=::1 host=[::1]
uri.hostname = "::1" # => hostname=::1 host=[::1]
uri.hostname = "[::1]" # => hostname=::1 host=[::1]IMO this behavior makes sense. |
|
That's a good point. Our Then we'd need to figure out if/how this should be available in the constructor, though. Maybe we could add an overload with a named parameter Raising on invalid values is directly related to #12798. I'd keep that out of this PR. |
|
does the behavior make sense? No one who uses the library will care if their IPv6 address is formatted correctly and no one wants to have their app raise an error because they didn't look closely enough at the two ways to set the host component. Put it this way, all existing apps that don't manually wrap IPv6 addresses will still be broken if an IPv6 address is provided if we raise on unwrapped IPv6 (as per my example code) - just differently broken (breaking change) |
|
Hum, I guess we should have never imported the But that would be a breaking change 😭 |
|
Wow this took me back - much nostalgia: |
|
I noticed this is breaking a specs in crystal-sqlite3, https://github.com/crystal-lang/crystal-sqlite3/actions/runs/18456712142/job/52579325805
Is the right thing to do to update crystal-sqlite3? |
|
@bcardiff Good question. This patch assumes that a host that includes a Maybe use |
|
Yeah, We should try to improve this situation, though. For example we could make the condition more strict to look check that the host value is actually an IPv6 pattern. Or maybe |
|
Switching to |
The square brackets around IPv6 addresses are only required for URIs
We found this issue when migrating a service to run on IPv6 - this change would make the code compatible with either IPv4 or IPv6 without additional logic changes to the service
This ensures a URI initialized with otherwise valid data is a valid URI