cookie.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package sessions
  2. import (
  3. "net"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/kataras/iris/context"
  9. )
  10. var (
  11. // CookieExpireDelete may be set on Cookie.Expire for expiring the given cookie.
  12. CookieExpireDelete = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  13. // CookieExpireUnlimited indicates that the cookie doesn't expire.
  14. CookieExpireUnlimited = time.Now().AddDate(24, 10, 10)
  15. )
  16. // GetCookie returns cookie's value by it's name
  17. // returns empty string if nothing was found
  18. func GetCookie(ctx context.Context, name string) string {
  19. c, err := ctx.Request().Cookie(name)
  20. if err != nil {
  21. return ""
  22. }
  23. return c.Value
  24. }
  25. // AddCookie adds a cookie
  26. func AddCookie(ctx context.Context, cookie *http.Cookie, reclaim bool) {
  27. if reclaim {
  28. ctx.Request().AddCookie(cookie)
  29. }
  30. ctx.SetCookie(cookie)
  31. }
  32. // RemoveCookie deletes a cookie by it's name/key
  33. // If "purge" is true then it removes the, temp, cookie from the request as well.
  34. func RemoveCookie(ctx context.Context, config Config) {
  35. cookie, err := ctx.Request().Cookie(config.Cookie)
  36. if err != nil {
  37. return
  38. }
  39. cookie.Expires = CookieExpireDelete
  40. // MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
  41. cookie.MaxAge = -1
  42. cookie.Value = ""
  43. cookie.Path = "/"
  44. cookie.Domain = formatCookieDomain(ctx, config.DisableSubdomainPersistence)
  45. AddCookie(ctx, cookie, config.AllowReclaim)
  46. if config.AllowReclaim {
  47. // delete request's cookie also, which is temporary available.
  48. ctx.Request().Header.Set("Cookie", "")
  49. }
  50. }
  51. // IsValidCookieDomain returns true if the receiver is a valid domain to set
  52. // valid means that is recognised as 'domain' by the browser, so it(the cookie) can be shared with subdomains also
  53. func IsValidCookieDomain(domain string) bool {
  54. if net.IP([]byte(domain)).IsLoopback() {
  55. // for these type of hosts, we can't allow subdomains persistence,
  56. // the web browser doesn't understand the mysubdomain.0.0.0.0 and mysubdomain.127.0.0.1 mysubdomain.32.196.56.181. as scorrectly ubdomains because of the many dots
  57. // so don't set a cookie domain here, let browser handle this
  58. return false
  59. }
  60. dotLen := strings.Count(domain, ".")
  61. if dotLen == 0 {
  62. // we don't have a domain, maybe something like 'localhost', browser doesn't see the .localhost as wildcard subdomain+domain
  63. return false
  64. }
  65. if dotLen >= 3 {
  66. if lastDotIdx := strings.LastIndexByte(domain, '.'); lastDotIdx != -1 {
  67. // chekc the last part, if it's number then propably it's ip
  68. if len(domain) > lastDotIdx+1 {
  69. _, err := strconv.Atoi(domain[lastDotIdx+1:])
  70. if err == nil {
  71. return false
  72. }
  73. }
  74. }
  75. }
  76. return true
  77. }
  78. func formatCookieDomain(ctx context.Context, disableSubdomainPersistence bool) string {
  79. if disableSubdomainPersistence {
  80. return ""
  81. }
  82. requestDomain := ctx.Host()
  83. if portIdx := strings.IndexByte(requestDomain, ':'); portIdx > 0 {
  84. requestDomain = requestDomain[0:portIdx]
  85. }
  86. if !IsValidCookieDomain(requestDomain) {
  87. return ""
  88. }
  89. // RFC2109, we allow level 1 subdomains, but no further
  90. // if we have localhost.com , we want the localhost.com.
  91. // so if we have something like: mysubdomain.localhost.com we want the localhost here
  92. // if we have mysubsubdomain.mysubdomain.localhost.com we want the .mysubdomain.localhost.com here
  93. // slow things here, especially the 'replace' but this is a good and understable( I hope) way to get the be able to set cookies from subdomains & domain with 1-level limit
  94. if dotIdx := strings.IndexByte(requestDomain, '.'); dotIdx > 0 {
  95. // is mysubdomain.localhost.com || mysubsubdomain.mysubdomain.localhost.com
  96. if strings.IndexByte(requestDomain[dotIdx+1:], '.') > 0 {
  97. requestDomain = requestDomain[dotIdx+1:]
  98. }
  99. }
  100. // finally set the .localhost.com (for(1-level) || .mysubdomain.localhost.com (for 2-level subdomain allow)
  101. return "." + requestDomain // . to allow persistence
  102. }