-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add protected fields to settings to mitigate high-severity vulne…
…rability Credits to @jorgectf for the advisories.
- Loading branch information
Showing
3 changed files
with
61 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,57 @@ | ||
package system | ||
|
||
import ( | ||
"github.com/0xJacky/Nginx-UI/api" | ||
"github.com/0xJacky/Nginx-UI/settings" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
"github.com/0xJacky/Nginx-UI/api" | ||
"github.com/0xJacky/Nginx-UI/settings" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
"reflect" | ||
) | ||
|
||
func GetSettings(c *gin.Context) { | ||
c.JSON(http.StatusOK, gin.H{ | ||
"server": settings.ServerSettings, | ||
"nginx": settings.NginxSettings, | ||
"openai": settings.OpenAISettings, | ||
}) | ||
c.JSON(http.StatusOK, gin.H{ | ||
"server": settings.ServerSettings, | ||
"nginx": settings.NginxSettings, | ||
"openai": settings.OpenAISettings, | ||
}) | ||
} | ||
|
||
func SaveSettings(c *gin.Context) { | ||
var json struct { | ||
Server settings.Server `json:"server"` | ||
Nginx settings.Nginx `json:"nginx"` | ||
Openai settings.OpenAI `json:"openai"` | ||
} | ||
|
||
if !api.BindAndValid(c, &json) { | ||
return | ||
} | ||
|
||
settings.ServerSettings = json.Server | ||
settings.NginxSettings = json.Nginx | ||
settings.OpenAISettings = json.Openai | ||
|
||
settings.ReflectFrom() | ||
|
||
err := settings.Save() | ||
if err != nil { | ||
api.ErrHandler(c, err) | ||
return | ||
} | ||
var json struct { | ||
Server settings.Server `json:"server"` | ||
Nginx settings.Nginx `json:"nginx"` | ||
Openai settings.OpenAI `json:"openai"` | ||
} | ||
|
||
if !api.BindAndValid(c, &json) { | ||
return | ||
} | ||
|
||
// todo: omit protected fields when binding | ||
fillSettings(&settings.ServerSettings, &json.Server) | ||
fillSettings(&settings.NginxSettings, &json.Nginx) | ||
fillSettings(&settings.OpenAISettings, &json.Openai) | ||
|
||
settings.ReflectFrom() | ||
|
||
err := settings.Save() | ||
if err != nil { | ||
api.ErrHandler(c, err) | ||
return | ||
} | ||
|
||
GetSettings(c) | ||
} | ||
|
||
GetSettings(c) | ||
func fillSettings(targetSettings interface{}, newSettings interface{}) { | ||
s := reflect.TypeOf(targetSettings).Elem() | ||
vt := reflect.ValueOf(targetSettings).Elem() | ||
vn := reflect.ValueOf(newSettings).Elem() | ||
|
||
// copy the values from new to target settings if it is not protected | ||
for i := 0; i < s.NumField(); i++ { | ||
if s.Field(i).Tag.Get("protected") != "true" { | ||
vt.Field(i).Set(vn.Field(i)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters