Unify browser bridge request normalization

This commit is contained in:
Joe Julian
2026-04-12 06:48:12 -07:00
parent 57870ca4f1
commit a02d4a3b1c
3 changed files with 24 additions and 18 deletions
+1 -5
View File
@@ -117,11 +117,7 @@ func runNativeMessage() error {
} }
func dialBridge(ctx context.Context, req browserbridge.Request) (*grpc.ClientConn, *browserbridge.GRPCClient, context.Context, error) { func dialBridge(ctx context.Context, req browserbridge.Request) (*grpc.ClientConn, *browserbridge.GRPCClient, context.Context, error) {
connCfg, err := req.Connection() return browserbridge.DialRequest(ctx, req)
if err != nil {
return nil, nil, nil, err
}
return browserbridge.Dial(ctx, connCfg)
} }
func defaultBinaryPath() (string, error) { func defaultBinaryPath() (string, error) {
+9 -4
View File
@@ -140,16 +140,21 @@ func WriteResponse(w io.Writer, resp Response) error {
} }
func (r Request) Connection() (Connection, error) { func (r Request) Connection() (Connection, error) {
conn := Connection{ return normalizeConnection(Connection{
GRPCAddress: strings.TrimSpace(r.GRPCAddress), GRPCAddress: strings.TrimSpace(r.GRPCAddress),
BearerToken: strings.TrimSpace(r.BearerToken), BearerToken: strings.TrimSpace(r.BearerToken),
} })
if conn.GRPCAddress == "" { }
func normalizeConnection(conn Connection) (Connection, error) {
if strings.TrimSpace(conn.GRPCAddress) == "" {
conn.GRPCAddress = grpcaddr.Default(runtime.GOOS) conn.GRPCAddress = grpcaddr.Default(runtime.GOOS)
} }
if conn.BearerToken == "" { if strings.TrimSpace(conn.BearerToken) == "" {
return Connection{}, fmt.Errorf("browser bridge bearer token is required") return Connection{}, fmt.Errorf("browser bridge bearer token is required")
} }
conn.GRPCAddress = strings.TrimSpace(conn.GRPCAddress)
conn.BearerToken = strings.TrimSpace(conn.BearerToken)
return conn, nil return conn, nil
} }
+14 -9
View File
@@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"net" "net"
"runtime"
"strings" "strings"
"git.julianfamily.org/keepassgo/internal/grpcaddr" "git.julianfamily.org/keepassgo/internal/grpcaddr"
@@ -18,14 +17,20 @@ type GRPCClient struct {
client keepassgov1.VaultServiceClient client keepassgov1.VaultServiceClient
} }
func DialRequest(ctx context.Context, req Request) (*grpc.ClientConn, *GRPCClient, context.Context, error) {
conn, err := req.Connection()
if err != nil {
return nil, nil, nil, err
}
return Dial(ctx, conn)
}
func Dial(ctx context.Context, conn Connection) (*grpc.ClientConn, *GRPCClient, context.Context, error) { func Dial(ctx context.Context, conn Connection) (*grpc.ClientConn, *GRPCClient, context.Context, error) {
if strings.TrimSpace(conn.GRPCAddress) == "" { normalized, err := normalizeConnection(conn)
conn.GRPCAddress = grpcaddr.Default(runtime.GOOS) if err != nil {
return nil, nil, nil, err
} }
if strings.TrimSpace(conn.BearerToken) == "" { network, endpoint, err := grpcaddr.Parse(normalized.GRPCAddress)
return nil, nil, nil, fmt.Errorf("browser bridge bearer token is required")
}
network, endpoint, err := grpcaddr.Parse(conn.GRPCAddress)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
@@ -40,9 +45,9 @@ func Dial(ctx context.Context, conn Connection) (*grpc.ClientConn, *GRPCClient,
}), }),
) )
if err != nil { if err != nil {
return nil, nil, nil, fmt.Errorf("dial gRPC host %s: %w", strings.TrimSpace(conn.GRPCAddress), err) return nil, nil, nil, fmt.Errorf("dial gRPC host %s: %w", normalized.GRPCAddress, err)
} }
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+strings.TrimSpace(conn.BearerToken)) ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+normalized.BearerToken)
return grpcConn, &GRPCClient{client: keepassgov1.NewVaultServiceClient(grpcConn)}, ctx, nil return grpcConn, &GRPCClient{client: keepassgov1.NewVaultServiceClient(grpcConn)}, ctx, nil
} }