Skip to content

Commit

Permalink
fix(cli): preserve hostname specified with --api in http request head…
Browse files Browse the repository at this point in the history
…ers (#10497)

Preserve hostname specified with --api in http request headers

- Replaces PR #10233
- Add test to check for hostname in HTTP header
- Update docs/changelogs/v0.30.md

(cherry picked from commit 5fe9604)
  • Loading branch information
gammazero authored and lidel committed Aug 28, 2024
1 parent 2b245e0 commit 3bb09f8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
5 changes: 4 additions & 1 deletion cmd/ipfs/kubo/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions docs/changelogs/v0.30.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -128,6 +129,10 @@ Opt-in support for `mode` and `mtime` was also added to MFS (`ipfs files --help`
> [!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 [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/<domain>/` 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
6 changes: 5 additions & 1 deletion test/sharness/t0235-cli-request.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 3bb09f8

Please sign in to comment.