Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Commit

Permalink
lock jsVM to prevent concurrent map writes in otto
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucaber committed Jan 11, 2024
1 parent 802c5f9 commit 9668168
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion auth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"io"
"net/http"
"strings"
"sync"
"time"

"github.com/dgrijalva/jwt-go"
Expand All @@ -49,7 +50,8 @@ type AuthenticationHandler struct {

expCache *cache.Cache

jsVM *otto.Otto
jsVM *otto.Otto
jsVMLock *sync.Mutex
}

type JWTResponse struct {
Expand All @@ -72,6 +74,7 @@ func NewAuthenticationHandler(
logger: logger,
verifier: verifier,
expCache: cache.New(cache.NoExpiration, 5*time.Minute),
jsVMLock: &sync.Mutex{},
}

if cfg.ProviderConfig.PreAuthenticationHook != "" {
Expand Down Expand Up @@ -118,17 +121,21 @@ func (h *AuthenticationHandler) Authenticate(username string, password string, a
requestURL := h.config.ProviderConfig.Url + "/authenticate"

if h.hookPreAuth != nil {
h.jsVMLock.Lock()
_, err := h.jsVM.Run(h.hookPreAuth)
if err != nil {
h.jsVMLock.Unlock()
return nil, err
}

export, _ := h.jsVM.Get("exports")
if !export.IsFunction() {
h.jsVMLock.Unlock()
return nil, fmt.Errorf("hook script must export a function!")
}

hookResult, err := export.Call(otto.UndefinedValue(), username, password, additionalBodyProperties)
h.jsVMLock.Unlock()
if err != nil {
return nil, fmt.Errorf("error while calling hook function: %s", err.Error())
}
Expand Down

0 comments on commit 9668168

Please sign in to comment.