mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
fix: use valid ip mask in api keys when remote address is ipv6 (#2695)
This commit is contained in:
@ -172,10 +172,11 @@ func ExtractAPIKey(db database.Store, oauth *OAuth2Configs) func(http.Handler) h
|
|||||||
if remoteIP == nil {
|
if remoteIP == nil {
|
||||||
remoteIP = net.IPv4(0, 0, 0, 0)
|
remoteIP = net.IPv4(0, 0, 0, 0)
|
||||||
}
|
}
|
||||||
|
bitlen := len(remoteIP) * 8
|
||||||
key.IPAddress = pqtype.Inet{
|
key.IPAddress = pqtype.Inet{
|
||||||
IPNet: net.IPNet{
|
IPNet: net.IPNet{
|
||||||
IP: remoteIP,
|
IP: remoteIP,
|
||||||
Mask: remoteIP.DefaultMask(),
|
Mask: net.CIDRMask(bitlen, bitlen),
|
||||||
},
|
},
|
||||||
Valid: true,
|
Valid: true,
|
||||||
}
|
}
|
||||||
|
@ -815,6 +815,7 @@ func (api *API) createAPIKey(rw http.ResponseWriter, r *http.Request, params dat
|
|||||||
if ip == nil {
|
if ip == nil {
|
||||||
ip = net.IPv4(0, 0, 0, 0)
|
ip = net.IPv4(0, 0, 0, 0)
|
||||||
}
|
}
|
||||||
|
bitlen := len(ip) * 8
|
||||||
key, err := api.Database.InsertAPIKey(r.Context(), database.InsertAPIKeyParams{
|
key, err := api.Database.InsertAPIKey(r.Context(), database.InsertAPIKeyParams{
|
||||||
ID: keyID,
|
ID: keyID,
|
||||||
UserID: params.UserID,
|
UserID: params.UserID,
|
||||||
@ -822,7 +823,7 @@ func (api *API) createAPIKey(rw http.ResponseWriter, r *http.Request, params dat
|
|||||||
IPAddress: pqtype.Inet{
|
IPAddress: pqtype.Inet{
|
||||||
IPNet: net.IPNet{
|
IPNet: net.IPNet{
|
||||||
IP: ip,
|
IP: ip,
|
||||||
Mask: ip.DefaultMask(),
|
Mask: net.CIDRMask(bitlen, bitlen),
|
||||||
},
|
},
|
||||||
Valid: true,
|
Valid: true,
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user