Skip to content

Commit 8d30f1e

Browse files
committed
Use same error always
1 parent f3f285b commit 8d30f1e

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

coderd/httpmw/userparam.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ import (
1414

1515
type userParamContextKey struct{}
1616

17+
const (
18+
// userErrorMessage is a constant so that no information about the state
19+
// of the queried user can be gained. We return the same error if the user
20+
// does not exist, or if the input is just garbage.
21+
userErrorMessage = "\"user\" must be an existing uuid or username"
22+
)
23+
1724
// UserParam returns the user from the ExtractUserParam handler.
1825
func UserParam(r *http.Request) database.User {
1926
user, ok := r.Context().Value(userParamContextKey{}).(database.User)
@@ -51,8 +58,8 @@ func ExtractUserParam(db database.Store) func(http.Handler) http.Handler {
5158
// If the userQuery is a valid uuid
5259
user, err = db.GetUserByID(r.Context(), userID)
5360
if err != nil {
54-
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
55-
Message: fmt.Sprintf("get user: %s", err.Error()),
61+
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
62+
Message: userErrorMessage,
5663
})
5764
return
5865
}
@@ -62,12 +69,8 @@ func ExtractUserParam(db database.Store) func(http.Handler) http.Handler {
6269
Username: userQuery,
6370
})
6471
if err != nil {
65-
// If the error is no rows, they might have inputted something
66-
// that is not a username or uuid. Regardless, let's not indicate if
67-
// the user exists or not. Just lump all these errors into
68-
// something generic.
6972
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
70-
Message: "\"user\" must be a uuid or username",
73+
Message: userErrorMessage,
7174
})
7275
return
7376
}

0 commit comments

Comments
 (0)