Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proto.NetworkCookie.Expires.Time should map -1 to a zero time value. #1159

Open
kvii opened this issue Dec 4, 2024 · 1 comment
Open

proto.NetworkCookie.Expires.Time should map -1 to a zero time value. #1159

kvii opened this issue Dec 4, 2024 · 1 comment
Labels
enhance New feature or request

Comments

@kvii
Copy link
Contributor

kvii commented Dec 4, 2024

Rod Version: v0.116.2

中文省流:如果在 rod cookie (a) 转 go http cookie (b) 的时候直接 b.Expires = a.Expires.Time() 的话,http 这边发请求时所有的 session cookie 就都没了。因为 rod session cookie 的值是 -1 而不是 0,转成 time 就成了 23:59:59 这样的值了。

proto.NetworkCookie.Expires(line 969) is a TimeSinceEpoch field.

rod/lib/proto/network.go

Lines 954 to 969 in 3025dde

// NetworkCookie Cookie object.
type NetworkCookie struct {
// Name Cookie name.
Name string `json:"name"`
// Value Cookie value.
Value string `json:"value"`
// Domain Cookie domain.
Domain string `json:"domain"`
// Path Cookie path.
Path string `json:"path"`
// Expires Cookie expiration date
Expires TimeSinceEpoch `json:"expires"`

TimeSinceEpoch.Time maps a TimeSinceEpoch value to time.Time value.

rod/lib/proto/a_patch.go

Lines 14 to 22 in 3025dde

// For session cookie, the value should be -1.
type TimeSinceEpoch float64
// Time interface.
func (t TimeSinceEpoch) Time() time.Time {
return (time.Unix(0, 0)).Add(
time.Duration(t * TimeSinceEpoch(time.Second)),
)
}

Please note the line 14, "For session cookie, the value should be -1. ". So when a cookie is a session cookie, the result will be unix(0) - 1("1969-12-31 23:59:59"). When you are using some codes like below:

func CookieFrom(c *proto.NetworkCookie) *http.Cookie {
	hc := &http.Cookie{
		Name:        c.Name,
		Value:       c.Value,
		Quoted:      false,
		Path:        c.Path,
		Domain:      c.Domain,
		Expires:     c.Expires.Time(), // <--
		RawExpires:  "",
		MaxAge:      0,
		Secure:      c.Secure,
		HttpOnly:    c.HTTPOnly,
		SameSite:    SameSite(c.SameSite),
		Partitioned: false,
		Raw:         "",
		Unparsed:    []string{},
	}
	return hc
}

You will loss all session cookie when you're sending requests. Because hc.Expires is not a zero value and it has expired.

@TQloveXue
Copy link

大佬!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhance New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants