refact: simplify hop route normalization and remove excessive defensive checks
Lee Yunjin committed
May 12, 2026 at 10:57 UTC
7acbe6cbe3d4bdd0b0a910464cc1eedeed3be78f
1 file changed
+6
-19
portal/auth/hop_route.go
+6
-19
@@ -3,7 +3,6 @@ package auth
3
import (
4
"errors"
5
"fmt"
6
- "strings"
6
"time"
7
8
"github.com/gosuda/portal-tunnel/v2/types"
@@ -15,21 +14,16 @@ var ErrHopRouteSignatureInvalid = errors.New("hop route signature is invalid")
14
func SignHopRoute(method string, route types.HopRoute, identity types.Identity, expiresAt time.Time) (types.HopRoute, error) {
15
route.ExpiresAt = expiresAt.UTC()
16
route.Signature = ""
18
- route.OwnerPublicKey = ""
17
+ route.OwnerPublicKey = identity.PublicKey
18
20
- route, err := normalizeHopRoute(route, false)
21
- if err != nil {
22
- return types.HopRoute{}, err
23
- }
24
- identity, err = utils.NormalizeStoredIdentity(identity)
19
+ route, err := normalizeHopRoute(route, true)
20
if err != nil {
21
return types.HopRoute{}, err
22
}
28
- if strings.TrimSpace(identity.PrivateKey) == "" || strings.TrimSpace(identity.PublicKey) == "" {
23
+ if identity.PrivateKey == "" || identity.PublicKey == "" {
24
return types.HopRoute{}, errors.New("hop route owner identity is required")
25
}
26
32
- route.OwnerPublicKey = identity.PublicKey
27
payload, err := types.HopRouteBytes(method, route)
28
if err != nil {
29
return types.HopRoute{}, err
@@ -42,7 +36,7 @@ func SignHopRoute(method string, route types.HopRoute, identity types.Identity,
36
}
37
38
func VerifyHopRoute(method string, route types.HopRoute) (types.HopRoute, error) {
45
- signature := strings.TrimSpace(route.Signature)
39
+ signature := route.Signature
40
route.Signature = ""
41
42
route, err := normalizeHopRoute(route, true)
@@ -61,9 +55,8 @@ func VerifyHopRoute(method string, route types.HopRoute) (types.HopRoute, error)
55
}
56
57
func normalizeHopRoute(route types.HopRoute, requireOwner bool) (types.HopRoute, error) {
64
- ownerPublicKey := strings.ToLower(utils.TrimHexPrefix(strings.TrimSpace(route.OwnerPublicKey)))
65
- if ownerPublicKey != "" {
66
- if _, err := utils.ParseSecp256k1PublicKeyHex(ownerPublicKey); err != nil {
58
+ if route.OwnerPublicKey != "" {
59
+ if _, err := utils.ParseSecp256k1PublicKeyHex(route.OwnerPublicKey); err != nil {
60
return types.HopRoute{}, fmt.Errorf("hop route owner public key: %w", err)
61
}
62
} else if requireOwner {
@@ -75,15 +68,9 @@ func normalizeHopRoute(route types.HopRoute, requireOwner bool) (types.HopRoute,
68
return types.HopRoute{}, fmt.Errorf("hop relay url: %w", err)
69
}
70
78
- route.OwnerPublicKey = ownerPublicKey
71
route.RelayURL = relayURL
72
route.PublicHostname = utils.NormalizeHostname(route.PublicHostname)
73
route.RouteHostname = utils.NormalizeHostname(route.RouteHostname)
82
- route.HostnameHash = strings.TrimSpace(route.HostnameHash)
83
- route.MatchToken = strings.TrimSpace(route.MatchToken)
84
- route.Metadata = route.Metadata.Copy()
85
- route.ForwardToken = strings.TrimSpace(route.ForwardToken)
74
route.ExpiresAt = route.ExpiresAt.UTC()
87
- route.Signature = strings.TrimSpace(route.Signature)
75
return route, nil
76
}