Skip to content

Commit

Permalink
Create a session on ReverseProxy and ensure that ReverseProxy users c…
Browse files Browse the repository at this point in the history
…annot change username

ReverseProxy users should generate a session on reverse proxy username change.

Also prevent ReverseProxy users from changing their username.

Fix go-gitea#2407

Signed-off-by: Andrew Thornton <[email protected]>
  • Loading branch information
zeripath committed Apr 6, 2021
1 parent 1ba8b95 commit ffb3f41
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
20 changes: 15 additions & 5 deletions modules/auth/sso/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/web/middleware"

gouuid "github.com/google/uuid"
)
Expand Down Expand Up @@ -68,13 +69,22 @@ func (r *ReverseProxy) VerifyAuthData(req *http.Request, w http.ResponseWriter,

user, err := models.GetUserByName(username)
if err != nil {
if models.IsErrUserNotExist(err) && r.isAutoRegisterAllowed() {
return r.newUser(req)
if !models.IsErrUserNotExist(err) || r.isAutoRegisterAllowed() {
log.Error("GetUserByName: %v", err)
return nil
}
log.Error("GetUserByName: %v", err)
return nil
user = r.newUser(req)
}

// Make sure requests to API paths and PWA resources do not create a new session
if !middleware.IsAPIPath(req) && !isAttachmentDownload(req) && !isGitOrLFSPath(req) {
if sess.Get("uid").(int64) != user.ID {
handleSignIn(w, req, sess, user)
}
}
log.Info("Setting IsReverseProxy")
store.GetData()["IsReverseProxy"] = true

return user
}

Expand Down Expand Up @@ -102,13 +112,13 @@ func (r *ReverseProxy) newUser(req *http.Request) *models.User {
user := &models.User{
Name: username,
Email: email,
Passwd: username,
IsActive: true,
}
if err := models.CreateUser(user); err != nil {
// FIXME: should I create a system notice?
log.Error("CreateUser: %v", err)
return nil
}

return user
}
17 changes: 16 additions & 1 deletion modules/auth/sso/sso.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"fmt"
"net/http"
"reflect"
"regexp"
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/web/middleware"
)

Expand All @@ -27,8 +29,8 @@ import (
// for users that have already signed in.
var ssoMethods = []SingleSignOn{
&OAuth2{},
&Session{},
&ReverseProxy{},
&Session{},
&Basic{},
}

Expand Down Expand Up @@ -98,6 +100,19 @@ func isAttachmentDownload(req *http.Request) bool {
return strings.HasPrefix(req.URL.Path, "/attachments/") && req.Method == "GET"
}

var gitPathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/(?:git-(?:(?:upload)|(?:receive))-pack$)|(?:info/refs$)|(?:HEAD$)|(?:objects/)`)
var lfsPathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/info/lfs/`)

func isGitOrLFSPath(req *http.Request) bool {
if gitPathRe.MatchString(req.URL.Path) {
return true
}
if setting.LFS.StartServer {
return lfsPathRe.MatchString(req.URL.Path)
}
return false
}

// handleSignIn clears existing session variables and stores new ones for the specified user object
func handleSignIn(resp http.ResponseWriter, req *http.Request, sess SessionStore, user *models.User) {
_ = sess.Delete("openid_verified_uri")
Expand Down
3 changes: 3 additions & 0 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,9 @@ func Contexter() func(next http.Handler) http.Handler {
} else {
ctx.Data["SignedUserID"] = int64(0)
ctx.Data["SignedUserName"] = ""

// ensure the session uid is deleted
_ = ctx.Session.Delete("uid")
}

ctx.Resp.Header().Set(`X-Frame-Options`, `SAMEORIGIN`)
Expand Down
4 changes: 2 additions & 2 deletions templates/user/settings/profile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<span class="text red hide" id="name-change-prompt"> {{.i18n.Tr "settings.change_username_prompt"}}</span>
<span class="text red hide" id="name-change-redirect-prompt"> {{.i18n.Tr "settings.change_username_redirect_prompt"}}</span>
</label>
<input id="username" name="name" value="{{.SignedUser.Name}}" data-name="{{.SignedUser.Name}}" autofocus required {{if not .SignedUser.IsLocal}}disabled{{end}}>
{{if not .SignedUser.IsLocal}}
<input id="username" name="name" value="{{.SignedUser.Name}}" data-name="{{.SignedUser.Name}}" autofocus required {{if or (not .SignedUser.IsLocal) .IsReverseProxy}}disabled{{end}}>
{{if or (not .SignedUser.IsLocal) .IsReverseProxy}}
<p class="help text blue">{{$.i18n.Tr "settings.password_username_disabled"}}</p>
{{end}}
</div>
Expand Down

0 comments on commit ffb3f41

Please sign in to comment.