From a59388990f124a920e7a6807f6640bb73184c8ba Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Tue, 6 Dec 2022 21:01:29 +0100 Subject: [PATCH] fix(identify): ascii-only versions --- core/commands/id.go | 6 +++--- core/commands/stat_dht.go | 9 +++++++-- core/commands/swarm.go | 3 ++- test/sharness/t0026-id.sh | 23 +++++++++++++---------- version.go | 21 +++++++++++++++++++-- 5 files changed, 44 insertions(+), 18 deletions(-) diff --git a/core/commands/id.go b/core/commands/id.go index 887f93efe311..f8d6cbb1b4a1 100644 --- a/core/commands/id.go +++ b/core/commands/id.go @@ -176,18 +176,18 @@ func printPeer(keyEnc ke.KeyEncoder, ps pstore.Peerstore, p peer.ID) (interface{ protocols, _ := ps.GetProtocols(p) // don't care about errors here. for _, p := range protocols { - info.Protocols = append(info.Protocols, string(p)) + info.Protocols = append(info.Protocols, version.TrimVersion(string(p))) } sort.Strings(info.Protocols) if v, err := ps.Get(p, "ProtocolVersion"); err == nil { if vs, ok := v.(string); ok { - info.ProtocolVersion = vs + info.ProtocolVersion = version.TrimVersion(vs) } } if v, err := ps.Get(p, "AgentVersion"); err == nil { if vs, ok := v.(string); ok { - info.AgentVersion = vs + info.AgentVersion = version.TrimVersion(vs) } } diff --git a/core/commands/stat_dht.go b/core/commands/stat_dht.go index e6006e439d3b..54a2a8477974 100644 --- a/core/commands/stat_dht.go +++ b/core/commands/stat_dht.go @@ -6,6 +6,7 @@ import ( "text/tabwriter" "time" + version "github.com/ipfs/kubo" cmdenv "github.com/ipfs/kubo/core/commands/cmdenv" cmds "github.com/ipfs/go-ipfs-cmds" @@ -92,7 +93,9 @@ This interface is not stable and may change from release to release. info := dhtPeerInfo{ID: p.String()} if ver, err := nd.Peerstore.Get(p, "AgentVersion"); err == nil { - info.AgentVersion, _ = ver.(string) + if vs, ok := ver.(string); ok { + info.AgentVersion = version.TrimVersion(vs) + } } else if err == pstore.ErrNotFound { // ignore } else { @@ -143,7 +146,9 @@ This interface is not stable and may change from release to release. info := dhtPeerInfo{ID: pi.Id.String()} if ver, err := nd.Peerstore.Get(pi.Id, "AgentVersion"); err == nil { - info.AgentVersion, _ = ver.(string) + if vs, ok := ver.(string); ok { + info.AgentVersion = version.TrimVersion(vs) + } } else if err == pstore.ErrNotFound { // ignore } else { diff --git a/core/commands/swarm.go b/core/commands/swarm.go index 1508efcb8a8f..9e6d53c67120 100644 --- a/core/commands/swarm.go +++ b/core/commands/swarm.go @@ -13,6 +13,7 @@ import ( "time" files "github.com/ipfs/go-ipfs-files" + version "github.com/ipfs/kubo" "github.com/ipfs/kubo/commands" "github.com/ipfs/kubo/config" "github.com/ipfs/kubo/core/commands/cmdenv" @@ -284,7 +285,7 @@ var swarmPeersCmd = &cmds.Command{ } for _, s := range strs { - ci.Streams = append(ci.Streams, streamInfo{Protocol: string(s)}) + ci.Streams = append(ci.Streams, streamInfo{Protocol: version.TrimVersion(string(s))}) } } sort.Sort(&ci) diff --git a/test/sharness/t0026-id.sh b/test/sharness/t0026-id.sh index 5d6d3db094a4..ae6650db89da 100755 --- a/test/sharness/t0026-id.sh +++ b/test/sharness/t0026-id.sh @@ -23,7 +23,8 @@ test_id_compute_agent() { fi AGENT_VERSION="$AGENT_VERSION$AGENT_SUFFIX" fi - echo "$AGENT_VERSION" + # limit of 64 characters: https://github.com/ipfs/kubo/pull/9465 + echo -n "$AGENT_VERSION" | head -c 64 } test_expect_success "checking AgentVersion" ' @@ -52,20 +53,22 @@ test_expect_success "checking and converting ID of a random peer while offline" ' # agent-version-suffix (local, offline) -test_launch_ipfs_daemon --agent-version-suffix=test-suffix -test_expect_success "checking AgentVersion with suffix (local)" ' - test_id_compute_agent test-suffix > expected-agent-version && - ipfs id -f "\n" > actual-agent-version && - test_cmp expected-agent-version actual-agent-version +test_launch_ipfs_daemon --agent-version-suffix=test-suffix-ąę-123456789012345678901234567890 +test_expect_success "checking AgentVersion with suffix (local, only ascii chars)" ' + test_id_compute_agent "test-suffix--1234567890123456789" > expected && + ipfs id -f "" > actual && + test_should_not_contain "ą" actual && + test_cmp expected actual ' # agent-version-suffix (over libp2p identify protocol) iptb testbed create -type localipfs -count 2 -init -startup_cluster 2 --agent-version-suffix=test-suffix-identify +startup_cluster 2 --agent-version-suffix=test-suffix-identify-óź-123456789012345678901234567890 test_expect_success "checking AgentVersion with suffix (fetched via libp2p identify protocol)" ' - ipfsi 0 id -f "\n" > expected-identify-agent-version && - ipfsi 1 id "$(ipfsi 0 config Identity.PeerID)" -f "\n" > actual-libp2p-identify-agent-version && - test_cmp expected-identify-agent-version actual-libp2p-identify-agent-version + test_id_compute_agent "test-suffix-identify--1234567890" > expected && + ipfsi 1 id "$(ipfsi 0 config Identity.PeerID)" -f "" > actual && + test_should_not_contain "ó" actual && + test_cmp expected actual ' test_kill_ipfs_daemon diff --git a/version.go b/version.go index 2762442eeab1..26c5a7d04ac3 100644 --- a/version.go +++ b/version.go @@ -2,6 +2,7 @@ package ipfs import ( "fmt" + "regexp" "runtime" "github.com/ipfs/kubo/repo/fsrepo" @@ -15,6 +16,8 @@ const CurrentVersionNumber = "0.18.0-dev" const ApiVersion = "/kubo/" + CurrentVersionNumber + "/" //nolint +const maxVersionLen = 64 + // GetUserAgentVersion is the libp2p user agent used by go-ipfs. // // Note: This will end in `/` when no commit is available. This is expected. @@ -26,13 +29,27 @@ func GetUserAgentVersion() string { } userAgent += userAgentSuffix } - return userAgent + return TrimVersion(userAgent) } var userAgentSuffix string +var onlyASCII = regexp.MustCompile("[[:^ascii:]]") func SetUserAgentSuffix(suffix string) { - userAgentSuffix = suffix + userAgentSuffix = TrimVersion(suffix) +} + +func TrimVersion(version string) string { + ascii := onlyASCII.ReplaceAllLiteralString(version, "") + chars := 0 + for i := range ascii { + if chars >= maxVersionLen { + ascii = ascii[:i] + break + } + chars++ + } + return ascii } type VersionInfo struct {