Skip to content

Commit

Permalink
errorFilteredFallbackClient: avoids leaking URL in fallback client
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoximenes committed Jan 29, 2025
1 parent 7db00ed commit 096a778
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions arbitrum/apibackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/big"
"net/url"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -51,6 +52,22 @@ type APIBackend struct {
sync SyncProgressBackend
}

type errorFilteredFallbackClient struct {
impl types.FallbackClient
url string
}

func (c *errorFilteredFallbackClient) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
err := c.impl.CallContext(ctx, result, method, args...)
if err != nil && strings.Contains(err.Error(), c.url) {
// avoids leaking the URL in the error message.
// URL can contain sensitive information such as API keys.
log.Warn("fallback client error", "error", err)
return errors.New("Failed to call fallback API")
}
return err
}

type timeoutFallbackClient struct {
impl types.FallbackClient
timeout time.Duration
Expand All @@ -77,12 +94,22 @@ func CreateFallbackClient(fallbackClientUrl string, fallbackClientTimeout time.D
types.SetFallbackError(strings.Join(fields, ":"), int(errNumber))
return nil, nil
}

var fallbackClient types.FallbackClient
var err error
fallbackClient, err = rpc.Dial(fallbackClientUrl)
if err != nil {
return nil, fmt.Errorf("failed creating fallback connection: %w", err)
}
url, err := url.Parse(fallbackClientUrl)
if err != nil {
return nil, fmt.Errorf("failed parsing fallback URL: %w", err)
}
fallbackClient = &errorFilteredFallbackClient{
impl: fallbackClient,
url: url.String(),
}

if fallbackClientTimeout != 0 {
fallbackClient = &timeoutFallbackClient{
impl: fallbackClient,
Expand Down

0 comments on commit 096a778

Please sign in to comment.