util.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package server
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "mime"
  8. "net/http"
  9. "net/netip"
  10. "regexp"
  11. "strings"
  12. "heckel.io/ntfy/v2/util"
  13. )
  14. var (
  15. mimeDecoder mime.WordDecoder
  16. // priorityHeaderIgnoreRegex matches specific patterns of the "Priority" header (RFC 9218), so that it can be ignored
  17. priorityHeaderIgnoreRegex = regexp.MustCompile(`^u=\d,\s*(i|\d)$|^u=\d$`)
  18. // forwardedHeaderRegex parses IPv4 and IPv6 addresses from the "Forwarded" header (RFC 7239)
  19. // IPv6 addresses in Forwarded header are enclosed in square brackets. The port is optional.
  20. //
  21. // Examples:
  22. // for="1.2.3.4"
  23. // for="[2001:db8::1]"; for=1.2.3.4:8080, by=phil
  24. // for="1.2.3.4:8080"
  25. forwardedHeaderRegex = regexp.MustCompile(`(?i)\bfor="?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|\[[0-9a-f:]+])(?::\d+)?"?`)
  26. )
  27. func readBoolParam(r *http.Request, defaultValue bool, names ...string) bool {
  28. value := strings.ToLower(readParam(r, names...))
  29. if value == "" {
  30. return defaultValue
  31. }
  32. return toBool(value)
  33. }
  34. func isBoolValue(value string) bool {
  35. return value == "1" || value == "yes" || value == "true" || value == "0" || value == "no" || value == "false"
  36. }
  37. func toBool(value string) bool {
  38. return value == "1" || value == "yes" || value == "true"
  39. }
  40. func readCommaSeparatedParam(r *http.Request, names ...string) []string {
  41. if paramStr := readParam(r, names...); paramStr != "" {
  42. return util.Map(util.SplitNoEmpty(paramStr, ","), strings.TrimSpace)
  43. }
  44. return []string{}
  45. }
  46. func readParam(r *http.Request, names ...string) string {
  47. value := readHeaderParam(r, names...)
  48. if value != "" {
  49. return value
  50. }
  51. return readQueryParam(r, names...)
  52. }
  53. func readHeaderParam(r *http.Request, names ...string) string {
  54. for _, name := range names {
  55. value := strings.TrimSpace(maybeDecodeHeader(name, r.Header.Get(name)))
  56. if value != "" {
  57. return value
  58. }
  59. }
  60. return ""
  61. }
  62. func readQueryParam(r *http.Request, names ...string) string {
  63. for _, name := range names {
  64. value := r.URL.Query().Get(strings.ToLower(name))
  65. if value != "" {
  66. return strings.TrimSpace(value)
  67. }
  68. }
  69. return ""
  70. }
  71. // extractIPAddress extracts the IP address of the visitor from the request,
  72. // either from the TCP socket or from a proxy header.
  73. func extractIPAddress(r *http.Request, behindProxy bool, proxyForwardedHeader string, proxyTrustedPrefixes []netip.Prefix) netip.Addr {
  74. if behindProxy && proxyForwardedHeader != "" {
  75. if addr, err := extractIPAddressFromHeader(r, proxyForwardedHeader, proxyTrustedPrefixes); err == nil {
  76. return addr
  77. }
  78. // Fall back to the remote address if the header is not found or invalid
  79. }
  80. addrPort, err := netip.ParseAddrPort(r.RemoteAddr)
  81. if err != nil {
  82. logr(r).Err(err).Warn("unable to parse IP (%s), new visitor with unspecified IP (0.0.0.0) created", r.RemoteAddr)
  83. return netip.IPv4Unspecified()
  84. }
  85. return addrPort.Addr()
  86. }
  87. // extractIPAddressFromHeader extracts the last IP address from the specified header.
  88. //
  89. // It supports multiple formats:
  90. // - single IP address
  91. // - comma-separated list
  92. // - RFC 7239-style list (Forwarded header)
  93. //
  94. // If there are multiple addresses, we first remove the trusted IP addresses from the list, and
  95. // then take the right-most address in the list (as this is the one added by our proxy server).
  96. // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For for details.
  97. func extractIPAddressFromHeader(r *http.Request, forwardedHeader string, trustedPrefixes []netip.Prefix) (netip.Addr, error) {
  98. value := strings.TrimSpace(strings.ToLower(r.Header.Get(forwardedHeader)))
  99. if value == "" {
  100. return netip.IPv4Unspecified(), fmt.Errorf("no %s header found", forwardedHeader)
  101. }
  102. // Extract valid addresses
  103. addrsStrs := util.Map(util.SplitNoEmpty(value, ","), strings.TrimSpace)
  104. var validAddrs []netip.Addr
  105. for _, addrStr := range addrsStrs {
  106. // Handle Forwarded header with for="[IPv6]" or for="IPv4"
  107. if m := forwardedHeaderRegex.FindStringSubmatch(addrStr); len(m) == 2 {
  108. addrRaw := m[1]
  109. if strings.HasPrefix(addrRaw, "[") && strings.HasSuffix(addrRaw, "]") {
  110. addrRaw = addrRaw[1 : len(addrRaw)-1]
  111. }
  112. if addr, err := netip.ParseAddr(addrRaw); err == nil {
  113. validAddrs = append(validAddrs, addr)
  114. }
  115. } else if addr, err := netip.ParseAddr(addrStr); err == nil {
  116. validAddrs = append(validAddrs, addr)
  117. }
  118. }
  119. // Filter out proxy addresses
  120. clientAddrs := util.Filter(validAddrs, func(addr netip.Addr) bool {
  121. for _, prefix := range trustedPrefixes {
  122. if prefix.Contains(addr) {
  123. return false // Address is in the trusted range, ignore it
  124. }
  125. }
  126. return true
  127. })
  128. if len(clientAddrs) == 0 {
  129. return netip.IPv4Unspecified(), fmt.Errorf("no client IP address found in %s header: %s", forwardedHeader, value)
  130. }
  131. return clientAddrs[len(clientAddrs)-1], nil
  132. }
  133. func readJSONWithLimit[T any](r io.ReadCloser, limit int, allowEmpty bool) (*T, error) {
  134. obj, err := util.UnmarshalJSONWithLimit[T](r, limit, allowEmpty)
  135. if errors.Is(err, util.ErrUnmarshalJSON) {
  136. return nil, errHTTPBadRequestJSONInvalid
  137. } else if errors.Is(err, util.ErrTooLargeJSON) {
  138. return nil, errHTTPEntityTooLargeJSONBody
  139. } else if err != nil {
  140. return nil, err
  141. }
  142. return obj, nil
  143. }
  144. func withContext(r *http.Request, ctx map[contextKey]any) *http.Request {
  145. c := r.Context()
  146. for k, v := range ctx {
  147. c = context.WithValue(c, k, v)
  148. }
  149. return r.WithContext(c)
  150. }
  151. func fromContext[T any](r *http.Request, key contextKey) (T, error) {
  152. t, ok := r.Context().Value(key).(T)
  153. if !ok {
  154. return t, fmt.Errorf("cannot find key %v in request context", key)
  155. }
  156. return t, nil
  157. }
  158. // maybeDecodeHeader decodes the given header value if it is MIME encoded, e.g. "=?utf-8?q?Hello_World?=",
  159. // or returns the original header value if it is not MIME encoded. It also calls maybeIgnoreSpecialHeader
  160. // to ignore the new HTTP "Priority" header.
  161. func maybeDecodeHeader(name, value string) string {
  162. decoded, err := mimeDecoder.DecodeHeader(value)
  163. if err != nil {
  164. return maybeIgnoreSpecialHeader(name, value)
  165. }
  166. return maybeIgnoreSpecialHeader(name, decoded)
  167. }
  168. // maybeIgnoreSpecialHeader ignores the new HTTP "Priority" header (RFC 9218, see https://datatracker.ietf.org/doc/html/rfc9218)
  169. //
  170. // Cloudflare (and potentially other providers) add this to requests when forwarding to the backend (ntfy),
  171. // so we just ignore it. If the "Priority" header is set to "u=*, i" or "u=*" (by Cloudflare), the header will be ignored.
  172. // Returning an empty string will allow the rest of the logic to continue searching for another header (x-priority, prio, p),
  173. // or in the Query parameters.
  174. func maybeIgnoreSpecialHeader(name, value string) string {
  175. if strings.ToLower(name) == "priority" && priorityHeaderIgnoreRegex.MatchString(strings.TrimSpace(value)) {
  176. return ""
  177. }
  178. return value
  179. }