mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2026-11-24 07:42:32 +00:00
Backported fixes from another similar projecT
This commit is contained in:
1 parent
72e4a78fbf
commit
2b3bef186c
21 files changed
+225
-35
No files matched your search
@@ -201,9 +201,23 @@ func newTokenLDAP(w http.ResponseWriter, r *http.Request, payload tokenPayload)
|
||||
// Route: POST /auth/refresh
|
||||
func RefreshToken() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO: Use your own methods to verify an existing user is
|
||||
// able to refresh their token and then give them a new one
|
||||
userObj, _ := user.GetByEmail("jc@jc21.com")
|
||||
userID, ok := r.Context().Value(c.UserIDCtxKey).(uint)
|
||||
if !ok {
|
||||
h.ResultErrorJSON(w, r, http.StatusUnauthorized, "Unauthorised", nil)
|
||||
return
|
||||
}
|
||||
|
||||
userObj, userErr := user.GetByID(userID)
|
||||
if userErr != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusUnauthorized, "Unauthorised", nil)
|
||||
return
|
||||
}
|
||||
|
||||
if userObj.IsDisabled {
|
||||
h.ResultErrorJSON(w, r, http.StatusUnauthorized, errors.ErrUserDisabled.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
if response, err := njwt.Generate(&userObj, false); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusInternalServerError, err.Error(), nil)
|
||||
} else {
|
||||
|
||||
@@ -12,6 +12,11 @@ import (
|
||||
|
||||
const defaultLimit = 10
|
||||
|
||||
// maxLimit caps ?limit= on every paginated list endpoint - without a cap, a
|
||||
// client could ask for an arbitrarily large page and force the server to
|
||||
// pull an unbounded result set in one query.
|
||||
const maxLimit = 500
|
||||
|
||||
func getPageInfoFromRequest(r *http.Request) (model.PageInfo, error) {
|
||||
pageInfo := model.PageInfo{}
|
||||
var err error
|
||||
@@ -99,6 +104,9 @@ func getPagination(r *http.Request) (int, int, error) {
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
if limit > maxLimit {
|
||||
limit = maxLimit
|
||||
}
|
||||
|
||||
return offset, limit, nil
|
||||
}
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const something = "something"
|
||||
|
||||
func TestGetPageInfoFromRequest(t *testing.T) {
|
||||
t.Run("basic test", func(t *testing.T) {
|
||||
r := httptest.NewRequest(http.MethodGet, "/hosts", nil)
|
||||
@@ -28,6 +30,25 @@ func TestGetPageInfoFromRequest(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetPageInfoFromRequestClampsLimit(t *testing.T) {
|
||||
// Without this clamp, ?limit= passed straight through to the DB query
|
||||
// unbounded would let a client force an arbitrarily large result set -
|
||||
// see maxLimit's own doc comment.
|
||||
r := httptest.NewRequest(http.MethodGet, "/hosts?limit=999999999", nil)
|
||||
p, err := getPageInfoFromRequest(r)
|
||||
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, maxLimit, p.Limit)
|
||||
}
|
||||
|
||||
func TestGetPageInfoFromRequestAllowsLimitUnderTheCap(t *testing.T) {
|
||||
r := httptest.NewRequest(http.MethodGet, "/hosts?limit=200", nil)
|
||||
p, err := getPageInfoFromRequest(r)
|
||||
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, 200, p.Limit)
|
||||
}
|
||||
|
||||
func TestGetQueryVarInt(t *testing.T) {
|
||||
type want struct {
|
||||
val int
|
||||
@@ -45,7 +66,7 @@ func TestGetQueryVarInt(t *testing.T) {
|
||||
{
|
||||
name: "simple default",
|
||||
url: "/hosts",
|
||||
queryVar: "something",
|
||||
queryVar: something,
|
||||
required: false,
|
||||
defaultValue: 100,
|
||||
want: want{
|
||||
@@ -56,7 +77,7 @@ func TestGetQueryVarInt(t *testing.T) {
|
||||
{
|
||||
name: "required flag",
|
||||
url: "/hosts",
|
||||
queryVar: "something",
|
||||
queryVar: something,
|
||||
required: true,
|
||||
want: want{
|
||||
val: 0,
|
||||
@@ -66,7 +87,7 @@ func TestGetQueryVarInt(t *testing.T) {
|
||||
{
|
||||
name: "simple get",
|
||||
url: "/hosts?something=50",
|
||||
queryVar: "something",
|
||||
queryVar: something,
|
||||
required: true,
|
||||
want: want{
|
||||
val: 50,
|
||||
@@ -76,7 +97,7 @@ func TestGetQueryVarInt(t *testing.T) {
|
||||
{
|
||||
name: "invalid number",
|
||||
url: "/hosts?something=aaa",
|
||||
queryVar: "something",
|
||||
queryVar: something,
|
||||
required: true,
|
||||
want: want{
|
||||
val: 0,
|
||||
@@ -86,7 +107,7 @@ func TestGetQueryVarInt(t *testing.T) {
|
||||
{
|
||||
name: "preceding zeros",
|
||||
url: "/hosts?something=0000050",
|
||||
queryVar: "something",
|
||||
queryVar: something,
|
||||
required: true,
|
||||
want: want{
|
||||
val: 50,
|
||||
@@ -96,7 +117,7 @@ func TestGetQueryVarInt(t *testing.T) {
|
||||
{
|
||||
name: "decimals",
|
||||
url: "/hosts?something=50.50",
|
||||
queryVar: "something",
|
||||
queryVar: something,
|
||||
required: true,
|
||||
want: want{
|
||||
val: 0,
|
||||
|
||||
@@ -9,6 +9,6 @@ import (
|
||||
// NotAllowed is a json error handler for when method is not allowed
|
||||
func NotAllowed() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not allowed", nil)
|
||||
h.ResultErrorJSON(w, r, http.StatusMethodNotAllowed, "Not allowed", nil)
|
||||
}
|
||||
}
|
||||
@@ -28,10 +28,7 @@ func NotFound() func(http.ResponseWriter, *http.Request) {
|
||||
defaultFile := "index.html"
|
||||
path := strings.TrimLeft(r.URL.Path, "/")
|
||||
|
||||
isAPI := false
|
||||
if len(path) >= 3 && path[0:3] == "api" {
|
||||
isAPI = true
|
||||
}
|
||||
isAPI := len(path) >= 4 && path[0:4] == "api/"
|
||||
|
||||
if path == "" {
|
||||
path = defaultFile
|
||||
|
||||
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
c "npm/internal/api/context"
|
||||
@@ -85,6 +86,8 @@ func UpdateUser() func(http.ResponseWriter, *http.Request) {
|
||||
case nil:
|
||||
// nolint: errcheck,gosec
|
||||
userObject.Expand([]string{"capabilities"})
|
||||
hadFullAdmin := slices.Contains(userObject.Capabilities, user.CapabilityFullAdmin)
|
||||
|
||||
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
err := json.Unmarshal(bodyBytes, &userObject)
|
||||
if err != nil {
|
||||
@@ -97,6 +100,16 @@ func UpdateUser() func(http.ResponseWriter, *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Only reachable via the admin (UpdateUserAdmin-schema'd) route,
|
||||
// since the self-service schema never allows "capabilities" in
|
||||
// the payload at all - but an admin can still target their own
|
||||
// numeric id, so guard it explicitly rather than relying on
|
||||
// that alone.
|
||||
if self && hadFullAdmin && !slices.Contains(userObject.Capabilities, user.CapabilityFullAdmin) {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, "You cannot remove your own full-admin capability!", nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err = userObject.Save(); err != nil {
|
||||
if err == errors.ErrDuplicateEmailUser || err == errors.ErrSystemUserReadonly {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
|
||||
Reference in new issue
Block a user