Skip to content

Commit 135ce67

Browse files
committed
Fixed issue with concurrent map reads and writes causing crashes
1 parent c766c6c commit 135ce67

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

src/tools/proxy.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ import (
1111
"net/url"
1212
"strconv"
1313
"strings"
14+
"sync"
1415
)
1516

1617
var originalUrlResolver = make(map[string]*url.URL)
18+
var mutex = &sync.Mutex{}
1719

1820
// ProxyRequestHandler intercepts requests to CodeArtifact and add the Authorization header + correct Host header
1921
func ProxyRequestHandler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
2022
return func(w http.ResponseWriter, r *http.Request) {
23+
24+
mutex.Lock()
2125
// Store the original host header for each request
2226
originalUrlResolver[r.RemoteAddr] = r.URL
2327
originalUrlResolver[r.RemoteAddr].Host = r.Host
@@ -28,6 +32,7 @@ func ProxyRequestHandler(p *httputil.ReverseProxy) func(http.ResponseWriter, *ht
2832
} else {
2933
originalUrlResolver[r.RemoteAddr].Scheme = "http"
3034
}
35+
mutex.Unlock()
3136

3237
// Override the Host header with the CodeArtifact Host
3338
u, _ := url.Parse(CodeArtifactAuthInfo.Url)
@@ -51,8 +56,10 @@ func ProxyResponseHandler() func(*http.Response) error {
5156

5257
contentType := r.Header.Get("Content-Type")
5358

59+
mutex.Lock()
5460
originalUrl := originalUrlResolver[r.Request.RemoteAddr]
5561
delete(originalUrlResolver, r.Request.RemoteAddr)
62+
mutex.Unlock()
5663

5764
u, _ := url.Parse(CodeArtifactAuthInfo.Url)
5865
hostname := u.Host + ":443"

0 commit comments

Comments
 (0)