Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions multinode/sanitize_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package multinode

import "regexp"

// sanitizedError redacts the message of an error while preserving the original for chain traversal.
type sanitizedError struct {
err error // original error, retained for chain traversal/classification
msg string // redacted, safe-to-display message
}

func (e sanitizedError) Error() string {
return e.msg
}

// Unwrap and Cause expose the original error so both the standard library
// (errors.Is/errors.As) and pkg/errors (pkgerrors.Cause) can still traverse to
// the underlying RPC error. This is required for error classification — e.g.
// extracting the JSON-RPC error via pkgerrors.Cause(err) — and for sentinel
// matching such as context.DeadlineExceeded.
//
// Only the Error() string is redacted. That is the value logged and returned to
// workflow users, which is where the provider URL/API key was leaking.
func (e sanitizedError) Unwrap() error {
return e.err
}

func (e sanitizedError) Cause() error {
return e.err
}

// rpcURLRegexp matches full HTTP(S)/WS(S) URLs so provider URLs, paths, query params, credentials, and API keys can be redacted from errors.
var rpcURLRegexp = regexp.MustCompile(`(?i)(?:https?|wss?)://[^\s"']+`)

// SanitizeRPCError returns an error whose message has all URLs replaced with "[REDACTED URL]".
// The original error remains reachable via errors.Is/errors.As and pkgerrors.Cause.
// Returns nil if err is nil.
func SanitizeRPCError(err error) error {
if err == nil {
return nil
}
return sanitizedError{
err: err,
msg: rpcURLRegexp.ReplaceAllString(err.Error(), "[REDACTED URL]"),
}
}
70 changes: 70 additions & 0 deletions multinode/sanitize_error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package multinode

import (
"context"
"fmt"
"testing"

pkgerrors "github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

func TestSanitizeRPCError(t *testing.T) {
t.Parallel()

tests := []struct {
name string
err error
want string
}{
{
name: "https url with path",
err: fmt.Errorf(`Post "https://test-cl-03.test.xyz/dOnOtLeAkAnyOfThis/doNotLeak/test/mainnet/": %w`, context.DeadlineExceeded),
want: `Post "[REDACTED URL]": context deadline exceeded`,
},
{
name: "http url with path",
err: fmt.Errorf(`Post "http://secret.com/doNotLeak/": %w`, context.DeadlineExceeded),
want: `Post "[REDACTED URL]": context deadline exceeded`,
},
{
name: "ws url with path",
err: fmt.Errorf(`dial "ws://secret.com/doNotLeak/": %w`, context.DeadlineExceeded),
want: `dial "[REDACTED URL]": context deadline exceeded`,
},
{
name: "wss url with path",
err: fmt.Errorf(`dial "wss://secret.com/doNotLeak/": %w`, context.DeadlineExceeded),
want: `dial "[REDACTED URL]": context deadline exceeded`,
},
{
name: "https url with user info",
err: fmt.Errorf(`Post "https://user:pass@secret.com/": %w`, context.DeadlineExceeded),
want: `Post "[REDACTED URL]": context deadline exceeded`,
},
{
name: "multiple urls",
err: fmt.Errorf(`primary http://secret.com/doNotLeak/ fallback wss://secret.com/doNotLeak/ failed: %w`, context.DeadlineExceeded),
want: `primary [REDACTED URL] fallback [REDACTED URL] failed: context deadline exceeded`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
sanitized := SanitizeRPCError(tt.err)

require.EqualError(t, sanitized, tt.want)
require.NotContains(t, sanitized.Error(), "doNotLeak")
require.NotContains(t, sanitized.Error(), "user:pass")
require.NotContains(t, sanitized.Error(), "secret.com")
require.ErrorIs(t, sanitized, context.DeadlineExceeded)
require.Equal(t, tt.err, pkgerrors.Cause(sanitized))
})
}
}

func TestSanitizeRPCError_Nil(t *testing.T) {
t.Parallel()
require.NoError(t, SanitizeRPCError(nil))
}
Loading