Skip to content

Commit

Permalink
fix(routing/http): avoid escaping comma separator
Browse files Browse the repository at this point in the history
The comma is in the "sub-delims" set of characters that don't need to be
encoded in most parts of a URL, including query parameters. Golang
standard library percent-escapes it for consistency, but we prefer
human-readable /routing/v1 URLs, and real comma is restored here to
ensure human and machine requests hit the same HTTP cache keys.
  • Loading branch information
lidel committed Oct 10, 2024
1 parent ceb514c commit 003cf53
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ The following emojis are used to highlight certain changes:

### Fixed

- `routing/http/client`: optional address and protocol filter parameters from [IPIP-484](https://github.com/ipfs/specs/pull/484) use `,` instead of `%2C`.

### Security

## [v0.24.0]
Expand Down
8 changes: 7 additions & 1 deletion routing/http/filters/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ func AddFiltersToURL(baseURL string, protocolFilter, addrFilter []string) string
query.Set("filter-addrs", strings.Join(addrFilter, ","))
}

parsedURL.RawQuery = query.Encode()
// The comma is in the "sub-delims" set of characters that don't need to be
// encoded in most parts of a URL, including query parameters. Golang
// standard library percent-escapes it for consistency, but we prefer
// human-readable /routing/v1 URLs, and real comma is restored here to
// ensure human and machine requests hit the same HTTP cache keys.
parsedURL.RawQuery = strings.ReplaceAll(query.Encode(), "%2C", ",")

return parsedURL.String()
}

Expand Down
6 changes: 3 additions & 3 deletions routing/http/filters/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ func TestAddFiltersToURL(t *testing.T) {
baseURL: "https://example.com/routing/v1/providers/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
protocolFilter: []string{"transport-bitswap", "transport-ipfs-gateway-http"},
addrFilter: nil,
expected: "https://example.com/routing/v1/providers/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi?filter-protocols=transport-bitswap%2Ctransport-ipfs-gateway-http",
expected: "https://example.com/routing/v1/providers/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi?filter-protocols=transport-bitswap,transport-ipfs-gateway-http",
},
{
name: "Only addr filter",
baseURL: "https://example.com/routing/v1/providers/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
protocolFilter: nil,
addrFilter: []string{"ip4", "ip6"},
expected: "https://example.com/routing/v1/providers/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi?filter-addrs=ip4%2Cip6",
expected: "https://example.com/routing/v1/providers/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi?filter-addrs=ip4,ip6",
},
{
name: "Both filters",
baseURL: "https://example.com/routing/v1/providers/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
protocolFilter: []string{"transport-bitswap", "transport-graphsync-filecoinv1"},
addrFilter: []string{"ip4", "ip6"},
expected: "https://example.com/routing/v1/providers/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi?filter-addrs=ip4%2Cip6&filter-protocols=transport-bitswap%2Ctransport-graphsync-filecoinv1",
expected: "https://example.com/routing/v1/providers/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi?filter-addrs=ip4,ip6&filter-protocols=transport-bitswap,transport-graphsync-filecoinv1",
},
{
name: "URL with existing query parameters",
Expand Down

0 comments on commit 003cf53

Please sign in to comment.