Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aws lb cookie settings for session id #244

Merged
merged 1 commit into from
May 22, 2024
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
17 changes: 17 additions & 0 deletions core/vm/contracts_suave.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ func (c *consoleLogPrecompile) Run(input []byte) ([]byte, error) {
return nil, nil
}

var contextCookieKeyPrefix = "__cookie_"

func (s *suaveRuntime) doHTTPRequest(request types.HttpRequest) ([]byte, error) {
if request.Method != "GET" && request.Method != "POST" {
return nil, fmt.Errorf("only GET and POST methods are supported")
Expand All @@ -232,6 +234,13 @@ func (s *suaveRuntime) doHTTPRequest(request types.HttpRequest) ([]byte, error)
return nil, err
}

// add any cookies stored in the context
for key, val := range s.suaveContext.Context {
if strings.HasPrefix(key, contextCookieKeyPrefix) {
req.Header.Add("Cookie", string(val))
}
}

for _, header := range request.Headers {
indx := strings.Index(header, ":")
if indx == -1 {
Expand Down Expand Up @@ -269,6 +278,14 @@ func (s *suaveRuntime) doHTTPRequest(request types.HttpRequest) ([]byte, error)
if resp.StatusCode > 299 {
return nil, fmt.Errorf("http error: %s: %v", resp.Status, data)
}

// parse the LB cookies (AWSALB, AWSALBCORS) and set them in the context
for _, cookie := range resp.Cookies() {
if cookie.Name == "AWSALB" || cookie.Name == "AWSALBCORS" {
s.suaveContext.Context[contextCookieKeyPrefix+cookie.Name] = []byte(cookie.String())
}
}

return data, nil
}

Expand Down
54 changes: 54 additions & 0 deletions core/vm/contracts_suave_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,57 @@ func TestSuave_HttpRequest_FlashbotsSignatue(t *testing.T) {
_, err := s.doHTTPRequest(req)
require.NoError(t, err)
}

func TestSuave_HttpRequest_Cookies(t *testing.T) {
cookies := map[string]http.Cookie{
"AWSALB": {Name: "AWSALB", Value: "value1"},
"AWSALBCORS": {Name: "AWSALBCORS", Value: "value2"},
"OTHER": {Name: "OTHER", Value: "value3"},
}

firstCall := true
srv := httptest.NewServer(&httpTestHandler{
fn: func(w http.ResponseWriter, r *http.Request) {
if firstCall {
firstCall = false
for _, c := range cookies {
http.SetCookie(w, &c)
}
} else {
// check the cookies in the second call
for _, c := range r.Cookies() {
if val, found := cookies[c.Name]; found {
require.Equal(t, val.Value, c.Value)
}
}
}
w.Write([]byte("ok"))
},
})

s := &suaveRuntime{
suaveContext: &SuaveContext{
Context: map[string][]byte{},
Backend: &SuaveExecutionBackend{
ExternalWhitelist: []string{"127.0.0.1"},
ServiceAliasRegistry: map[string]string{"goerli": srv.URL},
},
},
}

defer srv.Close()

req := types.HttpRequest{Url: srv.URL, Method: "GET"}
_, err := s.doHTTPRequest(req)
require.NoError(t, err)

// validate the only the AWS cookies are stored
require.Len(t, s.suaveContext.Context, 2)
for key, val := range s.suaveContext.Context {
require.True(t, strings.HasPrefix(key, contextCookieKeyPrefix))
require.Contains(t, []string{"AWSALB", "AWSALBCORS"}, strings.Split(string(val), "=")[0])
}

_, err = s.doHTTPRequest(req)
require.NoError(t, err)
}
Loading