Skip to content
Draft
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
6 changes: 5 additions & 1 deletion internal/bootstrap/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ func Start() {
if conf.Conf.Scheme.UnixFile != "" {
fmt.Printf("start unix server @ %s\n", conf.Conf.Scheme.UnixFile)
utils.Log.Infof("start unix server @ %s", conf.Conf.Scheme.UnixFile)
unixSrv = &http.Server{Handler: httpHandler}
unixHandler := httpHandler
if conf.Conf.Scheme.UnixFileTrusted {
unixHandler = middlewares.UnixFileTrusted(unixHandler)
}
unixSrv = &http.Server{Handler: unixHandler}
go func() {
listener, err := net.Listen("unix", conf.Conf.Scheme.UnixFile)
if err != nil {
Expand Down
21 changes: 11 additions & 10 deletions internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ type Meilisearch struct {
}

type Scheme struct {
Address string `json:"address" env:"ADDR"`
HttpPort int `json:"http_port" env:"HTTP_PORT"`
HttpsPort int `json:"https_port" env:"HTTPS_PORT"`
ForceHttps bool `json:"force_https" env:"FORCE_HTTPS"`
CertFile string `json:"cert_file" env:"CERT_FILE"`
KeyFile string `json:"key_file" env:"KEY_FILE"`
UnixFile string `json:"unix_file" env:"UNIX_FILE"`
UnixFilePerm string `json:"unix_file_perm" env:"UNIX_FILE_PERM"`
EnableH2c bool `json:"enable_h2c" env:"ENABLE_H2C"`
EnableH3 bool `json:"enable_h3" env:"ENABLE_H3"`
Address string `json:"address" env:"ADDR"`
HttpPort int `json:"http_port" env:"HTTP_PORT"`
HttpsPort int `json:"https_port" env:"HTTPS_PORT"`
ForceHttps bool `json:"force_https" env:"FORCE_HTTPS"`
CertFile string `json:"cert_file" env:"CERT_FILE"`
KeyFile string `json:"key_file" env:"KEY_FILE"`
UnixFile string `json:"unix_file" env:"UNIX_FILE"`
UnixFileTrusted bool `json:"unix_file_trusted" env:"UNIX_FILE_TRUSTED"`
UnixFilePerm string `json:"unix_file_perm" env:"UNIX_FILE_PERM"`
EnableH2c bool `json:"enable_h2c" env:"ENABLE_H2C"`
EnableH3 bool `json:"enable_h3" env:"ENABLE_H3"`
}

type LogConfig struct {
Expand Down
38 changes: 38 additions & 0 deletions server/middlewares/auth.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package middlewares

import (
"context"
"crypto/subtle"
"net/http"

"github.com/OpenListTeam/OpenList/v4/internal/conf"
"github.com/OpenListTeam/OpenList/v4/internal/model"
Expand All @@ -16,6 +18,9 @@ import (
// if token is empty, set user to guest
func Auth(allowDisabledGuest bool) func(c *gin.Context) {
return func(c *gin.Context) {
if authenticateUnixFileTrusted(c) {
return
}
token := c.GetHeader("Authorization")
if subtle.ConstantTimeCompare([]byte(token), []byte(setting.GetStr(conf.Token))) == 1 {
admin, err := op.GetAdmin()
Expand Down Expand Up @@ -76,6 +81,9 @@ func Auth(allowDisabledGuest bool) func(c *gin.Context) {
}

func Authn(c *gin.Context) {
if authenticateUnixFileTrusted(c) {
return
}
token := c.GetHeader("Authorization")
if subtle.ConstantTimeCompare([]byte(token), []byte(setting.GetStr(conf.Token))) == 1 {
admin, err := op.GetAdmin()
Expand Down Expand Up @@ -129,6 +137,36 @@ func Authn(c *gin.Context) {
c.Next()
}

func authenticateUnixFileTrusted(c *gin.Context) bool {
if !IsUnixFileTrusted(c) {
return false
}
admin, err := op.GetAdmin()
if err != nil {
common.ErrorResp(c, err, 500)
c.Abort()
return true
}
common.GinAppendValues(c, conf.UserKey, admin)
log.Debug("use admin identity for trusted Unix socket request")
c.Next()
return true
}

type unixFileTrustedKey struct{}

func UnixFileTrusted(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), unixFileTrustedKey{}, true)
next.ServeHTTP(w, r.WithContext(ctx))
})
}

func IsUnixFileTrusted(c *gin.Context) bool {
trusted, _ := c.Request.Context().Value(unixFileTrustedKey{}).(bool)
return conf.Conf.Scheme.UnixFileTrusted && trusted
}

func AuthNotGuest(c *gin.Context) {
user := c.Request.Context().Value(conf.UserKey).(*model.User)
if user.IsGuest() {
Expand Down
2 changes: 1 addition & 1 deletion server/middlewares/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Down(verifyFunc func(string, string) error) func(c *gin.Context) {
}
common.GinAppendValues(c, conf.MetaKey, meta)
// verify sign
if needSign(meta, rawPath) {
if !IsUnixFileTrusted(c) && needSign(meta, rawPath) {
s := c.Query("sign")
err = verifyFunc(rawPath, strings.TrimSuffix(s, "/"))
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions server/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ func ServeWebDAV(c *gin.Context) {
}

func WebDAVAuth(c *gin.Context) {
if middlewares.IsUnixFileTrusted(c) {
admin, err := op.GetAdmin()
if err != nil {
common.ErrorResp(c, err, http.StatusInternalServerError)
c.Abort()
return
}
common.GinAppendValues(c, conf.UserKey, admin)
c.Next()
return
}
// check count of login
ip := c.ClientIP()
guest, _ := op.GetGuest()
Expand Down