url.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Package policy validates navigation destinations. It is not an egress firewall.
  2. package policy
  3. import (
  4. "context"
  5. "net"
  6. "net/url"
  7. "strings"
  8. "clusterflow.dev/cfbrowser/api"
  9. )
  10. type Resolver interface {
  11. LookupIPAddr(context.Context, string) ([]net.IPAddr, error)
  12. }
  13. type URLPolicy struct {
  14. // AllowOrigins authorizes exact normalized origins, including private hosts.
  15. AllowOrigins []string
  16. Resolver Resolver
  17. }
  18. func origin(u *url.URL) string {
  19. port := u.Port()
  20. if port == "" {
  21. if u.Scheme == "https" {
  22. port = "443"
  23. } else {
  24. port = "80"
  25. }
  26. }
  27. return strings.ToLower(u.Scheme) + "://" + net.JoinHostPort(strings.ToLower(u.Hostname()), port)
  28. }
  29. func public(ip net.IP) bool {
  30. if ip == nil || !ip.IsGlobalUnicast() || ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsUnspecified() {
  31. return false
  32. }
  33. for _, cidr := range []string{"100.64.0.0/10", "198.18.0.0/15", "192.0.0.0/24", "192.0.2.0/24", "198.51.100.0/24", "203.0.113.0/24", "2001:db8::/32"} {
  34. _, n, _ := net.ParseCIDR(cidr)
  35. if n.Contains(ip) {
  36. return false
  37. }
  38. }
  39. return true
  40. }
  41. func (p URLPolicy) Validate(ctx context.Context, raw string) (string, error) {
  42. u, err := url.Parse(strings.TrimSpace(raw))
  43. if err != nil || u == nil || (u.Scheme != "http" && u.Scheme != "https") || u.Hostname() == "" || u.User != nil || u.Opaque != "" {
  44. return "", api.Err("BROWSER_INVALID_URL", "an HTTP(S) URL without credentials is required")
  45. }
  46. if len(raw) > 8192 {
  47. return "", api.Err("BROWSER_INVALID_URL", "URL exceeds 8 KiB")
  48. }
  49. for _, a := range p.AllowOrigins {
  50. v, e := url.Parse(a)
  51. if e == nil && v.Host != "" && v.User == nil && origin(v) == origin(u) {
  52. return u.String(), nil
  53. }
  54. }
  55. if ip := net.ParseIP(u.Hostname()); ip != nil {
  56. if public(ip) {
  57. return u.String(), nil
  58. }
  59. return "", api.Err("BROWSER_NETWORK_BLOCKED", "destination is not public or explicitly allowed")
  60. }
  61. host := strings.ToLower(u.Hostname())
  62. if host == "localhost" || strings.HasSuffix(host, ".localhost") {
  63. return "", api.Err("BROWSER_NETWORK_BLOCKED", "localhost requires an exact allowed origin")
  64. }
  65. resolver := p.Resolver
  66. if resolver == nil {
  67. resolver = net.DefaultResolver
  68. }
  69. ips, err := resolver.LookupIPAddr(ctx, host)
  70. if err != nil || len(ips) == 0 {
  71. return "", api.Err("BROWSER_DNS_FAILED", "destination did not resolve")
  72. }
  73. for _, ip := range ips {
  74. if !public(ip.IP) {
  75. return "", api.Err("BROWSER_NETWORK_BLOCKED", "destination resolves to a non-public address")
  76. }
  77. }
  78. return u.String(), nil
  79. }