Skip to content

Commit 771cae8

Browse files
committed
Default to external IP
1 parent bfce967 commit 771cae8

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

coderd/httpmw/apikey.go

+3
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ func ExtractAPIKey(db database.Store, oauth *OAuth2Configs) func(http.Handler) h
168168
if now.Sub(key.LastUsed) > time.Hour {
169169
key.LastUsed = now
170170
remoteIP := net.ParseIP(r.RemoteAddr)
171+
if remoteIP == nil {
172+
remoteIP = net.IPv4(0, 0, 0, 0)
173+
}
171174
key.IPAddress = pqtype.Inet{
172175
IPNet: net.IPNet{
173176
IP: remoteIP,

coderd/users.go

+3
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,9 @@ func (api *API) createAPIKey(rw http.ResponseWriter, r *http.Request, params dat
801801
}
802802

803803
ip := net.ParseIP(r.RemoteAddr)
804+
if ip == nil {
805+
ip = net.IPv4(0, 0, 0, 0)
806+
}
804807
key, err := api.Database.InsertAPIKey(r.Context(), database.InsertAPIKeyParams{
805808
ID: keyID,
806809
UserID: params.UserID,

coderd/users_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,9 @@ func TestPostLogin(t *testing.T) {
175175
require.NoError(t, err, "fetch api key")
176176

177177
err = api.Database.UpdateAPIKeyByID(ctx, database.UpdateAPIKeyByIDParams{
178-
ID: apiKey.ID,
179-
LastUsed: apiKey.LastUsed,
178+
ID: apiKey.ID,
179+
LastUsed: apiKey.LastUsed,
180+
IPAddress: apiKey.IPAddress,
180181
// This should cause a refresh
181182
ExpiresAt: apiKey.ExpiresAt.Add(time.Hour * -2),
182183
OAuthAccessToken: apiKey.OAuthAccessToken,

0 commit comments

Comments
 (0)