requireAPIKeyForBind rejects a non-loopback bind address that has no API key. An empty or unspecified host binds each interface, and that counts as non-loopback. It warns when the key is short.
(address, apiKey string)
| 102 | // API key. An empty or unspecified host binds each interface, and that |
| 103 | // counts as non-loopback. It warns when the key is short. |
| 104 | func requireAPIKeyForBind(address, apiKey string) error { |
| 105 | if apiKey != "" && len(apiKey) < minAPIKeyLength { |
| 106 | slog.Warn("API key is short: use a random key of 16 or more characters", "length", len(apiKey)) |
| 107 | } |
| 108 | // Only the exact name "localhost" or a loopback IP is accepted. A name |
| 109 | // such as "app.localhost" goes to DNS on some systems, and the server |
| 110 | // can then listen on a public interface with no key. |
| 111 | host := hostOnly(address) |
| 112 | if apiKey != "" || host == "localhost" || net.ParseIP(host).IsLoopback() { |
| 113 | return nil |
| 114 | } |
| 115 | return fmt.Errorf(i18n.T("server_api_key_required"), address) |
| 116 | } |
| 117 | |
| 118 | // APIKeyMiddleware validates API key for protected endpoints. |
| 119 | // Swagger documentation endpoints (/swagger/*) are exempt from authentication |