Skip to content

Commit 3a4363c

Browse files
author
Tobias Cudnik
authored
NET-163: Return 403 instead of 401 (#2326)
* return 401 instead of 403 * fixed http.StatusForbidden * Tagged build version (temp) * Unauthorized_Err when applicable * untagged version
1 parent 7b5bef7 commit 3a4363c

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

.dockerignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
config/dnsconfig/
2-
data/
2+
data/
3+
/.git
4+
/*.tar

controllers/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func authenticate(response http.ResponseWriter, request *http.Request) {
157157
func authorize(hostAllowed, networkCheck bool, authNetwork string, next http.Handler) http.HandlerFunc {
158158
return func(w http.ResponseWriter, r *http.Request) {
159159
var errorResponse = models.ErrorResponse{
160-
Code: http.StatusUnauthorized, Message: logic.Unauthorized_Msg,
160+
Code: http.StatusForbidden, Message: logic.Forbidden_Msg,
161161
}
162162

163163
var params = mux.Vars(r)

controllers/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func getStatus(w http.ResponseWriter, r *http.Request) {
5656
func allowUsers(next http.Handler) http.HandlerFunc {
5757
return func(w http.ResponseWriter, r *http.Request) {
5858
var errorResponse = models.ErrorResponse{
59-
Code: http.StatusInternalServerError, Message: logic.Unauthorized_Msg,
59+
Code: http.StatusInternalServerError, Message: logic.Forbidden_Msg,
6060
}
6161
bearerToken := r.Header.Get("Authorization")
6262
var tokenSplit = strings.Split(bearerToken, " ")

logic/security.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const (
1818
ALL_NETWORK_ACCESS = "THIS_USER_HAS_ALL"
1919

2020
master_uname = "masteradministrator"
21+
Forbidden_Msg = "forbidden"
22+
Forbidden_Err = models.Error(Forbidden_Msg)
2123
Unauthorized_Msg = "unauthorized"
2224
Unauthorized_Err = models.Error(Unauthorized_Msg)
2325
)
@@ -27,7 +29,7 @@ func SecurityCheck(reqAdmin bool, next http.Handler) http.HandlerFunc {
2729

2830
return func(w http.ResponseWriter, r *http.Request) {
2931
var errorResponse = models.ErrorResponse{
30-
Code: http.StatusUnauthorized, Message: Unauthorized_Msg,
32+
Code: http.StatusForbidden, Message: Forbidden_Msg,
3133
}
3234

3335
var params = mux.Vars(r)
@@ -66,7 +68,7 @@ func SecurityCheck(reqAdmin bool, next http.Handler) http.HandlerFunc {
6668
func NetUserSecurityCheck(isNodes, isClients bool, next http.Handler) http.HandlerFunc {
6769
return func(w http.ResponseWriter, r *http.Request) {
6870
var errorResponse = models.ErrorResponse{
69-
Code: http.StatusUnauthorized, Message: "unauthorized",
71+
Code: http.StatusForbidden, Message: Forbidden_Msg,
7072
}
7173
r.Header.Set("ismaster", "no")
7274

@@ -152,18 +154,18 @@ func UserPermissions(reqAdmin bool, netname string, token string) ([]string, str
152154
return nil, username, Unauthorized_Err
153155
}
154156
if !isadmin && reqAdmin {
155-
return nil, username, Unauthorized_Err
157+
return nil, username, Forbidden_Err
156158
}
157159
userNetworks = networks
158160
if isadmin {
159161
return []string{ALL_NETWORK_ACCESS}, username, nil
160162
}
161163
// check network admin access
162164
if len(netname) > 0 && (len(userNetworks) == 0 || !authenticateNetworkUser(netname, userNetworks)) {
163-
return nil, username, Unauthorized_Err
165+
return nil, username, Forbidden_Err
164166
}
165167
if isEE && len(netname) > 0 && !pro.IsUserNetAdmin(netname, username) {
166-
return nil, "", Unauthorized_Err
168+
return nil, "", Forbidden_Err
167169
}
168170
return userNetworks, username, nil
169171
}
@@ -193,7 +195,7 @@ func authenticateDNSToken(tokenString string) bool {
193195
func ContinueIfUserMatch(next http.Handler) http.HandlerFunc {
194196
return func(w http.ResponseWriter, r *http.Request) {
195197
var errorResponse = models.ErrorResponse{
196-
Code: http.StatusUnauthorized, Message: Unauthorized_Msg,
198+
Code: http.StatusForbidden, Message: Forbidden_Msg,
197199
}
198200
var params = mux.Vars(r)
199201
var requestedUser = params["username"]

0 commit comments

Comments
 (0)