Skip to content

Commit 20b1cbe

Browse files
authored
[Solana] Fix: solana-client allowed header only set on solana requests (#426)
## Summary Fix: solana-client allowed header only set on solana requests ## Issue `solana-client` should only be set in `Access-Control-Allow-Headers` if the request is targeted at Solana. - Issue or PR: #{ISSUE_OR_PR_NUMBER} ## Type of change Select one or more from the following: - [ ] New feature, functionality or library - [x] Bug fix - [ ] Code health or cleanup - [ ] Documentation - [ ] Other (specify) ## QoS Checklist ### E2E Validation & Tests - [ ] `make path_up` - [ ] `make test_e2e_evm_shannon` ### Observability - [x] 1. `make path_up` - [ ] 2. Run the following E2E test: `make test_request__shannon_relay_util_100` - [ ] 3. View results in LocalNet's [PATH Relay Grafana Dashboard](http://localhost:3003/d/relays/path-service-requests) ## Sanity Checklist - [ ] I have updated the GitHub Issue `assignees`, `reviewers`, `labels`, `project`, `iteration` and `milestone` - [ ] For docs, I have run `make docusaurus_start` - [ ] For code, I have run `make test_all` - [ ] For configurations, I have updated the documentation - [ ] I added `TODO`s where applicable
1 parent fc09015 commit 20b1cbe

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

router/router.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ import (
1818
"github.com/buildwithgrove/path/request"
1919
)
2020

21+
// TODO_TECHDEBT(@adshmh): Make this configurable.
22+
// DEV_NOTE: This MUST be updated if Solana service's ID changes or if new Solana services with different IDs are staked.
23+
const (
24+
// Used to determine if solana-specific headers need to be set on the response.
25+
serviceIDSolana = "solana"
26+
)
27+
2128
type (
2229
router struct {
2330
logger polylog.Logger
@@ -113,18 +120,37 @@ func methodCheckMiddleware(next http.HandlerFunc) http.HandlerFunc {
113120

114121
// TODO_IMPROVE: gather the CORS config from the config YAML
115122
func (r *router) corsMiddleware(next http.HandlerFunc) http.HandlerFunc {
116-
return func(w http.ResponseWriter, r *http.Request) {
117-
origin := r.Header.Get("Origin")
123+
return func(w http.ResponseWriter, req *http.Request) {
124+
origin := req.Header.Get("Origin")
118125
w.Header().Set("Access-Control-Allow-Origin", origin)
119126
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT")
120-
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, solana-client")
121-
if r.Method == "OPTIONS" {
127+
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
128+
129+
// Add service-specific allowed headers.
130+
// As of PR #424, this only applies to Solana service.
131+
for _, allowedHeader := range r.getCORSServiceAllowedHeaders(req) {
132+
w.Header().Add("Access-Control-Allow-Headers", allowedHeader)
133+
}
134+
135+
if req.Method == "OPTIONS" {
122136
// Handle preflight request, which is necessary for CORS to work.
123137
w.WriteHeader(http.StatusOK)
124138
return
125139
}
126-
next(w, r)
140+
next(w, req)
141+
}
142+
}
143+
144+
// TODO_TECHDEBT(@adshmh): Make this configurable.
145+
// DEV_NOTE: This MUST be updated if Solana service's ID changes or if new Solana services with different IDs are staked.
146+
func (r *router) getCORSServiceAllowedHeaders(req *http.Request) []string {
147+
svcID := req.Header.Get(request.HTTPHeaderTargetServiceID)
148+
// Solana-specific Allowed HTTP headers
149+
if svcID == serviceIDSolana {
150+
return []string{"solana-client"}
127151
}
152+
153+
return nil
128154
}
129155

130156
// TODO_TECHDEBT: Rename removeGrovePortalPrefixMiddleware to removePrefixMiddleware

0 commit comments

Comments
 (0)