From f826093d9a431e9c6e014a51cae7b9e9d5278b9a Mon Sep 17 00:00:00 2001 From: gammazero <11790789+gammazero@users.noreply.github.com> Date: Wed, 28 Aug 2024 09:08:44 -0700 Subject: [PATCH 1/2] Preserve hostname specified with --api in http request headers - Replaces PR #10233 - Add test to check for hostname in HTTP header --- cmd/ipfs/kubo/start.go | 5 ++++- docs/changelogs/v0.30.md | 5 +++++ test/sharness/t0235-cli-request.sh | 6 +++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cmd/ipfs/kubo/start.go b/cmd/ipfs/kubo/start.go index cae1e2c1b70..474045e7102 100644 --- a/cmd/ipfs/kubo/start.go +++ b/cmd/ipfs/kubo/start.go @@ -303,7 +303,10 @@ func makeExecutor(req *cmds.Request, env interface{}) (cmds.Executor, error) { } // Resolve the API addr. - apiAddr, err = resolveAddr(req.Context, apiAddr) + // + // Do not replace apiAddr with the resolved addr so that the requested + // hostname is kept for use in the request's HTTP header. + _, err = resolveAddr(req.Context, apiAddr) if err != nil { return nil, err } diff --git a/docs/changelogs/v0.30.md b/docs/changelogs/v0.30.md index e7f5d8b3f23..c59c9a0fcdd 100644 --- a/docs/changelogs/v0.30.md +++ b/docs/changelogs/v0.30.md @@ -13,6 +13,7 @@ - [`/unix/` socket support in `Addresses.API`](#unix-socket-support-in-addressesapi) - [Cleaned Up `ipfs daemon` Startup Log](#cleaned-up-ipfs-daemon-startup-log) - [UnixFS 1.5: Mode and Modification Time Support](#unixfs-15-mode-and-modification-time-support) + - [Commands Preserve Specified Hostname](#commands-preserve-specified-hostname) - [๐Ÿ“ Changelog](#-changelog) - [๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Contributors](#-contributors) @@ -129,6 +130,10 @@ Modification time support was also added to the Gateway. If present, value from > [!NOTE] > Storing `mode` and `mtime` requires root block to be `dag-pb` and disabled `raw-leaves` setting to create envelope for storing the metadata. +#### Commands Preserve Specified Hostname + +When executing a comman, if a hostname is specified by `--api=/dns4//` the resulting HTTP request now contains the hostname, instead of the the IP address that the hostname resolved to, as was the previous behavior. This makes it easier for those trying to run kubo behind a reverse proxy using hostname-based rules. + ### ๐Ÿ“ Changelog ### ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Contributors diff --git a/test/sharness/t0235-cli-request.sh b/test/sharness/t0235-cli-request.sh index 3b2281894ad..02ef514dedf 100755 --- a/test/sharness/t0235-cli-request.sh +++ b/test/sharness/t0235-cli-request.sh @@ -28,7 +28,7 @@ test_expect_success "start nc" ' ' test_expect_success "can make http request against nc server" ' - ipfs cat /ipfs/Qmabcdef --api /ip4/127.0.0.1/tcp/5005 & + ipfs cat /ipfs/Qmabcdef --api /dns4/localhost/tcp/5005 & IPFSPID=$! # handle request for /api/v0/version @@ -80,4 +80,8 @@ test_expect_success "api flag does not appear in request" ' test_expect_code 1 grep "api=/ip4" nc_out ' +test_expect_success "host has dns name not ip address" ' + grep "Host: localhost:5005" nc_out +' + test_done From ae0c200364abd6b788b1880541da775b7f05785c Mon Sep 17 00:00:00 2001 From: Andrew Gillis <11790789+gammazero@users.noreply.github.com> Date: Wed, 28 Aug 2024 09:34:27 -0700 Subject: [PATCH 2/2] Update docs/changelogs/v0.30.md Co-authored-by: Marcin Rataj --- docs/changelogs/v0.30.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changelogs/v0.30.md b/docs/changelogs/v0.30.md index c59c9a0fcdd..4c22e40ba11 100644 --- a/docs/changelogs/v0.30.md +++ b/docs/changelogs/v0.30.md @@ -132,7 +132,7 @@ Modification time support was also added to the Gateway. If present, value from #### Commands Preserve Specified Hostname -When executing a comman, if a hostname is specified by `--api=/dns4//` the resulting HTTP request now contains the hostname, instead of the the IP address that the hostname resolved to, as was the previous behavior. This makes it easier for those trying to run kubo behind a reverse proxy using hostname-based rules. +When executing a [CLI command](https://docs.ipfs.tech/reference/kubo/cli/) over [Kubo RPC API](https://docs.ipfs.tech/reference/kubo/rpc/), if a hostname is specified by `--api=/dns4//` the resulting HTTP request now contains the hostname, instead of the the IP address that the hostname resolved to, as was the previous behavior. This makes it easier for those trying to run Kubo behind a reverse proxy using hostname-based rules. ### ๐Ÿ“ Changelog