Skip to content

Commit

Permalink
make URL parameters case-insensitive (#141)
Browse files Browse the repository at this point in the history
Make URL parameters case-insensitive
Add relevant tests
  • Loading branch information
odysseus0 authored May 29, 2024
1 parent a7b0c21 commit 1b2a3a1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
22 changes: 18 additions & 4 deletions server/url_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ type URLParameters struct {
fast bool
}

// normalizeQueryParams takes a URL and returns a map of query parameters with all keys normalized to lowercase.
// This helps in making the parameter extraction case-insensitive.
func normalizeQueryParams(url *url.URL) map[string][]string {
normalizedQuery := make(map[string][]string)
for key, values := range url.Query() {
normalizedKey := strings.ToLower(key)
normalizedQuery[normalizedKey] = values
}
return normalizedQuery
}

// ExtractParametersFromUrl extracts the auction preference from the url query
// Allowed query params:
// - hint: mev share hints, can be set multiple times, default: hash, special_logs
Expand All @@ -42,8 +53,11 @@ func ExtractParametersFromUrl(url *url.URL, allBuilders []string) (params URLPar
if strings.HasPrefix(url.Path, "/fast") {
params.fast = true
}
// Normalize all query parameters to lowercase keys
normalizedQuery := normalizeQueryParams(url)

var hint []string
hintQuery, ok := url.Query()["hint"]
hintQuery, ok := normalizedQuery["hint"]
if ok {
if len(hintQuery) == 0 {
return params, ErrEmptyHintQuery
Expand All @@ -61,15 +75,15 @@ func ExtractParametersFromUrl(url *url.URL, allBuilders []string) (params URLPar
}
params.pref.Privacy.Hints = hint

originIdQuery, ok := url.Query()["originId"]
originIdQuery, ok := normalizedQuery["originid"]
if ok {
if len(originIdQuery) == 0 {
return params, ErrIncorrectOriginId
}
params.originId = originIdQuery[0]
}

targetBuildersQuery, ok := url.Query()["builder"]
targetBuildersQuery, ok := normalizedQuery["builder"]
if ok {
if len(targetBuildersQuery) == 0 {
return params, ErrEmptyTargetBuilderQuery
Expand All @@ -81,7 +95,7 @@ func ExtractParametersFromUrl(url *url.URL, allBuilders []string) (params URLPar
params.pref.Privacy.Builders = allBuilders
}

refundAddressQuery, ok := url.Query()["refund"]
refundAddressQuery, ok := normalizedQuery["refund"]
if ok {
if len(refundAddressQuery) == 0 {
return params, ErrIncorrectRefundQuery
Expand Down
24 changes: 24 additions & 0 deletions server/url_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ func TestExtractAuctionPreferenceFromUrl(t *testing.T) {
},
err: nil,
},
"origin id common spelling 1": {
url: "https://rpc.flashbots.net?originid=123",
want: URLParameters{
pref: types.PrivateTxPreferences{
Privacy: types.TxPrivacyPreferences{Hints: []string{"hash", "special_logs"}},
Validity: types.TxValidityPreferences{},
},
prefWasSet: false,
originId: "123",
},
err: nil,
},
"origin id common spelling 2": {
url: "https://rpc.flashbots.net?originID=123",
want: URLParameters{
pref: types.PrivateTxPreferences{
Privacy: types.TxPrivacyPreferences{Hints: []string{"hash", "special_logs"}},
Validity: types.TxValidityPreferences{},
},
prefWasSet: false,
originId: "123",
},
err: nil,
},
"target builder": {
url: "https://rpc.flashbots.net?builder=builder1&builder=builder2",
want: URLParameters{
Expand Down

0 comments on commit 1b2a3a1

Please sign in to comment.